Skip to content

Commit b1094db

Browse files
guybedfordMylesBorins
authored andcommitted
esm: phase two of 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. The largest differences from the current implementation include * `packge.type` which can be either `module` or `commonjs` - `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 * `--entry-type=[mode]` - allows you set the type on 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. * `--es-module-specifier-resolution=[type]` - options are `explicit` (default) and `node` - 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 * `--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 whatwg/html#4315 * 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 Refs: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md Refs: https://github.com/GeoffreyBooth/node-import-file-specifier-resolution-proposal Refs: nodejs/modules#180 Refs: nodejs/ecmascript-modules#6 Refs: nodejs/ecmascript-modules#12 Refs: nodejs/ecmascript-modules#28 Refs: nodejs/modules#255 Refs: whatwg/html#4315 Refs: WICG/webcomponents#770 Co-authored-by: Myles Borins <[email protected]> Co-authored-by: John-David Dalton <[email protected]> Co-authored-by: Evan Plaice <[email protected]> Co-authored-by: Geoffrey Booth <[email protected]> Co-authored-by: Michaël Zasso <[email protected]> PR-URL: #26745 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Ben Coe <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
1 parent 3914142 commit b1094db

File tree

124 files changed

+1822
-482
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+1822
-482
lines changed

.eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ module.exports = {
3838
{
3939
files: [
4040
'doc/api/esm.md',
41+
'test/es-module/test-esm-type-flag.js',
42+
'test/es-module/test-esm-type-flag-alias.js',
4143
'*.mjs',
4244
'test/es-module/test-esm-example-loader.js',
4345
],

doc/api/cli.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,43 @@ conjunction with native stack and other runtime environment data.
131131
added: v6.0.0
132132
-->
133133

134+
### `--entry-type=type`
135+
<!-- YAML
136+
added: REPLACEME
137+
-->
138+
139+
Used with `--experimental-modules`, this configures Node.js to interpret the
140+
initial entry point as CommonJS or as an ES module.
141+
142+
Valid values are `"commonjs"` and `"module"`. The default is to infer from
143+
the file extension and the `"type"` field in the nearest parent `package.json`.
144+
145+
Works for executing a file as well as `--eval`, `--print`, `STDIN`.
146+
134147
Enable FIPS-compliant crypto at startup. (Requires Node.js to be built with
135148
`./configure --openssl-fips`.)
136149

150+
### `--es-module-specifier-resolution=mode`
151+
<!-- YAML
152+
added: REPLACEME
153+
-->
154+
155+
To be used in conjunction with `--experimental-modules`. Sets the resolution
156+
algorithm for resolving specifiers. Valid options are `explicit` and `node`.
157+
158+
The default is `explicit`, which requires providing the full path to a
159+
module. The `node` mode will enable support for optional file extensions and
160+
the ability to import a directory that has an index file.
161+
162+
Please see [customizing esm specifier resolution][] for example usage.
163+
164+
### `--experimental-json-modules`
165+
<!-- YAML
166+
added: REPLACEME
167+
-->
168+
169+
Enable experimental JSON support for the ES Module loader.
170+
137171
### `--experimental-modules`
138172
<!-- YAML
139173
added: v8.5.0
@@ -927,9 +961,10 @@ greater than `4` (its current default value). For more information, see the
927961
[REPL]: repl.html
928962
[ScriptCoverage]: https://chromedevtools.github.io/devtools-protocol/tot/Profiler#type-ScriptCoverage
929963
[V8 JavaScript code coverage]: https://v8project.blogspot.com/2017/12/javascript-code-coverage.html
964+
[customizing esm specifier resolution]: esm.html#esm_customizing_esm_specifier_resolution_algorithm
930965
[debugger]: debugger.html
931966
[debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications
932967
[emit_warning]: process.html#process_process_emitwarning_warning_type_code_ctor
933-
[experimental ECMAScript Module]: esm.html#esm_loader_hooks
968+
[experimental ECMAScript Module]: esm.html#esm_resolve_hook
934969
[libuv threadpool documentation]: http://docs.libuv.org/en/latest/threadpool.html
935970
[remote code execution]: https://www.owasp.org/index.php/Code_Injection

doc/api/errors.md

+31-15
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,18 @@ provided.
844844
Encoding provided to `TextDecoder()` API was not one of the
845845
[WHATWG Supported Encodings][].
846846

847+
<a id="ERR_ENTRY_TYPE_MISMATCH"></a>
848+
#### ERR_ENTRY_TYPE_MISMATCH
849+
850+
> Stability: 1 - Experimental
851+
852+
The `--entry-type=commonjs` flag was used to attempt to execute an `.mjs` file
853+
or a `.js` file where the nearest parent `package.json` contains
854+
`"type": "module"`; or
855+
the `--entry-type=module` flag was used to attempt to execute a `.cjs` file or
856+
a `.js` file where the nearest parent `package.json` either lacks a `"type"`
857+
field or contains `"type": "commonjs"`.
858+
847859
<a id="ERR_FALSY_VALUE_REJECTION"></a>
848860
### ERR_FALSY_VALUE_REJECTION
849861

@@ -1267,6 +1279,11 @@ An invalid or unexpected value was passed in an options object.
12671279

12681280
An invalid or unknown file encoding was passed.
12691281

1282+
<a id="ERR_INVALID_PACKAGE_CONFIG"></a>
1283+
### ERR_INVALID_PACKAGE_CONFIG
1284+
1285+
An invalid `package.json` file was found which failed parsing.
1286+
12701287
<a id="ERR_INVALID_PERFORMANCE_MARK"></a>
12711288
### ERR_INVALID_PERFORMANCE_MARK
12721289

@@ -1440,7 +1457,7 @@ strict compliance with the API specification (which in some cases may accept
14401457

14411458
> Stability: 1 - Experimental
14421459
1443-
An [ES6 module][] loader hook specified `format: 'dynamic'` but did not provide
1460+
An [ES Module][] loader hook specified `format: 'dynamic'` but did not provide
14441461
a `dynamicInstantiate` hook.
14451462

14461463
<a id="ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST"></a>
@@ -1449,26 +1466,19 @@ a `dynamicInstantiate` hook.
14491466
A `MessagePort` was found in the object passed to a `postMessage()` call,
14501467
but not provided in the `transferList` for that call.
14511468

1452-
<a id="ERR_MISSING_MODULE"></a>
1453-
### ERR_MISSING_MODULE
1454-
1455-
> Stability: 1 - Experimental
1456-
1457-
An [ES6 module][] could not be resolved.
1458-
14591469
<a id="ERR_MISSING_PLATFORM_FOR_WORKER"></a>
14601470
### ERR_MISSING_PLATFORM_FOR_WORKER
14611471

14621472
The V8 platform used by this instance of Node.js does not support creating
14631473
Workers. This is caused by lack of embedder support for Workers. In particular,
14641474
this error will not occur with standard builds of Node.js.
14651475

1466-
<a id="ERR_MODULE_RESOLUTION_LEGACY"></a>
1467-
### ERR_MODULE_RESOLUTION_LEGACY
1476+
<a id="ERR_MODULE_NOT_FOUND"></a>
1477+
### ERR_MODULE_NOT_FOUND
14681478

14691479
> Stability: 1 - Experimental
14701480
1471-
A failure occurred resolving imports in an [ES6 module][].
1481+
An [ES Module][] could not be resolved.
14721482

14731483
<a id="ERR_MULTIPLE_CALLBACK"></a>
14741484
### ERR_MULTIPLE_CALLBACK
@@ -1555,7 +1565,7 @@ A given value is out of the accepted range.
15551565

15561566
> Stability: 1 - Experimental
15571567
1558-
An attempt was made to `require()` an [ES6 module][].
1568+
An attempt was made to `require()` an [ES Module][].
15591569

15601570
<a id="ERR_SCRIPT_EXECUTION_INTERRUPTED"></a>
15611571
### ERR_SCRIPT_EXECUTION_INTERRUPTED
@@ -2220,10 +2230,17 @@ A non-specific HTTP/2 error has occurred.
22202230
Used in the `repl` in case the old history file is used and an error occurred
22212231
while trying to read and parse it.
22222232

2233+
<a id="ERR_INVALID_REPL_TYPE"></a>
2234+
#### ERR_INVALID_REPL_TYPE
2235+
2236+
> Stability: 1 - Experimental
2237+
2238+
The `--entry-type=...` flag is not compatible with the Node.js REPL.
2239+
22232240
<a id="ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK"></a>
22242241
#### ERR_MISSING_DYNAMIC_INSTANTIATE_HOOK
22252242

2226-
Used when an [ES6 module][] loader hook specifies `format: 'dynamic'` but does
2243+
Used when an [ES Module][] loader hook specifies `format: 'dynamic'` but does
22272244
not provide a `dynamicInstantiate` hook.
22282245

22292246
<a id="ERR_STREAM_HAS_STRINGDECODER"></a>
@@ -2250,7 +2267,6 @@ size.
22502267
This `Error` is thrown when a read is attempted on a TTY `WriteStream`,
22512268
such as `process.stdout.on('data')`.
22522269

2253-
22542270
[`'uncaughtException'`]: process.html#process_event_uncaughtexception
22552271
[`--force-fips`]: cli.html#cli_force_fips
22562272
[`Class: assert.AssertionError`]: assert.html#assert_class_assert_assertionerror
@@ -2293,7 +2309,7 @@ such as `process.stdout.on('data')`.
22932309
[`subprocess.kill()`]: child_process.html#child_process_subprocess_kill_signal
22942310
[`subprocess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
22952311
[`zlib`]: zlib.html
2296-
[ES6 module]: esm.html
2312+
[ES Module]: esm.html
22972313
[ICU]: intl.html#intl_internationalization_support
22982314
[Node.js Error Codes]: #nodejs-error-codes
22992315
[V8's stack trace API]: https://github.com/v8/v8/wiki/Stack-Trace-API

0 commit comments

Comments
 (0)