-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
esm: detect ESM syntax in ambiguous JavaScript #50096
Merged
nodejs-github-bot
merged 8 commits into
nodejs:main
from
GeoffreyBooth:ambiguous-detection
Oct 20, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cc2ee5
esm: detect ESM syntax in ambiguous JavaScript
GeoffreyBooth 8d6d675
--experimental-detect-module flag
GeoffreyBooth 47d7f9e
Add tests
GeoffreyBooth d0dd993
Detect ambiguous files and string input
GeoffreyBooth 7d642ec
Optimize entry point loading by reading file within containsModuleSyntax
GeoffreyBooth 333d9b0
Tests for extensionless files; eval()
GeoffreyBooth 6e81e23
Patch problematic test
GeoffreyBooth b9af116
Add benchmark
GeoffreyBooth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
// This benchmarks the cost of running `containsModuleSyntax` on a CommonJS module being imported. | ||
// We use the TypeScript fixture because it's a very large CommonJS file with no ESM syntax: the worst case. | ||
const common = require('../common.js'); | ||
const tmpdir = require('../../test/common/tmpdir.js'); | ||
const fixtures = require('../../test/common/fixtures.js'); | ||
const scriptPath = fixtures.path('snapshot', 'typescript.js'); | ||
const fs = require('node:fs'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
type: ['with-module-syntax-detection', 'without-module-syntax-detection'], | ||
n: [1e4], | ||
}, { | ||
flags: ['--experimental-detect-module'], | ||
}); | ||
|
||
const benchmarkDirectory = tmpdir.fileURL('benchmark-detect-esm-syntax'); | ||
const ambiguousURL = new URL('./typescript.js', benchmarkDirectory); | ||
const explicitURL = new URL('./typescript.cjs', benchmarkDirectory); | ||
|
||
async function main({ n, type }) { | ||
tmpdir.refresh(); | ||
|
||
fs.mkdirSync(benchmarkDirectory, { recursive: true }); | ||
fs.cpSync(scriptPath, ambiguousURL); | ||
fs.cpSync(scriptPath, explicitURL); | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; i++) { | ||
const url = type === 'with-module-syntax-detection' ? ambiguousURL : explicitURL; | ||
await import(url); | ||
} | ||
|
||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,7 +33,7 @@ const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; | |||||||||||||
/** | ||||||||||||||
* @param {URL} url URL to the module | ||||||||||||||
* @param {ESModuleContext} context used to decorate error messages | ||||||||||||||
* @returns {{ responseURL: string, source: string | BufferView }} | ||||||||||||||
* @returns {Promise<{ responseURL: string, source: string | BufferView }>} | ||||||||||||||
*/ | ||||||||||||||
async function getSource(url, context) { | ||||||||||||||
const { protocol, href } = url; | ||||||||||||||
|
@@ -105,7 +105,7 @@ function getSourceSync(url, context) { | |||||||||||||
* @param {LoadContext} context | ||||||||||||||
* @returns {LoadReturn} | ||||||||||||||
*/ | ||||||||||||||
async function defaultLoad(url, context = kEmptyObject) { | ||||||||||||||
async function defaultLoad(url, context = { __proto__: null }) { | ||||||||||||||
let responseURL = url; | ||||||||||||||
let { | ||||||||||||||
importAttributes, | ||||||||||||||
|
@@ -127,19 +127,24 @@ async function defaultLoad(url, context = kEmptyObject) { | |||||||||||||
|
||||||||||||||
throwIfUnsupportedURLScheme(urlInstance, experimentalNetworkImports); | ||||||||||||||
|
||||||||||||||
format ??= await defaultGetFormat(urlInstance, context); | ||||||||||||||
|
||||||||||||||
validateAttributes(url, format, importAttributes); | ||||||||||||||
|
||||||||||||||
if ( | ||||||||||||||
format === 'builtin' || | ||||||||||||||
format === 'commonjs' | ||||||||||||||
) { | ||||||||||||||
if (urlInstance.protocol === 'node:') { | ||||||||||||||
source = null; | ||||||||||||||
} else if (source == null) { | ||||||||||||||
({ responseURL, source } = await getSource(urlInstance, context)); | ||||||||||||||
context.source = source; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (format == null || format === 'commonjs') { | ||||||||||||||
// Now that we have the source for the module, run `defaultGetFormat` again in case we detect ESM syntax. | ||||||||||||||
format = await defaultGetFormat(urlInstance, context); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+137
to
+140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we try to detect ESM if the
Suggested change
|
||||||||||||||
|
||||||||||||||
if (format === 'commonjs') { | ||||||||||||||
source = null; // Let the CommonJS loader handle it (for now) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
validateAttributes(url, format, importAttributes); | ||||||||||||||
|
||||||||||||||
return { | ||||||||||||||
__proto__: null, | ||||||||||||||
format, | ||||||||||||||
|
@@ -178,16 +183,17 @@ function defaultLoadSync(url, context = kEmptyObject) { | |||||||||||||
|
||||||||||||||
throwIfUnsupportedURLScheme(urlInstance, false); | ||||||||||||||
|
||||||||||||||
format ??= defaultGetFormat(urlInstance, context); | ||||||||||||||
|
||||||||||||||
validateAttributes(url, format, importAttributes); | ||||||||||||||
|
||||||||||||||
if (format === 'builtin') { | ||||||||||||||
if (urlInstance.protocol === 'node:') { | ||||||||||||||
source = null; | ||||||||||||||
} else if (source == null) { | ||||||||||||||
({ responseURL, source } = getSourceSync(urlInstance, context)); | ||||||||||||||
context.source = source; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
format ??= defaultGetFormat(urlInstance, context); | ||||||||||||||
|
||||||||||||||
validateAttributes(url, format, importAttributes); | ||||||||||||||
|
||||||||||||||
return { | ||||||||||||||
__proto__: null, | ||||||||||||||
format, | ||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only supports string source, that's a bit annoying. Why can't we pass a
Uint8Array
to the C++ binding?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not opposed if you can get that to work.
compileFunction
expects a string though.