react-native: [Discussion] Upgrade to Babel 6 breaks parts of the package ecosystem

With the upgrade to Babel 6, two things changed:

  • Babel or the packager appears to be looking at .babelrc in each npm package. When libraries like Redux publish a .babelrc for Babel 5 (ex: with the “stage” field), Babel 6 throws a TransformError.
  • Babel appears to not apply the .babelrc of the project root to each npm package, even if they don’t contain .babelrc files

Before, you could configure .babelrc in your project root and Babel would compute something like the union of .babelrc + the options passed to transform(), and it would apply all those options to everything under node_modules. But now it seems not to apply the options specified in .babelrc to each npm package, so perhaps each package needs to ship with its own .babelrc and list its plugins in the package.json dependencies.

cc @sebmck you’re probably the best person to comment on the state of things

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (14 by maintainers)

Commits related to this issue

Most upvoted comments

The solution that I think we’ve standardized on is asking library authors to exclude .babelrc files from their npm packages, and the message has percolated through the ecosystem by now.

There are times when you want to transpile things in node_modules, but this should be done under the assumption that the code in node_modules is pure, standards compliant JavaScript code. It should not make use of .babelrc files, and it should not add strict mode if the file wasn’t already in strict mode. Running babel in its default mode on node_modules is highly risky and should be avoided. Having the ability to run carefully chosen transforms against node_modules may be useful in the future, but it is a pretty specialised use case.

One temporary workaround would be, to delete all .babelrc files in the npm postinstall hook,

{
    ...
    "scripts": {
        "postinstall": "find node_modules -type f -name .babelrc | grep -v /react-native/ | xargs rm"
    }
    ...
}

Processing npm modules with babel is a bug and will break many packages. This is not how any of the rest of the node.js/JavaScript ecosystem operates. This problem exclusively affects react-native.