From b1094dbe19f31f7a69ad16d193748f610b159073 Mon Sep 17 00:00:00 2001 From: guybedford Date: Tue, 28 Aug 2018 17:28:46 +0200 Subject: esm: phase two of new esm implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR updates the current `--experimental-modules` implementation based on the work of the modules team and reflects Phase 2 of our new modules plan. The largest differences from the current implementation include * `packge.type` which can be either `module` or `commonjs` - `type: "commonjs"`: - `.js` is parsed as commonjs - default for entry point without an extension is commonjs - `type: "module"`: - `.js` is parsed as esm - does not support loading JSON or Native Module by default - default for entry point without an extension is esm * `--entry-type=[mode]` - allows you set the type on entry point. * A new file extension `.cjs`. - this is specifically to support importing commonjs in the `module` mode. - this is only in the esm loader, the commonjs loader remains untouched, but the extension will work in the old loader if you use the full file path. * `--es-module-specifier-resolution=[type]` - options are `explicit` (default) and `node` - by default our loader will not allow for optional extensions in the import, the path for a module must include the extension if there is one - by default our loader will not allow for importing directories that have an index file - developers can use `--es-module-specifier-resolution=node` to enable the commonjs specifier resolution algorithm - This is not a “feature” but rather an implementation for experimentation. It is expected to change before the flag is removed * `--experimental-json-loader` - the only way to import json when `"type": "module"` - when enable all `import 'thing.json'` will go through the experimental loader independent of mode - based on https://github.com/whatwg/html/issues/4315 * You can use `package.main` to set an entry point for a module - the file extensions used in main will be resolved based on the `type` of the module Refs: https://github.com/nodejs/modules/blob/master/doc/plan-for-new-modules-implementation.md Refs: https://github.com/GeoffreyBooth/node-import-file-specifier-resolution-proposal Refs: https://github.com/nodejs/modules/pull/180 Refs: https://github.com/nodejs/ecmascript-modules/pull/6 Refs: https://github.com/nodejs/ecmascript-modules/pull/12 Refs: https://github.com/nodejs/ecmascript-modules/pull/28 Refs: https://github.com/nodejs/modules/issues/255 Refs: https://github.com/whatwg/html/issues/4315 Refs: https://github.com/w3c/webcomponents/issues/770 Co-authored-by: Myles Borins Co-authored-by: John-David Dalton Co-authored-by: Evan Plaice Co-authored-by: Geoffrey Booth Co-authored-by: Michaël Zasso PR-URL: https://github.com/nodejs/node/pull/26745 Reviewed-By: Gus Caplan Reviewed-By: Guy Bedford Reviewed-By: Ben Coe Reviewed-By: James M Snell Reviewed-By: Joyee Cheung Reviewed-By: Ruben Bridgewater Reviewed-By: Сковорода Никита Андреевич --- lib/internal/main/check_syntax.js | 36 +++++++++++++++++++++++++++++++----- lib/internal/main/eval_stdin.js | 6 +++++- lib/internal/main/eval_string.js | 10 +++++++--- lib/internal/main/repl.js | 6 ++++++ 4 files changed, 49 insertions(+), 9 deletions(-) (limited to 'lib/internal/main') diff --git a/lib/internal/main/check_syntax.js b/lib/internal/main/check_syntax.js index 7df70b2720..8c73d522ed 100644 --- a/lib/internal/main/check_syntax.js +++ b/lib/internal/main/check_syntax.js @@ -11,12 +11,18 @@ const { readStdin } = require('internal/process/execution'); -const CJSModule = require('internal/modules/cjs/loader'); +const { pathToFileURL } = require('url'); + const vm = require('vm'); const { stripShebang, stripBOM } = require('internal/modules/cjs/helpers'); +let CJSModule; +function CJSModuleInit() { + if (!CJSModule) + CJSModule = require('internal/modules/cjs/loader'); +} if (process.argv[1] && process.argv[1] !== '-') { // Expand process.argv[1] into a full path. @@ -25,7 +31,7 @@ if (process.argv[1] && process.argv[1] !== '-') { // TODO(joyeecheung): not every one of these are necessary prepareMainThreadExecution(); - + CJSModuleInit(); // Read the source. const filename = CJSModule._resolveFilename(process.argv[1]); @@ -34,20 +40,40 @@ if (process.argv[1] && process.argv[1] !== '-') { markBootstrapComplete(); - checkScriptSyntax(source, filename); + checkSyntax(source, filename); } else { // TODO(joyeecheung): not every one of these are necessary prepareMainThreadExecution(); + CJSModuleInit(); markBootstrapComplete(); readStdin((code) => { - checkScriptSyntax(code, '[stdin]'); + checkSyntax(code, '[stdin]'); }); } -function checkScriptSyntax(source, filename) { +function checkSyntax(source, filename) { // Remove Shebang. source = stripShebang(source); + + const { getOptionValue } = require('internal/options'); + const experimentalModules = getOptionValue('--experimental-modules'); + if (experimentalModules) { + let isModule = false; + if (filename === '[stdin]' || filename === '[eval]') { + isModule = getOptionValue('--entry-type') === 'module'; + } else { + const resolve = require('internal/modules/esm/default_resolve'); + const { format } = resolve(pathToFileURL(filename).toString()); + isModule = format === 'module'; + } + if (isModule) { + const { ModuleWrap } = internalBinding('module_wrap'); + new ModuleWrap(source, filename); + return; + } + } + // Remove BOM. source = stripBOM(source); // Wrap it. diff --git a/lib/internal/main/eval_stdin.js b/lib/internal/main/eval_stdin.js index 2a2ef6d38a..4face9e61e 100644 --- a/lib/internal/main/eval_stdin.js +++ b/lib/internal/main/eval_stdin.js @@ -7,6 +7,7 @@ const { } = require('internal/bootstrap/pre_execution'); const { + evalModule, evalScript, readStdin } = require('internal/process/execution'); @@ -16,5 +17,8 @@ markBootstrapComplete(); readStdin((code) => { process._eval = code; - evalScript('[stdin]', process._eval, process._breakFirstLine); + if (require('internal/options').getOptionValue('--entry-type') === 'module') + evalModule(process._eval); + else + evalScript('[stdin]', process._eval, process._breakFirstLine); }); diff --git a/lib/internal/main/eval_string.js b/lib/internal/main/eval_string.js index 953fab386d..b032281925 100644 --- a/lib/internal/main/eval_string.js +++ b/lib/internal/main/eval_string.js @@ -6,11 +6,15 @@ const { prepareMainThreadExecution } = require('internal/bootstrap/pre_execution'); -const { evalScript } = require('internal/process/execution'); +const { evalModule, evalScript } = require('internal/process/execution'); const { addBuiltinLibsToObject } = require('internal/modules/cjs/helpers'); -const source = require('internal/options').getOptionValue('--eval'); +const { getOptionValue } = require('internal/options'); +const source = getOptionValue('--eval'); prepareMainThreadExecution(); addBuiltinLibsToObject(global); markBootstrapComplete(); -evalScript('[eval]', source, process._breakFirstLine); +if (getOptionValue('--entry-type') === 'module') + evalModule(source); +else + evalScript('[eval]', source, process._breakFirstLine); diff --git a/lib/internal/main/repl.js b/lib/internal/main/repl.js index e6b9885351..c2bf54f8bb 100644 --- a/lib/internal/main/repl.js +++ b/lib/internal/main/repl.js @@ -13,6 +13,12 @@ const { prepareMainThreadExecution(); +// --entry-type flag not supported in REPL +if (require('internal/options').getOptionValue('--entry-type')) { + console.error('Cannot specify --entry-type for REPL'); + process.exit(1); +} + const cliRepl = require('internal/repl'); cliRepl.createInternalRepl(process.env, (err, repl) => { if (err) { -- cgit v1.2.3