Skip to content
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

module: execute --import sequentially #50474

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,9 @@ added:

> Stability: 1 - Experimental

Preload the specified module at startup.
Preload the specified module at startup. If that flag is provided several times,
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
each module will be executed sequentially in the order they appear, starting
with the ones provided in [`NODE_OPTIONS`][].

Follows [ECMAScript module][] resolution rules.
Use [`--require`][] to load a [CommonJS module][].
Expand Down
12 changes: 3 additions & 9 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

const {
SafePromiseAllReturnVoid,
} = primordials;

const { createModuleLoader } = require('internal/modules/esm/loader');
const { getOptionValue } = require('internal/options');
const {
Expand All @@ -23,11 +19,9 @@ module.exports = {
const userImports = getOptionValue('--import');
if (userImports.length > 0) {
const parentURL = getCWDURL().href;
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
specifier,
parentURL,
kEmptyObject,
));
for (let i = 0; i < userImports.length; i++) {
await esmLoader.import(userImports[i], parentURL, kEmptyObject);
}
} else {
esmLoader.forceLoadHooks();
}
Expand Down
33 changes: 33 additions & 0 deletions test/es-module/test-esm-import-flag.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,37 @@ describe('import modules using --import', { concurrency: true }, () => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should import files sequentially', async () => {
const { code, signal, stderr, stdout } = await spawnPromisified(
execPath,
[
'--import', fixtures.fileURL('es-modules', 'esm-top-level-await.mjs'),
'--import', fixtures.fileURL('es-modules', 'print-3.mjs'),
fixtures.path('empty.js'),
]
);

assert.strictEqual(stderr, '');
assert.match(stdout, /^1\r?\n2\r?\n3\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should import files from the env before ones from the CLI', async () => {
const { code, signal, stderr, stdout } = await spawnPromisified(
execPath,
[
'--import', fixtures.fileURL('es-modules', 'print-3.mjs'),
fixtures.path('empty.js'),
],
{ env: { ...process.env, NODE_OPTIONS: `--import ${JSON.stringify(fixtures.fileURL('es-modules', 'esm-top-level-await.mjs'))}` } }
);

assert.strictEqual(stderr, '');
assert.match(stdout, /^1\r?\n2\r?\n3\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);

});
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
});
4 changes: 2 additions & 2 deletions test/fixtures/es-modules/esm-top-level-await.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setImmediate } from 'node:timers/promises';
import { setTimeout } from 'node:timers/promises';

await setImmediate();
await setTimeout(9);
console.log(1);
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
console.log(2);
Loading