-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
module: detect ESM syntax by trying to recompile as SourceTextModule #52413
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectGetPrototypeOf, | ||
StringPrototypeEndsWith, | ||
SyntaxErrorPrototype, | ||
globalThis, | ||
} = primordials; | ||
|
||
|
@@ -159,27 +161,29 @@ function runEntryPointWithESMLoader(callback) { | |
function executeUserEntryPoint(main = process.argv[1]) { | ||
const resolvedMain = resolveMainPath(main); | ||
const useESMLoader = shouldUseESMLoader(resolvedMain); | ||
|
||
let mainURL; | ||
// Unless we know we should use the ESM loader to handle the entry point per the checks in `shouldUseESMLoader`, first | ||
// try to run the entry point via the CommonJS loader; and if that fails under certain conditions, retry as ESM. | ||
let retryAsESM = false; | ||
if (!useESMLoader) { | ||
const cjsLoader = require('internal/modules/cjs/loader'); | ||
const { Module } = cjsLoader; | ||
if (getOptionValue('--experimental-detect-module')) { | ||
// TODO(joyeecheung): handle this in the CJS loader. Don't try-catch here. | ||
try { | ||
// Module._load is the monkey-patchable CJS module loader. | ||
Module._load(main, null, true); | ||
} catch (error) { | ||
const source = cjsLoader.entryPointSource; | ||
const { shouldRetryAsESM } = require('internal/modules/helpers'); | ||
retryAsESM = shouldRetryAsESM(error.message, source); | ||
// In case the entry point is a large file, such as a bundle, | ||
// ensure no further references can prevent it being garbage-collected. | ||
cjsLoader.entryPointSource = undefined; | ||
if (error != null && ObjectGetPrototypeOf(error) === SyntaxErrorPrototype) { | ||
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. Potential follow-up: Do we control the error being thrown? If so, I'm wondering if we should create a sub-class like 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. The follow up is just don't do the check in JS, and check right after parsing in C++ :) |
||
const { shouldRetryAsESM } = internalBinding('contextify'); | ||
const mainPath = resolvedMain || main; | ||
mainURL = pathToFileURL(mainPath).href; | ||
joyeecheung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
retryAsESM = shouldRetryAsESM(error.message, cjsLoader.entryPointSource, mainURL); | ||
// In case the entry point is a large file, such as a bundle, | ||
// ensure no further references can prevent it being garbage-collected. | ||
cjsLoader.entryPointSource = undefined; | ||
} | ||
if (!retryAsESM) { | ||
const { enrichCJSError } = require('internal/modules/esm/translators'); | ||
enrichCJSError(error, source, resolvedMain); | ||
Comment on lines
-181
to
-182
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. How is removing these lines not causing any tests to break? There are tests that check the output of this “enriched” error that I’d think should be failing without these lines. 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. Those only check part of the sentence because the rest keeps changing (typically it's not a good idea to check all of the message, either). The error is now "enriched" in C++. 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. Actually, I was talking about my followup. In this branch, this part is already just unreachable, even before this patch, because if |
||
throw error; | ||
} | ||
} | ||
|
@@ -190,7 +194,9 @@ function executeUserEntryPoint(main = process.argv[1]) { | |
|
||
if (useESMLoader || retryAsESM) { | ||
const mainPath = resolvedMain || main; | ||
const mainURL = pathToFileURL(mainPath).href; | ||
if (mainURL === undefined) { | ||
mainURL = pathToFileURL(mainPath).href; | ||
} | ||
joyeecheung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
runEntryPointWithESMLoader((cascadedLoader) => { | ||
// Note that if the graph contains unsettled TLA, this may never resolve | ||
|
Uh oh!
There was an error while loading. Please reload this page.