Skip to content

Commit b55c643

Browse files
committed
Turbopack: add support for multiple options for chunk suffix
1 parent 5b97f1f commit b55c643

File tree

10 files changed

+78
-26
lines changed

10 files changed

+78
-26
lines changed

crates/next-api/src/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ impl Project {
11441144
client_root: self.client_relative_path().owned().await?,
11451145
client_root_to_root_path: rcstr!("/ROOT"),
11461146
asset_prefix: self.next_config().computed_asset_prefix(),
1147-
chunk_suffix_path: self.next_config().chunk_suffix_path(),
1147+
chunk_suffix: self.next_config().chunk_suffix(),
11481148
environment: self.client_compile_time_info().environment(),
11491149
module_id_strategy: self.module_ids(),
11501150
export_usage: self.export_usage(),

crates/next-core/src/next_client/context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use turbopack::{
1414
resolve_options_context::ResolveOptionsContext,
1515
};
1616
use turbopack_browser::{
17-
BrowserChunkingContext, ContentHashing, CurrentChunkMethod,
17+
BrowserChunkingContext, ChunkSuffix, ContentHashing, CurrentChunkMethod,
1818
react_refresh::assert_can_resolve_react_refresh,
1919
};
2020
use turbopack_core::{
@@ -414,7 +414,7 @@ pub struct ClientChunkingContextOptions {
414414
pub client_root: FileSystemPath,
415415
pub client_root_to_root_path: RcStr,
416416
pub asset_prefix: Vc<RcStr>,
417-
pub chunk_suffix_path: Vc<Option<RcStr>>,
417+
pub chunk_suffix: Vc<ChunkSuffix>,
418418
pub environment: Vc<Environment>,
419419
pub module_id_strategy: Vc<Box<dyn ModuleIdStrategy>>,
420420
pub export_usage: Vc<OptionExportUsageInfo>,
@@ -437,7 +437,7 @@ pub async fn get_client_chunking_context(
437437
client_root,
438438
client_root_to_root_path,
439439
asset_prefix,
440-
chunk_suffix_path,
440+
chunk_suffix,
441441
environment,
442442
module_id_strategy,
443443
export_usage,
@@ -452,7 +452,7 @@ pub async fn get_client_chunking_context(
452452

453453
let next_mode = mode.await?;
454454
let asset_prefix = asset_prefix.owned().await?;
455-
let chunk_suffix_path = chunk_suffix_path.to_resolved().await?;
455+
let chunk_suffix = chunk_suffix.to_resolved().await?;
456456
let mut builder = BrowserChunkingContext::builder(
457457
root_path,
458458
client_root.clone(),
@@ -464,7 +464,7 @@ pub async fn get_client_chunking_context(
464464
next_mode.runtime_type(),
465465
)
466466
.chunk_base_path(Some(asset_prefix.clone()))
467-
.chunk_suffix_path(chunk_suffix_path)
467+
.chunk_suffix(chunk_suffix)
468468
.minify_type(if *minify.await? {
469469
MinifyType::Minify {
470470
mangle: (!*no_mangling.await?).then_some(MangleType::OptimalSize),

crates/next-core/src/next_config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use turbopack::module_options::{
1616
ConditionItem, ConditionPath, LoaderRuleItem, WebpackRules,
1717
module_options_context::MdxTransformOptions,
1818
};
19+
use turbopack_browser::ChunkSuffix;
1920
use turbopack_core::{
2021
chunk::SourceMapsType,
2122
issue::{Issue, IssueExt, IssueStage, OptionStyledString, StyledString},
@@ -1673,6 +1674,19 @@ impl NextConfig {
16731674
}
16741675
}
16751676

1677+
/// Returns the suffix to use for chunk loading.
1678+
#[turbo_tasks::function]
1679+
pub async fn chunk_suffix(self: Vc<Self>) -> Result<Vc<ChunkSuffix>> {
1680+
let this = self.await?;
1681+
1682+
match &this.deployment_id {
1683+
Some(deployment_id) => {
1684+
Ok(ChunkSuffix::Constant(format!("?dpl={deployment_id}").into()).cell())
1685+
}
1686+
None => Ok(ChunkSuffix::None.cell()),
1687+
}
1688+
}
1689+
16761690
#[turbo_tasks::function]
16771691
pub fn enable_taint(&self) -> Vc<bool> {
16781692
Vc::cell(self.experimental.taint.unwrap_or(false))

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use turbopack_ecmascript::{
3333
chunk::EcmascriptChunk,
3434
manifest::{chunk_asset::ManifestAsyncModule, loader_item::ManifestLoaderChunkItem},
3535
};
36-
use turbopack_ecmascript_runtime::RuntimeType;
36+
use turbopack_ecmascript_runtime::{ChunkSuffix, RuntimeType};
3737

3838
use crate::ecmascript::{
3939
chunk::EcmascriptBrowserChunk,
@@ -130,8 +130,8 @@ impl BrowserChunkingContextBuilder {
130130
self
131131
}
132132

133-
pub fn chunk_suffix_path(mut self, chunk_suffix_path: ResolvedVc<Option<RcStr>>) -> Self {
134-
self.chunking_context.chunk_suffix_path = Some(chunk_suffix_path);
133+
pub fn chunk_suffix(mut self, chunk_suffix: ResolvedVc<ChunkSuffix>) -> Self {
134+
self.chunking_context.chunk_suffix = Some(chunk_suffix);
135135
self
136136
}
137137

@@ -253,9 +253,9 @@ pub struct BrowserChunkingContext {
253253
/// Base path that will be prepended to all chunk URLs when loading them.
254254
/// This path will not appear in chunk paths or chunk data.
255255
chunk_base_path: Option<RcStr>,
256-
/// Suffix path that will be appended to all chunk URLs when loading them.
256+
/// Suffix that will be appended to all chunk URLs when loading them.
257257
/// This path will not appear in chunk paths or chunk data.
258-
chunk_suffix_path: Option<ResolvedVc<Option<RcStr>>>,
258+
chunk_suffix: Option<ResolvedVc<ChunkSuffix>>,
259259
/// URL prefix that will be prepended to all static asset URLs when loading
260260
/// them.
261261
asset_base_path: Option<RcStr>,
@@ -322,7 +322,7 @@ impl BrowserChunkingContext {
322322
asset_root_path,
323323
asset_root_paths: Default::default(),
324324
chunk_base_path: None,
325-
chunk_suffix_path: None,
325+
chunk_suffix: None,
326326
asset_base_path: None,
327327
asset_base_paths: Default::default(),
328328
enable_hot_module_replacement: false,
@@ -425,11 +425,11 @@ impl BrowserChunkingContext {
425425

426426
/// Returns the asset suffix path.
427427
#[turbo_tasks::function]
428-
pub fn chunk_suffix_path(&self) -> Vc<Option<RcStr>> {
429-
if let Some(chunk_suffix_path) = self.chunk_suffix_path {
430-
*chunk_suffix_path
428+
pub fn chunk_suffix(&self) -> Vc<ChunkSuffix> {
429+
if let Some(chunk_suffix) = self.chunk_suffix {
430+
*chunk_suffix
431431
} else {
432-
Vc::cell(None)
432+
ChunkSuffix::None.cell()
433433
}
434434
}
435435

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl EcmascriptBrowserEvaluateChunk {
173173
let runtime_code = turbopack_ecmascript_runtime::get_browser_runtime_code(
174174
environment,
175175
this.chunking_context.chunk_base_path(),
176-
this.chunking_context.chunk_suffix_path(),
176+
this.chunking_context.chunk_suffix(),
177177
runtime_type,
178178
output_root_to_root_path,
179179
source_maps,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pub mod react_refresh;
1010
pub use chunking_context::{
1111
BrowserChunkingContext, BrowserChunkingContextBuilder, ContentHashing, CurrentChunkMethod,
1212
};
13+
pub use turbopack_ecmascript_runtime::ChunkSuffix;

turbopack/crates/turbopack-ecmascript-runtime/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,3 @@ turbo-tasks-fs = { workspace = true }
2929
turbopack = { workspace = true }
3030
turbopack-core = { workspace = true }
3131
turbopack-ecmascript = { workspace = true }
32-

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ use turbopack_core::{
1111
};
1212
use turbopack_ecmascript::utils::StringifyJs;
1313

14-
use crate::{RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code};
14+
use crate::{
15+
ChunkSuffix, RuntimeType, asset_context::get_runtime_asset_context, embed_js::embed_static_code,
16+
};
1517

1618
/// Returns the code for the ECMAScript runtime.
1719
#[turbo_tasks::function]
1820
pub async fn get_browser_runtime_code(
1921
environment: ResolvedVc<Environment>,
2022
chunk_base_path: Vc<Option<RcStr>>,
21-
chunk_suffix_path: Vc<Option<RcStr>>,
23+
chunk_suffix: Vc<ChunkSuffix>,
2224
runtime_type: RuntimeType,
2325
output_root_to_root_path: RcStr,
2426
generate_source_map: bool,
@@ -80,10 +82,7 @@ pub async fn get_browser_runtime_code(
8082
let relative_root_path = output_root_to_root_path;
8183
let chunk_base_path = chunk_base_path.await?;
8284
let chunk_base_path = chunk_base_path.as_ref().map_or_else(|| "", |f| f.as_str());
83-
let chunk_suffix_path = chunk_suffix_path.await?;
84-
let chunk_suffix_path = chunk_suffix_path
85-
.as_ref()
86-
.map_or_else(|| "", |f| f.as_str());
85+
let chunk_suffix = chunk_suffix.await?;
8786

8887
writedoc!(
8988
code,
@@ -94,16 +93,42 @@ pub async fn get_browser_runtime_code(
9493
}}
9594
9695
const CHUNK_BASE_PATH = {};
97-
const CHUNK_SUFFIX_PATH = {};
9896
const RELATIVE_ROOT_PATH = {};
9997
const RUNTIME_PUBLIC_PATH = {};
10098
"#,
10199
StringifyJs(chunk_base_path),
102-
StringifyJs(chunk_suffix_path),
103100
StringifyJs(relative_root_path.as_str()),
104101
StringifyJs(chunk_base_path),
105102
)?;
106103

104+
match &*chunk_suffix {
105+
ChunkSuffix::None => {
106+
writedoc!(
107+
code,
108+
r#"
109+
const CHUNK_SUFFIX_PATH = "";
110+
"#
111+
)?;
112+
}
113+
ChunkSuffix::Constant(suffix) => {
114+
writedoc!(
115+
code,
116+
r#"
117+
const CHUNK_SUFFIX_PATH = {};
118+
"#,
119+
StringifyJs(suffix.as_str())
120+
)?;
121+
}
122+
ChunkSuffix::FromScriptSrc => {
123+
writedoc!(
124+
code,
125+
r#"
126+
const CHUNK_SUFFIX_PATH = document?.currentScript?.getAttribute?.('src')?.replace(/^(.*(?=\?)|^.*$)/, "") || "";
127+
"#
128+
)?;
129+
}
130+
}
131+
107132
code.push_code(&*shared_runtime_utils_code.await?);
108133
for runtime_code in runtime_base_code {
109134
code.push_code(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use turbo_rcstr::RcStr;
2+
3+
#[turbo_tasks::value(shared)]
4+
pub enum ChunkSuffix {
5+
/// No suffix.
6+
None,
7+
/// A constant suffix to append to chunk URLs.
8+
Constant(RcStr),
9+
/// Read the chunk suffix from the `src` attribute of the current script tag.
10+
FromScriptSrc,
11+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
pub(crate) mod asset_context;
55
pub(crate) mod browser_runtime;
6+
pub(crate) mod chunk_suffix;
67
#[cfg(feature = "test")]
78
pub(crate) mod dummy_runtime;
89
pub(crate) mod embed_js;
910
pub(crate) mod nodejs_runtime;
1011
pub(crate) mod runtime_type;
1112

1213
pub use browser_runtime::get_browser_runtime_code;
14+
pub use chunk_suffix::ChunkSuffix;
1315
#[cfg(feature = "test")]
1416
pub use dummy_runtime::get_dummy_runtime_code;
1517
pub use embed_js::{embed_file, embed_file_path, embed_fs};

0 commit comments

Comments
 (0)