Skip to content

Commit

Permalink
rewrite based on review comments and defaultGetFormat implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dygabo committed Dec 17, 2021
1 parent db7a12a commit 6f7d871
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const { getOptionValue } = require('internal/options');
const policy = getOptionValue('--experimental-policy') ?
require('internal/process/policy') :
null;
const { sep, relative, resolve } = require('path');
const { sep, relative, resolve, extname } = require('path');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const typeFlag = getOptionValue('--input-type');
Expand All @@ -62,6 +62,9 @@ const userConditions = getOptionValue('--conditions');
const noAddons = getOptionValue('--no-addons');
const addonConditions = noAddons ? [] : ['node-addons'];

const experimentalSpecifierResolution =
getOptionValue('--experimental-specifier-resolution');

const DEFAULT_CONDITIONS = ObjectFreeze([
'node',
'import',
Expand Down Expand Up @@ -467,11 +470,19 @@ function resolvePackageTargetString(
const composeResult = (resolved) => {
let format;
try {
// Extension has higher priority than package.json type descriptor
if (StringPrototypeEndsWith(resolved.href, '.mjs')) {
format = 'module';
} else {
const ext = extname(resolved.pathname);
if (ext === '.js') {
format = getPackageType(resolved);
} else {
format = extensionFormatMap[ext];
}
if (!format) {
if (experimentalSpecifierResolution === 'node') {
process.emitWarning(
'The Node.js specifier resolution in ESM is experimental.',
'ExperimentalWarning');
format = legacyExtensionFormatMap[ext];
}
}
} catch (err) {
if (err.code === 'ERR_INVALID_FILE_URL_PATH') {
Expand Down Expand Up @@ -1112,4 +1123,6 @@ module.exports = {
};

// cycle
const { defaultGetFormat } = require('internal/modules/esm/get_format');
const { defaultGetFormat,
extensionFormatMap,
legacyExtensionFormatMap } = require('internal/modules/esm/get_format');

0 comments on commit 6f7d871

Please sign in to comment.