Skip to content

Commit 3cb017d

Browse files
committed
fix: use globalThis[xx] to replace globalThis.xx
1 parent bf64165 commit 3cb017d

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

turbopack/crates/turbopack-browser/src/chunking_context.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ impl BrowserChunkingContextBuilder {
240240
self
241241
}
242242

243+
pub fn chunk_loading_global(mut self, chunk_loading_global: RcStr) -> Self {
244+
self.chunking_context.chunk_loading_global = Some(chunk_loading_global);
245+
self
246+
}
247+
243248
pub fn build(self) -> Vc<BrowserChunkingContext> {
244249
BrowserChunkingContext::cell(self.chunking_context)
245250
}
@@ -328,6 +333,9 @@ pub struct BrowserChunkingContext {
328333
filename: Option<RcStr>,
329334
/// Non evaluate chunk filename template
330335
chunk_filename: Option<RcStr>,
336+
/// The global variable name used for chunk loading.
337+
/// Default: "TURBOPACK"
338+
chunk_loading_global: Option<RcStr>,
331339
}
332340

333341
impl BrowserChunkingContext {
@@ -377,6 +385,7 @@ impl BrowserChunkingContext {
377385
should_use_absolute_url_references: false,
378386
filename: Default::default(),
379387
chunk_filename: Default::default(),
388+
chunk_loading_global: Default::default(),
380389
},
381390
}
382391
}
@@ -480,6 +489,17 @@ impl BrowserChunkingContext {
480489
self.minify_type.cell()
481490
}
482491

492+
/// Returns the chunk loading global variable name.
493+
/// Defaults to "TURBOPACK" if not set.
494+
#[turbo_tasks::function]
495+
pub fn chunk_loading_global(&self) -> Vc<RcStr> {
496+
Vc::cell(
497+
self.chunk_loading_global
498+
.clone()
499+
.unwrap_or_else(|| rcstr!("TURBOPACK")),
500+
)
501+
}
502+
483503
/// Returns the chunk path information.
484504
#[turbo_tasks::function]
485505
fn chunk_path_info(&self) -> Vc<ChunkPathInfo> {

turbopack/crates/turbopack-browser/src/ecmascript/content.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,18 @@ impl EcmascriptBrowserChunkContent {
9999

100100
// When a chunk is executed, it will either register itself with the current
101101
// instance of the runtime, or it will push itself onto the list of pending
102-
// chunks (`self.TURBOPACK`).
102+
// chunks (using the configured chunk loading global variable).
103103
//
104104
// When the runtime executes (see the `evaluate` module), it will pick up and
105105
// register all pending chunks, and replace the list of pending chunks
106106
// with itself so later chunks can register directly with it.
107+
let chunk_loading_global = this.chunking_context.chunk_loading_global().await?;
107108
write!(
108109
code,
109110
// `||=` would be better but we need to be es2020 compatible
110111
//`x || (x = default)` is better than `x = x || default` simply because we avoid _writing_ the property in the common case.
111-
"(globalThis.TURBOPACK || (globalThis.TURBOPACK = [])).push([{script_or_path},"
112+
"(globalThis[\"{chunk_loading_global}\"] || (globalThis[\"{chunk_loading_global}\"] = \
113+
[])).push([{script_or_path},"
112114
)?;
113115

114116
let content = this.content.await?;

turbopack/crates/turbopack-browser/src/ecmascript/evaluate/chunk.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,21 @@ impl EcmascriptBrowserEvaluateChunk {
171171
*this.chunking_context.debug_ids_enabled().await?,
172172
);
173173

174-
// We still use the `TURBOPACK` global variable to store the chunk here,
175-
// as there may be another runtime already loaded in the page.
176-
// This is the case in integration tests.
174+
// Use the configured chunk loading global variable to store the chunk here.
175+
// This allows multiple runtimes to coexist on the same page when using different global
176+
// names.
177+
let chunk_loading_global = this.chunking_context.chunk_loading_global().await?;
177178
writedoc!(
178179
code,
179180
// `||=` would be better but we need to be es2020 compatible
180181
//`x || (x = default)` is better than `x = x || default` simply because we avoid _writing_ the property in the common case.
181182
r#"
182-
(globalThis.TURBOPACK || (globalThis.TURBOPACK = [])).push([
183+
(globalThis["{chunk_loading_global}"] || (globalThis["{chunk_loading_global}"] = [])).push([
183184
{script_or_path},
184-
{}
185+
{params}
185186
]);
186187
"#,
187-
StringifyJs(&params),
188+
params = StringifyJs(&params),
188189
)?;
189190

190191
let runtime_type = *this.chunking_context.runtime_type().await?;
@@ -197,6 +198,7 @@ impl EcmascriptBrowserEvaluateChunk {
197198
runtime_type,
198199
output_root_to_root_path,
199200
source_maps,
201+
this.chunking_context.chunk_loading_global(),
200202
);
201203
code.push_code(&*runtime_code.await?);
202204
}

turbopack/crates/turbopack-browser/src/ecmascript/list/content.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ pub(super) struct EcmascriptDevChunkListContent {
4545
#[bincode(with = "turbo_bincode::indexmap")]
4646
pub(super) chunks_contents: FxIndexMap<String, ResolvedVc<Box<dyn VersionedContent>>>,
4747
source: EcmascriptDevChunkListSource,
48+
/// The global variable name used for chunk loading (derived from chunkLoadingGlobal config).
49+
chunk_loading_global: RcStr,
4850
}
4951

5052
#[turbo_tasks::value_impl]
@@ -70,6 +72,11 @@ impl EcmascriptDevChunkListContent {
7072
CurrentChunkMethodWithData::DocumentCurrentScript
7173
}
7274
};
75+
let chunk_loading_global = (*chunk_list_ref
76+
.chunking_context
77+
.chunk_loading_global()
78+
.await?)
79+
.clone();
7380
Ok(EcmascriptDevChunkListContent {
7481
current_chunk_method,
7582
chunks_contents: chunk_list_ref
@@ -90,6 +97,7 @@ impl EcmascriptDevChunkListContent {
9097
.filter_map(|(path, content)| path.map(|path| (path, content)))
9198
.collect(),
9299
source: chunk_list_ref.source,
100+
chunk_loading_global,
93101
}
94102
.cell())
95103
}
@@ -153,15 +161,16 @@ impl EcmascriptDevChunkListContent {
153161

154162
let mut code = CodeBuilder::default();
155163

156-
// When loaded, JS chunks must register themselves with the `TURBOPACK` global
164+
// When loaded, JS chunks must register themselves with the chunk loading global
157165
// variable. Similarly, we register the chunk list with the
158-
// `TURBOPACK_CHUNK_LISTS` global variable.
166+
// `{chunk_loading_global}_CHUNK_LISTS` global variable.
167+
let chunk_lists_global = format!("{}_CHUNK_LISTS", this.chunk_loading_global);
159168
writedoc!(
160169
code,
161170
// `||=` would be better but we need to be es2020 compatible
162171
//`x || (x = default)` is better than `x = x || default` simply because we avoid _writing_ the property in the common case.
163172
r#"
164-
(globalThis.TURBOPACK_CHUNK_LISTS || (globalThis.TURBOPACK_CHUNK_LISTS = [])).push({{
173+
(globalThis["{chunk_lists_global}"] || (globalThis["{chunk_lists_global}"] = [])).push({{
165174
script: {script_or_path},
166175
chunks: {:#},
167176
source: {:#}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub async fn get_browser_runtime_code(
2424
runtime_type: RuntimeType,
2525
output_root_to_root_path: RcStr,
2626
generate_source_map: bool,
27+
chunk_loading_global: Vc<RcStr>,
2728
) -> Result<Vc<Code>> {
2829
let asset_context = get_runtime_asset_context(*environment).resolve().await?;
2930

@@ -83,12 +84,14 @@ pub async fn get_browser_runtime_code(
8384
let chunk_base_path = chunk_base_path.await?;
8485
let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
8586
let chunk_suffix = chunk_suffix.await?;
87+
let chunk_loading_global = chunk_loading_global.await?;
88+
let chunk_lists_global = format!("{}_CHUNK_LISTS", &*chunk_loading_global);
8689

8790
writedoc!(
8891
code,
8992
r#"
9093
(() => {{
91-
if (!Array.isArray(globalThis.TURBOPACK)) {{
94+
if (!Array.isArray(globalThis["{chunk_loading_global}"])) {{
9295
return;
9396
}}
9497
@@ -178,17 +181,17 @@ pub async fn get_browser_runtime_code(
178181
writedoc!(
179182
code,
180183
r#"
181-
const chunksToRegister = globalThis.TURBOPACK;
182-
globalThis.TURBOPACK = {{ push: registerChunk }};
184+
const chunksToRegister = globalThis["{chunk_loading_global}"];
185+
globalThis["{chunk_loading_global}"] = {{ push: registerChunk }};
183186
chunksToRegister.forEach(registerChunk);
184187
"#
185188
)?;
186189
if matches!(runtime_type, RuntimeType::Development) {
187190
writedoc!(
188191
code,
189192
r#"
190-
const chunkListsToRegister = globalThis.TURBOPACK_CHUNK_LISTS || [];
191-
globalThis.TURBOPACK_CHUNK_LISTS = {{ push: registerChunkList }};
193+
const chunkListsToRegister = globalThis["{chunk_lists_global}"] || [];
194+
globalThis["{chunk_lists_global}"] = {{ push: registerChunkList }};
192195
chunkListsToRegister.forEach(registerChunkList);
193196
"#
194197
)?;

0 commit comments

Comments
 (0)