module: execute --import sequentially

PR-URL: https://github.com/nodejs/node/pull/50474
Fixes: https://github.com/nodejs/node/issues/50427
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit is contained in:
Antoine du Hamel 2023-11-01 13:38:01 +01:00 committed by GitHub
parent 86afefafda
commit a3e09b3fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 12 deletions

View File

@ -1007,7 +1007,9 @@ added:
> Stability: 1 - Experimental
Preload the specified module at startup.
Preload the specified module at startup. If the flag is provided several times,
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][].

View File

@ -1,9 +1,5 @@
'use strict';
const {
SafePromiseAllReturnVoid,
} = primordials;
const { createModuleLoader } = require('internal/modules/esm/loader');
const { getOptionValue } = require('internal/options');
const {
@ -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();
}

View File

@ -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);
});
});

View File

@ -1,5 +1,7 @@
import { setImmediate } from 'node:timers/promises';
import { setTimeout } from 'node:timers/promises';
await setImmediate();
// Waiting some arbitrary amount of time to make sure other tasks won't start
// executing in the mean time.
await setTimeout(9);
console.log(1);
console.log(2);