Skip to content

Commit 77e8361

Browse files
aduh95UlisesGascon
authored andcommitted
module: execute --import sequentially
PR-URL: #50474Fixes: #50427 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent 2664012 commit 77e8361

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

‎doc/api/cli.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,9 @@ added: v19.0.0
992992

993993
> Stability: 1 - Experimental
994994
995-
Preload the specified module at startup.
995+
Preload the specified module at startup. If the flag is provided several times,
996+
each module will be executed sequentially in the order they appear, starting
997+
with the ones provided in [`NODE_OPTIONS`][].
996998

997999
Follows [ECMAScript module][] resolution rules.
9981000
Use [`--require`][] to load a [CommonJS module][].

‎lib/internal/process/esm_loader.js‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
'use strict';
22

3-
const{
4-
SafePromiseAllReturnVoid,
5-
}=primordials;
6-
73
const{ createModuleLoader }=require('internal/modules/esm/loader');
84
const{ getOptionValue }=require('internal/options');
95
const{
@@ -23,11 +19,9 @@ module.exports ={
2319
constuserImports=getOptionValue('--import');
2420
if(userImports.length>0){
2521
constparentURL=getCWDURL().href;
26-
awaitSafePromiseAllReturnVoid(userImports,(specifier)=>esmLoader.import(
27-
specifier,
28-
parentURL,
29-
kEmptyObject,
30-
));
22+
for(leti=0;i<userImports.length;i++){
23+
awaitesmLoader.import(userImports[i],parentURL,kEmptyObject);
24+
}
3125
}else{
3226
esmLoader.forceLoadHooks();
3327
}

‎test/es-module/test-esm-import-flag.mjs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,37 @@ describe('import modules using --import',{concurrency: true }, () =>{
182182
assert.strictEqual(code,0);
183183
assert.strictEqual(signal,null);
184184
});
185+
186+
it('should import files sequentially',async()=>{
187+
const{ code, signal, stderr, stdout }=awaitspawnPromisified(
188+
execPath,
189+
[
190+
'--import',fixtures.fileURL('es-modules','esm-top-level-await.mjs'),
191+
'--import',fixtures.fileURL('es-modules','print-3.mjs'),
192+
fixtures.path('empty.js'),
193+
]
194+
);
195+
196+
assert.strictEqual(stderr,'');
197+
assert.match(stdout,/^1\r?\n2\r?\n3\r?\n$/);
198+
assert.strictEqual(code,0);
199+
assert.strictEqual(signal,null);
200+
});
201+
202+
it('should import files from the env before ones from the CLI',async()=>{
203+
const{ code, signal, stderr, stdout }=awaitspawnPromisified(
204+
execPath,
205+
[
206+
'--import',fixtures.fileURL('es-modules','print-3.mjs'),
207+
fixtures.path('empty.js'),
208+
],
209+
{env: { ...process.env,NODE_OPTIONS: `--import ${JSON.stringify(fixtures.fileURL('es-modules','esm-top-level-await.mjs'))}`}}
210+
);
211+
212+
assert.strictEqual(stderr,'');
213+
assert.match(stdout,/^1\r?\n2\r?\n3\r?\n$/);
214+
assert.strictEqual(code,0);
215+
assert.strictEqual(signal,null);
216+
217+
});
185218
});
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import{setImmediate}from'node:timers/promises';
1+
import{setTimeout}from'node:timers/promises';
22

3-
awaitsetImmediate();
3+
// Waiting some arbitrary amount of time to make sure other tasks won't start
4+
// executing in the mean time.
5+
awaitsetTimeout(9);
46
console.log(1);
57
console.log(2);

0 commit comments

Comments
(0)