Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor experimental-modules check into runMain #21350

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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();
};
Expand Down
29 changes: 25 additions & 4 deletions test/es-module/test-esm-cjs-main.js
Original file line number Diff line number Diff line change
@@ -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);
}));
2 changes: 1 addition & 1 deletion test/es-module/test-esm-error-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/es-modules/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

// test we can use commonjs require
require('path');
console.log('executed');