Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review comments, test cases added for query and hash export urls
  • Loading branch information
dygabo committed Dec 17, 2021
commit f9fe067bbd2eceb0661090a99e411d8d5c0ea73b
37 changes: 22 additions & 15 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,34 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), {
if (ext === '.js') {
format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs';
} else {
format = extensionFormatMap[ext];
}
if (!format) {
if (experimentalSpecifierResolution === 'node') {
if (!experimentalSpecifierResolutionWarned) {
process.emitWarning(
'The Node.js specifier resolution in ESM is experimental.',
'ExperimentalWarning');
experimentalSpecifierResolutionWarned = true;
}
format = legacyExtensionFormatMap[ext];
} else {
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));
}
format = extensionFormatMap[ext] ??
throwIfNotExperimentalSpecifierResolution(ext, url);
}

return format || null;
},
'node:'() { return 'builtin'; },
});

function throwIfNotExperimentalSpecifierResolution(ext, url) {
if (experimentalSpecifierResolution !== 'node') {
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));
}
return getLegacyExtensionFormat(ext);
}

function getLegacyExtensionFormat(ext) {
if (experimentalSpecifierResolution === 'node') {
if (!experimentalSpecifierResolutionWarned) {
process.emitWarning(
'The Node.js specifier resolution in ESM is experimental.',
'ExperimentalWarning');
experimentalSpecifierResolutionWarned = true;
}
}
return legacyExtensionFormatMap[ext];
}

function defaultGetFormat(url, context) {
const parsed = new URL(url);

Expand All @@ -95,5 +102,5 @@ module.exports = {
defaultGetFormat,
extensionFormatMap,
legacyExtensionFormatMap,
experimentalSpecifierResolutionWarned
getLegacyExtensionFormat
};
20 changes: 3 additions & 17 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ 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 @@ -484,17 +481,7 @@ function resolvePackageTargetString(
throw err;
}
} else {
format = extensionFormatMap[ext];
}

if (format == null && experimentalSpecifierResolution === 'node') {
if (!experimentalSpecifierResolutionWarned) {
process.emitWarning(
'The Node.js specifier resolution in ESM is experimental.',
'ExperimentalWarning');
experimentalSpecifierResolutionWarned = true;
}
format = legacyExtensionFormatMap[ext];
format = extensionFormatMap[ext] ?? getLegacyExtensionFormat(ext);
}

return { resolved, ...(format !== 'none') && { format } };
Expand Down Expand Up @@ -1127,9 +1114,8 @@ module.exports = {
};

// cycle
let {
const {
defaultGetFormat,
extensionFormatMap,
legacyExtensionFormatMap,
experimentalSpecifierResolutionWarned,
getLegacyExtensionFormat,
} = require('internal/modules/esm/get_format');
11 changes: 8 additions & 3 deletions test/es-module/test-esm-resolve-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,15 @@ try {
testDualPackageWithJsMainScriptAndModuleType();

// TestParameters are ModuleName, mainRequireScript, mainImportScript,
// mainPackageType, subdirPkgJsonType, expectedResolvedFormat
// mainPackageType, subdirPkgJsonType, expectedResolvedFormat, mainSuffix
[ [ 'mjs-mod-mod', 'index.js', 'index.mjs', 'module', 'module', 'module'],
[ 'mjs-com-com', 'idx.js', 'idx.mjs', 'commonjs', 'commonjs', 'module'],
[ 'mjs-mod-com', 'index.js', 'imp.mjs', 'module', 'commonjs', 'module'],
[ 'js-com-com', 'index.js', 'imp.js', 'commonjs', 'commonjs', 'commonjs'],
[ 'js-com-mod', 'index.js', 'imp.js', 'commonjs', 'module', 'module'],
[ 'qmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '?k=v'],
[ 'hmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '#Key'],
[ 'qhmod', 'index.js', 'imp.js', 'commonjs', 'module', 'module', '?k=v#h'],
[ 'ts-mod-com', 'index.js', 'imp.ts', 'module', 'commonjs', undefined],
].forEach((testVariant) => {
const [
Expand All @@ -198,7 +201,8 @@ try {
mainImportScript,
mainPackageType,
subdirPackageType,
expectedResolvedFormat ] = testVariant;
expectedResolvedFormat,
mainSuffix ] = testVariant;

const mDir = rel(`node_modules/${moduleName}`);
const subDir = rel(`node_modules/${moduleName}/subdir`);
Expand All @@ -211,13 +215,14 @@ try {
createDir(mDir);
createDir(subDir);

const mainScript = mainImportScript + (mainSuffix ?? '');
const mainPkgJsonContent = {
type: mainPackageType,
main: `./subdir/${mainRequireScript}`,
exports: {
'.': {
'require': `./subdir/${mainRequireScript}`,
'import': `./subdir/${mainImportScript}`
'import': `./subdir/${mainScript}`
},
'./package.json': './package.json',
}
Expand Down