Search code examples
node.jsrequirebabeljs

Using the babel-register `only` option


I have the following config in my .babelrc:

{
    "presets": ["react", "es2015"],
    "only": ["app/**/*.js", "src/**/*.js", "test/**/*.js", "node_modules/my-module/**/*.js"]
}

I basically want my-module in the node_modules directory to be passed through babel, and so am using the only option.

However, it doesn't seem to be working. Any ES6 I write—whether in my-module or even just app/app.js—isn't transpiled, and so breaks my app.

Any ideas? Am I misunderstanding the documentation?

Previously I was doing the following:

require('babel-register')({
    ignore: function (filename) {
        if (filename.indexOf('node_modules') === -1) {
            return false;
        }

        return filename.indexOf('@lostmyname/styleguide') === -1;
    }
});

However, I need to use the .babelrc because I'm adding mocha, which only supports the require hook with no options.


Solution

  • Apparently this was caused by a bug in babel: https://github.com/babel/babel/issues/3789

    It's been fixed: https://github.com/babel/babel/pull/3644