Skip to content

Commit

Permalink
module: fix specifier resolution algorithm
Browse files Browse the repository at this point in the history
Fixes: #30520

PR-URL: #30574
Reviewed-By: Guy Bedford <[email protected]>
Reviewed-By: Myles Borins <[email protected]>
  • Loading branch information
pd4d10 authored and BethGriggs committed Feb 6, 2020
1 parent 2cbb358 commit d65566a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ function shouldUseESMLoader(mainPath) {
const userLoader = getOptionValue('--experimental-loader');
if (userLoader)
return true;
const experimentalSpecifierResolution =
getOptionValue('--experimental-specifier-resolution');
if (experimentalSpecifierResolution === 'node')
return true;
// Determine the module format of the main
if (mainPath && mainPath.endsWith('.mjs'))
return true;
Expand Down
24 changes: 24 additions & 0 deletions test/es-module/test-esm-specifiers.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Flags: --experimental-modules --experimental-specifier-resolution=node
import { mustNotCall } from '../common/index.mjs';
import assert from 'assert';
import path from 'path';
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';

// commonJS index.js
import commonjs from '../fixtures/es-module-specifiers/package-type-commonjs';
Expand Down Expand Up @@ -33,3 +36,24 @@ async function main() {
}

main().catch(mustNotCall);

// Test path from command line arguments
[
'package-type-commonjs',
'package-type-module',
'/',
'/index',
].forEach((item) => {
const modulePath = path.join(
fileURLToPath(import.meta.url),
'../../fixtures/es-module-specifiers',
item,
);
spawn(process.execPath, [
'--experimental-modules',
'--es-module-specifier-resolution=node',
modulePath
], { stdio: 'inherit' }).on('exit', (code) => {
assert.strictEqual(code, 0);
});
});

0 comments on commit d65566a

Please sign in to comment.