Skip to content

Commit 3606a0a

Browse files
aduh95targos
authored andcommitted
module: execute --import sequentially
PR-URL: #50474 Fixes: #50427 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 773cfa5 commit 3606a0a

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

doc/api/cli.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,9 @@ added:
10071007

10081008
> Stability: 1 - Experimental
10091009
1010-
Preload the specified module at startup.
1010+
Preload the specified module at startup. If the flag is provided several times,
1011+
each module will be executed sequentially in the order they appear, starting
1012+
with the ones provided in [`NODE_OPTIONS`][].
10111013

10121014
Follows [ECMAScript module][] resolution rules.
10131015
Use [`--require`][] to load a [CommonJS module][].

lib/internal/process/esm_loader.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
'use strict';
22

3-
const {
4-
SafePromiseAllReturnVoid,
5-
} = primordials;
6-
73
const { createModuleLoader } = require('internal/modules/esm/loader');
84
const { getOptionValue } = require('internal/options');
95
const {
@@ -23,11 +19,9 @@ module.exports = {
2319
const userImports = getOptionValue('--import');
2420
if (userImports.length > 0) {
2521
const parentURL = getCWDURL().href;
26-
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
27-
specifier,
28-
parentURL,
29-
kEmptyObject,
30-
));
22+
for (let i = 0; i < userImports.length; i++) {
23+
await esmLoader.import(userImports[i], parentURL, kEmptyObject);
24+
}
3125
} else {
3226
esmLoader.forceLoadHooks();
3327
}

test/es-module/test-esm-import-flag.mjs

+33
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,37 @@ describe('import modules using --import', { concurrency: true }, () => {
182182
assert.strictEqual(code, 0);
183183
assert.strictEqual(signal, null);
184184
});
185+
186+
it('should import files sequentially', async () => {
187+
const { code, signal, stderr, stdout } = await spawnPromisified(
188+
execPath,
189+
[
190+
'--import', fixtures.fileURL('es-modules', 'esm-top-level-await.mjs'),
191+
'--import', fixtures.fileURL('es-modules', 'print-3.mjs'),
192+
fixtures.path('empty.js'),
193+
]
194+
);
195+
196+
assert.strictEqual(stderr, '');
197+
assert.match(stdout, /^1\r?\n2\r?\n3\r?\n$/);
198+
assert.strictEqual(code, 0);
199+
assert.strictEqual(signal, null);
200+
});
201+
202+
it('should import files from the env before ones from the CLI', async () => {
203+
const { code, signal, stderr, stdout } = await spawnPromisified(
204+
execPath,
205+
[
206+
'--import', fixtures.fileURL('es-modules', 'print-3.mjs'),
207+
fixtures.path('empty.js'),
208+
],
209+
{ env: { ...process.env, NODE_OPTIONS: `--import ${JSON.stringify(fixtures.fileURL('es-modules', 'esm-top-level-await.mjs'))}` } }
210+
);
211+
212+
assert.strictEqual(stderr, '');
213+
assert.match(stdout, /^1\r?\n2\r?\n3\r?\n$/);
214+
assert.strictEqual(code, 0);
215+
assert.strictEqual(signal, null);
216+
217+
});
185218
});
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { setImmediate } from 'node:timers/promises';
1+
import { setTimeout } from 'node:timers/promises';
22

3-
await setImmediate();
3+
// Waiting some arbitrary amount of time to make sure other tasks won't start
4+
// executing in the mean time.
5+
await setTimeout(9);
46
console.log(1);
57
console.log(2);

0 commit comments

Comments
 (0)