Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: add a condition to if in case of a bug. #40549

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
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
12 changes: 8 additions & 4 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,29 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), {
},
'file:'(parsed, url) {
const ext = extname(parsed.pathname);
let format;
let format = null;

if (ext === '.js') {
format = getPackageType(parsed.href) === 'module' ? 'module' : 'commonjs';
iam-frankqiu marked this conversation as resolved.
Show resolved Hide resolved
} else {
format = extensionFormatMap[ext];
}
if (!format) {
if (experimentalSpecifierResolution === 'node') {
if (
experimentalSpecifierResolution === 'node' &&
legacyExtensionFormatMap[ext]
) {
process.emitWarning(
'The Node.js specifier resolution in ESM is experimental.',
'ExperimentalWarning');
'ExperimentalWarning'
);
iam-frankqiu marked this conversation as resolved.
Show resolved Hide resolved
format = legacyExtensionFormatMap[ext];
} else {
throw new ERR_UNKNOWN_FILE_EXTENSION(ext, fileURLToPath(url));
}
}

return format || null;
return format;
},
'node:'() { return 'builtin'; },
});
Expand Down
13 changes: 13 additions & 0 deletions test/es-module/test-esm-get-format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require('../common');
const assert = require('assert');
const { defaultGetFormat} = require('internal/modules/esm/get_format');

{
const url = new URL('file://example.com/foo/bar.js')
assert.strictEqual(defaultGetFormat(url), 'commonjs')
}

{
const url = new URL('file://example.com/foo/bar.whatever')
assert.throws(() => defaultGetFormat(url), {name: 'TypeError', message: /Unknown file extension whatever/})
}