Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit d89854b

Browse files
guybedfordnodejs-ci
authored andcommitted
esm: top-level --type, -m flags, spec updates
1 parent 8b077c8 commit d89854b

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

doc/api/cli.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,18 @@ added: v2.4.0
485485

486486
Track heap object allocations for heap snapshots.
487487

488+
### `-m`, `--type=type`
489+
490+
When using `--experimental-modules`, this informs the module resolution type
491+
to interpret the top-level entry into Node.js.
492+
493+
Works with stdin, `--eval`, `--print` as well as standard execution.
494+
495+
Valid values are `"commonjs"` and `"module"`, where the default is to infer
496+
from the file extension and package type boundary.
497+
498+
`-m` is an alias for `--type=module`.
499+
488500
### `--use-bundled-ca`, `--use-openssl-ca`
489501
<!-- YAML
490502
added: v6.11.0

doc/api/esm.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ The algorithm to load an ES module specifier is given through the
158158
module specifier relative to a parentURL, in addition to the unique module
159159
format for that resolved URL given by the **ESM_FORMAT** routine.
160160
161-
The _"esm"_ format is returned for an ECMAScript Module, while the
162-
_"legacy"_ format is used to indicate loading through the legacy
161+
The _"module"_ format is returned for an ECMAScript Module, while the
162+
_"commonjs"_ format is used to indicate loading through the legacy
163163
CommonJS loader. Additional formats such as _"wasm"_ or _"addon"_ can be
164164
extended in future updates.
165165
@@ -168,6 +168,12 @@ of these top-level routines.
168168
169169
_isMain_ is **true** when resolving the Node.js application entry point.
170170
171+
If the top-level `--type` is _"commonjs"_, then the ESM resolver is skipped
172+
entirely for the CommonJS loader.
173+
174+
If the top-level `--type` is _"module"_, then the ESM resolver is used
175+
as described here, with the conditional `--type` check in **ESM_FORMAT**.
176+
171177
**ESM_RESOLVE(_specifier_, _parentURL_, _isMain_)**
172178
> 1. Let _resolvedURL_ be **undefined**.
173179
> 1. If _specifier_ is a valid URL, then
@@ -234,7 +240,7 @@ PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
234240
> _pjson.main_.
235241
> 1. If the file at _resolvedMain_ exists, then
236242
> 1. Return _resolvedMain_.
237-
> 1. If _pjson.type_ is equal to _"esm"_, then
243+
> 1. If _pjson.type_ is equal to _"module"_, then
238244
> 1. Throw a _Module Not Found_ error.
239245
> 1. Let _legacyMainURL_ be the result applying the legacy
240246
> **LOAD_AS_DIRECTORY** CommonJS resolver to _packageURL_, throwing a
@@ -245,18 +251,24 @@ PACKAGE_MAIN_RESOLVE(_packageURL_, _pjson_)
245251
246252
**ESM_FORMAT(_url_, _isMain_)**
247253
> 1. Assert: _url_ corresponds to an existing file.
254+
> 1. If _isMain_ is **true** and the `--type` flag is _"module"_, then
255+
> 1. If _url_ ends with _".cjs"_, then
256+
> 1. Throw an _Invalid File Extension_ error.
257+
> 1. Return _"module"_.
248258
> 1. Let _pjson_ be the result of **READ_PACKAGE_BOUNDARY**(_url_).
249259
> 1. If _pjson_ is **null** and _isMain_ is **true**, then
250-
> 1. Return _"legacy"_.
251-
> 1. If _pjson.type_ exists and is _"esm"_, then
260+
> 1. Return _"commonjs"_.
261+
> 1. If _pjson.type_ exists and is _"module"_, then
262+
> 1. If _url_ ends in _".cjs"_, then
263+
> 1. Return _"commonjs"_.
252264
> 1. If _url_ does not end in _".js"_ or _".mjs"_, then
253265
> 1. Throw an _Unsupported File Extension_ error.
254-
> 1. Return _"esm"_.
266+
> 1. Return _"module"_.
255267
> 1. Otherwise,
256268
> 1. If _url_ ends in _".mjs"_, then
257-
> 1. Return _"esm"_.
269+
> 1. Return _"module"_.
258270
> 1. Otherwise,
259-
> 1. Return _"legacy"_.
271+
> 1. Return _"commonjs"_.
260272
261273
READ_PACKAGE_BOUNDARY(_url_)
262274
> 1. Let _boundaryURL_ be _url_.

doc/node.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ Print stack traces for process warnings (including deprecations).
267267
.It Fl -track-heap-objects
268268
Track heap object allocations for heap snapshots.
269269
.
270+
.It Fl -type Ns = Ns Ar type
271+
Set the top-level module resolution type.
272+
.
270273
.It Fl -use-bundled-ca , Fl -use-openssl-ca
271274
Use bundled Mozilla CA store as supplied by current Node.js version or use OpenSSL's default CA store.
272275
The default store is selectable at build-time.

0 commit comments

Comments
 (0)