-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
esm: add --import flag
#43942
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
esm: add --import flag
#43942
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
--import flag
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -445,7 +445,8 @@ Only the root context is supported. There is no guarantee that | |
| `globalThis.Array` is indeed the default intrinsic reference. Code may break | ||
| under this flag. | ||
|
|
||
| To allow polyfills to be added, `--require` runs before freezing intrinsics. | ||
| To allow polyfills to be added, | ||
| [`--require`][] and [`--import`][] both run before freezing intrinsics. | ||
|
|
||
| ### `--force-node-api-uncaught-exceptions-policy` | ||
|
|
||
|
|
@@ -594,6 +595,18 @@ added: v0.11.15 | |
|
|
||
| Specify ICU data load path. (Overrides `NODE_ICU_DATA`.) | ||
|
|
||
| ### `--import=module` | ||
|
|
||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
|
|
||
| Preload the specified module at startup. | ||
|
|
||
| Follows [ECMAScript module][] resolution rules. | ||
| Use [`--require`][] to load a [CommonJS module][]. | ||
|
MoLow marked this conversation as resolved.
Comment on lines
+604
to
+607
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should document somewhere that
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems unfortunate. I’d expect loading order to match argument order.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be much harder to implement since
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, we probably shouldn't allow both to be provided in a mixed order - iow, if there's a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is currently no way to know in what order the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there was no way to know, how could we ensure require ordering now? (which we do)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only know the relative order for flags that allow multiple values (they are pushed in an array), but the C++ parser doesn't give us more information. |
||
| modules preloaded with `--require` will run before modules preloaded with `--import` | ||
|
MoLow marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### `--input-type=type` | ||
|
|
||
| <!-- YAML | ||
|
|
@@ -1527,8 +1540,9 @@ Preload the specified module at startup. | |
| Follows `require()`'s module resolution | ||
| rules. `module` may be either a path to a file, or a node module name. | ||
|
|
||
| Only CommonJS modules are supported. Attempting to preload a | ||
| ES6 Module using `--require` will fail with an error. | ||
| Only CommonJS modules are supported. | ||
| Use [`--import`][] to preload an [ECMAScript module][]. | ||
| modules preloaded with `--require` will run before modules preloaded with `--import` | ||
|
MoLow marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### `-v`, `--version` | ||
|
|
||
|
|
@@ -1682,6 +1696,7 @@ Node.js options that are allowed are: | |
| * `--heapsnapshot-signal` | ||
| * `--http-parser` | ||
| * `--icu-data-dir` | ||
| * `--import` | ||
| * `--input-type` | ||
| * `--insecure-http-parser` | ||
| * `--inspect-brk` | ||
|
|
@@ -2086,7 +2101,9 @@ done | |
| [#42511]: https://github.com/nodejs/node/issues/42511 | ||
| [Chrome DevTools Protocol]: https://chromedevtools.github.io/devtools-protocol/ | ||
| [CommonJS]: modules.md | ||
| [CommonJS module]: modules.md | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you have two links to the same page here |
||
| [CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent | ||
| [ECMAScript module]: esm.md#modules-ecmascript-modules | ||
| [ECMAScript module loader]: esm.md#loaders | ||
| [Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API | ||
| [Modules loaders]: packages.md#modules-loaders | ||
|
|
@@ -2103,8 +2120,10 @@ done | |
| [`--diagnostic-dir`]: #--diagnostic-dirdirectory | ||
| [`--experimental-wasm-modules`]: #--experimental-wasm-modules | ||
| [`--heap-prof-dir`]: #--heap-prof-dir | ||
| [`--import`]: #--importmodule | ||
| [`--openssl-config`]: #--openssl-configfile | ||
| [`--redirect-warnings`]: #--redirect-warningsfile | ||
| [`--require`]: #-r---require-module | ||
| [`Atomics.wait()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/wait | ||
| [`Buffer`]: buffer.md#class-buffer | ||
| [`CRYPTO_secure_malloc_init`]: https://www.openssl.org/docs/man1.1.0/man3/CRYPTO_secure_malloc_init.html | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| // If user passed `-c` or `--check` arguments to Node, check its syntax | ||
| // instead of actually running the file. | ||
|
|
||
| const { getOptionValue } = require('internal/options'); | ||
| const { | ||
| prepareMainThreadExecution | ||
| } = require('internal/bootstrap/pre_execution'); | ||
|
|
@@ -36,18 +37,29 @@ if (process.argv[1] && process.argv[1] !== '-') { | |
|
|
||
| markBootstrapComplete(); | ||
|
|
||
| checkSyntax(source, filename); | ||
| loadESMIfNeeded(() => checkSyntax(source, filename)); | ||
| } else { | ||
| markBootstrapComplete(); | ||
|
|
||
| readStdin((code) => { | ||
| loadESMIfNeeded(() => readStdin((code) => { | ||
| checkSyntax(code, '[stdin]'); | ||
| }); | ||
| })); | ||
| } | ||
|
|
||
| async function checkSyntax(source, filename) { | ||
| function loadESMIfNeeded(cb) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: not sure about the name. It sounds like it would be the primary function here (or check a lot more than it does), but it's a helper.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any suggestions? |
||
| const { getOptionValue } = require('internal/options'); | ||
| let isModule = false; | ||
| const hasModulePreImport = getOptionValue('--import').length > 0; | ||
|
|
||
| if (hasModulePreImport) { | ||
| const { loadESM } = require('internal/process/esm_loader'); | ||
| loadESM(cb); | ||
| return; | ||
| } | ||
| cb(); | ||
| } | ||
|
|
||
| async function checkSyntax(source, filename) { | ||
| let isModule = true; | ||
| if (filename === '[stdin]' || filename === '[eval]') { | ||
| isModule = getOptionValue('--input-type') === 'module'; | ||
| } else { | ||
|
|
@@ -57,11 +69,14 @@ async function checkSyntax(source, filename) { | |
| const format = await defaultGetFormat(url); | ||
| isModule = format === 'module'; | ||
| } | ||
|
|
||
| if (isModule) { | ||
| const { ModuleWrap } = internalBinding('module_wrap'); | ||
| new ModuleWrap(filename, undefined, source, 0, 0); | ||
| return; | ||
| } | ||
|
|
||
| wrapSafe(filename, source); | ||
| const { loadESM } = require('internal/process/esm_loader'); | ||
| const { handleMainPromise } = require('internal/modules/run_main'); | ||
| handleMainPromise(loadESM((loader) => wrapSafe(filename, source))); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.