Configure babel to not transpile ES6 modules to CommonJS#688
Conversation
…ise "module" and only use "browser" in the browser build
…n importing names that don't exist
… false in webpack without breaking node scripts
|
@tannerlinsley after implementing this I had a few thoughts about what we discussed in #683
|
|
This is great. What about the circumstance where a user consumes |
|
That case works (uses ES modules), because the options for the In a project, the following {
"extends": "react-static/.babelrc",
}{
"presets": ["react-static/babel-preset"],
}A difficulty is that since node does not support ES modules, its necessary to transform them to CommonJS on the compiler side. All the code that runs/configures webpack, does SSR etc can't have ES modules in it otherwise node will crash. For example, this makes the build crash: {
"presets": [["react-static/babel-preset", { "modules": false }]],
}If I understand babel/babel#6905 correctly (also https://babeljs.io/blog/2017/12/27/nearing-the-7.0-release#the-env-option-in-babelrc-is-not-being-deprecated), users can still add other presets in their Another option would be to use the |
Fixes #683
Description
This changes the default configuration for babel passed from babel-loader such that the
modulesoption is set tofalse, as well as some other changes to webpack configuration to ensure that ES modules are used instead of CommonJS modules wherever possible.Changes/Tasks
modulesoption to the built in babel preset. This is necessary because it must beundefinedwhen running the node scripts, since node does not support ES modules.jsLoadersuch that the babel preset is applied with themodulesoption set tofalsemodule.strictExportPresencetotruein webpack configs. This causes the import of an identifier that is not exported to be an error rather than a warning.resolve.mainFieldsin the webpack configs. This has the effect of changing the custom value['browser', 'main']to the current webpack default of['browser', 'module', 'main']. This means that ES imports are also used for packages which include the standard module field inpackage.json.resolve.extensionsin webpack config from['.js', '.json', '.jsx']to['.wasm', '.mjs', '.js', '.json', '.jsx']. This reflects the current webpack default with an added.jsxoption. It has the effect of prioritising.mjsfiles, which generally have ES exports instead of CommonJS.Motivation and Context
See #683
undefined. This includes both user code (provided they use ES6 imports/exports), and packages (provided the package provides ES modules in themodulefield ofpackage.json). This means the developer gets promptly warned about import errors during compilation instead of experiencing confusing errors in testing or production.Screenshots (if appropriate):
Types of changes
Checklist: