Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions turbopack/crates/turbopack-browser/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,11 @@ impl ChunkingContext for BrowserChunkingContext {
let ChunkPathInfo {
root_path,
chunk_root_path,
chunk_content_hashing: _,
chunk_content_hashing,
} = &*self.chunk_path_info().await?;

let output_name = ident
.output_name(root_path.clone(), prefix, extension.clone())
.output_name(root_path.clone(), prefix.clone(), extension.clone())
.owned()
.await?;

Expand Down Expand Up @@ -850,8 +850,9 @@ impl ChunkingContext for BrowserChunkingContext {
}
};

match filename_template {
Some(filename) => {
// Explicit templates take precedence over the default content-hashed names.
match (filename_template, *chunk_content_hashing) {
(Some(filename), _) => {
let mut filename = filename.to_string();
let name = escape_file_path(name);

Expand All @@ -877,10 +878,31 @@ impl ChunkingContext for BrowserChunkingContext {

filename
}
None => name.to_string(),
(None, Some(ContentHashing::Direct { length })) => {
let hash = asset
.content()
.content_hash(*this.hash_salt, HashAlgorithm::Xxh3Hash128Base38)
.await?;
let hash = hash.as_ref().context(
"chunk_path requires an asset with file content when content hashing \
is enabled",
)?;
let hash = &hash[..min(length as usize, hash.len())];
if let Some(prefix) = prefix {
format!("{prefix}-{hash}")
} else {
hash.to_string()
}
}
(None, None) => name.to_string(),
}
}
None => {
if chunk_content_hashing.is_some() {
bail!("chunk_path requires an asset when content hashing is enabled");
}
output_name.to_string()
}
None => output_name.to_string(),
};

if !filename.ends_with(extension.as_str()) {
Expand Down
Loading