Skip to content

Commit 78ee509

Browse files
authored
feat(turbopack): runtime code support publicPath runtime (#63)
1 parent 6e7674d commit 78ee509

File tree

1 file changed

+18
-3
lines changed
  • turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base

1 file changed

+18
-3
lines changed

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/runtime-base.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined
2121
declare var CHUNK_BASE_PATH: string
2222
declare var CHUNK_SUFFIX_PATH: string
2323

24+
// Support runtime public path from window.publicPath
25+
function getRuntimeChunkBasePath(): string {
26+
if (CHUNK_BASE_PATH === "__RUNTIME_PUBLIC_PATH__") {
27+
if (typeof globalThis !== 'undefined' && typeof (globalThis as any).publicPath === "string") {
28+
return (globalThis as any).publicPath;
29+
}
30+
console.warn(
31+
"publicPath is set to 'runtime' but window.publicPath is not defined or not a string, falling back to '/'"
32+
);
33+
return "/";
34+
}
35+
return CHUNK_BASE_PATH;
36+
}
37+
2438
interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
2539
R: ResolvePathFromModule
2640
}
@@ -344,7 +358,7 @@ function instantiateRuntimeModule(
344358
* Returns the URL relative to the origin where a chunk can be fetched from.
345359
*/
346360
function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl {
347-
return `${CHUNK_BASE_PATH}${chunkPath
361+
return `${getRuntimeChunkBasePath()}${chunkPath
348362
.split('/')
349363
.map((p) => encodeURIComponent(p))
350364
.join('/')}${CHUNK_SUFFIX_PATH}` as ChunkUrl
@@ -368,8 +382,9 @@ function getPathFromScript(
368382
? TURBOPACK_NEXT_CHUNK_URLS.pop()!
369383
: chunkScript.getAttribute('src')!
370384
const src = decodeURIComponent(chunkUrl.replace(/[?#].*$/, ''))
371-
const path = src.startsWith(CHUNK_BASE_PATH)
372-
? src.slice(CHUNK_BASE_PATH.length)
385+
const runtimeBasePath = getRuntimeChunkBasePath()
386+
const path = src.startsWith(runtimeBasePath)
387+
? src.slice(runtimeBasePath.length)
373388
: src
374389
return path as ChunkPath | ChunkListPath
375390
}

0 commit comments

Comments
 (0)