Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit a17b130

Browse files
committed
esm: rename flag --esm-specifier-resolution
There are currently two supported values "none" and "node" Also fixed a lot of bugs
1 parent 2168312 commit a17b130

File tree

24 files changed

+93
-50
lines changed

24 files changed

+93
-50
lines changed

src/module_wrap.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ using v8::String;
4343
using v8::Undefined;
4444
using v8::Value;
4545

46-
static const char* const EXTENSIONS[] = {".mjs", ".js", ".json", ".node"};
46+
static const char* const EXTENSIONS[] = {
47+
".mjs",
48+
".cjs",
49+
".js",
50+
".json",
51+
".node"
52+
};
4753

4854
ModuleWrap::ModuleWrap(Environment* env,
4955
Local<Object> object,
@@ -699,15 +705,23 @@ inline Maybe<URL> ResolveIndex(const URL& search) {
699705
Maybe<URL> FinalizeResolution(Environment* env,
700706
const URL& resolved,
701707
const URL& base) {
702-
if (env->options()->legacy_resolution) {
708+
if (env->options()->es_module_specifier_resolution == "node") {
703709
Maybe<URL> file = ResolveExtensions<TRY_EXACT_NAME>(resolved);
704710
if (!file.IsNothing()) {
705711
return file;
706712
}
707713
if (resolved.path().back() != '/') {
708-
ResolveIndex(URL(resolved.path() + "/", &base));
714+
file = ResolveIndex(URL(resolved.path() + "/", &base));
715+
} else {
716+
file = ResolveIndex(resolved);
717+
}
718+
if (!file.IsNothing()) {
719+
return file;
709720
}
710-
return ResolveIndex(resolved);
721+
std::string msg = "Cannot find module '" + resolved.path() +
722+
"' imported from " + base.ToFilePath();
723+
node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
724+
return Nothing<URL>();
711725
}
712726

713727
const std::string& path = resolved.ToFilePath();
@@ -734,6 +748,7 @@ Maybe<URL> PackageMainResolve(Environment* env,
734748
return Nothing<URL>();
735749
}
736750
if (pcfg.has_main == HasMain::Yes &&
751+
pcfg.main.substr(pcfg.main.length() - 4, 4) != ".cjs" &&
737752
(pcfg.main.substr(pcfg.main.length() - 4, 4) == ".mjs" ||
738753
pcfg.esm == IsESM::Yes)) {
739754
return FinalizeResolution(env, URL(pcfg.main, pjson_url), base);
@@ -742,6 +757,10 @@ Maybe<URL> PackageMainResolve(Environment* env,
742757
Maybe<URL> resolved = LegacyMainResolve(pjson_url, pcfg);
743758
// Legacy main resolution error
744759
if (resolved.IsNothing()) {
760+
std::string msg = "Cannot find main entry point for '" +
761+
URL(".", pjson_url).ToFilePath() + "' imported from " +
762+
base.ToFilePath();
763+
node::THROW_ERR_MODULE_NOT_FOUND(env, msg.c_str());
745764
return Nothing<URL>();
746765
}
747766
return resolved;

src/node_options.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
236236
"(default: llhttp).",
237237
&EnvironmentOptions::http_parser,
238238
kAllowedInEnvironment);
239-
AddOption("--legacy-resolution",
240-
"legacy extension and folder resolution for ES Modules",
241-
&EnvironmentOptions::legacy_resolution,
239+
AddOption("--es-module-specifier-resolution",
240+
"Select extension resolution algorithm for es modules; "
241+
"either 'none' or 'node'",
242+
&EnvironmentOptions::es_module_specifier_resolution,
242243
kAllowedInEnvironment);
243244
AddOption("--no-deprecation",
244245
"silence deprecation warnings",

src/node_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class EnvironmentOptions : public Options {
8686
public:
8787
bool abort_on_uncaught_exception = false;
8888
bool experimental_modules = false;
89-
bool legacy_resolution = false;
89+
std::string es_module_specifier_resolution = "none";
9090
std::string module_type;
9191
std::string experimental_policy;
9292
bool experimental_repl_await = false;

test/es-module/test-esm-package-scope.mjs

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Flags: --experimental-modules --es-module-specifier-resolution=node
2+
import { mustNotCall } from '../common';
3+
import assert from 'assert';
4+
5+
// commonJS index.js
6+
import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs';
7+
// esm index.js
8+
import module from '../fixtures/es-module-specifiers/package-type-module';
9+
// notice the trailing slash
10+
import success, { explicit, implicit, implicitModule, getImplicitCommonjs }
11+
from '../fixtures/es-module-specifiers/';
12+
13+
assert.strictEqual(commonjs, 'commonjs');
14+
assert.strictEqual(module, 'module');
15+
assert.strictEqual(success, 'success');
16+
assert.strictEqual(explicit, 'esm');
17+
assert.strictEqual(implicit, 'cjs');
18+
assert.strictEqual(implicitModule, 'esm');
19+
20+
async function main() {
21+
try {
22+
await import('../fixtures/es-module-specifiers/do-not-exist.js');
23+
} catch (e) {
24+
// Files that do not exist should throw
25+
assert.strictEqual(e.name, 'Error');
26+
}
27+
try {
28+
await getImplicitCommonjs();
29+
} catch (e) {
30+
// Legacy loader cannot resolve .mjs automatically from main
31+
assert.strictEqual(e.name, 'Error');
32+
}
33+
}
34+
35+
main().catch(mustNotCall);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import explicit from 'explicit-main';
2+
import implicit from 'implicit-main';
3+
import implicitModule from 'implicit-main-type-module';
4+
5+
function getImplicitCommonjs () {
6+
return import('implicit-main-type-commonjs');
7+
}
8+
9+
export {explicit, implicit, implicitModule, getImplicitCommonjs};
10+
export default 'success';

test/fixtures/es-module-specifiers/node_modules/explicit-main/entry.mjs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/es-module-specifiers/node_modules/explicit-main/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/es-module-specifiers/node_modules/implicit-main/entry.mjs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)