Skip to content

Commit cfb9a43

Browse files
committed
fix WebWorkers
1 parent 10f75a8 commit cfb9a43

9 files changed

+38
-32
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
// Used in WebWorkers to tell the runtime about the chunk base path
1515
declare var TURBOPACK_WORKER_LOCATION: string
16+
// Used in WebWorkers to tell the runtime about the chunk suffix
17+
declare var TURBOPACK_CHUNK_SUFFIX: string
1618
// Used in WebWorkers to tell the runtime about the current chunk url since it can't be detected via document.currentScript
1719
// Note it's stored in reversed order to use push and pop
1820
declare var TURBOPACK_NEXT_CHUNK_URLS: ChunkUrl[] | undefined
1921

2022
// Injected by rust code
2123
declare var CHUNK_BASE_PATH: string
22-
declare var CHUNK_SUFFIX_PATH: string
24+
declare var CHUNK_SUFFIX: string
2325

2426
interface TurbopackBrowserBaseContext<M> extends TurbopackBaseContext<M> {
2527
R: ResolvePathFromModule
@@ -322,8 +324,9 @@ function getWorkerBlobURL(chunks: ChunkPath[]): string {
322324
// It is important to reverse the array so when bootstrapping we can infer what chunk is being
323325
// evaluated by poping urls off of this array. See `getPathFromScript`
324326
let bootstrap = `self.TURBOPACK_WORKER_LOCATION = ${JSON.stringify(location.origin)};
327+
self.TURBOPACK_CHUNK_SUFFIX = ${JSON.stringify(CHUNK_SUFFIX)};
325328
self.TURBOPACK_NEXT_CHUNK_URLS = ${JSON.stringify(chunks.reverse().map(getChunkRelativeUrl), null, 2)};
326-
importScripts(...self.TURBOPACK_NEXT_CHUNK_URLS.map(c => self.TURBOPACK_WORKER_LOCATION + c).reverse());`
329+
importScripts(...self.TURBOPACK_NEXT_CHUNK_URLS.map(c => self.TURBOPACK_WORKER_LOCATION + c + self.TURBOPACK_CHUNK_SUFFIX).reverse());`
327330
let blob = new Blob([bootstrap], { type: 'text/javascript' })
328331
return URL.createObjectURL(blob)
329332
}
@@ -345,7 +348,7 @@ function getChunkRelativeUrl(chunkPath: ChunkPath | ChunkListPath): ChunkUrl {
345348
return `${CHUNK_BASE_PATH}${chunkPath
346349
.split('/')
347350
.map((p) => encodeURIComponent(p))
348-
.join('/')}${CHUNK_SUFFIX_PATH}` as ChunkUrl
351+
.join('/')}${CHUNK_SUFFIX}` as ChunkUrl
349352
}
350353

351354
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ let DEV_BACKEND: DevRuntimeBackend
111111

112112
function _eval({ code, url, map }: EcmascriptModuleEntry): ModuleFactory {
113113
code += `\n\n//# sourceURL=${encodeURI(
114-
location.origin + CHUNK_BASE_PATH + url + CHUNK_SUFFIX_PATH
114+
location.origin + CHUNK_BASE_PATH + url + CHUNK_SUFFIX
115115
)}`
116116
if (map) {
117117
code += `\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,${btoa(

turbopack/crates/turbopack-ecmascript-runtime/src/browser_runtime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,15 @@ pub async fn get_browser_runtime_code(
106106
writedoc!(
107107
code,
108108
r#"
109-
const CHUNK_SUFFIX_PATH = "";
109+
const CHUNK_SUFFIX = "";
110110
"#
111111
)?;
112112
}
113113
ChunkSuffix::Constant(suffix) => {
114114
writedoc!(
115115
code,
116116
r#"
117-
const CHUNK_SUFFIX_PATH = {};
117+
const CHUNK_SUFFIX = {};
118118
"#,
119119
StringifyJs(suffix.as_str())
120120
)?;
@@ -123,7 +123,7 @@ pub async fn get_browser_runtime_code(
123123
writedoc!(
124124
code,
125125
r#"
126-
const CHUNK_SUFFIX_PATH = document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, "") || "";
126+
const CHUNK_SUFFIX = (self.TURBOPACK_CHUNK_SUFFIX ?? document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, "")) || "";
127127
"#
128128
)?;
129129
}

turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/output/aaf3a_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js.map

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/output/ba425_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_0151fefb.js

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/5c1d0_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_c0f7e0b0.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/780ce_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_c0f7e0b0.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/5c1d0_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_5aaf1327.js

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/swc_transforms/preset_env/output/780ce_turbopack-tests_tests_snapshot_swc_transforms_preset_env_input_index_5aaf1327.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)