new ESM implementation
This PR updates the current --experimental-modules
implementation based on the work of the modules team and reflects Phase 2 of our new modules plan.
A longer form description of these changes can be found in our draft blog post.
The largest differences from the current implementation include
-
packge.type
which can be eithermodule
orcommonjs
-
type: "commonjs"
:-
.js
is parsed as commonjs - default for entry point without an extension is commonjs
-
-
type: "module"
:-
.js
is parsed as esm - does not support loading JSON or Native Module by default
- default for entry point without an extension is esm
-
-
-
--type=[mode]
to let you set the type on entry point. Will overridepackage.type
for entry point. - A new file extension
.cjs
.- this is specifically to support importing commonjs in the
module
mode. - this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path.
- this is specifically to support importing commonjs in the
-
--es-module-specifier-resolution=[type]
- options are
explicit
(default) andnode
- by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one
- by default our loader will not allow for importing directories that have an index file
- developers can use
--es-module-specifier-resolution=node
to enable the commonjs specifier resolution algorithm - This is not a “feature” but rather an implementation for experimentation. It is expected to change before the flag is removed
- options are
-
--experimental-json-loader
- the only way to import json when
"type": "module"
- when enable all
import 'thing.json'
will go through the experimental loader independent of mode - based on https://github.com/whatwg/html/issues/4315
- the only way to import json when
- You can use
package.main
to set an entry point for a module- the file extensions used in main will be resolved based on the
type
of the module
- the file extensions used in main will be resolved based on the