Skip to content

Commit 6f7d871

Browse files
committed
rewrite based on review comments and defaultGetFormat implementation
1 parent db7a12a commit 6f7d871

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

lib/internal/modules/esm/resolve.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const { getOptionValue } = require('internal/options');
3737
const policy = getOptionValue('--experimental-policy') ?
3838
require('internal/process/policy') :
3939
null;
40-
const { sep, relative, resolve } = require('path');
40+
const { sep, relative, resolve, extname } = require('path');
4141
const preserveSymlinks = getOptionValue('--preserve-symlinks');
4242
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4343
const typeFlag = getOptionValue('--input-type');
@@ -62,6 +62,9 @@ const userConditions = getOptionValue('--conditions');
6262
const noAddons = getOptionValue('--no-addons');
6363
const addonConditions = noAddons ? [] : ['node-addons'];
6464

65+
const experimentalSpecifierResolution =
66+
getOptionValue('--experimental-specifier-resolution');
67+
6568
const DEFAULT_CONDITIONS = ObjectFreeze([
6669
'node',
6770
'import',
@@ -467,11 +470,19 @@ function resolvePackageTargetString(
467470
const composeResult = (resolved) => {
468471
let format;
469472
try {
470-
// Extension has higher priority than package.json type descriptor
471-
if (StringPrototypeEndsWith(resolved.href, '.mjs')) {
472-
format = 'module';
473-
} else {
473+
const ext = extname(resolved.pathname);
474+
if (ext === '.js') {
474475
format = getPackageType(resolved);
476+
} else {
477+
format = extensionFormatMap[ext];
478+
}
479+
if (!format) {
480+
if (experimentalSpecifierResolution === 'node') {
481+
process.emitWarning(
482+
'The Node.js specifier resolution in ESM is experimental.',
483+
'ExperimentalWarning');
484+
format = legacyExtensionFormatMap[ext];
485+
}
475486
}
476487
} catch (err) {
477488
if (err.code === 'ERR_INVALID_FILE_URL_PATH') {
@@ -1112,4 +1123,6 @@ module.exports = {
11121123
};
11131124

11141125
// cycle
1115-
const { defaultGetFormat } = require('internal/modules/esm/get_format');
1126+
const { defaultGetFormat,
1127+
extensionFormatMap,
1128+
legacyExtensionFormatMap } = require('internal/modules/esm/get_format');

0 commit comments

Comments
 (0)