Skip to content

Commit 5cd78ba

Browse files
guybedfordtargos
authored andcommitted
module: experimental modules runMain separation
PR-URL: #21350 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 9e31684 commit 5cd78ba

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

lib/internal/modules/cjs/loader.js

+13-14
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-
return loader.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
var filename = Module._resolveFilename(request, parent, isMain);
521508

522509
var cachedModule = 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+
return loader.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
};

test/es-module/test-esm-cjs-main.js

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
// Flags: --experimental-modules
21
'use strict';
3-
require('../common');
2+
3+
const common = require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const { spawn } = require('child_process');
46
const assert = require('assert');
5-
exports.asdf = 'asdf';
6-
assert.strictEqual(require.main.exports.asdf, 'asdf');
7+
8+
const entry = fixtures.path('/es-modules/cjs.js');
9+
10+
const child = spawn(process.execPath, ['--experimental-modules', entry]);
11+
let experimentalWarning = false;
12+
let validatedExecution = false;
13+
child.stderr.on('data', (data) => {
14+
if (!experimentalWarning) {
15+
experimentalWarning = true;
16+
return;
17+
}
18+
throw new Error(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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const assert = require('assert');
77

88
common.crashOnUnhandledRejection();
99

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

1212
let error;
1313
(async () => {

test/fixtures/es-modules/cjs.js

+5
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)