From 5cd78ba911c7d22c4e83865dda9d0a35bede6c5c Mon Sep 17 00:00:00 2001 From: guybedford Date: Fri, 15 Jun 2018 14:23:44 +0200 Subject: [PATCH] module: experimental modules runMain separation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/21350 Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: Gus Caplan Reviewed-By: Bradley Farias Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum --- lib/internal/modules/cjs/loader.js | 27 ++++++++++++------------ test/es-module/test-esm-cjs-main.js | 29 ++++++++++++++++++++++---- test/es-module/test-esm-error-cache.js | 2 +- test/fixtures/es-modules/cjs.js | 5 +++++ 4 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 test/fixtures/es-modules/cjs.js diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 8bb5f9268437fc..c218dc2cac2a50 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -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(); }; diff --git a/test/es-module/test-esm-cjs-main.js b/test/es-module/test-esm-cjs-main.js index ed6730aec1d2a2..8de923d37b1887 100644 --- a/test/es-module/test-esm-cjs-main.js +++ b/test/es-module/test-esm-cjs-main.js @@ -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); +})); diff --git a/test/es-module/test-esm-error-cache.js b/test/es-module/test-esm-error-cache.js index 80431512d75773..22ffa9abe96a37 100644 --- a/test/es-module/test-esm-error-cache.js +++ b/test/es-module/test-esm-error-cache.js @@ -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 () => { diff --git a/test/fixtures/es-modules/cjs.js b/test/fixtures/es-modules/cjs.js new file mode 100644 index 00000000000000..3a09c83abff9e8 --- /dev/null +++ b/test/fixtures/es-modules/cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +// test we can use commonjs require +require('path'); +console.log('executed');