-
Notifications
You must be signed in to change notification settings - Fork 759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configure babel to not transpile ES6 modules to CommonJS #688
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
modules
option 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
modules
option to the built in babel preset. This is necessary because it must beundefined
when running the node scripts, since node does not support ES modules.jsLoader
such that the babel preset is applied with themodules
option set tofalse
module.strictExportPresence
totrue
in webpack configs. This causes the import of an identifier that is not exported to be an error rather than a warning.resolve.mainFields
in 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.extensions
in webpack config from['.js', '.json', '.jsx']
to['.wasm', '.mjs', '.js', '.json', '.jsx']
. This reflects the current webpack default with an added.jsx
option. It has the effect of prioritising.mjs
files, 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 themodule
field 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: