Skip to content

Commit

Permalink
module: experimental modules runMain separation
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
guybedford committed Jun 21, 2018
1 parent 182ee78 commit 883e1cd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
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');

0 comments on commit 883e1cd

Please sign in to comment.