module: experimental modules runMain separation
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
182ee78164
commit
883e1cd21d
@ -504,19 +504,6 @@ Module._load = function(request, parent, isMain) {
|
||||
debug('Module._load REQUEST %s parent: %s', request, parent.id);
|
||||
}
|
||||
|
||||
if (experimentalModules && isMain) {
|
||||
if (asyncESM === undefined) lazyLoadESM();
|
||||
asyncESM.loaderPromise.then((loader) => {
|
||||
return loader.import(getURLFromFilePath(request).pathname);
|
||||
})
|
||||
.catch((e) => {
|
||||
decorateErrorStack(e);
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
var filename = Module._resolveFilename(request, parent, isMain);
|
||||
|
||||
var cachedModule = Module._cache[filename];
|
||||
@ -741,7 +728,19 @@ if (experimentalModules) {
|
||||
// bootstrap main module.
|
||||
Module.runMain = function() {
|
||||
// Load the main module--the command line argument.
|
||||
Module._load(process.argv[1], null, true);
|
||||
if (experimentalModules) {
|
||||
if (asyncESM === undefined) lazyLoadESM();
|
||||
asyncESM.loaderPromise.then((loader) => {
|
||||
return loader.import(getURLFromFilePath(process.argv[1]).pathname);
|
||||
})
|
||||
.catch((e) => {
|
||||
decorateErrorStack(e);
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
} else {
|
||||
Module._load(process.argv[1], null, true);
|
||||
}
|
||||
// Handle any nextTicks added in the first tick of the program
|
||||
process._tickCallback();
|
||||
};
|
||||
|
@ -1,6 +1,27 @@
|
||||
// Flags: --experimental-modules
|
||||
'use strict';
|
||||
require('../common');
|
||||
|
||||
const common = require('../common');
|
||||
const fixtures = require('../common/fixtures');
|
||||
const { spawn } = require('child_process');
|
||||
const assert = require('assert');
|
||||
exports.asdf = 'asdf';
|
||||
assert.strictEqual(require.main.exports.asdf, 'asdf');
|
||||
|
||||
const entry = fixtures.path('/es-modules/cjs.js');
|
||||
|
||||
const child = spawn(process.execPath, ['--experimental-modules', entry]);
|
||||
let experimentalWarning = false;
|
||||
let validatedExecution = false;
|
||||
child.stderr.on('data', (data) => {
|
||||
if (!experimentalWarning) {
|
||||
experimentalWarning = true;
|
||||
return;
|
||||
}
|
||||
throw new Error(data.toString());
|
||||
});
|
||||
child.stdout.on('data', (data) => {
|
||||
assert.strictEqual(data.toString(), 'executed\n');
|
||||
validatedExecution = true;
|
||||
});
|
||||
child.on('close', common.mustCall((code, stdout) => {
|
||||
assert.strictEqual(validatedExecution, true);
|
||||
assert.strictEqual(code, 0);
|
||||
}));
|
||||
|
@ -7,7 +7,7 @@ const assert = require('assert');
|
||||
|
||||
common.crashOnUnhandledRejection();
|
||||
|
||||
const file = '../../fixtures/syntax/bad_syntax.js';
|
||||
const file = '../fixtures/syntax/bad_syntax.js';
|
||||
|
||||
let error;
|
||||
(async () => {
|
||||
|
5
test/fixtures/es-modules/cjs.js
vendored
Normal file
5
test/fixtures/es-modules/cjs.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
// test we can use commonjs require
|
||||
require('path');
|
||||
console.log('executed');
|
Loading…
x
Reference in New Issue
Block a user