Skip to content

Commit

Permalink
module: execute --import sequentially
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
aduh95 authored and UlisesGascon committed Dec 11, 2023
1 parent 2664012 commit 77e8361
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
4 changes: 3 additions & 1 deletion doc/api/cli.md
Expand Up @@ -992,7 +992,9 @@ added: v19.0.0

> 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][].
Expand Down
12 changes: 3 additions & 9 deletions lib/internal/process/esm_loader.js
@@ -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
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);

});
});
6 changes: 4 additions & 2 deletions test/fixtures/es-modules/esm-top-level-await.mjs
@@ -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);

0 comments on commit 77e8361

Please sign in to comment.