diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 616b2cf52309ea..b73a1312bc3c02 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -14,8 +14,9 @@ const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes; const extensionFormatMap = { '__proto__': null, '.cjs': 'commonjs', - '.js': 'module', - '.mjs': 'module' + '.mjs': 'module', + '.json': 'unsupported', + '.wasm': 'unsupported' }; const legacyExtensionFormatMap = { @@ -24,7 +25,8 @@ const legacyExtensionFormatMap = { '.js': 'commonjs', '.json': 'commonjs', '.mjs': 'module', - '.node': 'commonjs' + '.node': 'commonjs', + '.wasm': 'unsupported' }; if (experimentalWasmModules) @@ -39,7 +41,7 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { } const parsed = new URL(url); if (parsed.protocol === 'data:') { - const [ , mime ] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [ null, null, null ]; + const [, mime] = /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/.exec(parsed.pathname) || [null, null, null]; const format = ({ '__proto__': null, 'text/javascript': 'module', @@ -49,22 +51,21 @@ function defaultGetFormat(url, context, defaultGetFormatUnused) { return { format }; } else if (parsed.protocol === 'file:') { const ext = extname(parsed.pathname); - let format; - if (ext === '.js') { - format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; - } else { - format = extensionFormatMap[ext]; - } + let format = extensionFormatMap[ext]; if (!format) { if (experimentalSpeciferResolution === 'node') { process.emitWarning( 'The Node.js specifier resolution in ESM is experimental.', 'ExperimentalWarning'); format = legacyExtensionFormatMap[ext]; - } else { - throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); } } + if (format === 'unsupported') { + throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url)); + } + if (!format) { + format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs'; + } return { format: format || null }; } return { format: null };