Skip to content

Commit

Permalink
improve asset path (#5072)
Browse files Browse the repository at this point in the history
### Description

* include basename in static path
* only use 8 chars for hash

This aligns with next.js, which seem to be a reasonable default.

Production mode can omit the basename and only include the hash.
  • Loading branch information
sokra authored May 23, 2023
1 parent b0de3d8 commit 49ac908
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 11 deletions.
6 changes: 5 additions & 1 deletion crates/turbopack-core/src/chunk/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub trait ChunkingContext {

fn can_be_in_same_chunk(&self, asset_a: AssetVc, asset_b: AssetVc) -> BoolVc;

fn asset_path(&self, content_hash: &str, extension: &str) -> FileSystemPathVc;
fn asset_path(
&self,
content_hash: &str,
original_asset_ident: AssetIdentVc,
) -> FileSystemPathVc;

fn is_hot_module_replacement_enabled(&self) -> BoolVc {
BoolVc::cell(false)
Expand Down
22 changes: 19 additions & 3 deletions crates/turbopack-dev/src/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,25 @@ impl ChunkingContext for DevChunkingContext {
}

#[turbo_tasks::function]
fn asset_path(&self, content_hash: &str, extension: &str) -> FileSystemPathVc {
self.asset_root_path
.join(&format!("{content_hash}.{extension}"))
async fn asset_path(
&self,
content_hash: &str,
original_asset_ident: AssetIdentVc,
) -> Result<FileSystemPathVc> {
let source_path = original_asset_ident.path().await?;
let basename = source_path.file_name();
let asset_path = match source_path.extension() {
Some(ext) => format!(
"{basename}.{content_hash}.{ext}",
basename = &basename[..basename.len() - ext.len() - 1],
content_hash = &content_hash[..8]
),
None => format!(
"{basename}.{content_hash}",
content_hash = &content_hash[..8]
),
};
Ok(self.asset_root_path.join(&asset_path))
}

#[turbo_tasks::function]
Expand Down
8 changes: 3 additions & 5 deletions crates/turbopack-static/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ struct StaticAsset {
impl Asset for StaticAsset {
#[turbo_tasks::function]
async fn ident(&self) -> Result<AssetIdentVc> {
let source_path = self.source.ident().path();
let content = self.source.content();
let content_hash = if let AssetContent::File(file) = &*content.await? {
if let FileContent::Content(file) = &*file.await? {
Expand All @@ -150,10 +149,9 @@ impl Asset for StaticAsset {
return Err(anyhow!("StaticAsset::path: unsupported file content"));
};
let content_hash_b16 = turbo_tasks_hash::encode_hex(content_hash);
let asset_path = match source_path.await?.extension() {
Some(ext) => self.context.asset_path(&content_hash_b16, ext),
None => self.context.asset_path(&content_hash_b16, "bin"),
};
let asset_path = self
.context
.asset_path(&content_hash_b16, self.source.ident());
Ok(AssetIdentVc::from_path(asset_path))
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 49ac908

Please sign in to comment.