From 3606a0a8487460e18061d207d5d7e180f0149d8b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 1 Nov 2023 13:38:01 +0100 Subject: [PATCH] module: execute `--import` sequentially MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/50474 Fixes: https://github.com/nodejs/node/issues/50427 Reviewed-By: Jacob Smith Reviewed-By: Michaƫl Zasso Reviewed-By: Moshe Atlow --- 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 | 6 ++-- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/doc/api/cli.md b/doc/api/cli.md index 3cb36818ae4997..5f29c497426db6 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 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][]. 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..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 { 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);