Skip to content

Commit 94ac44f

Browse files
MylesBorinstargos
authored andcommitted
esm: modify resolution order for specifier flag
Currently `--es-module-specifier-resolution=node` has an alternative resolution order than the default in common.js, this causes inconsistencies. As discussed in @nodejs/modules we want to preserve resolution order between implementations. PR-URL: #29974 Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 5616f22 commit 94ac44f

4 files changed

Lines changed: 10 additions & 7 deletions

File tree

‎lib/internal/modules/esm/default_resolve.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const { getOptionValue } = require('internal/options');
99
constpreserveSymlinks=getOptionValue('--preserve-symlinks');
1010
constpreserveSymlinksMain=getOptionValue('--preserve-symlinks-main');
1111
constexperimentalJsonModules=getOptionValue('--experimental-json-modules');
12+
constesModuleSpecifierResolution=
13+
getOptionValue('--es-module-specifier-resolution');
1214
consttypeFlag=getOptionValue('--input-type');
1315
constexperimentalWasmModules=getOptionValue('--experimental-wasm-modules');
1416
const{resolve: moduleWrapResolve,
@@ -110,6 +112,8 @@ function resolve(specifier, parentURL) {
110112
if(!format){
111113
if(isMain)
112114
format=type===TYPE_MODULE ? 'module' : 'commonjs';
115+
elseif(esModuleSpecifierResolution==='node')
116+
format='commonjs';
113117
else
114118
thrownewERR_UNKNOWN_FILE_EXTENSION(fileURLToPath(url));
115119
}

‎src/module_wrap.cc‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ using v8::Undefined;
4848
using v8::Value;
4949

5050
staticconstchar* constEXTENSIONS[] = {
51-
".mjs",
52-
".cjs",
5351
".js",
5452
".json",
55-
".node"
53+
".node",
54+
".mjs"
5655
};
5756

5857
ModuleWrap::ModuleWrap(Environment* env,

‎test/es-module/test-esm-specifiers.mjs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Flags: --experimental-modules --es-module-specifier-resolution=node
2-
import{mustNotCall}from'../common';
2+
import{mustNotCall}from'../common/index.mjs';
33
importassertfrom'assert';
44

55
// commonJS index.js
@@ -14,8 +14,8 @@ assert.strictEqual(commonjs, 'commonjs');
1414
assert.strictEqual(module,'module');
1515
assert.strictEqual(success,'success');
1616
assert.strictEqual(explicit,'esm');
17-
assert.strictEqual(implicit,'esm');
18-
assert.strictEqual(implicitModule,'esm');
17+
assert.strictEqual(implicit,'cjs');
18+
assert.strictEqual(implicitModule,'cjs');
1919

2020
asyncfunctionmain(){
2121
try{

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

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

0 commit comments

Comments
 (0)