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
Use synchronous import.meta.resolve
#15575
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
65 changes: 23 additions & 42 deletions
65
packages/babel-core/src/config/files/import-meta-resolve.ts
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 |
---|---|---|
@@ -1,48 +1,29 @@ | ||
import { createRequire } from "module"; | ||
import { resolve as polyfill } from "../../vendor/import-meta-resolve"; | ||
|
||
const require = createRequire(import.meta.url); | ||
declare const USE_ESM: boolean; | ||
|
||
let import_; | ||
try { | ||
// Node < 13.3 doesn't support import() syntax. | ||
import_ = require("./import.cjs"); | ||
} catch {} | ||
let importMetaResolve: (specifier: string, parent: string) => string; | ||
|
||
// import.meta.resolve is only available in ESM, but this file is compiled to CJS. | ||
// We can extract it using dynamic import. | ||
const importMetaResolveP: Promise<ImportMeta["resolve"]> = | ||
import_ && | ||
// Due to a Node.js/V8 bug (https://github.com/nodejs/node/issues/35889), we cannot | ||
// use always dynamic import because it segfaults when running in a Node.js `vm` context, | ||
// which is used by the default Jest environment and by webpack-cli. | ||
// | ||
// However, import.meta.resolve is experimental and only enabled when Node.js is run | ||
// with the `--experimental-import-meta-resolve` flag: we can avoid calling import() | ||
// when that flag is not enabled, so that the default behavior never segfaults. | ||
// | ||
// Hopefully, before Node.js unflags import.meta.resolve, either: | ||
// - we will move to ESM, so that we have direct access to import.meta.resolve, or | ||
// - the V8 bug will be fixed so that we can safely use dynamic import by default. | ||
// | ||
// I (@nicolo-ribaudo) am really annoyed by this bug, because there is no known | ||
// work-around other than "don't use dynamic import if you are running in a `vm` context", | ||
// but there is no reliable way to detect it (you cannot try/catch segfaults). | ||
// | ||
// This is the only place where we *need* to use dynamic import because we need to access | ||
// an ES module. All the other places will first try using require() and *then*, if | ||
// it throws because it's a module, will fallback to import(). | ||
process.execArgv.includes("--experimental-import-meta-resolve") | ||
? import_("data:text/javascript,export default import.meta.resolve").then( | ||
(m: { default: ImportMeta["resolve"] | undefined }) => | ||
m.default || polyfill, | ||
() => polyfill, | ||
) | ||
: Promise.resolve(polyfill); | ||
if (USE_ESM) { | ||
// Node.js < 20, when using the `--experimental-import-meta-resolve` flag, | ||
// have an asynchronous implementation of import.meta.resolve. | ||
if ( | ||
typeof import.meta.resolve === "function" && | ||
typeof import.meta.resolve(import.meta.url) === "string" | ||
) { | ||
// @ts-expect-error: TS defines import.meta as returning a promise | ||
importMetaResolve = import.meta.resolve; | ||
} else { | ||
importMetaResolve = polyfill; | ||
} | ||
} else { | ||
importMetaResolve = polyfill; | ||
} | ||
|
||
export default async function resolve( | ||
specifier: Parameters<ImportMeta["resolve"]>[0], | ||
parent?: Parameters<ImportMeta["resolve"]>[1], | ||
): ReturnType<ImportMeta["resolve"]> { | ||
return (await importMetaResolveP)(specifier, parent); | ||
export default function resolve( | ||
specifier: string, | ||
parent?: string | URL, | ||
): string { | ||
// @ts-expect-error: TS defines import.meta.resolve as returning a promises | ||
return importMetaResolve(specifier, parent); | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export function resolve(specifier: string, parent: string): Promise<string>; | ||
export function resolve(specifier: string, parent: string): string; |
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
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 change is necessary because sometimes
dirname
is undefined here, and Node.js 20 as stricter validation.