From 6f7d8714b9376ec3c9eedebd4df71a62042d101a Mon Sep 17 00:00:00 2001 From: Gabriel Bota Date: Fri, 17 Dec 2021 15:14:08 +0100 Subject: [PATCH] rewrite based on review comments and defaultGetFormat implementation --- lib/internal/modules/esm/resolve.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 3cdffe3f77ed3e..002661cf9b40ae 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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'); @@ -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', @@ -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') { @@ -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');