-
Notifications
You must be signed in to change notification settings - Fork 0
feat(turbopack): Support auto public path in Turbopack runtime #149
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 |
|---|---|---|
|
|
@@ -834,11 +834,53 @@ contextPrototype.z = requireStub | |
| // Make `globalThis` available to the module in a way that cannot be shadowed by a local variable. | ||
| contextPrototype.g = globalThis | ||
|
|
||
| let cachedAutomaticPublicPath: string | undefined | ||
|
|
||
| function getAutomaticPublicPath(): string { | ||
| if (cachedAutomaticPublicPath !== undefined) { | ||
| return cachedAutomaticPublicPath | ||
| } | ||
|
|
||
| let scriptUrl: string | undefined | ||
| if (typeof document === 'object') { | ||
| const currentScript = document.currentScript as HTMLScriptElement | null | ||
| scriptUrl = currentScript?.src | ||
|
|
||
| if (!scriptUrl) { | ||
| const scripts = document.getElementsByTagName('script') | ||
| const script = scripts[scripts.length - 1] | ||
| scriptUrl = script?.src | ||
| } | ||
|
Comment on lines
+849
to
+853
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 current fallback logic only checks the last script tag in the document. If the last script is an inline script (which lacks a if (!scriptUrl) {
const scripts = document.getElementsByTagName('script')
for (let i = scripts.length - 1; i >= 0; i--) {
const script = scripts[i] as HTMLScriptElement | undefined
if (script?.src) {
scriptUrl = script.src
break
}
}
} |
||
| } | ||
|
|
||
| if ( | ||
| !scriptUrl && | ||
| typeof (globalThis as any).importScripts === 'function' && | ||
| (globalThis as any).location | ||
| ) { | ||
| scriptUrl = String((globalThis as any).location) | ||
| } | ||
|
|
||
| cachedAutomaticPublicPath = scriptUrl | ||
| ? scriptUrl | ||
| .replace(/^blob:/, '') | ||
| .replace(/#.*$/, '') | ||
| .replace(/\?.*$/, '') | ||
| .replace(/\/[^/]*$/, '/') | ||
| : '' | ||
|
|
||
| return cachedAutomaticPublicPath | ||
| } | ||
|
|
||
| /** | ||
| * Gets the public path for runtime assets. | ||
| * Checks globalThis.publicPath and falls back to empty string. | ||
| */ | ||
| function getPublicPath(): string { | ||
| function getPublicPath(mode?: 'auto'): string { | ||
| if (mode === 'auto') { | ||
| return getAutomaticPublicPath() | ||
| } | ||
|
|
||
| if ( | ||
| typeof globalThis !== 'undefined' && | ||
| typeof (globalThis as any).publicPath === 'string' | ||
|
|
||
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.
In ES modules,
document.currentScriptis alwaysnull. To support environments where the runtime might be loaded as an ES module, consider checkingimport.meta.urlif available. This is the standard way to determine the current script's URL in ESM and would improve the reliability of the automatic public path in modern environments.