8000 esm: modify resolution order for specifier flag · nodejs/node@9729c5d · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 9729c5d

Browse files
committed
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 cfd45eb commit 9729c5d

File tree

4 files changed

+10
-7
lines changed
  • test
  • 4 files changed

    +10
    -7
    lines changed

    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
    const preserveSymlinks = getOptionValue('--preserve-symlinks');
    1010
    const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
    1111
    const experimentalJsonModules = getOptionValue('--experimental-json-modules');
    12+
    const esModuleSpecifierResolution =
    13+
    getOptionValue('--es-module-specifier-resolution');
    1214
    const typeFlag = getOptionValue('--input-type');
    1315
    const experimentalWasmModules = 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+
    else if (esModuleSpecifierResolution === 'node')
    116+
    format = 'commonjs';
    113117
    else
    114118
    throw new ERR_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
    static const char* const EXTENSIONS[] = {
    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
    import assert from '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
    async function main() {
    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)
    0