Skip to content

Commit 5cd78ba

Browse files
guybedfordtargos
authored andcommitted
module: experimental modules runMain separation
PR-URL: #21350 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 9e31684 commit 5cd78ba

4 files changed

Lines changed: 44 additions & 19 deletions

File tree

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,6 @@ Module._load = function(request, parent, isMain) {
504504
debug('Module._load REQUEST %s parent: %s',request,parent.id);
505505
}
506506

507-
if(experimentalModules&&isMain){
508-
if(asyncESM===undefined)lazyLoadESM();
509-
asyncESM.loaderPromise.then((loader)=>{
510-
returnloader.import(getURLFromFilePath(request).pathname);
511-
})
512-
.catch((e)=>{
513-
decorateErrorStack(e);
514-
console.error(e);
515-
process.exit(1);
516-
});
517-
return;
518-
}
519-
520507
varfilename=Module._resolveFilename(request,parent,isMain);
521508

522509
varcachedModule=Module._cache[filename];
@@ -741,7 +728,19 @@ if (experimentalModules) {
741728
// bootstrap main module.
742729
Module.runMain=function(){
743730
// Load the main module--the command line argument.
744-
Module._load(process.argv[1],null,true);
731+
if(experimentalModules){
732+
if(asyncESM===undefined)lazyLoadESM();
733+
asyncESM.loaderPromise.then((loader)=>{
734+
returnloader.import(getURLFromFilePath(process.argv[1]).pathname);
735+
})
736+
.catch((e)=>{
737+
decorateErrorStack(e);
738+
console.error(e);
739+
process.exit(1);
740+
});
741+
}else{
742+
Module._load(process.argv[1],null,true);
743+
}
745744
// Handle any nextTicks added in the first tick of the program
746745
process._tickCallback();
747746
};
Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
// Flags: --experimental-modules
21
'use strict';
3-
require('../common');
2+
3+
constcommon=require('../common');
4+
constfixtures=require('../common/fixtures');
5+
const{ spawn }=require('child_process');
46
constassert=require('assert');
5-
exports.asdf='asdf';
6-
assert.strictEqual(require.main.exports.asdf,'asdf');
7+
8+
constentry=fixtures.path('/es-modules/cjs.js');
9+
10+
constchild=spawn(process.execPath,['--experimental-modules',entry]);
11+
letexperimentalWarning=false;
12+
letvalidatedExecution=false;
13+
child.stderr.on('data',(data)=>{
14+
if(!experimentalWarning){
15+
experimentalWarning=true;
16+
return;
17+
}
18+
thrownewError(data.toString());
19+
});
20+
child.stdout.on('data',(data)=>{
21+
assert.strictEqual(data.toString(),'executed\n');
22+
validatedExecution=true;
23+
});
24+
child.on('close',common.mustCall((code,stdout)=>{
25+
assert.strictEqual(validatedExecution,true);
26+
assert.strictEqual(code,0);
27+
}));

‎test/es-module/test-esm-error-cache.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77

88
common.crashOnUnhandledRejection();
99

10-
constfile='../../fixtures/syntax/bad_syntax.js';
10+
constfile='../fixtures/syntax/bad_syntax.js';
1111

1212
leterror;
1313
(async()=>{

‎test/fixtures/es-modules/cjs.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
// test we can use commonjs require
4+
require('path');
5+
console.log('executed');

0 commit comments

Comments
 (0)