diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 7139cc4607d..096f0b98b72 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -324,7 +324,8 @@ export function formatDiagnostic(diagnostic: JsDiagnostic): ExternalObject<'Diag export interface JsAddingRuntimeModule { name: string generator: () => String - cacheable: boolean + dependentHash: boolean + fullHash: boolean isolate: boolean stage: number } diff --git a/crates/rspack_binding_values/src/codegen_result.rs b/crates/rspack_binding_values/src/codegen_result.rs index 4d258228b87..c6bf7577d91 100644 --- a/crates/rspack_binding_values/src/codegen_result.rs +++ b/crates/rspack_binding_values/src/codegen_result.rs @@ -29,11 +29,10 @@ impl From for JsCodegenerationResult { impl From for JsCodegenerationResults { fn from(results: CodeGenerationResults) -> Self { - let id_result_map = results.module_generation_result_map; + let (map, id_result_map) = results.into_inner(); Self { - map: results - .map + map: map .into_iter() .map(|(module_id, runtime_result_map)| { let mut runtime_map: HashMap = Default::default(); diff --git a/crates/rspack_binding_values/src/module.rs b/crates/rspack_binding_values/src/module.rs index afcfb2110e8..972e44eb562 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/rspack_binding_values/src/module.rs @@ -499,7 +499,8 @@ pub struct JsAddingRuntimeModule { pub name: String, #[napi(ts_type = "() => String")] pub generator: GenerateFn, - pub cacheable: bool, + pub dependent_hash: bool, + pub full_hash: bool, pub isolate: bool, pub stage: u32, } @@ -508,7 +509,8 @@ impl From for RuntimeModuleFromJs { fn from(value: JsAddingRuntimeModule) -> Self { Self { name: value.name, - cacheable: value.cacheable, + full_hash: value.full_hash, + dependent_hash: value.dependent_hash, isolate: value.isolate, stage: RuntimeModuleStage::from(value.stage), generator: Arc::new(move || value.generator.blocking_call_with_sync(())), diff --git a/crates/rspack_core/src/cgm_runtime_requirement_results.rs b/crates/rspack_core/src/cgm_runtime_requirement_results.rs index 0fab8b77821..4c54e4f6165 100644 --- a/crates/rspack_core/src/cgm_runtime_requirement_results.rs +++ b/crates/rspack_core/src/cgm_runtime_requirement_results.rs @@ -13,19 +13,6 @@ impl CgmRuntimeRequirementsResults { requirements.get(runtime) } - pub fn set( - &mut self, - module: ModuleIdentifier, - runtime: RuntimeSpec, - runtime_requirements: RuntimeGlobals, - ) { - let requirements = self - .module_to_runtime_requirements - .entry(module) - .or_default(); - requirements.set(runtime, runtime_requirements); - } - pub fn set_runtime_requirements( &mut self, module: ModuleIdentifier, @@ -35,4 +22,8 @@ impl CgmRuntimeRequirementsResults { .module_to_runtime_requirements .insert(module, runtime_requirements_map); } + + pub fn remove(&mut self, module: &ModuleIdentifier) -> Option> { + self.module_to_runtime_requirements.remove(module) + } } diff --git a/crates/rspack_core/src/chunk.rs b/crates/rspack_core/src/chunk.rs index 8f03ad750bc..faf686382cb 100644 --- a/crates/rspack_core/src/chunk.rs +++ b/crates/rspack_core/src/chunk.rs @@ -426,12 +426,28 @@ impl Chunk { .chunk_graph .get_ordered_chunk_modules(&self.ukey, &compilation.get_module_graph()) { - if let Some(hash) = compilation + let module_identifier = module.identifier(); + let hash = compilation .code_generation_results - .get_hash(&module.identifier(), Some(&self.runtime)) - { - hash.hash(hasher); - } + .get_hash(&module_identifier, Some(&self.runtime)) + .unwrap_or_else(|| { + panic!("Module ({module_identifier}) should have hash result when updating chunk hash."); + }); + hash.hash(hasher); + } + for (runtime_module_identifier, _) in compilation + .chunk_graph + .get_chunk_runtime_modules_in_order(&self.ukey, compilation) + { + let hash = compilation + .runtime_modules_hash + .get(runtime_module_identifier) + .unwrap_or_else(|| { + panic!( + "Runtime module ({runtime_module_identifier}) should have hash result when updating chunk hash." + ); + }); + hash.hash(hasher); } "entry".hash(hasher); for (module, chunk_group) in compilation diff --git a/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs b/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs index d758d2dc43d..7a5938d1b0a 100644 --- a/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs +++ b/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs @@ -314,11 +314,19 @@ impl ChunkGraph { .collect() } - pub fn get_chunk_module_identifiers(&self, chunk: &ChunkUkey) -> &IdentifierSet { + pub fn get_chunk_modules_identifier(&self, chunk: &ChunkUkey) -> &IdentifierSet { let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk); &chunk_graph_chunk.modules } + pub fn get_ordered_chunk_modules_identifier(&self, chunk: &ChunkUkey) -> Vec { + let chunk_graph_chunk = self.expect_chunk_graph_chunk(chunk); + let mut modules: Vec = chunk_graph_chunk.modules.iter().copied().collect(); + // SAFETY: module identifier is unique + modules.sort_unstable_by_key(|m| m.as_str()); + modules + } + pub fn get_ordered_chunk_modules<'module>( &self, chunk: &ChunkUkey, @@ -427,6 +435,23 @@ impl ChunkGraph { cgc.entry_modules.len() } + pub fn has_chunk_full_hash_modules( + &self, + chunk: &ChunkUkey, + runtime_modules: &IdentifierMap>, + ) -> bool { + let cgc = self.expect_chunk_graph_chunk(chunk); + for runtime_module in &cgc.runtime_modules { + let runtime_module = runtime_modules + .get(runtime_module) + .expect("should have runtime_module"); + if !runtime_module.full_hash() { + return true; + } + } + false + } + pub fn add_chunk_runtime_requirements( &mut self, chunk_ukey: &ChunkUkey, diff --git a/crates/rspack_core/src/code_generation_results.rs b/crates/rspack_core/src/code_generation_results.rs index c27b815538d..1eef745e7ed 100644 --- a/crates/rspack_core/src/code_generation_results.rs +++ b/crates/rspack_core/src/code_generation_results.rs @@ -171,6 +171,7 @@ impl CodeGenerationResult { source.hash(&mut hasher); } self.chunk_init_fragments.hash(&mut hasher); + self.runtime_requirements.hash(&mut hasher); self.hash = Some(hasher.digest(hash_digest)); } } @@ -188,8 +189,8 @@ pub static CODE_GEN_RESULT_ID: AtomicU32 = AtomicU32::new(0); #[derive(Debug, Default, Clone)] pub struct CodeGenerationResults { - pub module_generation_result_map: HashMap, - pub map: IdentifierMap>, + module_generation_result_map: HashMap, + map: IdentifierMap>, } impl CodeGenerationResults { @@ -210,6 +211,21 @@ impl CodeGenerationResults { }) } + pub fn insert( + &mut self, + module_identifier: ModuleIdentifier, + codegen_res: CodeGenerationResult, + runtimes: impl IntoIterator, + ) { + let codegen_res_id = codegen_res.id; + self + .module_generation_result_map + .insert(codegen_res_id, codegen_res); + for runtime in runtimes { + self.add(module_identifier, runtime, codegen_res_id); + } + } + pub fn remove(&mut self, module_identifier: &ModuleIdentifier) -> Option<()> { let runtime_map = self.map.remove(module_identifier)?; for result in runtime_map.get_values() { @@ -303,6 +319,15 @@ impl CodeGenerationResults { code_generation_result.hash.as_ref() } + + pub fn into_inner( + self, + ) -> ( + IdentifierMap>, + HashMap, + ) { + (self.map, self.module_generation_result_map) + } } #[derive(Debug)] diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 458e55e09fc..5bb6eec8083 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -10,10 +10,8 @@ use dashmap::DashSet; use indexmap::{IndexMap, IndexSet}; use itertools::Itertools; use rayon::prelude::*; -use rspack_collections::{ - Identifiable, Identifier, IdentifierDashMap, IdentifierMap, IdentifierSet, UkeySet, -}; -use rspack_error::{error, Diagnostic, Result, Severity}; +use rspack_collections::{Identifiable, IdentifierDashMap, IdentifierMap, IdentifierSet, UkeySet}; +use rspack_error::{error, miette::diagnostic, Diagnostic, DiagnosticExt, Result, Severity}; use rspack_fs::ReadableFileSystem; use rspack_futures::FuturesResults; use rspack_hash::{RspackHash, RspackHashDigest}; @@ -148,7 +146,8 @@ pub struct Compilation { other_module_graph: Option, pub dependency_factories: HashMap>, pub runtime_modules: IdentifierMap>, - pub runtime_module_code_generation_results: IdentifierMap<(RspackHashDigest, BoxSource)>, + pub runtime_modules_hash: IdentifierMap, + pub runtime_modules_code_generation_source: IdentifierMap, pub chunk_graph: ChunkGraph, pub chunk_by_ukey: ChunkByUkey, pub chunk_group_by_ukey: ChunkGroupByUkey, @@ -242,7 +241,8 @@ impl Compilation { other_module_graph: None, dependency_factories: Default::default(), runtime_modules: Default::default(), - runtime_module_code_generation_results: Default::default(), + runtime_modules_hash: Default::default(), + runtime_modules_code_generation_source: Default::default(), chunk_by_ukey: Default::default(), chunk_group_by_ukey: Default::default(), entries: Default::default(), @@ -855,10 +855,8 @@ impl Compilation { .into_par_iter() .map(|job| { let module = job.module; - let runtimes = job.runtimes.clone(); ( module, - runtimes, self .old_cache .code_generate_occasion @@ -866,22 +864,21 @@ impl Compilation { let module = module_graph .module_by_identifier(&module) .expect("should have module"); - module.code_generation(self, Some(runtime), None) + module + .code_generation(self, Some(runtime), None) + .map(|mut codegen_res| { + codegen_res.set_hash( + &self.options.output.hash_function, + &self.options.output.hash_digest, + &self.options.output.hash_salt, + ); + codegen_res + }) }), ) }) - .map(|(module, runtime, result)| match result { - Ok((result, runtime, from_cache)) => (module, result, runtime, from_cache, None), - Err(err) => ( - module, - CodeGenerationResult::default(), - runtime, - false, - Some(err), - ), - }) .collect::>(); - for (module, codegen_res, runtimes, from_cache, err) in results { + for (module, (codegen_res, runtimes, from_cache)) in results { if let Some(counter) = cache_counter { if from_cache { counter.hit(); @@ -889,22 +886,23 @@ impl Compilation { counter.miss(); } } - - let codegen_res_id = codegen_res.id; + let codegen_res = match codegen_res { + Ok(codegen_res) => codegen_res, + Err(err) => { + self.push_diagnostic(Diagnostic::from(err).with_module_identifier(Some(module))); + let mut codegen_res = CodeGenerationResult::default(); + codegen_res.set_hash( + &self.options.output.hash_function, + &self.options.output.hash_digest, + &self.options.output.hash_salt, + ); + codegen_res + } + }; self .code_generation_results - .module_generation_result_map - .insert(codegen_res_id, codegen_res); - for runtime in runtimes { - self - .code_generation_results - .add(module, runtime, codegen_res_id); - } + .insert(module, codegen_res, runtimes); self.code_generated_modules.insert(module); - - if let Some(err) = err { - self.push_diagnostic(Diagnostic::from(err).with_module_identifier(Some(module))); - } } Ok(()) } @@ -1283,7 +1281,7 @@ impl Compilation { }); for revoked_module in revoked_modules { self - .runtime_module_code_generation_results + .cgm_runtime_requirements_results .remove(&revoked_module); } mutations.get_affected_modules_with_chunk_graph(self) @@ -1307,6 +1305,7 @@ impl Compilation { let start = logger.time("hashing"); self.create_hash(plugin_driver.clone()).await?; + self.runtime_modules_code_generation()?; logger.time_end(start); let start = logger.time("create module assets"); @@ -1570,8 +1569,6 @@ impl Compilation { pub async fn create_hash(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> { let logger = self.get_logger("rspack.Compilation"); let mut compilation_hasher = RspackHash::from(&self.options.output); - // TODO: runtimeChunk referencedBy for correct hashing AsyncEntrypoint - let runtime_chunk_ukeys = self.get_chunk_graph_entries(); fn try_process_chunk_hash_results( compilation: &mut Compilation, @@ -1592,38 +1589,170 @@ impl Compilation { Ok(()) } + let unordered_runtime_chunks = self.get_chunk_graph_entries(); let start = logger.time("hashing: hash chunks"); - let other_chunk_hash_results: Vec> = - self - .chunk_by_ukey - .keys() - .filter(|key| !runtime_chunk_ukeys.contains(key)) + let other_chunks: Vec<_> = self + .chunk_by_ukey + .keys() + .filter(|key| !unordered_runtime_chunks.contains(key)) + .collect(); + // create hash for runtime modules in other chunks + for chunk in &other_chunks { + for runtime_module_identifier in self.chunk_graph.get_chunk_runtime_modules_iterable(chunk) { + let runtime_module = &self.runtime_modules[runtime_module_identifier]; + let mut hasher = RspackHash::from(&self.options.output); + runtime_module.update_hash(&mut hasher, self, None)?; + let digest = hasher.digest(&self.options.output.hash_digest); + self + .runtime_modules_hash + .insert(*runtime_module_identifier, digest); + } + } + // create hash for other chunks + let other_chunks_hash_results: Vec> = + other_chunks + .into_iter() .map(|chunk| async { let hash_result = self.process_chunk_hash(*chunk, &plugin_driver).await?; Ok((*chunk, hash_result)) }) .collect::>() .into_inner(); - - try_process_chunk_hash_results(self, other_chunk_hash_results)?; + try_process_chunk_hash_results(self, other_chunks_hash_results)?; logger.time_end(start); - // runtime chunks should be hashed after all other chunks - let start = logger.time("hashing: hash runtime chunks"); - self.create_runtime_module_hash()?; - - let runtime_chunk_hash_results: Vec> = - runtime_chunk_ukeys + // collect references for runtime chunks + let mut runtime_chunks_map: HashMap, u32)> = + unordered_runtime_chunks + .into_iter() + .map(|runtime_chunk| (runtime_chunk, (Vec::new(), 0))) + .collect(); + let mut remaining: u32 = 0; + for runtime_chunk_ukey in runtime_chunks_map.keys().copied().collect::>() { + let runtime_chunk = self.chunk_by_ukey.expect_get(&runtime_chunk_ukey); + let groups = runtime_chunk.get_all_referenced_async_entrypoints(&self.chunk_group_by_ukey); + for other in groups + .into_iter() + .map(|group| self.chunk_group_by_ukey.expect_get(&group)) + .map(|group| group.get_runtime_chunk(&self.chunk_group_by_ukey)) + { + let (other_referenced_by, _) = runtime_chunks_map + .get_mut(&other) + .expect("should in runtime_chunks_map"); + other_referenced_by.push(runtime_chunk_ukey); + let info = runtime_chunks_map + .get_mut(&runtime_chunk_ukey) + .expect("should in runtime_chunks_map"); + info.1 += 1; + remaining += 1; + } + } + // sort runtime chunks by its references + let mut runtime_chunks = Vec::with_capacity(runtime_chunks_map.len()); + for (runtime_chunk, (_, remaining)) in &runtime_chunks_map { + if *remaining == 0 { + runtime_chunks.push(*runtime_chunk); + } + } + let mut ready_chunks = Vec::new(); + let mut full_hash_chunks = UkeySet::default(); + let mut i = 0; + while i < runtime_chunks.len() { + let chunk_ukey = runtime_chunks[i]; + let has_full_hash_modules = full_hash_chunks.contains(&chunk_ukey) + || self + .chunk_graph + .has_chunk_full_hash_modules(&chunk_ukey, &self.runtime_modules); + if has_full_hash_modules { + full_hash_chunks.insert(chunk_ukey); + } + let referenced_by = runtime_chunks_map + .get(&chunk_ukey) + .expect("should in runtime_chunks_map") + .0 + .clone(); + for other in referenced_by { + if has_full_hash_modules { + for runtime_module in self.chunk_graph.get_chunk_runtime_modules_iterable(&other) { + let runtime_module = self + .runtime_modules + .get(runtime_module) + .expect("should have runtime_module"); + if runtime_module.dependent_hash() { + full_hash_chunks.insert(other); + break; + } + } + } + remaining -= 1; + let (_, other_remaining) = runtime_chunks_map + .get_mut(&other) + .expect("should in runtime_chunks_map"); + *other_remaining -= 1; + if *other_remaining == 0 { + ready_chunks.push(other); + } + } + if !ready_chunks.is_empty() { + runtime_chunks.append(&mut ready_chunks); + } + i += 1; + } + // create warning for remaining circular references + if remaining > 0 { + let mut circular: Vec<_> = runtime_chunks_map .iter() - .map(|chunk| async { - let hash_result = self.process_chunk_hash(*chunk, &plugin_driver).await?; - Ok((*chunk, hash_result)) + .filter(|(_, (_, remaining))| *remaining != 0) + .map(|(chunk_ukey, _)| self.chunk_by_ukey.expect_get(chunk_ukey)) + .collect(); + circular.sort_unstable_by(|a, b| a.id.cmp(&b.id)); + runtime_chunks.extend(circular.iter().map(|chunk| chunk.ukey)); + let circular_names = circular + .iter() + .map(|chunk| { + chunk + .name + .as_deref() + .or(chunk.id.as_deref()) + .unwrap_or("no id chunk") }) - .collect::>() - .into_inner(); - try_process_chunk_hash_results(self, runtime_chunk_hash_results)?; + .join(", "); + self.push_diagnostic(diagnostic!(severity = Severity::Warn, "Circular dependency between chunks with runtime ({})\nThis prevents using hashes of each other and should be avoided.", circular_names).boxed().into()); + } + + // create hash for runtime chunks and the runtime modules within them + // The subsequent runtime chunks and runtime modules will depend on + // the hash results of the previous runtime chunks and runtime modules. + // Therefore, create hashes one by one in sequence. + let start = logger.time("hashing: hash runtime chunks"); + for runtime_chunk_ukey in runtime_chunks { + for runtime_module_identifier in self + .chunk_graph + .get_chunk_runtime_modules_iterable(&runtime_chunk_ukey) + { + let runtime_module = &self.runtime_modules[runtime_module_identifier]; + let mut hasher = RspackHash::from(&self.options.output); + runtime_module.update_hash(&mut hasher, self, None)?; + let digest = hasher.digest(&self.options.output.hash_digest); + self + .runtime_modules_hash + .insert(*runtime_module_identifier, digest); + } + let (chunk_hash, content_hash) = self + .process_chunk_hash(runtime_chunk_ukey, &plugin_driver) + .await?; + let chunk = self.chunk_by_ukey.expect_get_mut(&runtime_chunk_ukey); + chunk.rendered_hash = Some( + chunk_hash + .rendered(self.options.output.hash_digest_length) + .into(), + ); + chunk.hash = Some(chunk_hash); + chunk.content_hash = content_hash; + } logger.time_end(start); + // create full hash self .chunk_by_ukey .values() @@ -1635,33 +1764,61 @@ impl Compilation { self.hot_index.hash(&mut compilation_hasher); self.hash = Some(compilation_hasher.digest(&self.options.output.hash_digest)); - // here omit re-create full hash runtime module hash, only update compilation hash to runtime chunk content hash + // re-create runtime chunk hash that depend on full hash let start = logger.time("hashing: process full hash chunks"); - self.chunk_by_ukey.values_mut().for_each(|chunk| { - if runtime_chunk_ukeys.contains(&chunk.ukey) { - if let Some(chunk_hash) = &mut chunk.hash { - let mut hasher = RspackHash::from(&self.options.output); - chunk_hash.hash(&mut hasher); - self.hash.hash(&mut hasher); - *chunk_hash = hasher.digest(&self.options.output.hash_digest); - chunk.rendered_hash = Some( - chunk_hash - .rendered(self.options.output.hash_digest_length) - .into(), - ); - } - if let Some(content_hash) = chunk.content_hash.get_mut(&SourceType::JavaScript) { + for runtime_chunk_ukey in full_hash_chunks { + for runtime_module_identifier in self + .chunk_graph + .get_chunk_runtime_modules_iterable(&runtime_chunk_ukey) + { + let runtime_module = &self.runtime_modules[runtime_module_identifier]; + if runtime_module.full_hash() || runtime_module.dependent_hash() { let mut hasher = RspackHash::from(&self.options.output); - content_hash.hash(&mut hasher); - self.hash.hash(&mut hasher); - *content_hash = hasher.digest(&self.options.output.hash_digest); + runtime_module.update_hash(&mut hasher, self, None)?; + let digest = hasher.digest(&self.options.output.hash_digest); + self + .runtime_modules_hash + .insert(*runtime_module_identifier, digest); } } - }); + let chunk = self.chunk_by_ukey.expect_get_mut(&runtime_chunk_ukey); + if let Some(chunk_hash) = &mut chunk.hash { + let mut hasher = RspackHash::from(&self.options.output); + chunk_hash.hash(&mut hasher); + self.hash.hash(&mut hasher); + *chunk_hash = hasher.digest(&self.options.output.hash_digest); + chunk.rendered_hash = Some( + chunk_hash + .rendered(self.options.output.hash_digest_length) + .into(), + ); + } + if let Some(content_hash) = chunk.content_hash.get_mut(&SourceType::JavaScript) { + let mut hasher = RspackHash::from(&self.options.output); + content_hash.hash(&mut hasher); + self.hash.hash(&mut hasher); + *content_hash = hasher.digest(&self.options.output.hash_digest); + } + } logger.time_end(start); Ok(()) } + pub fn runtime_modules_code_generation(&mut self) -> Result<()> { + self.runtime_modules_code_generation_source = self + .runtime_modules + .par_iter() + .map(|(runtime_module_identifier, runtime_module)| -> Result<_> { + let result = runtime_module.code_generation(self, None, None)?; + let source = result + .get(&SourceType::Runtime) + .expect("should have source"); + Ok((*runtime_module_identifier, source.clone())) + }) + .collect::>()?; + Ok(()) + } + async fn process_chunk_hash( &self, chunk_ukey: ChunkUkey, @@ -1724,37 +1881,6 @@ impl Compilation { Ok(()) } - #[instrument(name = "compilation:create_runtime_module_hash", skip_all)] - pub fn create_runtime_module_hash(&mut self) -> Result<()> { - self.runtime_module_code_generation_results = self - .runtime_modules - .par_iter() - .filter_map( - |(identifier, module)| -> Option> { - match module.code_generation(self, None, None) { - Ok(result) => { - let source = result.get(&SourceType::Runtime); - source.map(|source| { - let mut hasher = RspackHash::from(&self.options.output); - module.identifier().hash(&mut hasher); - source.source().hash(&mut hasher); - Ok(( - *identifier, - ( - hasher.digest(&self.options.output.hash_digest), - source.clone(), - ), - )) - }) - } - Err(r) => Some(Err(r)), - } - }, - ) - .collect::>>()?; - Ok(()) - } - pub fn add_runtime_module( &mut self, chunk_ukey: &ChunkUkey, @@ -1763,7 +1889,7 @@ impl Compilation { // add chunk runtime to prefix module identifier to avoid multiple entry runtime modules conflict let chunk = self.chunk_by_ukey.expect_get(chunk_ukey); let runtime_module_identifier = - ModuleIdentifier::from(format!("{:?}/{}", chunk.runtime, module.identifier())); + ModuleIdentifier::from(format!("{}/{}", &chunk.runtime, module.identifier())); module.attach(*chunk_ukey); self.chunk_graph.add_module(runtime_module_identifier); self diff --git a/crates/rspack_core/src/compiler/module_executor/execute.rs b/crates/rspack_core/src/compiler/module_executor/execute.rs index bd935e45641..997f5e1b7fb 100644 --- a/crates/rspack_core/src/compiler/module_executor/execute.rs +++ b/crates/rspack_core/src/compiler/module_executor/execute.rs @@ -194,15 +194,12 @@ impl Task for ExecuteTask { runtime_module_source.size() as f64, ); let result = CodeGenerationResult::default().with_javascript(runtime_module_source); - let result_id = result.id; - compilation - .code_generation_results - .module_generation_result_map - .insert(result.id, result); - compilation - .code_generation_results - .add(*runtime_id, runtime.clone(), result_id); + compilation.code_generation_results.insert( + *runtime_id, + result, + std::iter::once(runtime.clone()), + ); compilation .code_generated_modules .insert(runtime_module.identifier()); @@ -298,7 +295,7 @@ impl Task for ExecuteTask { name: runtime_module.name().to_string(), name_for_condition: runtime_module.name_for_condition().map(|n| n.to_string()), module_type: *runtime_module.module_type(), - cacheable: runtime_module.cacheable(), + cacheable: !(runtime_module.full_hash() || runtime_module.dependent_hash()), size: runtime_module_size .get(&identifier) .map_or(0 as f64, |s| s.to_owned()), diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index a4a8ebbbb1e..c00e88fa40d 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -1037,11 +1037,6 @@ impl Module for ContextModule { .insert(RuntimeGlobals::CREATE_FAKE_NAMESPACE_OBJECT); } } - code_generation_result.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); Ok(code_generation_result) } diff --git a/crates/rspack_core/src/external_module.rs b/crates/rspack_core/src/external_module.rs index 8643d1d3671..f8c64dc1c1c 100644 --- a/crates/rspack_core/src/external_module.rs +++ b/crates/rspack_core/src/external_module.rs @@ -553,11 +553,6 @@ impl Module for ExternalModule { cgr.add(SourceType::JavaScript, source); cgr.chunk_init_fragments = chunk_init_fragments; cgr.runtime_requirements.insert(runtime_requirements); - cgr.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); } }; if concatenation_scope.is_none() { diff --git a/crates/rspack_core/src/normal_module.rs b/crates/rspack_core/src/normal_module.rs index f0f054879aa..e3c80131311 100644 --- a/crates/rspack_core/src/normal_module.rs +++ b/crates/rspack_core/src/normal_module.rs @@ -600,11 +600,6 @@ impl Module for NormalModule { )?; code_generation_result.add(*source_type, CachedSource::new(generation_result).boxed()); } - code_generation_result.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); code_generation_result.concatenation_scope = concatenation_scope; Ok(code_generation_result) } else if let NormalModuleSource::BuiltFailed(error_message) = &self.source { @@ -619,11 +614,6 @@ impl Module for NormalModule { RawSource::from(format!("throw new Error({});\n", json!(error))).boxed(), ); } - code_generation_result.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); Ok(code_generation_result) } else { Err(error!( diff --git a/crates/rspack_core/src/old_cache/occasion/code_generate.rs b/crates/rspack_core/src/old_cache/occasion/code_generate.rs index 9fdef9fe274..a29751506ef 100644 --- a/crates/rspack_core/src/old_cache/occasion/code_generate.rs +++ b/crates/rspack_core/src/old_cache/occasion/code_generate.rs @@ -21,21 +21,25 @@ impl CodeGenerateOccasion { &self, job: CodeGenerationJob, provide: impl Fn(ModuleIdentifier, &RuntimeSpec) -> Result, - ) -> Result<(CodeGenerationResult, Vec, bool)> { + ) -> (Result, Vec, bool) { let storage = match &self.storage { Some(s) => s, None => { - let res = provide(job.module, &job.runtime)?; - return Ok((res, job.runtimes, false)); + let res = provide(job.module, &job.runtime); + return (res, job.runtimes, false); } }; let cache_key = Identifier::from(format!("{}|{}", job.module, job.hash.encoded())); if let Some(value) = storage.get(&cache_key) { - Ok((value, job.runtimes, true)) + (Ok(value), job.runtimes, true) } else { - let res = provide(job.module, &job.runtime)?; - storage.set(cache_key, res.clone()); - Ok((res, job.runtimes, false)) + match provide(job.module, &job.runtime) { + Ok(res) => { + storage.set(cache_key, res.clone()); + (Ok(res), job.runtimes, false) + } + Err(err) => (Err(err), job.runtimes, false), + } } } } diff --git a/crates/rspack_core/src/raw_module.rs b/crates/rspack_core/src/raw_module.rs index af3b054b7b3..90980bed815 100644 --- a/crates/rspack_core/src/raw_module.rs +++ b/crates/rspack_core/src/raw_module.rs @@ -127,18 +127,13 @@ impl Module for RawModule { #[tracing::instrument(name = "RawModule::code_generation", skip_all, fields(identifier = ?self.identifier()))] fn code_generation( &self, - compilation: &crate::Compilation, + _compilation: &crate::Compilation, _runtime: Option<&RuntimeSpec>, _: Option, ) -> Result { let mut cgr = CodeGenerationResult::default(); cgr.runtime_requirements.insert(self.runtime_requirements); cgr.add(SourceType::JavaScript, self.source.clone()); - cgr.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); Ok(cgr) } diff --git a/crates/rspack_core/src/runtime.rs b/crates/rspack_core/src/runtime.rs index c97b5ef92b4..bb1960264d7 100644 --- a/crates/rspack_core/src/runtime.rs +++ b/crates/rspack_core/src/runtime.rs @@ -13,6 +13,20 @@ pub struct RuntimeSpec { key: String, } +impl std::fmt::Display for RuntimeSpec { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut iter = self.iter(); + if let Some(first) = iter.next() { + write!(f, "{first}")?; + } + for r in iter { + write!(f, ",")?; + write!(f, "{r}")?; + } + Ok(()) + } +} + impl std::hash::Hash for RuntimeSpec { fn hash(&self, state: &mut H) { self.key.hash(state); @@ -167,8 +181,7 @@ pub fn merge_runtime(a: &RuntimeSpec, b: &RuntimeSpec) -> RuntimeSpec { } pub fn runtime_to_string(runtime: &RuntimeSpec) -> String { - let arr = runtime.iter().map(|item| item.as_ref()).collect::>(); - arr.join(",") + format!("{runtime}") } pub fn filter_runtime( diff --git a/crates/rspack_core/src/runtime_module.rs b/crates/rspack_core/src/runtime_module.rs index bdfa542ec26..bc5f1587b96 100644 --- a/crates/rspack_core/src/runtime_module.rs +++ b/crates/rspack_core/src/runtime_module.rs @@ -12,9 +12,11 @@ pub trait RuntimeModule: Module + CustomSourceRuntimeModule { fn stage(&self) -> RuntimeModuleStage { RuntimeModuleStage::Normal } - // webpack fullHash || dependentHash - fn cacheable(&self) -> bool { - true + fn full_hash(&self) -> bool { + false + } + fn dependent_hash(&self) -> bool { + false } // if wrap iife fn should_isolate(&self) -> bool { diff --git a/crates/rspack_core/src/stats/mod.rs b/crates/rspack_core/src/stats/mod.rs index 3b2ed5b1fe7..8f16a05d106 100644 --- a/crates/rspack_core/src/stats/mod.rs +++ b/crates/rspack_core/src/stats/mod.rs @@ -1109,9 +1109,9 @@ impl Stats<'_> { let code_generated = self.compilation.code_generated_modules.contains(identifier); let size = self .compilation - .runtime_module_code_generation_results + .runtime_modules_code_generation_source .get(identifier) - .map_or(0 as f64, |(_, source)| source.size() as f64); + .map_or(0 as f64, |source| source.size() as f64); let mut stats = StatsModule { r#type: "module", @@ -1165,7 +1165,7 @@ impl Stats<'_> { stats.identifier = Some(module.identifier()); stats.name = Some(module.name().as_str().into()); stats.name_for_condition = module.name_for_condition().map(|n| n.to_string()); - stats.cacheable = Some(module.cacheable()); + stats.cacheable = Some(!(module.full_hash() || module.dependent_hash())); stats.optional = Some(false); stats.orphan = Some(orphan); stats.dependent = Some(false); diff --git a/crates/rspack_macros/src/runtime_module.rs b/crates/rspack_macros/src/runtime_module.rs index 847a84a5183..2ad699f46af 100644 --- a/crates/rspack_macros/src/runtime_module.rs +++ b/crates/rspack_macros/src/runtime_module.rs @@ -163,11 +163,6 @@ pub fn impl_runtime_module( ) -> rspack_error::Result<::rspack_core::CodeGenerationResult> { let mut result = ::rspack_core::CodeGenerationResult::default(); result.add(::rspack_core::SourceType::Runtime, self.get_generated_code(compilation)?); - result.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); Ok(result) } @@ -180,7 +175,11 @@ pub fn impl_runtime_module( use rspack_util::ext::DynHash; self.name().dyn_hash(hasher); self.stage().dyn_hash(hasher); - self.get_generated_code(compilation)?.dyn_hash(hasher); + if self.full_hash() || self.dependent_hash() { + self.generate_with_custom(compilation)?.dyn_hash(hasher); + } else { + self.get_generated_code(compilation)?.dyn_hash(hasher); + } Ok(()) } } diff --git a/crates/rspack_plugin_javascript/src/plugin/impl_plugin_for_js_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/impl_plugin_for_js_plugin.rs index d8e7edf6197..207378fecac 100644 --- a/crates/rspack_plugin_javascript/src/plugin/impl_plugin_for_js_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/impl_plugin_for_js_plugin.rs @@ -215,8 +215,8 @@ async fn content_hash( .chunk_graph .get_chunk_runtime_modules_in_order(chunk_ukey, compilation) { - if let Some((hash, _)) = compilation - .runtime_module_code_generation_results + if let Some(hash) = compilation + .runtime_modules_hash .get(runtime_module_idenfitier) { hash.hash(&mut hasher); diff --git a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs index 0ca0b290c81..feeb2be87b8 100644 --- a/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/module_concatenation_plugin.rs @@ -10,10 +10,10 @@ use rspack_core::concatenated_module::{ is_esm_dep_like, ConcatenatedInnerModule, ConcatenatedModule, RootModuleContext, }; use rspack_core::{ - filter_runtime, merge_runtime, runtime_to_string, ApplyContext, Compilation, - CompilationOptimizeChunkModules, CompilerOptions, ExportInfoProvided, ExtendedReferencedExport, - LibIdentOptions, Logger, Module, ModuleExt, ModuleGraph, ModuleGraphModule, ModuleIdentifier, - Plugin, PluginContext, ProvidedExports, RuntimeCondition, RuntimeSpec, SourceType, + filter_runtime, merge_runtime, ApplyContext, Compilation, CompilationOptimizeChunkModules, + CompilerOptions, ExportInfoProvided, ExtendedReferencedExport, LibIdentOptions, Logger, Module, + ModuleExt, ModuleGraph, ModuleGraphModule, ModuleIdentifier, Plugin, PluginContext, + ProvidedExports, RuntimeCondition, RuntimeSpec, SourceType, }; use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; @@ -489,8 +489,8 @@ impl ModuleConcatenationPlugin { format!( "{} (expected runtime {}, module is only referenced in {})", readable_identifier, - runtime_to_string(runtime), - runtime_to_string(runtime_condition.as_spec().expect("should be spec")) + runtime, + runtime_condition.as_spec().expect("should be spec") ) }) .collect::>() diff --git a/crates/rspack_plugin_javascript/src/runtime.rs b/crates/rspack_plugin_javascript/src/runtime.rs index 603936922aa..5c26d2e63f5 100644 --- a/crates/rspack_plugin_javascript/src/runtime.rs +++ b/crates/rspack_plugin_javascript/src/runtime.rs @@ -196,13 +196,13 @@ pub fn render_runtime_modules( .map(|(identifier, runtime_module)| { ( compilation - .runtime_module_code_generation_results + .runtime_modules_code_generation_source .get(identifier) .expect("should have runtime module result"), runtime_module, ) }) - .try_for_each(|((_, source), module)| -> Result<()> { + .try_for_each(|(source, module)| -> Result<()> { if source.size() == 0 { return Ok(()); } @@ -226,7 +226,7 @@ pub fn render_runtime_modules( "!function() {\n" })); } - if module.cacheable() { + if !(module.full_hash() || module.dependent_hash()) { sources.add(source.clone()); } else { sources.add(module.generate_with_custom(compilation)?); diff --git a/crates/rspack_plugin_lazy_compilation/src/module.rs b/crates/rspack_plugin_lazy_compilation/src/module.rs index bae47470d13..88a23405a9f 100644 --- a/crates/rspack_plugin_lazy_compilation/src/module.rs +++ b/crates/rspack_plugin_lazy_compilation/src/module.rs @@ -284,11 +284,6 @@ impl Module for LazyCompilationProxyModule { let mut codegen_result = CodeGenerationResult::default().with_javascript(Arc::new(source)); codegen_result.runtime_requirements = runtime_requirements; codegen_result.data = codegen_data; - codegen_result.set_hash( - &compilation.options.output.hash_function, - &compilation.options.output.hash_digest, - &compilation.options.output.hash_salt, - ); Ok(codegen_result) } diff --git a/crates/rspack_plugin_library/src/amd_library_plugin.rs b/crates/rspack_plugin_library/src/amd_library_plugin.rs index a606fd9f683..7af154f0e24 100644 --- a/crates/rspack_plugin_library/src/amd_library_plugin.rs +++ b/crates/rspack_plugin_library/src/amd_library_plugin.rs @@ -101,7 +101,7 @@ fn render( let module_graph = compilation.get_module_graph(); let modules = compilation .chunk_graph - .get_chunk_module_identifiers(chunk_ukey) + .get_chunk_modules_identifier(chunk_ukey) .iter() .filter_map(|identifier| { module_graph diff --git a/crates/rspack_plugin_library/src/system_library_plugin.rs b/crates/rspack_plugin_library/src/system_library_plugin.rs index d9b2208a43e..29c0c775ab0 100644 --- a/crates/rspack_plugin_library/src/system_library_plugin.rs +++ b/crates/rspack_plugin_library/src/system_library_plugin.rs @@ -99,7 +99,7 @@ fn render( let module_graph = compilation.get_module_graph(); let modules = compilation .chunk_graph - .get_chunk_module_identifiers(chunk_ukey) + .get_chunk_modules_identifier(chunk_ukey) .iter() .filter_map(|identifier| { module_graph diff --git a/crates/rspack_plugin_library/src/umd_library_plugin.rs b/crates/rspack_plugin_library/src/umd_library_plugin.rs index 0f2f714440a..193f888e532 100644 --- a/crates/rspack_plugin_library/src/umd_library_plugin.rs +++ b/crates/rspack_plugin_library/src/umd_library_plugin.rs @@ -111,7 +111,7 @@ fn render( let module_graph = compilation.get_module_graph(); let modules = compilation .chunk_graph - .get_chunk_module_identifiers(chunk_ukey) + .get_chunk_modules_identifier(chunk_ukey) .iter() .filter_map(|identifier| { module_graph diff --git a/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs b/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs index 65c659ab01a..3ed40456b3b 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_filename.rs @@ -75,8 +75,8 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { self.id } - fn cacheable(&self) -> bool { - false + fn dependent_hash(&self) -> bool { + true } fn generate(&self, compilation: &Compilation) -> rspack_error::Result { @@ -317,7 +317,6 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { } } } - Ok( RawSource::from(format!( "// This function allow to reference chunks diff --git a/crates/rspack_plugin_runtime/src/runtime_module/get_full_hash.rs b/crates/rspack_plugin_runtime/src/runtime_module/get_full_hash.rs index 1e3a142fb74..fd77ad63530 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/get_full_hash.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/get_full_hash.rs @@ -34,7 +34,7 @@ impl RuntimeModule for GetFullHashRuntimeModule { ) } - fn cacheable(&self) -> bool { - false + fn full_hash(&self) -> bool { + true } } diff --git a/crates/rspack_plugin_runtime/src/runtime_module/public_path.rs b/crates/rspack_plugin_runtime/src/runtime_module/public_path.rs index a983917cb54..c61d0380b28 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/public_path.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/public_path.rs @@ -39,11 +39,11 @@ impl RuntimeModule for PublicPathRuntimeModule { } // be cacheable only when the template does not contain a hash placeholder - fn cacheable(&self) -> bool { + fn full_hash(&self) -> bool { if let Some(template) = self.public_path.template() { - !has_hash_placeholder(template) + has_hash_placeholder(template) } else { - false + true } } } diff --git a/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs b/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs index ef6e8b01b23..356852c1c81 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module_from_js.rs @@ -17,7 +17,8 @@ pub struct RuntimeModuleFromJs { pub name: String, #[derivative(Debug = "ignore")] pub generator: GenerateFn, - pub cacheable: bool, + pub full_hash: bool, + pub dependent_hash: bool, pub isolate: bool, pub stage: RuntimeModuleStage, } @@ -32,8 +33,12 @@ impl RuntimeModule for RuntimeModuleFromJs { Ok(RawSource::from(res).boxed()) } - fn cacheable(&self) -> bool { - self.cacheable + fn full_hash(&self) -> bool { + self.full_hash + } + + fn dependent_hash(&self) -> bool { + self.dependent_hash } fn should_isolate(&self) -> bool { diff --git a/crates/rspack_plugin_runtime/src/runtime_plugin.rs b/crates/rspack_plugin_runtime/src/runtime_plugin.rs index 2238c5bc276..97eccc1e4c2 100644 --- a/crates/rspack_plugin_runtime/src/runtime_plugin.rs +++ b/crates/rspack_plugin_runtime/src/runtime_plugin.rs @@ -167,10 +167,7 @@ async fn js_chunk_hash( .chunk_graph .get_chunk_runtime_modules_iterable(chunk_ukey) { - if let Some((hash, _)) = compilation - .runtime_module_code_generation_results - .get(identifier) - { + if let Some(hash) = compilation.runtime_modules_hash.get(identifier) { hash.hash(hasher); } } diff --git a/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap index 7beac82d6cb..6075bdfdf3f 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/StatsAPI.test.js.snap @@ -49,7 +49,7 @@ Object { main.js, ], filteredModules: undefined, - hash: 0d58a78612045381, + hash: 22db8248166378ff, id: 909, idHints: Array [], initial: true, @@ -173,7 +173,7 @@ Object { errorsCount: 0, filteredAssets: undefined, filteredModules: undefined, - hash: 3743948998412d2b, + hash: fb4a072f963063b0, modules: Array [ Object { assets: Array [], @@ -318,7 +318,7 @@ Object { main.js, ], filteredModules: undefined, - hash: d6a030a1f9f1c4a3, + hash: 798f48dc21ef4ad8, id: 909, idHints: Array [], initial: true, @@ -685,7 +685,7 @@ Object { errorsCount: 0, filteredAssets: undefined, filteredModules: undefined, - hash: 635581ec6eb08649, + hash: 5cf574f75016ac13, modules: Array [ Object { assets: Array [], @@ -1449,7 +1449,7 @@ Object { files: Array [ main.js, ], - hash: 0d58a78612045381, + hash: 22db8248166378ff, id: 909, idHints: Array [], initial: true, @@ -2000,7 +2000,7 @@ Object { main.js, ], filteredModules: undefined, - hash: e19af7b5a84caf66, + hash: b69382a83addabac, id: 909, idHints: Array [], initial: true, @@ -2348,7 +2348,7 @@ exports.c = require("./c?c=3");, errorsCount: 0, filteredAssets: undefined, filteredModules: undefined, - hash: 1652c7954b2e52cd, + hash: 748abbdfb709cfed, modules: Array [ Object { assets: Array [], diff --git a/packages/rspack-test-tools/tests/__snapshots__/StatsOutput.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/StatsOutput.test.js.snap index 118ce93938c..4b740652bb9 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/StatsOutput.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/StatsOutput.test.js.snap @@ -59,7 +59,7 @@ runtime modules 1.61 KiB [no exports] [used exports unknown] -Rspack compiled successfully (2d43d99dc1ab0913) +Rspack compiled successfully (b564b873f19b3062) `; exports[`statsOutput statsOutput/builtin-swc-loader-parse-error should print correct stats for 1`] = ` diff --git a/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/index.js b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/index.js new file mode 100644 index 00000000000..212c74ec259 --- /dev/null +++ b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/index.js @@ -0,0 +1,4 @@ +import { Worker } from "worker_threads"; + +const worker = new Worker(/* webpackChunkName: "worker" */new URL("./worker", import.meta.url)); +worker; diff --git a/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/rspack.config.js b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/rspack.config.js new file mode 100644 index 00000000000..ac30d941960 --- /dev/null +++ b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/rspack.config.js @@ -0,0 +1,29 @@ +const path = require("path"); + +function config(subpath, filename) { + return { + entry: { + main: { + import: "./index.js", + filename: "[name].[chunkhash].js", + } + }, + output: { + path: path.resolve(__dirname, `dist/${subpath}`), + filename, + }, + target: "node", + optimization: { + realContentHash: false, + moduleIds: "named", + chunkIds: "named", + minimize: false, + } + }; +} + +/** @type {import("@rspack/core").Configuration} */ +module.exports = [ + config("a", "[name].[chunkhash].js"), + config("b", "[name].[contenthash].js"), +]; diff --git a/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/test.config.js b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/test.config.js new file mode 100644 index 00000000000..8c3b2d55c54 --- /dev/null +++ b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/test.config.js @@ -0,0 +1,9 @@ +/** @type {import('../../..').THashCaseConfig} */ +module.exports = { + validate(stats) { + const a = stats.stats[0].toJson({ assets: true }); + const b = stats.stats[1].toJson({ assets: true }); + expect(a.assetsByChunkName.main).not.toEqual(b.assetsByChunkName.main); + expect(a.assetsByChunkName.worker).not.toEqual(b.assetsByChunkName.worker); + } +}; diff --git a/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/worker.js b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/worker.js new file mode 100644 index 00000000000..c583e4be7f2 --- /dev/null +++ b/packages/rspack-test-tools/tests/hashCases/depend-async-entrypoint/worker.js @@ -0,0 +1,5 @@ +import { parentPort } from "worker_threads"; + +parentPort.on("message", async data => { + parentPort.postMessage(`pong`); +}); diff --git a/packages/rspack-test-tools/tests/hookCases/compilation#processAssets/update-asset/hooks.snap.txt b/packages/rspack-test-tools/tests/hookCases/compilation#processAssets/update-asset/hooks.snap.txt index 951fc8c96fa..e5486cec677 100644 --- a/packages/rspack-test-tools/tests/hookCases/compilation#processAssets/update-asset/hooks.snap.txt +++ b/packages/rspack-test-tools/tests/hookCases/compilation#processAssets/update-asset/hooks.snap.txt @@ -5,7 +5,7 @@ ```javascript Array [ Object { - "main.c730a4b10be8bd2e.js": (() => { // webpackBootstrap + "main.1a8159bf5e440aaa.js": (() => { // webpackBootstrap var __webpack_modules__ = ({ "600": (function (module) { module.exports = "This is hook" diff --git a/packages/rspack-test-tools/tests/statsAPICases/basic.js b/packages/rspack-test-tools/tests/statsAPICases/basic.js index 58aa6e723d5..3eb6773ad35 100644 --- a/packages/rspack-test-tools/tests/statsAPICases/basic.js +++ b/packages/rspack-test-tools/tests/statsAPICases/basic.js @@ -39,7 +39,7 @@ module.exports = { entry ./fixtures/a cjs self exports reference self [585] ./fixtures/a.js - Rspack compiled successfully (3743948998412d2b) + Rspack compiled successfully (fb4a072f963063b0) `); } }; diff --git a/packages/rspack-test-tools/tests/statsAPICases/chunks.js b/packages/rspack-test-tools/tests/statsAPICases/chunks.js index c0e29650561..b465ef7c1b1 100644 --- a/packages/rspack-test-tools/tests/statsAPICases/chunks.js +++ b/packages/rspack-test-tools/tests/statsAPICases/chunks.js @@ -27,7 +27,7 @@ module.exports = { chunkB.js, ], filteredModules: undefined, - hash: 0d14da86b17fc47b, + hash: e97b56b3bd5c78d7, id: 250, idHints: Array [], initial: false, @@ -144,7 +144,7 @@ module.exports = { main.js, ], filteredModules: undefined, - hash: f21f3308f0dfb2fe, + hash: 717b293a41af1b65, id: 909, idHints: Array [], initial: true, diff --git a/packages/rspack/src/RuntimeModule.ts b/packages/rspack/src/RuntimeModule.ts index c5ed5b28aab..9598deb83cd 100644 --- a/packages/rspack/src/RuntimeModule.ts +++ b/packages/rspack/src/RuntimeModule.ts @@ -24,7 +24,8 @@ export class RuntimeModule { name: module.name, stage: module.stage, generator: module.generate.bind(module), - cacheable: !(module.fullHash || module.dependentHash), + fullHash: module.fullHash, + dependentHash: module.dependentHash, isolate: module.shouldIsolate() }; } diff --git a/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/async.$5246a448d9c24f5e21d4$.css b/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/async.$c351e5d25b5dca2a32cf$.css similarity index 100% rename from tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/async.$5246a448d9c24f5e21d4$.css rename to tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/async.$c351e5d25b5dca2a32cf$.css diff --git a/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.$5246a448d9c24f5e21d4$.css b/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.$c351e5d25b5dca2a32cf$.css similarity index 100% rename from tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.$5246a448d9c24f5e21d4$.css rename to tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.$c351e5d25b5dca2a32cf$.css diff --git a/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.js b/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.js index 2461ba460db..18bdc9ebafc 100644 --- a/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.js +++ b/tests/plugin-test/css-extract/cases/chunkFilename-fullhash/expected/main.js @@ -77,7 +77,7 @@ __webpack_require__.e = function (chunkId) { // webpack/runtime/get_full_hash (() => { __webpack_require__.h = function () { - return "5246a448d9c24f5e21d4"; + return "c351e5d25b5dca2a32cf"; }; })(); diff --git a/tests/plugin-test/css-extract/cases/issue-6649/expected/main.js b/tests/plugin-test/css-extract/cases/issue-6649/expected/main.js index 63e231ed783..cc86fab714a 100644 --- a/tests/plugin-test/css-extract/cases/issue-6649/expected/main.js +++ b/tests/plugin-test/css-extract/cases/issue-6649/expected/main.js @@ -89,7 +89,7 @@ __webpack_require__.e = function (chunkId) { // return url for filenames not based on template // return url for filenames based on template - return "" + chunkId + ".$" + {"\\css\\chunk": "1190e4d4c19931bc06ac","\\js\\chunk": "f91d90c484e23a385eb8",}[chunkId] + "$.js"; + return "" + chunkId + ".$" + {"\\css\\chunk": "046caa79b39b0d0047fa","\\js\\chunk": "cc036d98e921db211adc",}[chunkId] + "$.js"; }; })(); @@ -107,7 +107,7 @@ __webpack_require__.e = function (chunkId) { // webpack/runtime/get_full_hash (() => { __webpack_require__.h = function () { - return "2cb9f7fa16d1389f519a"; + return "ab7de4b64ec90ccb1b34"; }; })(); diff --git a/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap b/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap index 6185ee0df95..bc9d26624d0 100644 --- a/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap +++ b/tests/webpack-test/__snapshots__/StatsTestCases.basictest.js.snap @@ -293,12 +293,12 @@ Rspack x.x.x compiled successfully in X.23" `; exports[`StatsTestCases should print correct stats for commons-plugin-issue-4980 1`] = ` -"asset app.1c539f266d48d0a5-1.js 52 bytes [emitted] [immutable] (name: app) +"asset app.917a282aaa7649f0-1.js 52 bytes [emitted] [immutable] (name: app) orphan modules 205 bytes [orphan] 3 modules ./entry-1.js 67 bytes [built] [code generated] Rspack x.x.x compiled successfully in X.23 -asset app.2025578d070e061d-2.js 52 bytes [emitted] [immutable] (name: app) +asset app.867a35ac234b0999-2.js 52 bytes [emitted] [immutable] (name: app) orphan modules 212 bytes [orphan] 3 modules ./entry-2.js 67 bytes [built] [code generated] Rspack x.x.x compiled successfully in X.23" @@ -456,8 +456,8 @@ Rspack x.x.x compiled successfully in X.23" `; exports[`StatsTestCases should print correct stats for immutable 1`] = ` -"asset f414fa3908f1115c.js 10.4 KiB [emitted] [immutable] (name: main) -asset 40667e2577410dff.js 167 bytes [emitted] [immutable]" +"asset 5710f9225b4a9f1d.js 10.4 KiB [emitted] [immutable] (name: main) +asset a8cc7dbccc4a96b6.js 167 bytes [emitted] [immutable]" `; exports[`StatsTestCases should print correct stats for import-context-filter 1`] = ` @@ -540,19 +540,19 @@ Rspack x.x.x compiled with 2 warnings" `; exports[`StatsTestCases should print correct stats for issue-7577 1`] = ` -"asset a-runtime~main-17f21faa62bdb7bb.js 3.69 KiB [emitted] [immutable] (name: runtime~main) +"asset a-runtime~main-66c63fc1b760e4bf.js 3.69 KiB [emitted] [immutable] (name: runtime~main) asset a-main-c167d6c811589e05.js 387 bytes [emitted] [immutable] (name: main) -asset a-all-a_js-a472f6a8b86466dc.js 128 bytes [emitted] [immutable] (id hint: all) -Entrypoint main 4.19 KiB = a-runtime~main-17f21faa62bdb7bb.js 3.69 KiB a-all-a_js-a472f6a8b86466dc.js 128 bytes a-main-c167d6c811589e05.js 387 bytes +asset a-all-a_js-4453fb2377a6cd81.js 128 bytes [emitted] [immutable] (id hint: all) +Entrypoint main 4.19 KiB = a-runtime~main-66c63fc1b760e4bf.js 3.69 KiB a-all-a_js-4453fb2377a6cd81.js 128 bytes a-main-c167d6c811589e05.js 387 bytes runtime modules 2.59 KiB 3 modules ./a.js 18 bytes [built] [code generated] Rspack x.x.x compiled successfully in X.23 -asset b-runtime~main-5f7de16be4ee2043.js 4.38 KiB [emitted] [immutable] (name: runtime~main) -asset b-all-b_js-a9ca9198c8bdf730.js 453 bytes [emitted] [immutable] (id hint: all) +asset b-runtime~main-1b1cece62a4334b6.js 4.38 KiB [emitted] [immutable] (name: runtime~main) +asset b-all-b_js-925eeefa13181222.js 453 bytes [emitted] [immutable] (id hint: all) asset b-main-ffcebec46cc0de90.js 420 bytes [emitted] [immutable] (name: main) -asset b-vendors-node_modules_vendor_js-bc8505a1fb88bfa8.js 173 bytes [emitted] [immutable] (id hint: vendors) -Entrypoint main 5.41 KiB = b-runtime~main-5f7de16be4ee2043.js 4.38 KiB b-vendors-node_modules_vendor_js-bc8505a1fb88bfa8.js 173 bytes b-all-b_js-a9ca9198c8bdf730.js 453 bytes b-main-ffcebec46cc0de90.js 420 bytes +asset b-vendors-node_modules_vendor_js-1d0ff6f1d690d856.js 173 bytes [emitted] [immutable] (id hint: vendors) +Entrypoint main 5.41 KiB = b-runtime~main-1b1cece62a4334b6.js 4.38 KiB b-vendors-node_modules_vendor_js-1d0ff6f1d690d856.js 173 bytes b-all-b_js-925eeefa13181222.js 453 bytes b-main-ffcebec46cc0de90.js 420 bytes runtime modules 3.17 KiB 5 modules cacheable modules 40 bytes ./b.js 17 bytes [built] [code generated] @@ -560,12 +560,12 @@ cacheable modules 40 bytes Rspack x.x.x compiled successfully in X.23 assets by chunk 862 bytes (id hint: all) - asset c-all-b_js-74d59afb5f404d7b.js 480 bytes [emitted] [immutable] (id hint: all) - asset c-all-c_js-98bd0b9b8f3b16c9.js 382 bytes [emitted] [immutable] (id hint: all) -asset c-runtime~main-bde6f83f12591f30.js 11.1 KiB [emitted] [immutable] (name: runtime~main) -asset c-main-a033c6e853d0e224.js 643 bytes [emitted] [immutable] (name: main) -asset c-vendors-node_modules_vendor_js-bc8505a1fb88bfa8.js 173 bytes [emitted] [immutable] (id hint: vendors) -Entrypoint main 12.1 KiB = c-runtime~main-bde6f83f12591f30.js 11.1 KiB c-all-c_js-98bd0b9b8f3b16c9.js 382 bytes c-main-a033c6e853d0e224.js 643 bytes + asset c-all-b_js-fd9086bdc6af7526.js 480 bytes [emitted] [immutable] (id hint: all) + asset c-all-c_js-386c5201368b6f93.js 382 bytes [emitted] [immutable] (id hint: all) +asset c-runtime~main-e517c994bd9813ce.js 11.1 KiB [emitted] [immutable] (name: runtime~main) +asset c-main-8c8dbde8063310b6.js 643 bytes [emitted] [immutable] (name: main) +asset c-vendors-node_modules_vendor_js-1d0ff6f1d690d856.js 173 bytes [emitted] [immutable] (id hint: vendors) +Entrypoint main 12.1 KiB = c-runtime~main-e517c994bd9813ce.js 11.1 KiB c-all-c_js-386c5201368b6f93.js 382 bytes c-main-8c8dbde8063310b6.js 643 bytes runtime modules 9.7 KiB 13 modules cacheable modules 101 bytes ./c.js 61 bytes [built] [code generated]