Skip to content

Commit 6e7674d

Browse files
authored
fix(turbopack): adapat runtime CHUNK_BASE_PATH to original (#62)
1 parent 2566f38 commit 6e7674d

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

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

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

24-
function normalizeChunkPath(path: string) {
25-
if (path.startsWith('/')) {
26-
path = path.substring(1)
27-
} else if (path.startsWith('./')) {
28-
path = path.substring(2)
29-
}
30-
31-
if (!path.endsWith('/')) {
32-
path += '/'
33-
}
34-
35-
return path
36-
}
37-
38-
const NORMALIZED_CHUNK_BASE_PATH = normalizeChunkPath(CHUNK_BASE_PATH)
39-
4024
interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
4125
R: ResolvePathFromModule
4226
}
@@ -340,8 +324,8 @@ function getWorkerBlobURL(chunks: ChunkPath[]): string {
340324
// It is important to reverse the array so when bootstrapping we can infer what chunk is being
341325
// evaluated by poping urls off of this array. See `getPathFromScript`
342326
let bootstrap = `self.TURBOPACK_WORKER_LOCATION = ${JSON.stringify(location.origin)};
343-
self.TURBOPACK_NEXT_CHUNK_URLS = ${JSON.stringify(chunks.reverse(), null, 2)};
344-
importScripts(...self.TURBOPACK_NEXT_CHUNK_URLS.map(c => self.TURBOPACK_WORKER_LOCATION + "/" + c).reverse());`
327+
self.TURBOPACK_NEXT_CHUNK_URLS = ${JSON.stringify(chunks.reverse().map(getChunkRelativeUrl), null, 2)};
328+
importScripts(...self.TURBOPACK_NEXT_CHUNK_URLS.map(c => self.TURBOPACK_WORKER_LOCATION + c).reverse());`
345329
let blob = new Blob([bootstrap], { type: 'text/javascript' })
346330
return URL.createObjectURL(blob)
347331
}
@@ -360,7 +344,7 @@ function instantiateRuntimeModule(
360344
* Returns the URL relative to the origin where a chunk can be fetched from.
361345
*/
362346
function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl {
363-
return `${NORMALIZED_CHUNK_BASE_PATH}${chunkPath
347+
return `${CHUNK_BASE_PATH}${chunkPath
364348
.split('/')
365349
.map((p) => encodeURIComponent(p))
366350
.join('/')}${CHUNK_SUFFIX_PATH}` as ChunkUrl
@@ -379,18 +363,13 @@ function getPathFromScript(
379363
if (typeof chunkScript === 'string') {
380364
return chunkScript as ChunkPath | ChunkListPath
381365
}
382-
let chunkUrl =
366+
const chunkUrl =
383367
typeof TURBOPACK_NEXT_CHUNK_URLS !== 'undefined'
384368
? TURBOPACK_NEXT_CHUNK_URLS.pop()!
385369
: chunkScript.getAttribute('src')!
386-
if (chunkUrl.startsWith('/')) {
387-
chunkUrl = chunkUrl.substring(1)
388-
} else if (chunkUrl.startsWith('./')) {
389-
chunkUrl = chunkUrl.substring(2)
390-
}
391370
const src = decodeURIComponent(chunkUrl.replace(/[?#].*$/, ''))
392-
const path = src.startsWith(NORMALIZED_CHUNK_BASE_PATH)
393-
? src.slice(NORMALIZED_CHUNK_BASE_PATH.length)
371+
const path = src.startsWith(CHUNK_BASE_PATH)
372+
? src.slice(CHUNK_BASE_PATH.length)
394373
: src
395374
return path as ChunkPath | ChunkListPath
396375
}

turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/dev-backend-dom.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,15 @@ let DEV_BACKEND: DevRuntimeBackend
5151
return
5252
}
5353

54-
const withoutNormalizedChunkUrl = chunkUrl.replace(
55-
NORMALIZED_CHUNK_BASE_PATH,
56-
'/'
57-
)
58-
const decodedChunkUrl = decodeURI(withoutNormalizedChunkUrl)
54+
const decodedChunkUrl = decodeURI(chunkUrl)
5955
const previousLinks = document.querySelectorAll(
60-
`link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"],link[rel=stylesheet][href="${withoutNormalizedChunkUrl}"],link[rel=stylesheet][href^="${withoutNormalizedChunkUrl}?"]`
56+
`link[rel=stylesheet][href="${chunkUrl}"],link[rel=stylesheet][href^="${chunkUrl}?"],link[rel=stylesheet][href="${decodedChunkUrl}"],link[rel=stylesheet][href^="${decodedChunkUrl}?"]`
6157
)
6258

6359
if (previousLinks.length === 0) {
6460
reject(
6561
new Error(
66-
`No link element found for chunk ${withoutNormalizedChunkUrl}`
62+
`No link element found for chunk ${chunkUrl}`
6763
)
6864
)
6965
return
@@ -80,9 +76,9 @@ let DEV_BACKEND: DevRuntimeBackend
8076
//
8177
// Safari has a similar issue, but only if you have a `<link rel=preload ... />` tag
8278
// pointing to the same URL as the stylesheet: https://bugs.webkit.org/show_bug.cgi?id=187726
83-
link.href = `${withoutNormalizedChunkUrl}?ts=${Date.now()}`
79+
link.href = `${chunkUrl}?ts=${Date.now()}`
8480
} else {
85-
link.href = withoutNormalizedChunkUrl
81+
link.href = chunkUrl
8682
}
8783

8884
link.onerror = () => {
@@ -119,10 +115,7 @@ let DEV_BACKEND: DevRuntimeBackend
119115

120116
function _eval({ code, url, map }: EcmascriptModuleEntry): ModuleFactory {
121117
code += `\n\n//# sourceURL=${encodeURI(
122-
location.origin +
123-
NORMALIZED_CHUNK_BASE_PATH +
124-
normalizeChunkPath(url) +
125-
CHUNK_SUFFIX_PATH
118+
location.origin + CHUNK_BASE_PATH + url + CHUNK_SUFFIX_PATH
126119
)}`
127120
if (map) {
128121
code += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(

0 commit comments

Comments
 (0)