Skip to content

Commit

Permalink
module: execute --import sequentially
Browse files Browse the repository at this point in the history
PR-URL: nodejs#50474
Fixes: nodejs#50427
Reviewed-By: Jacob Smith <[email protected]>
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
  • Loading branch information
aduh95 authored and anonrig committed Nov 9, 2023
1 parent 05fe555 commit b672c61
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
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 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
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);

});
});
6 changes: 4 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,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 b672c61

Please sign in to comment.