Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d327c8

Browse filesBrowse files
aduh95marco-ippolito
authored andcommitted
esm: refactor get_format
PR-URL: #53872 Backport-PR-URL: #56927 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Refs: #52697
1 parent f9dc1ea commit 0d327c8
Copy full SHA for 0d327c8

File tree

Expand file treeCollapse file tree

1 file changed

+33
-25
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+33
-25
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/get_format.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/get_format.js
+33-25Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
mimeToFormat,
1818
} = require('internal/modules/esm/formats');
1919

20+
const detectModule = getOptionValue('--experimental-detect-module');
2021
const experimentalNetworkImports =
2122
getOptionValue('--experimental-network-imports');
2223
const { containsModuleSyntax } = internalBinding('contextify');
@@ -33,6 +34,17 @@ const protocolHandlers = {
3334
'node:'() { return 'builtin'; },
3435
};
3536

37+
/**
38+
* Determine whether the given ambiguous source contains CommonJS or ES module syntax.
39+
* @param {string | Buffer | undefined} source
40+
* @param {URL} url
41+
*/
42+
function detectModuleFormat(source, url) {
43+
if (!source) { return detectModule ? null : 'commonjs'; }
44+
if (!detectModule) { return 'commonjs'; }
45+
return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';
46+
}
47+
3648
/**
3749
* @param {URL} parsed
3850
* @returns {string | null}
@@ -112,26 +124,23 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
112124
default: { // The user did not pass `--experimental-default-type`.
113125
// `source` is undefined when this is called from `defaultResolve`;
114126
// but this gets called again from `defaultLoad`/`defaultLoadSync`.
115-
if (getOptionValue('--experimental-detect-module')) {
116-
const format = source ?
117-
(containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs') :
118-
null;
119-
if (format === 'module') {
120-
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
121-
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
122-
typelessPackageJsonFilesWarnedAbout ??= new SafeSet();
123-
if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
124-
const warning = `${url} parsed as an ES module because module syntax was detected;` +
125-
` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`;
126-
process.emitWarning(warning, {
127-
code: 'MODULE_TYPELESS_PACKAGE_JSON',
128-
});
129-
typelessPackageJsonFilesWarnedAbout.add(pjsonPath);
130-
}
127+
// For ambiguous files (no type field, .js extension) we return
128+
// undefined from `resolve` and re-run the check in `load`.
129+
const format = detectModuleFormat(source, url);
130+
if (format === 'module') {
131+
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
132+
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
133+
typelessPackageJsonFilesWarnedAbout ??= new SafeSet();
134+
if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
135+
const warning = `${url} parsed as an ES module because module syntax was detected;` +
136+
` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`;
137+
process.emitWarning(warning, {
138+
code: 'MODULE_TYPELESS_PACKAGE_JSON',
139+
});
140+
typelessPackageJsonFilesWarnedAbout.add(pjsonPath);
131141
}
132-
return format;
133142
}
134-
return 'commonjs';
143+
return format;
135144
}
136145
}
137146
}
@@ -154,15 +163,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
154163
return 'commonjs';
155164
}
156165
default: { // The user did not pass `--experimental-default-type`.
157-
if (getOptionValue('--experimental-detect-module')) {
158-
if (!source) { return null; }
159-
const format = getFormatOfExtensionlessFile(url);
160-
if (format === 'module') {
161-
return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';
162-
}
166+
if (!source) {
167+
return null;
168+
}
169+
const format = getFormatOfExtensionlessFile(url);
170+
if (format === 'wasm') {
163171
return format;
164172
}
165-
return 'commonjs';
173+
return detectModuleFormat(source, url);
166174
}
167175
}
168176
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.