From 411f61c2f270f51f19b60fb7fcca10f68def3ec6 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 30 Oct 2023 11:20:14 +0100 Subject: [PATCH 1/2] module: execute `--import` sequentially --- doc/api/cli.md | 4 ++- lib/internal/process/esm_loader.js | 12 ++----- test/es-module/test-esm-import-flag.mjs | 33 +++++++++++++++++++ .../es-modules/esm-top-level-await.mjs | 4 +-- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 3cb36818ae4997..01d926c0ead280 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -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, +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][]. diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js index a3451ddab307f2..47c909c9e55435 100644 --- a/lib/internal/process/esm_loader.js +++ b/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 { @@ -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(); } diff --git a/test/es-module/test-esm-import-flag.mjs b/test/es-module/test-esm-import-flag.mjs index 2f6f4dd48b49a8..ede317b1d585de 100644 --- a/test/es-module/test-esm-import-flag.mjs +++ b/test/es-module/test-esm-import-flag.mjs @@ -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); + + }); }); diff --git a/test/fixtures/es-modules/esm-top-level-await.mjs b/test/fixtures/es-modules/esm-top-level-await.mjs index 4a4745afafaaf2..03f8a36683e97a 100644 --- a/test/fixtures/es-modules/esm-top-level-await.mjs +++ b/test/fixtures/es-modules/esm-top-level-await.mjs @@ -1,5 +1,5 @@ -import { setImmediate } from 'node:timers/promises'; +import { setTimeout } from 'node:timers/promises'; -await setImmediate(); +await setTimeout(9); console.log(1); console.log(2); From 174c719db1a9a907e71b93dbe698c4b1cec8a129 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 31 Oct 2023 13:10:20 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> --- doc/api/cli.md | 2 +- test/fixtures/es-modules/esm-top-level-await.mjs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 01d926c0ead280..5f29c497426db6 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1007,7 +1007,7 @@ added: > Stability: 1 - Experimental -Preload the specified module at startup. If that flag is provided several times, +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`][]. diff --git a/test/fixtures/es-modules/esm-top-level-await.mjs b/test/fixtures/es-modules/esm-top-level-await.mjs index 03f8a36683e97a..672927f7753ec3 100644 --- a/test/fixtures/es-modules/esm-top-level-await.mjs +++ b/test/fixtures/es-modules/esm-top-level-await.mjs @@ -1,5 +1,7 @@ import { setTimeout } from 'node:timers/promises'; +// 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);