From 3e3bb272653cf07995696d839071c2505eb5228a Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Tue, 5 Mar 2019 04:20:15 -0500 Subject: [PATCH] esm: add --es-module-specifier-resolution There are currently two supported values "none" and "node" --- src/module_wrap.cc | 80 ++++++++++++++++--- src/node_options.cc | 5 ++ src/node_options.h | 1 + test/es-module/test-esm-package-scope.mjs | 12 --- test/es-module/test-esm-specifiers.mjs | 35 ++++++++ test/fixtures/es-module-specifiers/index.mjs | 10 +++ .../node_modules/explicit-main/entry.mjs | 1 + .../node_modules/explicit-main/package.json | 3 + .../node_modules/implicit-main/entry.js | 1 + .../node_modules/implicit-main/entry.mjs | 1 + .../node_modules/implicit-main/package.json | 3 + .../package-type-commonjs}/a.js | 0 .../package-type-commonjs}/b.mjs | 0 .../package-type-commonjs}/c.cjs | 0 .../package-type-commonjs}/index.mjs | 4 +- .../package-type-commonjs/package.json | 3 + .../package-type-module}/a.js | 0 .../package-type-module}/b.mjs | 0 .../package-type-module}/c.cjs | 0 .../package-type-module}/index.js | 4 +- .../package-type-module/package.json | 3 + .../es-module-specifiers/package.json | 1 + .../legacy-loader/package.json | 13 --- .../esm-package-scope/new-loader/package.json | 13 --- 24 files changed, 138 insertions(+), 55 deletions(-) delete mode 100644 test/es-module/test-esm-package-scope.mjs create mode 100644 test/es-module/test-esm-specifiers.mjs create mode 100644 test/fixtures/es-module-specifiers/index.mjs create mode 100644 test/fixtures/es-module-specifiers/node_modules/explicit-main/entry.mjs create mode 100644 test/fixtures/es-module-specifiers/node_modules/explicit-main/package.json create mode 100644 test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.js create mode 100644 test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.mjs create mode 100644 test/fixtures/es-module-specifiers/node_modules/implicit-main/package.json rename test/fixtures/{esm-package-scope/legacy-loader => es-module-specifiers/package-type-commonjs}/a.js (100%) rename test/fixtures/{esm-package-scope/legacy-loader => es-module-specifiers/package-type-commonjs}/b.mjs (100%) rename test/fixtures/{esm-package-scope/legacy-loader => es-module-specifiers/package-type-commonjs}/c.cjs (100%) rename test/fixtures/{esm-package-scope/legacy-loader => es-module-specifiers/package-type-commonjs}/index.mjs (82%) create mode 100644 test/fixtures/es-module-specifiers/package-type-commonjs/package.json rename test/fixtures/{esm-package-scope/new-loader => es-module-specifiers/package-type-module}/a.js (100%) rename test/fixtures/{esm-package-scope/new-loader => es-module-specifiers/package-type-module}/b.mjs (100%) rename test/fixtures/{esm-package-scope/new-loader => es-module-specifiers/package-type-module}/c.cjs (100%) rename test/fixtures/{esm-package-scope/new-loader => es-module-specifiers/package-type-module}/index.js (82%) create mode 100644 test/fixtures/es-module-specifiers/package-type-module/package.json create mode 100644 test/fixtures/es-module-specifiers/package.json delete mode 100644 test/fixtures/esm-package-scope/legacy-loader/package.json delete mode 100644 test/fixtures/esm-package-scope/new-loader/package.json diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 507ae11a6b..8793a1a049 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -43,6 +43,14 @@ using v8::String; using v8::Undefined; using v8::Value; +static const char* const EXTENSIONS[] = { + ".mjs", + ".cjs", + ".js", + ".json", + ".node" +}; + ModuleWrap::ModuleWrap(Environment* env, Local object, Local module, @@ -667,13 +675,57 @@ Maybe LegacyMainResolve(const URL& pjson_url, return Nothing(); } +enum ResolveExtensionsOptions { + TRY_EXACT_NAME, + ONLY_VIA_EXTENSIONS +}; + +template +Maybe ResolveExtensions(const URL& search) { + if (options == TRY_EXACT_NAME) { + if (FileExists(search)) { + return Just(search); + } + } + + for (const char* extension : EXTENSIONS) { + URL guess(search.path() + extension, &search); + if (FileExists(guess)) { + return Just(guess); + } + } + + return Nothing(); +} + +inline Maybe ResolveIndex(const URL& search) { + return ResolveExtensions(URL("index", search)); +} + Maybe FinalizeResolution(Environment* env, - const URL& resolved, - const URL& base, - bool check_exists) { - const std::string& path = resolved.ToFilePath(); + const URL& resolved, + const URL& base) { + if (env->options()->es_module_specifier_resolution == "node") { + Maybe file = ResolveExtensions(resolved); + if (!file.IsNothing()) { + return file; + } + if (resolved.path().back() != '/') { + file = ResolveIndex(URL(resolved.path() + "/", &base)); + } else { + file = ResolveIndex(resolved); + } + if (!file.IsNothing()) { + return file; + } + std::string msg = "Cannot find module '" + resolved.path() + + "' imported from " + base.ToFilePath(); + node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str()); + return Nothing(); + } - if (check_exists && CheckDescriptorAtPath(path) != FILE) { + const std::string& path = resolved.ToFilePath(); + if (CheckDescriptorAtPath(path) != FILE) { std::string msg = "Cannot find module '" + path + "' imported from " + base.ToFilePath(); node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str()); @@ -696,17 +748,19 @@ Maybe PackageMainResolve(Environment* env, return Nothing(); } if (pcfg.has_main == HasMain::Yes && - pcfg.main.substr(pcfg.main.length() - 4, 4) == ".mjs") { - return FinalizeResolution(env, URL(pcfg.main, pjson_url), base, true); - } - if (pcfg.esm == IsESM::Yes && - pcfg.main.substr(pcfg.main.length() - 3, 3) == ".js") { - return FinalizeResolution(env, URL(pcfg.main, pjson_url), base, true); + pcfg.main.substr(pcfg.main.length() - 4, 4) != ".cjs" && + (pcfg.main.substr(pcfg.main.length() - 4, 4) == ".mjs" || + pcfg.esm == IsESM::Yes)) { + return FinalizeResolution(env, URL(pcfg.main, pjson_url), base); } Maybe resolved = LegacyMainResolve(pjson_url, pcfg); // Legacy main resolution error if (resolved.IsNothing()) { + std::string msg = "Cannot find main entry point for '" + + URL(".", pjson_url).ToFilePath() + "' imported from " + + base.ToFilePath(); + node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str()); return Nothing(); } return resolved; @@ -759,7 +813,7 @@ Maybe PackageResolve(Environment* env, if (!pkg_subpath.length()) { return PackageMainResolve(env, pjson_url, *pcfg.FromJust(), base); } else { - return FinalizeResolution(env, URL(pkg_subpath, pjson_url), base, true); + return FinalizeResolution(env, URL(pkg_subpath, pjson_url), base); } CHECK(false); // Cross-platform root check. @@ -789,7 +843,7 @@ Maybe Resolve(Environment* env, return PackageResolve(env, specifier, base); } } - return FinalizeResolution(env, resolved, base, true); + return FinalizeResolution(env, resolved, base); } void ModuleWrap::Resolve(const FunctionCallbackInfo& args) { diff --git a/src/node_options.cc b/src/node_options.cc index aae891dd49..090282fa26 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -236,6 +236,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { "(default: llhttp).", &EnvironmentOptions::http_parser, kAllowedInEnvironment); + AddOption("--es-module-specifier-resolution", + "Select extension resolution algorithm for es modules; " + "either 'none' or 'node'", + &EnvironmentOptions::es_module_specifier_resolution, + kAllowedInEnvironment); AddOption("--no-deprecation", "silence deprecation warnings", &EnvironmentOptions::no_deprecation, diff --git a/src/node_options.h b/src/node_options.h index ee4cc98c9b..29eeb84839 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -86,6 +86,7 @@ class EnvironmentOptions : public Options { public: bool abort_on_uncaught_exception = false; bool experimental_modules = false; + std::string es_module_specifier_resolution = "none"; std::string module_type; std::string experimental_policy; bool experimental_repl_await = false; diff --git a/test/es-module/test-esm-package-scope.mjs b/test/es-module/test-esm-package-scope.mjs deleted file mode 100644 index 6e07a307e0..0000000000 --- a/test/es-module/test-esm-package-scope.mjs +++ /dev/null @@ -1,12 +0,0 @@ -// Flags: --experimental-modules -/* eslint-disable node-core/required-modules */ - -import '../common/index.mjs'; -import assert from 'assert'; - -import legacyLoader from - '../fixtures/esm-package-scope/legacy-loader/index.mjs'; -import newLoader from '../fixtures/esm-package-scope/new-loader/index.js'; - -assert.strictEqual(legacyLoader, 'legacy-loader'); -assert.strictEqual(newLoader, 'new-loader'); diff --git a/test/es-module/test-esm-specifiers.mjs b/test/es-module/test-esm-specifiers.mjs new file mode 100644 index 0000000000..636cc035ca --- /dev/null +++ b/test/es-module/test-esm-specifiers.mjs @@ -0,0 +1,35 @@ +// Flags: --experimental-modules --es-module-specifier-resolution=node +import { mustNotCall } from '../common'; +import assert from 'assert'; + +// commonJS index.js +import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs'; +// esm index.js +import module from '../fixtures/es-module-specifiers/package-type-module'; +// notice the trailing slash +import success, { explicit, implicit, implicitModule, getImplicitCommonjs } + from '../fixtures/es-module-specifiers/'; + +assert.strictEqual(commonjs, 'commonjs'); +assert.strictEqual(module, 'module'); +assert.strictEqual(success, 'success'); +assert.strictEqual(explicit, 'esm'); +assert.strictEqual(implicit, 'cjs'); +assert.strictEqual(implicitModule, 'esm'); + +async function main() { + try { + await import('../fixtures/es-module-specifiers/do-not-exist.js'); + } catch (e) { + // Files that do not exist should throw + assert.strictEqual(e.name, 'Error'); + } + try { + await getImplicitCommonjs(); + } catch (e) { + // Legacy loader cannot resolve .mjs automatically from main + assert.strictEqual(e.name, 'Error'); + } +} + +main().catch(mustNotCall); diff --git a/test/fixtures/es-module-specifiers/index.mjs b/test/fixtures/es-module-specifiers/index.mjs new file mode 100644 index 0000000000..2be7048513 --- /dev/null +++ b/test/fixtures/es-module-specifiers/index.mjs @@ -0,0 +1,10 @@ +import explicit from 'explicit-main'; +import implicit from 'implicit-main'; +import implicitModule from 'implicit-main-type-module'; + +function getImplicitCommonjs () { + return import('implicit-main-type-commonjs'); +} + +export {explicit, implicit, implicitModule, getImplicitCommonjs}; +export default 'success'; diff --git a/test/fixtures/es-module-specifiers/node_modules/explicit-main/entry.mjs b/test/fixtures/es-module-specifiers/node_modules/explicit-main/entry.mjs new file mode 100644 index 0000000000..914e3a97d5 --- /dev/null +++ b/test/fixtures/es-module-specifiers/node_modules/explicit-main/entry.mjs @@ -0,0 +1 @@ +export default 'esm'; diff --git a/test/fixtures/es-module-specifiers/node_modules/explicit-main/package.json b/test/fixtures/es-module-specifiers/node_modules/explicit-main/package.json new file mode 100644 index 0000000000..e9457582ac --- /dev/null +++ b/test/fixtures/es-module-specifiers/node_modules/explicit-main/package.json @@ -0,0 +1,3 @@ +{ + "main": "entry.mjs" +} \ No newline at end of file diff --git a/test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.js b/test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.js new file mode 100644 index 0000000000..b2825bd3c9 --- /dev/null +++ b/test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.js @@ -0,0 +1 @@ +module.exports = 'cjs'; diff --git a/test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.mjs b/test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.mjs new file mode 100644 index 0000000000..914e3a97d5 --- /dev/null +++ b/test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.mjs @@ -0,0 +1 @@ +export default 'esm'; diff --git a/test/fixtures/es-module-specifiers/node_modules/implicit-main/package.json b/test/fixtures/es-module-specifiers/node_modules/implicit-main/package.json new file mode 100644 index 0000000000..bf2e35593b --- /dev/null +++ b/test/fixtures/es-module-specifiers/node_modules/implicit-main/package.json @@ -0,0 +1,3 @@ +{ + "main": "entry" +} \ No newline at end of file diff --git a/test/fixtures/esm-package-scope/legacy-loader/a.js b/test/fixtures/es-module-specifiers/package-type-commonjs/a.js similarity index 100% rename from test/fixtures/esm-package-scope/legacy-loader/a.js rename to test/fixtures/es-module-specifiers/package-type-commonjs/a.js diff --git a/test/fixtures/esm-package-scope/legacy-loader/b.mjs b/test/fixtures/es-module-specifiers/package-type-commonjs/b.mjs similarity index 100% rename from test/fixtures/esm-package-scope/legacy-loader/b.mjs rename to test/fixtures/es-module-specifiers/package-type-commonjs/b.mjs diff --git a/test/fixtures/esm-package-scope/legacy-loader/c.cjs b/test/fixtures/es-module-specifiers/package-type-commonjs/c.cjs similarity index 100% rename from test/fixtures/esm-package-scope/legacy-loader/c.cjs rename to test/fixtures/es-module-specifiers/package-type-commonjs/c.cjs diff --git a/test/fixtures/esm-package-scope/legacy-loader/index.mjs b/test/fixtures/es-module-specifiers/package-type-commonjs/index.mjs similarity index 82% rename from test/fixtures/esm-package-scope/legacy-loader/index.mjs rename to test/fixtures/es-module-specifiers/package-type-commonjs/index.mjs index 1c78c389a2..ef2b30b19b 100644 --- a/test/fixtures/esm-package-scope/legacy-loader/index.mjs +++ b/test/fixtures/es-module-specifiers/package-type-commonjs/index.mjs @@ -5,7 +5,7 @@ import {b} from './b.mjs'; // import 'c.cjs'; import cjs from './c.cjs'; // proves cross boundary fun bits -import jsAsEsm from '../new-loader/a.js'; +import jsAsEsm from '../package-type-module/a.js'; // named export from core import {strictEqual, deepStrictEqual} from 'assert'; @@ -18,4 +18,4 @@ deepStrictEqual(cjs, { three: 3 }); -export default 'legacy-loader'; +export default 'commonjs'; diff --git a/test/fixtures/es-module-specifiers/package-type-commonjs/package.json b/test/fixtures/es-module-specifiers/package-type-commonjs/package.json new file mode 100644 index 0000000000..5bbefffbab --- /dev/null +++ b/test/fixtures/es-module-specifiers/package-type-commonjs/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/test/fixtures/esm-package-scope/new-loader/a.js b/test/fixtures/es-module-specifiers/package-type-module/a.js similarity index 100% rename from test/fixtures/esm-package-scope/new-loader/a.js rename to test/fixtures/es-module-specifiers/package-type-module/a.js diff --git a/test/fixtures/esm-package-scope/new-loader/b.mjs b/test/fixtures/es-module-specifiers/package-type-module/b.mjs similarity index 100% rename from test/fixtures/esm-package-scope/new-loader/b.mjs rename to test/fixtures/es-module-specifiers/package-type-module/b.mjs diff --git a/test/fixtures/esm-package-scope/new-loader/c.cjs b/test/fixtures/es-module-specifiers/package-type-module/c.cjs similarity index 100% rename from test/fixtures/esm-package-scope/new-loader/c.cjs rename to test/fixtures/es-module-specifiers/package-type-module/c.cjs diff --git a/test/fixtures/esm-package-scope/new-loader/index.js b/test/fixtures/es-module-specifiers/package-type-module/index.js similarity index 82% rename from test/fixtures/esm-package-scope/new-loader/index.js rename to test/fixtures/es-module-specifiers/package-type-module/index.js index 98c536cc34..a8baacb7c9 100644 --- a/test/fixtures/esm-package-scope/new-loader/index.js +++ b/test/fixtures/es-module-specifiers/package-type-module/index.js @@ -5,7 +5,7 @@ import {b} from './b.mjs'; // import 'c.cjs'; import cjs from './c.cjs'; // import across boundaries -import jsAsCjs from '../legacy-loader/a.js' +import jsAsCjs from '../package-type-commonjs/a.js' // named export from core import {strictEqual, deepStrictEqual} from 'assert'; @@ -18,4 +18,4 @@ deepStrictEqual(cjs, { three: 3 }); -export default 'new-loader'; +export default 'module'; diff --git a/test/fixtures/es-module-specifiers/package-type-module/package.json b/test/fixtures/es-module-specifiers/package-type-module/package.json new file mode 100644 index 0000000000..3dbc1ca591 --- /dev/null +++ b/test/fixtures/es-module-specifiers/package-type-module/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/test/fixtures/es-module-specifiers/package.json b/test/fixtures/es-module-specifiers/package.json new file mode 100644 index 0000000000..9e26dfeeb6 --- /dev/null +++ b/test/fixtures/es-module-specifiers/package.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/test/fixtures/esm-package-scope/legacy-loader/package.json b/test/fixtures/esm-package-scope/legacy-loader/package.json deleted file mode 100644 index 215a962248..0000000000 --- a/test/fixtures/esm-package-scope/legacy-loader/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "legacy-loader", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "Myles Borins ", - "license": "Apache-2.0", - "type": "commonjs" -} diff --git a/test/fixtures/esm-package-scope/new-loader/package.json b/test/fixtures/esm-package-scope/new-loader/package.json deleted file mode 100644 index 1f2c704322..0000000000 --- a/test/fixtures/esm-package-scope/new-loader/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "new-loader", - "version": "1.0.0", - "description": "", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [], - "author": "Myles Borins ", - "license": "Apache-2.0", - "type": "module" -}