Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
5 changes: 3 additions & 2 deletions crates/pack-core/src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use turbopack_browser::{BrowserChunkingContext, CurrentChunkMethod};
use turbopack_core::{
asset::AssetContent,
chunk::{
ChunkingConfig, ChunkingContext, MangleType, MinifyType, SourceMapSourceType,
SourceMapsType, UnusedReferences, chunk_id_strategy::ModuleIdStrategy,
ChunkingConfig, ChunkingContext, ContentHashing, MangleType, MinifyType,
SourceMapSourceType, SourceMapsType, UnusedReferences, chunk_id_strategy::ModuleIdStrategy,
},
compile_time_info::{
CompileTimeDefineValue, CompileTimeInfo, DefinableNameSegment, FreeVarReference,
Expand Down Expand Up @@ -779,6 +779,7 @@ pub async fn get_client_chunking_context(
Vc::<CssChunkType>::default().to_resolved().await?,
css_chunking_config,
)
.chunk_content_hashing(ContentHashing::Direct { length: 13 })
.module_merging(*scope_hoisting.await?);
}

Expand Down
98 changes: 98 additions & 0 deletions crates/pack-tests/tests/chunk_hashing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use anyhow::Result;
use pack_core::mode::Mode;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TurboTasks, Vc};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::{File, FileContent, FileSystem, VirtualFileSystem};
use turbopack_browser::BrowserChunkingContext;
use turbopack_core::{
asset::AssetContent,
chunk::{ChunkingContext, ContentHashing},
environment::{BrowserEnvironment, Environment, ExecutionEnvironment},
ident::AssetIdent,
virtual_output::VirtualOutputAsset,
};

async fn chunk_filename(salt: &str, length: u8, extension: &str) -> Result<String> {
let root = VirtualFileSystem::new_with_name(rcstr!("chunk-hashing"))
.root()
.owned()
.await?;
let environment = Environment::new(ExecutionEnvironment::Browser(
BrowserEnvironment {
dom: true,
web_worker: false,
service_worker: false,
browserslist_query: rcstr!("defaults"),
}
.cell()
.to_resolved()
.await?,
))
.to_resolved()
.await?;
let context = BrowserChunkingContext::builder(
root.clone(),
root.clone(),
rcstr!(""),
root.clone(),
root.clone(),
root.clone(),
environment,
Mode::Production.runtime_type(),
)
.hash_salt(ResolvedVc::cell(RcStr::from(salt)))
.chunk_content_hashing(ContentHashing::Direct { length })
.build();
let path = root.join("input.js")?;
let content = AssetContent::file(FileContent::Content(File::from("/* chunk */")).cell())
.to_resolved()
.await?;
let asset = VirtualOutputAsset::new(path.clone(), *content);
let output = context
.chunk_path(
Some(Vc::upcast(asset)),
AssetIdent::from_path(path).into_vc(),
Some(rcstr!("worker")),
extension.into(),
)
.await?;
Ok(output.path.to_string())
}

#[tokio::test]
async fn default_chunk_hash_respects_salt_and_length() -> Result<()> {
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
storage_mode: None,
..Default::default()
},
noop_backing_storage(),
));
tt.run_once(async {
check_chunk_hashing().read_strongly_consistent().await?;
Ok(())
})
.await
}

#[turbo_tasks::function(operation, root)]
async fn check_chunk_hashing() -> Result<Vc<()>> {
for extension in [".js", ".css"] {
let unsalted = chunk_filename("", 13, extension).await?;
let salted = chunk_filename("release-a", 13, extension).await?;
assert_ne!(unsalted, salted, "salt must change the output filename");
assert_ne!(salted, chunk_filename("release-b", 13, extension).await?);
assert_eq!(salted, chunk_filename("release-a", 13, extension).await?);
assert!(salted.starts_with("worker-"));
assert!(salted.ends_with(extension));
assert_eq!(salted.len(), "worker-".len() + 13 + extension.len());

let full = chunk_filename("release-a", 25, extension).await?;
assert_eq!(full.len(), "worker-".len() + 25 + extension.len());
for length in [26, u8::MAX] {
assert_eq!(chunk_filename("release-a", length, extension).await?, full);
}
}
Ok(Vc::cell(()))
}

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.

74 changes: 37 additions & 37 deletions crates/pack-tests/tests/snapshot/async_chunk/output/stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"main": {
"name": "main",
"chunks": [
"input_15liy8us72sxf.js",
"main.js"
"3iutdrd92konk.js",
"turbopack-42u84-9btqy5o.js"
],
"assets": [
{
"name": "input_15liy8us72sxf.js"
"name": "3iutdrd92konk.js"
},
{
"name": "main.js"
"name": "turbopack-42u84-9btqy5o.js"
}
]
}
Expand All @@ -22,109 +22,109 @@
"initial": false,
"entry": false,
"recorded": false,
"id": "input_0g__tdm89q74f.js",
"size": 710,
"id": "1nqwnu9guu79c.js",
"size": 704,
"hash": "",
"files": [
"input_0g__tdm89q74f.js"
"1nqwnu9guu79c.js"
]
},
{
"rendered": false,
"initial": false,
"entry": false,
"recorded": false,
"id": "input_15liy8us72sxf.js",
"size": 891,
"id": "3iutdrd92konk.js",
"size": 879,
"hash": "",
"files": [
"input_15liy8us72sxf.js"
"3iutdrd92konk.js"
]
},
{
"rendered": false,
"initial": false,
"entry": false,
"recorded": false,
"id": "main.js",
"size": 264,
"id": "turbopack-42u84-9btqy5o.js",
"size": 258,
"hash": "",
"files": [
"main.js"
"turbopack-42u84-9btqy5o.js"
]
}
],
"assets": [
{
"type": "asset",
"name": "input_0g__tdm89q74f.js",
"name": "007_nutjpavf2.js.map",
"info": {},
"size": 710,
"size": 798,
"emitted": false,
"comparedForEmit": false,
"cached": false,
"chunks": [
"input_0g__tdm89q74f.js"
"007_nutjpavf2.js.map"
]
},
{
"type": "asset",
"name": "input_0g__tdm89q74f.js.map",
"name": "1nqwnu9guu79c.js",
"info": {},
"size": 731,
"size": 704,
"emitted": false,
"comparedForEmit": false,
"cached": false,
"chunks": [
"input_0g__tdm89q74f.js.map"
"1nqwnu9guu79c.js"
]
},
{
"type": "asset",
"name": "input_15liy8us72sxf.js",
"name": "2wq8h34u5n9pv.js.map",
"info": {},
"size": 891,
"size": 731,
"emitted": false,
"comparedForEmit": false,
"cached": false,
"chunks": [
"input_15liy8us72sxf.js"
"2wq8h34u5n9pv.js.map"
]
},
{
"type": "asset",
"name": "input_15liy8us72sxf.js.map",
"name": "2z3sh82dl8glu.js.map",
"info": {},
"size": 798,
"size": 53,
"emitted": false,
"comparedForEmit": false,
"cached": false,
"chunks": [
"input_15liy8us72sxf.js.map"
"2z3sh82dl8glu.js.map"
]
},
{
"type": "asset",
"name": "main.js",
"name": "3iutdrd92konk.js",
"info": {},
"size": 264,
"size": 879,
"emitted": false,
"comparedForEmit": false,
"cached": false,
"chunks": [
"main.js"
"3iutdrd92konk.js"
]
},
{
"type": "asset",
"name": "main.js.map",
"name": "turbopack-42u84-9btqy5o.js",
"info": {},
"size": 53,
"size": 258,
"emitted": false,
"comparedForEmit": false,
"cached": false,
"chunks": [
"main.js.map"
"turbopack-42u84-9btqy5o.js"
]
}
],
Expand All @@ -133,41 +133,41 @@
"name": "async_chunk/input/node_modules/bar/index.js",
"id": 2,
"chunks": [
"input_15liy8us72sxf.js"
"3iutdrd92konk.js"
],
"size": 152
},
{
"name": "async_chunk/input/node_modules/foo/index.js",
"id": 26,
"chunks": [
"input_0g__tdm89q74f.js"
"1nqwnu9guu79c.js"
],
"size": 152
},
{
"name": "async_chunk/input/index.js",
"id": 45,
"chunks": [
"input_15liy8us72sxf.js"
"3iutdrd92konk.js"
],
"size": 266
},
{
"name": "async_chunk/input/import.js",
"id": 76,
"chunks": [
"input_0g__tdm89q74f.js"
"1nqwnu9guu79c.js"
],
"size": 327
},
{
"name": "async_chunk/input/import.js",
"id": 92,
"chunks": [
"input_15liy8us72sxf.js"
"3iutdrd92konk.js"
],
"size": 237
"size": 231
}
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(globalThis["utooChunk_async_chunk_test"] || (globalThis["utooChunk_async_chunk_test"] = [])).push([
typeof document === "object" ? document.currentScript : undefined,
{"otherChunks":["input_15liy8us72sxf.js"],"runtimeModuleIds":[45]}
{"otherChunks":["3iutdrd92konk.js"],"runtimeModuleIds":[45]}
]);
// Dummy runtime

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.

5 changes: 0 additions & 5 deletions crates/pack-tests/tests/snapshot/basic/alias/output/main.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
typeof document === "object" ? document.currentScript : undefined,
{"otherChunks":["37o_c1e84go2i.js","3ifzw8uwk49rx.css"],"runtimeModuleIds":["[project]/basic/alias/input/index.ts [client] (ecmascript)"]}
]);
// Dummy runtime

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

Loading
Loading