From 9372e2e71098e019d646cf039a2e6554539b615d Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Fri, 20 Dec 2024 20:48:32 +0800 Subject: [PATCH 1/5] feat(incremental): incrementally side effects optimize --- crates/node_binding/binding.d.ts | 1 + crates/rspack_binding_values/src/chunk.rs | 10 +- .../rspack_binding_values/src/chunk_graph.rs | 2 +- .../raw_experiments/raw_incremental.rs | 4 + crates/rspack_core/src/cgm_hash_results.rs | 28 -- .../src/cgm_runtime_requirement_results.rs | 33 -- crates/rspack_core/src/chunk.rs | 22 +- .../src/chunk_graph/chunk_graph_chunk.rs | 16 +- .../src/chunk_graph/chunk_graph_module.rs | 18 +- crates/rspack_core/src/chunk_group.rs | 2 +- .../src/code_generation_results.rs | 343 ------------------ .../rspack_core/src/compiler/compilation.rs | 134 +++---- crates/rspack_core/src/compiler/hmr.rs | 43 ++- .../src/compiler/module_executor/execute.rs | 4 +- crates/rspack_core/src/concatenated_module.rs | 4 +- crates/rspack_core/src/context_module.rs | 29 +- .../src/dependency/module_dependency.rs | 4 - .../src/dependency/runtime_template.rs | 18 +- crates/rspack_core/src/external_module.rs | 2 +- crates/rspack_core/src/incremental/mod.rs | 21 +- .../rspack_core/src/incremental/mutations.rs | 2 +- crates/rspack_core/src/lib.rs | 6 +- crates/rspack_core/src/module_graph/mod.rs | 6 +- .../src/old_cache/occasion/chunk_render.rs | 2 +- crates/rspack_core/src/stats/mod.rs | 31 +- crates/rspack_core/src/stats/utils.rs | 20 +- .../src/deterministic_chunk_ids_plugin.rs | 14 +- .../src/deterministic_module_ids_plugin.rs | 4 +- crates/rspack_ids/src/id_helpers.rs | 21 +- .../rspack_ids/src/named_chunk_ids_plugin.rs | 39 +- .../rspack_ids/src/named_module_ids_plugin.rs | 8 +- .../src/natural_chunk_ids_plugin.rs | 2 +- .../src/natural_module_ids_plugin.rs | 4 +- .../src/occurrence_chunk_ids_plugin.rs | 8 +- crates/rspack_plugin_asset/src/lib.rs | 6 +- crates/rspack_plugin_banner/src/lib.rs | 10 +- .../src/plugin/impl_plugin_for_css_plugin.rs | 16 +- crates/rspack_plugin_css/src/runtime/mod.rs | 2 +- crates/rspack_plugin_css/src/utils.rs | 4 +- .../src/module_filename_helpers.rs | 7 +- .../src/source_map_dev_tool_plugin.rs | 12 +- .../src/lib_manifest_plugin.rs | 24 +- .../rspack_plugin_extract_css/src/plugin.rs | 18 +- .../rspack_plugin_extract_css/src/runtime.rs | 4 +- crates/rspack_plugin_hmr/src/lib.rs | 16 +- .../commonjs/module_decorator_dependency.rs | 7 +- ...sm_export_imported_specifier_dependency.rs | 8 - .../dependency/esm/esm_import_dependency.rs | 4 - .../src/dependency/worker/mod.rs | 2 +- .../src/plugin/impl_plugin_for_js_plugin.rs | 16 +- .../src/plugin/infer_async_modules_plugin.rs | 2 +- .../src/plugin/mod.rs | 4 +- .../src/plugin/side_effects_flag_plugin.rs | 144 ++++++-- .../rspack_plugin_javascript/src/runtime.rs | 5 +- .../src/module.rs | 4 +- .../src/amd_library_plugin.rs | 12 +- .../src/assign_library_plugin.rs | 14 +- .../src/umd_library_plugin.rs | 12 +- crates/rspack_plugin_library/src/utils.rs | 2 +- .../src/container/fallback_module.rs | 4 +- .../src/container/remote_module.rs | 4 +- .../src/container/remote_runtime_module.rs | 12 +- .../sharing/consume_shared_runtime_module.rs | 4 +- .../src/array_push_callback_chunk_format.rs | 4 +- .../src/common_js_chunk_format.rs | 2 +- crates/rspack_plugin_runtime/src/helpers.rs | 22 +- .../src/module_chunk_format.rs | 6 +- .../src/runtime_module/auto_public_path.rs | 12 +- .../runtime_module/chunk_prefetch_startup.rs | 4 +- .../src/runtime_module/get_chunk_filename.rs | 28 +- .../get_chunk_update_filename.rs | 6 +- .../src/runtime_module/get_main_filename.rs | 12 +- .../startup_chunk_dependencies.rs | 2 +- .../src/runtime_module/utils.rs | 18 +- .../src/parser_and_generator.rs | 7 +- packages/rspack/etc/core.api.md | 16 +- packages/rspack/src/config/defaults.ts | 1 + packages/rspack/src/config/normalization.ts | 3 +- packages/rspack/src/config/types.ts | 5 + packages/rspack/src/config/zod.ts | 1 + 80 files changed, 627 insertions(+), 806 deletions(-) delete mode 100644 crates/rspack_core/src/cgm_hash_results.rs delete mode 100644 crates/rspack_core/src/cgm_runtime_requirement_results.rs delete mode 100644 crates/rspack_core/src/code_generation_results.rs diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 5156de84c69..453f3d81e92 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -1512,6 +1512,7 @@ export interface RawIncremental { inferAsyncModules: boolean providedExports: boolean dependenciesDiagnostics: boolean + sideEffects: boolean buildChunkGraph: boolean moduleIds: boolean chunkIds: boolean diff --git a/crates/rspack_binding_values/src/chunk.rs b/crates/rspack_binding_values/src/chunk.rs index 416db1ab26e..feb6529f289 100644 --- a/crates/rspack_binding_values/src/chunk.rs +++ b/crates/rspack_binding_values/src/chunk.rs @@ -41,7 +41,7 @@ impl JsChunk { #[napi(getter)] pub fn id(&self) -> napi::Result> { let (compilation, chunk) = self.as_ref()?; - Ok(match chunk.id(&compilation.chunk_ids) { + Ok(match chunk.id(&compilation.chunk_ids_artifact) { Some(id) => Either::A(id.as_str()), None => Either::B(()), }) @@ -52,7 +52,7 @@ impl JsChunk { let (compilation, chunk) = self.as_ref()?; Ok( chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .map(|id| vec![id.as_str()]) .unwrap_or_default(), ) @@ -107,7 +107,7 @@ impl JsChunk { let (compilation, chunk) = self.as_ref()?; Ok( match chunk - .hash(&compilation.chunk_hashes_results) + .hash(&compilation.chunk_hashes_artifact) .map(|d| d.encoded()) { Some(hash) => Either::A(hash), @@ -121,7 +121,7 @@ impl JsChunk { let (compilation, chunk) = self.as_ref()?; Ok( chunk - .content_hash(&compilation.chunk_hashes_results) + .content_hash(&compilation.chunk_hashes_artifact) .map(|content_hash| { content_hash .iter() @@ -137,7 +137,7 @@ impl JsChunk { let (compilation, chunk) = self.as_ref()?; Ok( match chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, ) { Some(hash) => Either::A(hash), diff --git a/crates/rspack_binding_values/src/chunk_graph.rs b/crates/rspack_binding_values/src/chunk_graph.rs index ab071c75df1..86d704a5c83 100644 --- a/crates/rspack_binding_values/src/chunk_graph.rs +++ b/crates/rspack_binding_values/src/chunk_graph.rs @@ -123,7 +123,7 @@ impl JsChunkGraph { pub fn get_module_id(&self, js_module: &JsModule) -> napi::Result> { let compilation = self.as_ref()?; Ok( - ChunkGraph::get_module_id(&compilation.module_ids, js_module.identifier) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, js_module.identifier) .map(|module_id| module_id.as_str()), ) } diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_incremental.rs b/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_incremental.rs index ffa80e58a05..43888ddf678 100644 --- a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_incremental.rs +++ b/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_incremental.rs @@ -8,6 +8,7 @@ pub struct RawIncremental { pub infer_async_modules: bool, pub provided_exports: bool, pub dependencies_diagnostics: bool, + pub side_effects: bool, pub build_chunk_graph: bool, pub module_ids: bool, pub chunk_ids: bool, @@ -35,6 +36,9 @@ impl From for IncrementalPasses { if value.dependencies_diagnostics { passes.insert(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS); } + if value.side_effects { + passes.insert(IncrementalPasses::SIDE_EFFECTS); + } if value.build_chunk_graph { passes.insert(IncrementalPasses::BUILD_CHUNK_GRAPH); } diff --git a/crates/rspack_core/src/cgm_hash_results.rs b/crates/rspack_core/src/cgm_hash_results.rs deleted file mode 100644 index 8699c76133e..00000000000 --- a/crates/rspack_core/src/cgm_hash_results.rs +++ /dev/null @@ -1,28 +0,0 @@ -use rspack_collections::IdentifierMap; -use rspack_hash::RspackHashDigest; - -use crate::{ModuleIdentifier, RuntimeSpec, RuntimeSpecMap}; - -#[derive(Debug, Default)] -pub struct CgmHashResults { - module_to_hashes: IdentifierMap>, -} - -impl CgmHashResults { - pub fn is_empty(&self) -> bool { - self.module_to_hashes.is_empty() - } - - pub fn get(&self, module: &ModuleIdentifier, runtime: &RuntimeSpec) -> Option<&RspackHashDigest> { - let hashes = self.module_to_hashes.get(module)?; - hashes.get(runtime) - } - - pub fn set_hashes(&mut self, module: ModuleIdentifier, hashes: RuntimeSpecMap) { - self.module_to_hashes.insert(module, hashes); - } - - pub fn remove(&mut self, module: &ModuleIdentifier) -> Option> { - self.module_to_hashes.remove(module) - } -} diff --git a/crates/rspack_core/src/cgm_runtime_requirement_results.rs b/crates/rspack_core/src/cgm_runtime_requirement_results.rs deleted file mode 100644 index ecd1e4457c9..00000000000 --- a/crates/rspack_core/src/cgm_runtime_requirement_results.rs +++ /dev/null @@ -1,33 +0,0 @@ -use rspack_collections::IdentifierMap; - -use crate::{ModuleIdentifier, RuntimeGlobals, RuntimeSpec, RuntimeSpecMap}; - -#[derive(Debug, Default)] -pub struct CgmRuntimeRequirementsResults { - module_to_runtime_requirements: IdentifierMap>, -} - -impl CgmRuntimeRequirementsResults { - pub fn is_empty(&self) -> bool { - self.module_to_runtime_requirements.is_empty() - } - - pub fn get(&self, module: &ModuleIdentifier, runtime: &RuntimeSpec) -> Option<&RuntimeGlobals> { - let requirements = self.module_to_runtime_requirements.get(module)?; - requirements.get(runtime) - } - - pub fn set_runtime_requirements( - &mut self, - module: ModuleIdentifier, - runtime_requirements_map: RuntimeSpecMap, - ) { - self - .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 26621dc2455..f0d8d35bc0d 100644 --- a/crates/rspack_core/src/chunk.rs +++ b/crates/rspack_core/src/chunk.rs @@ -12,7 +12,7 @@ use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher}; use crate::chunk_graph_chunk::ChunkId; use crate::{ compare_chunk_group, merge_runtime, sort_group_by_index, ChunkGraph, ChunkGroupOrderKey, - RenderManifestEntry, + ChunkIdsArtifact, RenderManifestEntry, }; use crate::{ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey, SourceType}; use crate::{Compilation, EntryOptions, Filename, ModuleGraph, RuntimeSpec}; @@ -108,21 +108,17 @@ impl Chunk { self.css_filename_template = filename_template; } - pub fn id<'a>(&self, chunk_ids: &'a UkeyMap) -> Option<&'a ChunkId> { + pub fn id<'a>(&self, chunk_ids: &'a ChunkIdsArtifact) -> Option<&'a ChunkId> { ChunkGraph::get_chunk_id(chunk_ids, &self.ukey) } - pub fn expect_id<'a>(&self, chunk_ids: &'a UkeyMap) -> &'a ChunkId { + pub fn expect_id<'a>(&self, chunk_ids: &'a ChunkIdsArtifact) -> &'a ChunkId { self .id(chunk_ids) .expect("Should set id before calling expect_id") } - pub fn set_id( - &self, - chunk_ids: &mut UkeyMap, - id: impl Into, - ) -> bool { + pub fn set_id(&self, chunk_ids: &mut ChunkIdsArtifact, id: impl Into) -> bool { let id = id.into(); ChunkGraph::set_chunk_id(chunk_ids, self.ukey, id) } @@ -594,7 +590,7 @@ impl Chunk { pub fn name_for_filename_template<'a>( &'a self, - chunk_ids: &'a UkeyMap, + chunk_ids: &'a ChunkIdsArtifact, ) -> Option<&'a str> { if self.name.is_some() { self.name.as_deref() @@ -612,7 +608,7 @@ impl Chunk { } pub fn update_hash(&self, hasher: &mut RspackHash, compilation: &Compilation) { - self.id(&compilation.chunk_ids).hash(hasher); + self.id(&compilation.chunk_ids_artifact).hash(hasher); for module in compilation .chunk_graph .get_ordered_chunk_modules(&self.ukey, &compilation.get_module_graph()) @@ -645,7 +641,7 @@ impl Chunk { .chunk_graph .get_chunk_entry_modules_with_chunk_group_iterable(&self.ukey) { - ChunkGraph::get_module_id(&compilation.module_ids, *module).hash(hasher); + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module).hash(hasher); if let Some(chunk_group) = compilation.chunk_group_by_ukey.get(chunk_group) { chunk_group.id(compilation).hash(hasher); } @@ -737,7 +733,7 @@ impl Chunk { compilation .chunk_by_ukey .expect_get(chunk_ukey) - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .cloned() }) }) @@ -764,7 +760,7 @@ impl Chunk { ) { let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey); if let (Some(chunk_id), Some(child_chunk_ids)) = ( - chunk.id(&compilation.chunk_ids).cloned(), + chunk.id(&compilation.chunk_ids_artifact).cloned(), chunk.get_child_ids_by_order(order, compilation), ) { result 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 5b228c01a05..174de5f00ce 100644 --- a/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs +++ b/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs @@ -16,8 +16,8 @@ use serde::{Serialize, Serializer}; use crate::{ find_graph_roots, merge_runtime, BoxModule, Chunk, ChunkByUkey, ChunkGraphModule, - ChunkGroupByUkey, ChunkGroupUkey, ChunkUkey, Module, ModuleGraph, ModuleIdentifier, - RuntimeGlobals, RuntimeModule, SourceType, + ChunkGroupByUkey, ChunkGroupUkey, ChunkIdsArtifact, ChunkUkey, Module, ModuleGraph, + ModuleIdentifier, RuntimeGlobals, RuntimeModule, SourceType, }; use crate::{ChunkGraph, Compilation}; @@ -512,7 +512,7 @@ impl ChunkGraph { runtime_requirements: RuntimeGlobals, ) { compilation - .cgc_runtime_requirements_results + .cgc_runtime_requirements_artifact .insert(chunk_ukey, runtime_requirements); } @@ -529,7 +529,7 @@ impl ChunkGraph { chunk_ukey: &ChunkUkey, ) -> &'a RuntimeGlobals { compilation - .cgc_runtime_requirements_results + .cgc_runtime_requirements_artifact .get(chunk_ukey) .unwrap_or_else(|| { let c = compilation.chunk_graph.expect_chunk_graph_chunk(chunk_ukey); @@ -537,7 +537,7 @@ impl ChunkGraph { "Chunk({:?} {:?}) should have runtime requirements, {:?}", c, chunk_ukey, - &compilation.cgc_runtime_requirements_results.keys() + &compilation.cgc_runtime_requirements_artifact.keys() ) }) } @@ -604,7 +604,7 @@ impl ChunkGraph { { let chunk = compilation.chunk_by_ukey.expect_get(c); map.insert( - chunk.expect_id(&compilation.chunk_ids).to_string(), + chunk.expect_id(&compilation.chunk_ids_artifact).to_string(), filter(c, compilation), ); } @@ -970,14 +970,14 @@ impl ChunkGraph { } pub fn get_chunk_id<'a>( - chunk_ids: &'a UkeyMap, + chunk_ids: &'a ChunkIdsArtifact, chunk_ukey: &ChunkUkey, ) -> Option<&'a ChunkId> { chunk_ids.get(chunk_ukey) } pub fn set_chunk_id( - chunk_ids: &mut UkeyMap, + chunk_ids: &mut ChunkIdsArtifact, chunk_ukey: ChunkUkey, id: ChunkId, ) -> bool { diff --git a/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs b/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs index ed2c6f9b830..ff3fab49f22 100644 --- a/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs +++ b/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs @@ -14,8 +14,8 @@ use tracing::instrument; use crate::{ AsyncDependenciesBlockIdentifier, ChunkByUkey, ChunkGroup, ChunkGroupByUkey, ChunkGroupUkey, - ChunkUkey, Compilation, ModuleGraph, ModuleIdentifier, RuntimeGlobals, RuntimeSpec, - RuntimeSpecMap, RuntimeSpecSet, + ChunkUkey, Compilation, ModuleGraph, ModuleIdentifier, ModuleIdsArtifact, RuntimeGlobals, + RuntimeSpec, RuntimeSpecMap, RuntimeSpecSet, }; use crate::{ChunkGraph, Module}; @@ -169,7 +169,7 @@ impl ChunkGraph { map: RuntimeSpecMap, ) { compilation - .cgm_runtime_requirements_results + .cgm_runtime_requirements_artifact .set_runtime_requirements(module_identifier, map); } @@ -179,7 +179,7 @@ impl ChunkGraph { runtime: &RuntimeSpec, ) -> Option<&'c RuntimeGlobals> { compilation - .cgm_runtime_requirements_results + .cgm_runtime_requirements_artifact .get(&module_identifier, runtime) } @@ -198,14 +198,14 @@ impl ChunkGraph { } pub fn get_module_id( - module_ids: &IdentifierMap, + module_ids: &ModuleIdsArtifact, module_identifier: ModuleIdentifier, ) -> Option<&ModuleId> { module_ids.get(&module_identifier) } pub fn set_module_id( - module_ids: &mut IdentifierMap, + module_ids: &mut ModuleIdsArtifact, module_identifier: ModuleIdentifier, id: ModuleId, ) -> bool { @@ -241,7 +241,7 @@ impl ChunkGraph { runtime: &RuntimeSpec, ) -> Option<&'c RspackHashDigest> { compilation - .cgm_hash_results + .cgm_hash_artifact .get(&module_identifier, runtime) } @@ -251,7 +251,7 @@ impl ChunkGraph { hashes: RuntimeSpecMap, ) { compilation - .cgm_hash_results + .cgm_hash_artifact .set_hashes(module_identifier, hashes); } @@ -303,7 +303,7 @@ impl ChunkGraph { let mut hasher = FxHasher::default(); let mg = compilation.get_module_graph(); let module_identifier = module.identifier(); - Self::get_module_id(&compilation.module_ids, module_identifier).dyn_hash(&mut hasher); + Self::get_module_id(&compilation.module_ids_artifact, module_identifier).dyn_hash(&mut hasher); module.source_types().dyn_hash(&mut hasher); ModuleGraph::is_async(compilation, &module_identifier).dyn_hash(&mut hasher); mg.get_exports_info(&module_identifier) diff --git a/crates/rspack_core/src/chunk_group.rs b/crates/rspack_core/src/chunk_group.rs index f03412d277a..6a2320bd32b 100644 --- a/crates/rspack_core/src/chunk_group.rs +++ b/crates/rspack_core/src/chunk_group.rs @@ -265,7 +265,7 @@ impl ChunkGroup { compilation .chunk_by_ukey .get(chunk) - .and_then(|item| item.id(&compilation.chunk_ids)) + .and_then(|item| item.id(&compilation.chunk_ids_artifact)) }) .join("+") } diff --git a/crates/rspack_core/src/code_generation_results.rs b/crates/rspack_core/src/code_generation_results.rs deleted file mode 100644 index 68ef01bd572..00000000000 --- a/crates/rspack_core/src/code_generation_results.rs +++ /dev/null @@ -1,343 +0,0 @@ -use std::collections::hash_map::Entry; -use std::hash::Hash; -use std::ops::{Deref, DerefMut}; -use std::sync::atomic::AtomicU32; - -use anymap::CloneAny; -use rspack_collections::IdentifierMap; -use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest}; -use rspack_sources::BoxSource; -use rspack_util::atom::Atom; -use rustc_hash::{FxHashMap as HashMap, FxHashSet}; -use serde::Serialize; - -use crate::{ - AssetInfo, ChunkInitFragments, ConcatenationScope, ModuleIdentifier, PublicPath, RuntimeGlobals, - RuntimeMode, RuntimeSpec, RuntimeSpecMap, SourceType, -}; - -#[derive(Clone, Debug)] -pub struct CodeGenerationDataUrl { - inner: String, -} - -impl CodeGenerationDataUrl { - pub fn new(inner: String) -> Self { - Self { inner } - } - - pub fn inner(&self) -> &str { - &self.inner - } -} - -#[derive(Clone, Debug)] -pub struct CodeGenerationDataFilename { - filename: String, - public_path: PublicPath, -} - -impl CodeGenerationDataFilename { - pub fn new(filename: String, public_path: PublicPath) -> Self { - Self { - filename, - public_path, - } - } - - pub fn filename(&self) -> &str { - &self.filename - } - - pub fn public_path(&self) -> &PublicPath { - &self.public_path - } -} - -#[derive(Clone, Debug)] -pub struct CodeGenerationDataAssetInfo { - inner: AssetInfo, -} - -impl CodeGenerationDataAssetInfo { - pub fn new(inner: AssetInfo) -> Self { - Self { inner } - } - - pub fn inner(&self) -> &AssetInfo { - &self.inner - } -} - -#[derive(Clone, Debug)] -pub struct CodeGenerationDataTopLevelDeclarations { - inner: FxHashSet, -} - -impl CodeGenerationDataTopLevelDeclarations { - pub fn new(inner: FxHashSet) -> Self { - Self { inner } - } - - pub fn inner(&self) -> &FxHashSet { - &self.inner - } -} - -#[derive(Clone, Debug)] -pub struct CodeGenerationExportsFinalNames { - inner: HashMap, -} - -impl CodeGenerationExportsFinalNames { - pub fn new(inner: HashMap) -> Self { - Self { inner } - } - - pub fn inner(&self) -> &HashMap { - &self.inner - } -} - -#[derive(Debug, Default, Clone)] -pub struct CodeGenerationData { - inner: anymap::Map, -} - -impl Deref for CodeGenerationData { - type Target = anymap::Map; - - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -impl DerefMut for CodeGenerationData { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.inner - } -} - -#[derive(Debug, Default, Clone)] -pub struct CodeGenerationResult { - pub inner: HashMap, - /// [definition in webpack](https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/Module.js#L75) - pub data: CodeGenerationData, - pub chunk_init_fragments: ChunkInitFragments, - pub runtime_requirements: RuntimeGlobals, - pub hash: Option, - pub id: CodeGenResultId, - pub concatenation_scope: Option, -} - -impl CodeGenerationResult { - pub fn with_javascript(mut self, generation_result: BoxSource) -> Self { - self.inner.insert(SourceType::JavaScript, generation_result); - self - } - - pub fn with_css(mut self, generation_result: BoxSource) -> Self { - self.inner.insert(SourceType::Css, generation_result); - self - } - - pub fn with_asset(mut self, generation_result: BoxSource) -> Self { - self.inner.insert(SourceType::Asset, generation_result); - self - } - - pub fn inner(&self) -> &HashMap { - &self.inner - } - - pub fn get(&self, source_type: &SourceType) -> Option<&BoxSource> { - self.inner.get(source_type) - } - - pub fn add(&mut self, source_type: SourceType, generation_result: BoxSource) { - let result = self.inner.insert(source_type, generation_result); - debug_assert!(result.is_none()); - } - - pub fn set_hash( - &mut self, - hash_function: &HashFunction, - hash_digest: &HashDigest, - hash_salt: &HashSalt, - ) { - let mut hasher = RspackHash::with_salt(hash_function, hash_salt); - for (source_type, source) in &self.inner { - source_type.hash(&mut hasher); - source.hash(&mut hasher); - } - self.chunk_init_fragments.hash(&mut hasher); - self.runtime_requirements.hash(&mut hasher); - self.hash = Some(hasher.digest(hash_digest)); - } -} - -#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)] -pub struct CodeGenResultId(u32); - -impl Default for CodeGenResultId { - fn default() -> Self { - Self(CODE_GEN_RESULT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) - } -} - -pub static CODE_GEN_RESULT_ID: AtomicU32 = AtomicU32::new(0); - -#[derive(Debug, Default, Clone)] -pub struct CodeGenerationResults { - module_generation_result_map: HashMap, - map: IdentifierMap>, -} - -impl CodeGenerationResults { - pub fn is_empty(&self) -> bool { - self.module_generation_result_map.is_empty() && self.map.is_empty() - } - - pub fn get_one(&self, module_identifier: &ModuleIdentifier) -> Option<&CodeGenerationResult> { - self - .map - .get(module_identifier) - .and_then(|spec| match spec.mode { - RuntimeMode::Empty => None, - RuntimeMode::SingleEntry => spec - .single_value - .and_then(|result_id| self.module_generation_result_map.get(&result_id)), - RuntimeMode::Map => spec - .map - .values() - .next() - .and_then(|result_id| self.module_generation_result_map.get(result_id)), - }) - } - - 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() { - self.module_generation_result_map.remove(result)?; - } - Some(()) - } - - pub fn get( - &self, - module_identifier: &ModuleIdentifier, - runtime: Option<&RuntimeSpec>, - ) -> &CodeGenerationResult { - if let Some(entry) = self.map.get(module_identifier) { - if let Some(runtime) = runtime { - entry - .get(runtime) - .and_then(|m| { - self.module_generation_result_map.get(m) - }) - .unwrap_or_else(|| { - panic!( - "Failed to code generation result for {module_identifier} with runtime {runtime:?} \n {entry:?}" - ) - }) - } else { - if entry.size() > 1 { - let results = entry.get_values(); - if results.len() != 1 { - panic!( - "No unique code generation entry for unspecified runtime for {module_identifier} ", - ); - } - - return results - .first() - .copied() - .and_then(|m| self.module_generation_result_map.get(m)) - .unwrap_or_else(|| panic!("Expected value exists")); - } - - entry - .get_values() - .first() - .copied() - .and_then(|m| self.module_generation_result_map.get(m)) - .unwrap_or_else(|| panic!("Expected value exists")) - } - } else { - panic!( - "No code generation entry for {} (existing entries: {:?})", - module_identifier, - self.map.keys().collect::>() - ) - } - } - - pub fn add( - &mut self, - module_identifier: ModuleIdentifier, - runtime: RuntimeSpec, - result: CodeGenResultId, - ) { - match self.map.entry(module_identifier) { - Entry::Occupied(mut record) => { - record.get_mut().set(runtime, result); - } - Entry::Vacant(record) => { - let mut spec_map = RuntimeSpecMap::default(); - spec_map.set(runtime, result); - record.insert(spec_map); - } - }; - } - - pub fn get_runtime_requirements( - &self, - module_identifier: &ModuleIdentifier, - runtime: Option<&RuntimeSpec>, - ) -> RuntimeGlobals { - self.get(module_identifier, runtime).runtime_requirements - } - - #[allow(clippy::unwrap_in_result)] - pub fn get_hash( - &self, - module_identifier: &ModuleIdentifier, - runtime: Option<&RuntimeSpec>, - ) -> Option<&RspackHashDigest> { - let code_generation_result = self.get(module_identifier, runtime); - - code_generation_result.hash.as_ref() - } - - pub fn into_inner( - self, - ) -> ( - IdentifierMap>, - HashMap, - ) { - (self.map, self.module_generation_result_map) - } -} - -#[derive(Debug)] -pub struct CodeGenerationJob { - pub module: ModuleIdentifier, - pub hash: RspackHashDigest, - pub runtime: RuntimeSpec, - pub runtimes: Vec, -} diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 9f03792fe9d..553ee1b59fe 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -38,22 +38,21 @@ use super::{ use crate::{ build_chunk_graph::build_chunk_graph, cache::Cache, - cgm_hash_results::CgmHashResults, - cgm_runtime_requirement_results::CgmRuntimeRequirementsResults, chunk_graph_chunk::ChunkId, - chunk_graph_module::ModuleId, get_runtime_key, incremental::{Incremental, IncrementalPasses, Mutation}, is_source_equal, old_cache::{use_code_splitting_cache, Cache as OldCache, CodeSplittingCache}, - to_identifier, BoxDependency, BoxModule, CacheCount, CacheOptions, Chunk, ChunkByUkey, - ChunkContentHash, ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkHashesResult, ChunkKind, - ChunkRenderResult, ChunkUkey, CodeGenerationJob, CodeGenerationResult, CodeGenerationResults, - CompilationLogger, CompilationLogging, CompilerOptions, DependencyId, DependencyType, Entry, - EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId, Filename, ImportVarMap, - LocalFilenameFn, Logger, ModuleFactory, ModuleGraph, ModuleGraphPartial, ModuleIdentifier, - PathData, ResolverFactory, RuntimeGlobals, RuntimeModule, RuntimeSpecMap, SharedPluginDriver, - SourceType, Stats, + to_identifier, AsyncModulesArtifact, BoxDependency, BoxModule, CacheCount, CacheOptions, + CgcRuntimeRequirementsArtifact, CgmHashArtifact, CgmRuntimeRequirementsArtifact, Chunk, + ChunkByUkey, ChunkContentHash, ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkHashesArtifact, + ChunkHashesResult, ChunkIdsArtifact, ChunkKind, ChunkRenderArtifact, ChunkRenderResult, + ChunkUkey, CodeGenerationJob, CodeGenerationResult, CodeGenerationResults, CompilationLogger, + CompilationLogging, CompilerOptions, DependenciesDiagnosticsArtifact, DependencyId, + DependencyType, Entry, EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId, + Filename, ImportVarMap, LocalFilenameFn, Logger, ModuleFactory, ModuleGraph, ModuleGraphPartial, + ModuleIdentifier, ModuleIdsArtifact, PathData, ResolverFactory, RuntimeGlobals, RuntimeModule, + RuntimeSpecMap, SharedPluginDriver, SideEffectsOptimizeArtifact, SourceType, Stats, }; pub type BuildDependency = ( @@ -184,25 +183,28 @@ pub struct Compilation { pub named_chunk_groups: HashMap, // artifact for infer_async_modules_plugin - pub async_modules: IdentifierSet, + pub async_modules_artifact: AsyncModulesArtifact, // artifact for collect_dependencies_diagnostics - pub dependencies_diagnostics: IdentifierMap>, + pub dependencies_diagnostics_artifact: DependenciesDiagnosticsArtifact, + // artifact for side_effects_flag_plugin + pub side_effects_optimize_artifact: SideEffectsOptimizeArtifact, // artifact for module_ids - pub module_ids: IdentifierMap, + pub module_ids_artifact: ModuleIdsArtifact, // artifact for chunk_ids - pub chunk_ids: UkeyMap, + pub chunk_ids_artifact: ChunkIdsArtifact, // artifact for code_generation pub code_generation_results: CodeGenerationResults, // artifact for create_module_hashes - pub cgm_hash_results: CgmHashResults, + pub cgm_hash_artifact: CgmHashArtifact, // artifact for process_modules_runtime_requirements - pub cgm_runtime_requirements_results: CgmRuntimeRequirementsResults, + pub cgm_runtime_requirements_artifact: CgmRuntimeRequirementsArtifact, // artifact for process_chunks_runtime_requirements - pub cgc_runtime_requirements_results: UkeyMap, + pub cgc_runtime_requirements_artifact: CgcRuntimeRequirementsArtifact, // artifact for create_hash - pub chunk_hashes_results: UkeyMap, + pub chunk_hashes_artifact: ChunkHashesArtifact, // artifact for create_chunk_assets - pub chunk_render_results: UkeyMap, + pub chunk_render_artifact: ChunkRenderArtifact, + pub code_generated_modules: IdentifierSet, pub build_time_executed_modules: IdentifierSet, pub cache: Arc, @@ -301,16 +303,17 @@ impl Compilation { named_chunks: Default::default(), named_chunk_groups: Default::default(), - async_modules: Default::default(), - dependencies_diagnostics: Default::default(), - module_ids: Default::default(), - chunk_ids: Default::default(), + async_modules_artifact: Default::default(), + dependencies_diagnostics_artifact: Default::default(), + side_effects_optimize_artifact: Default::default(), + module_ids_artifact: Default::default(), + chunk_ids_artifact: Default::default(), code_generation_results: Default::default(), - cgm_hash_results: Default::default(), - cgm_runtime_requirements_results: Default::default(), - cgc_runtime_requirements_results: Default::default(), - chunk_hashes_results: Default::default(), - chunk_render_results: Default::default(), + cgm_hash_artifact: Default::default(), + cgm_runtime_requirements_artifact: Default::default(), + cgc_runtime_requirements_artifact: Default::default(), + chunk_hashes_artifact: Default::default(), + chunk_render_artifact: Default::default(), code_generated_modules: Default::default(), build_time_executed_modules: Default::default(), cache, @@ -1022,17 +1025,17 @@ impl Compilation { .incremental .mutations_read(IncrementalPasses::CHUNKS_RENDER); let chunks = if let Some(mutations) = mutations - && !self.chunk_render_results.is_empty() + && !self.chunk_render_artifact.is_empty() { let removed_chunks = mutations.iter().filter_map(|mutation| match mutation { Mutation::ChunkRemove { chunk } => Some(*chunk), _ => None, }); for removed_chunk in removed_chunks { - self.chunk_render_results.remove(&removed_chunk); + self.chunk_render_artifact.remove(&removed_chunk); } self - .chunk_render_results + .chunk_render_artifact .retain(|chunk, _| self.chunk_by_ukey.contains(chunk)); let mut chunks = mutations.get_affected_chunks_with_chunk_graph(self); chunks.extend(self.get_chunk_graph_entries()); @@ -1072,8 +1075,8 @@ impl Compilation { .collect::>>()?; let chunk_ukey_and_manifest = if mutations.is_some() { - self.chunk_render_results.extend(chunk_render_results); - self.chunk_render_results.clone() + self.chunk_render_artifact.extend(chunk_render_results); + self.chunk_render_artifact.clone() } else { chunk_render_results }; @@ -1226,14 +1229,16 @@ impl Compilation { .mutations_read(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS); // TODO move diagnostic collect to make let modules = if let Some(mutations) = mutations - && !self.dependencies_diagnostics.is_empty() + && !self.dependencies_diagnostics_artifact.is_empty() { let revoked_modules = mutations.iter().filter_map(|mutation| match mutation { Mutation::ModuleRemove { module } => Some(*module), _ => None, }); for revoked_module in revoked_modules { - self.dependencies_diagnostics.remove(&revoked_module); + self + .dependencies_diagnostics_artifact + .remove(&revoked_module); } let modules = mutations.get_affected_modules_with_module_graph(&self.get_module_graph()); let logger = self.get_logger("rspack.incremental.dependenciesDiagnostics"); @@ -1247,7 +1252,7 @@ impl Compilation { self.get_module_graph().modules().keys().copied().collect() }; let module_graph = self.get_module_graph(); - let dependencies_diagnostics: IdentifierMap> = modules + let dependencies_diagnostics: DependenciesDiagnosticsArtifact = modules .par_iter() .map(|module_identifier| { let mgm = module_graph @@ -1265,9 +1270,9 @@ impl Compilation { .collect(); let all_modules_diagnostics = if mutations.is_some() { self - .dependencies_diagnostics + .dependencies_diagnostics_artifact .extend(dependencies_diagnostics); - self.dependencies_diagnostics.clone() + self.dependencies_diagnostics_artifact.clone() } else { dependencies_diagnostics }; @@ -1354,14 +1359,14 @@ impl Compilation { let create_module_hashes_modules = if let Some(mutations) = self .incremental .mutations_read(IncrementalPasses::MODULES_HASHES) - && !self.cgm_hash_results.is_empty() + && !self.cgm_hash_artifact.is_empty() { let revoked_modules = mutations.iter().filter_map(|mutation| match mutation { Mutation::ModuleRemove { module } => Some(*module), _ => None, }); for revoked_module in revoked_modules { - self.cgm_hash_results.remove(&revoked_module); + self.cgm_hash_artifact.remove(&revoked_module); } let modules = mutations.get_affected_modules_with_chunk_graph(self); let logger = self.get_logger("rspack.incremental.modulesHashes"); @@ -1414,7 +1419,7 @@ impl Compilation { let process_runtime_requirements_modules = if let Some(mutations) = self .incremental .mutations_read(IncrementalPasses::MODULES_RUNTIME_REQUIREMENTS) - && !self.cgm_runtime_requirements_results.is_empty() + && !self.cgm_runtime_requirements_artifact.is_empty() { let revoked_modules = mutations.iter().filter_map(|mutation| match mutation { Mutation::ModuleRemove { module } => Some(*module), @@ -1422,7 +1427,7 @@ impl Compilation { }); for revoked_module in revoked_modules { self - .cgm_runtime_requirements_results + .cgm_runtime_requirements_artifact .remove(&revoked_module); } let modules = mutations.get_affected_modules_with_chunk_graph(self); @@ -1446,24 +1451,26 @@ impl Compilation { let process_runtime_requirements_chunks = if let Some(mutations) = self .incremental .mutations_read(IncrementalPasses::CHUNKS_RUNTIME_REQUIREMENTS) - && !self.cgc_runtime_requirements_results.is_empty() + && !self.cgc_runtime_requirements_artifact.is_empty() { let removed_chunks = mutations.iter().filter_map(|mutation| match mutation { Mutation::ChunkRemove { chunk } => Some(chunk), _ => None, }); for removed_chunk in removed_chunks { - self.cgc_runtime_requirements_results.remove(removed_chunk); + self.cgc_runtime_requirements_artifact.remove(removed_chunk); } let affected_chunks = mutations.get_affected_chunks_with_chunk_graph(self); for affected_chunk in &affected_chunks { - self.cgc_runtime_requirements_results.remove(affected_chunk); + self + .cgc_runtime_requirements_artifact + .remove(affected_chunk); } for runtime_chunk in &runtime_chunks { - self.cgc_runtime_requirements_results.remove(runtime_chunk); + self.cgc_runtime_requirements_artifact.remove(runtime_chunk); } self - .cgc_runtime_requirements_results + .cgc_runtime_requirements_artifact .retain(|chunk, _| self.chunk_by_ukey.contains(chunk)); let logger = self.get_logger("rspack.incremental.chunksRuntimeRequirements"); logger.log(format!( @@ -1488,17 +1495,17 @@ impl Compilation { let create_hash_chunks = if let Some(mutations) = self .incremental .mutations_read(IncrementalPasses::CHUNKS_HASHES) - && !self.chunk_hashes_results.is_empty() + && !self.chunk_hashes_artifact.is_empty() { let removed_chunks = mutations.iter().filter_map(|mutation| match mutation { Mutation::ChunkRemove { chunk } => Some(*chunk), _ => None, }); for removed_chunk in removed_chunks { - self.chunk_hashes_results.remove(&removed_chunk); + self.chunk_hashes_artifact.remove(&removed_chunk); } self - .chunk_hashes_results + .chunk_hashes_artifact .retain(|chunk, _| self.chunk_by_ukey.contains(chunk)); let chunks = mutations.get_affected_chunks_with_chunk_graph(self); let logger = self.get_logger("rspack.incremental.chunksHashes"); @@ -1549,7 +1556,7 @@ impl Compilation { entrypoint_ukey: &ChunkGroupUkey, chunk_group_by_ukey: &ChunkGroupByUkey, chunk_by_ukey: &ChunkByUkey, - chunk_ids: &UkeyMap, + chunk_ids: &ChunkIdsArtifact, chunk_graph: &mut ChunkGraph, ) { let entrypoint = chunk_group_by_ukey.expect_get(entrypoint_ukey); @@ -1573,7 +1580,7 @@ impl Compilation { i.1, &self.chunk_group_by_ukey, &self.chunk_by_ukey, - &self.chunk_ids, + &self.chunk_ids_artifact, &mut self.chunk_graph, ) } @@ -1582,7 +1589,7 @@ impl Compilation { i, &self.chunk_group_by_ukey, &self.chunk_by_ukey, - &self.chunk_ids, + &self.chunk_ids_artifact, &mut self.chunk_graph, ) } @@ -1789,7 +1796,7 @@ impl Compilation { let (chunk_ukey, (chunk_hash, content_hash)) = hash_result?; let chunk = compilation.chunk_by_ukey.expect_get(&chunk_ukey); chunk.set_hashes( - &mut compilation.chunk_hashes_results, + &mut compilation.chunk_hashes_artifact, chunk_hash, content_hash, ); @@ -1912,14 +1919,17 @@ impl Compilation { .filter(|(_, (_, remaining))| *remaining != 0) .map(|(chunk_ukey, _)| self.chunk_by_ukey.expect_get(chunk_ukey)) .collect(); - circular.sort_unstable_by(|a, b| a.id(&self.chunk_ids).cmp(&b.id(&self.chunk_ids))); + circular.sort_unstable_by(|a, b| { + a.id(&self.chunk_ids_artifact) + .cmp(&b.id(&self.chunk_ids_artifact)) + }); runtime_chunks.extend(circular.iter().map(|chunk| chunk.ukey())); let circular_names = circular .iter() .map(|chunk| { chunk .name() - .or(chunk.id(&self.chunk_ids).map(|id| id.as_str())) + .or(chunk.id(&self.chunk_ids_artifact).map(|id| id.as_str())) .unwrap_or("no id chunk") }) .join(", "); @@ -1948,7 +1958,7 @@ impl Compilation { .process_chunk_hash(runtime_chunk_ukey, &plugin_driver) .await?; let chunk = self.chunk_by_ukey.expect_get(&runtime_chunk_ukey); - chunk.set_hashes(&mut self.chunk_hashes_results, chunk_hash, content_hash); + chunk.set_hashes(&mut self.chunk_hashes_artifact, chunk_hash, content_hash); } logger.time_end(start); @@ -1957,7 +1967,7 @@ impl Compilation { .chunk_by_ukey .values() .sorted_unstable_by_key(|chunk| chunk.ukey()) - .filter_map(|chunk| chunk.hash(&self.chunk_hashes_results)) + .filter_map(|chunk| chunk.hash(&self.chunk_hashes_artifact)) .for_each(|hash| { hash.hash(&mut compilation_hasher); }); @@ -1984,7 +1994,7 @@ impl Compilation { let chunk = self.chunk_by_ukey.expect_get(&runtime_chunk_ukey); let new_chunk_hash = { let chunk_hash = chunk - .hash(&self.chunk_hashes_results) + .hash(&self.chunk_hashes_artifact) .expect("should have chunk hash"); let mut hasher = RspackHash::from(&self.options.output); chunk_hash.hash(&mut hasher); @@ -1993,7 +2003,7 @@ impl Compilation { }; let new_content_hash = { let content_hash = chunk - .content_hash(&self.chunk_hashes_results) + .content_hash(&self.chunk_hashes_artifact) .expect("should have content hash"); content_hash .iter() @@ -2009,7 +2019,7 @@ impl Compilation { .collect() }; chunk.set_hashes( - &mut self.chunk_hashes_results, + &mut self.chunk_hashes_artifact, new_chunk_hash, new_content_hash, ); diff --git a/crates/rspack_core/src/compiler/hmr.rs b/crates/rspack_core/src/compiler/hmr.rs index 503989eaf02..80d47decfc5 100644 --- a/crates/rspack_core/src/compiler/hmr.rs +++ b/crates/rspack_core/src/compiler/hmr.rs @@ -37,7 +37,7 @@ impl Compiler { for (_, chunk) in old.compilation.chunk_by_ukey.iter() { if chunk.kind() != ChunkKind::HotUpdate { old_chunks.push(( - chunk.expect_id(&old.compilation.chunk_ids).clone(), + chunk.expect_id(&old.compilation.chunk_ids_artifact).clone(), chunk.runtime().clone(), )); } @@ -87,7 +87,6 @@ impl Compiler { new_compilation.hot_index = self.compilation.hot_index + 1; - // TODO: remove this if let Some(mutations) = new_compilation.incremental.mutations_write() && let Some(old_mutations) = self.compilation.incremental.mutations_write() { @@ -114,32 +113,42 @@ impl Compiler { .incremental .can_read_mutations(IncrementalPasses::INFER_ASYNC_MODULES) { - new_compilation.async_modules = std::mem::take(&mut self.compilation.async_modules); + new_compilation.async_modules_artifact = + std::mem::take(&mut self.compilation.async_modules_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS) { - new_compilation.dependencies_diagnostics = - std::mem::take(&mut self.compilation.dependencies_diagnostics); + new_compilation.dependencies_diagnostics_artifact = + std::mem::take(&mut self.compilation.dependencies_diagnostics_artifact); + } + if new_compilation + .incremental + .can_read_mutations(IncrementalPasses::SIDE_EFFECTS) + { + new_compilation.side_effects_optimize_artifact = + std::mem::take(&mut self.compilation.side_effects_optimize_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::MODULE_IDS) { - new_compilation.module_ids = std::mem::take(&mut self.compilation.module_ids); + new_compilation.module_ids_artifact = + std::mem::take(&mut self.compilation.module_ids_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::CHUNK_IDS) { - new_compilation.chunk_ids = std::mem::take(&mut self.compilation.chunk_ids); + new_compilation.chunk_ids_artifact = + std::mem::take(&mut self.compilation.chunk_ids_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::MODULES_HASHES) { - new_compilation.cgm_hash_results = std::mem::take(&mut self.compilation.cgm_hash_results); + new_compilation.cgm_hash_artifact = std::mem::take(&mut self.compilation.cgm_hash_artifact); } if new_compilation .incremental @@ -152,29 +161,29 @@ impl Compiler { .incremental .can_read_mutations(IncrementalPasses::MODULES_RUNTIME_REQUIREMENTS) { - new_compilation.cgm_runtime_requirements_results = - std::mem::take(&mut self.compilation.cgm_runtime_requirements_results); + new_compilation.cgm_runtime_requirements_artifact = + std::mem::take(&mut self.compilation.cgm_runtime_requirements_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::CHUNKS_RUNTIME_REQUIREMENTS) { - new_compilation.cgc_runtime_requirements_results = - std::mem::take(&mut self.compilation.cgc_runtime_requirements_results); + new_compilation.cgc_runtime_requirements_artifact = + std::mem::take(&mut self.compilation.cgc_runtime_requirements_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::CHUNKS_HASHES) { - new_compilation.chunk_hashes_results = - std::mem::take(&mut self.compilation.chunk_hashes_results); + new_compilation.chunk_hashes_artifact = + std::mem::take(&mut self.compilation.chunk_hashes_artifact); } if new_compilation .incremental .can_read_mutations(IncrementalPasses::CHUNKS_RENDER) { - new_compilation.chunk_render_results = - std::mem::take(&mut self.compilation.chunk_render_results); + new_compilation.chunk_render_artifact = + std::mem::take(&mut self.compilation.chunk_render_artifact); } // FOR BINDING SAFETY: @@ -218,7 +227,7 @@ pub fn collect_changed_modules(compilation: &Compilation) -> Result for ExecuteTask { let mut chunk = Chunk::new(Some("build time chunk".into()), ChunkKind::Normal); if let Some(name) = chunk.name() { - chunk.set_id(&mut compilation.chunk_ids, name); + chunk.set_id(&mut compilation.chunk_ids_artifact, name); } let runtime: RuntimeSpec = once("build time".into()).collect(); @@ -141,7 +141,7 @@ impl Task for ExecuteTask { // Assign ids to modules and modules to the chunk for &m in &modules { chunk_graph.add_module(m); - ChunkGraph::set_module_id(&mut compilation.module_ids, m, m.as_str().into()); + ChunkGraph::set_module_id(&mut compilation.module_ids_artifact, m, m.as_str().into()); chunk_graph.connect_chunk_and_module(chunk_ukey, m); } diff --git a/crates/rspack_core/src/concatenated_module.rs b/crates/rspack_core/src/concatenated_module.rs index 769e5456920..230264b0c40 100644 --- a/crates/rspack_core/src/concatenated_module.rs +++ b/crates/rspack_core/src/concatenated_module.rs @@ -1238,7 +1238,7 @@ impl Module for ConcatenatedModule { info.name.as_ref().expect("should have name"), RuntimeGlobals::REQUIRE, serde_json::to_string( - ChunkGraph::get_module_id(&compilation.module_ids, info.module) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, info.module) .expect("should have module id") ) .expect("should json stringify module id") @@ -1350,7 +1350,7 @@ impl Module for ConcatenatedModule { .update_hash(hasher, compilation, generation_runtime)?, ConcatenationEntry::External(e) => { ChunkGraph::get_module_id( - &compilation.module_ids, + &compilation.module_ids_artifact, e.module(&compilation.get_module_graph()), ) .dyn_hash(hasher); diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index 9ea0647cf1f..2e87ab2c864 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -28,7 +28,7 @@ use crate::{ CodeGenerationResult, Compilation, ConcatenationScope, ContextElementDependency, DependenciesBlock, Dependency, DependencyCategory, DependencyId, DependencyLocation, DynamicImportMode, ExportsType, FactoryMeta, FakeNamespaceObjectMode, GroupOptions, - ImportAttributes, LibIdentOptions, Module, ModuleId, ModuleLayer, ModuleType, + ImportAttributes, LibIdentOptions, Module, ModuleId, ModuleIdsArtifact, ModuleLayer, ModuleType, RealDependencyLocation, Resolve, RuntimeGlobals, RuntimeSpec, SourceType, }; @@ -193,7 +193,7 @@ impl ContextModule { } } - pub fn get_module_id<'a>(&self, module_ids: &'a IdentifierMap) -> &'a ModuleId { + pub fn get_module_id<'a>(&self, module_ids: &'a ModuleIdsArtifact) -> &'a ModuleId { ChunkGraph::get_module_id(module_ids, self.identifier).expect("module id not found") } @@ -216,7 +216,8 @@ impl ContextModule { .map(|m| (m, dep_id)) }) .filter_map(|(m, dep)| { - ChunkGraph::get_module_id(&compilation.module_ids, *m).map(|id| (id.to_string(), dep)) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *m) + .map(|id| (id.to_string(), dep)) }) .sorted_unstable_by_key(|(module_id, _)| module_id.to_string()); for (module_id, dep) in sorted_modules { @@ -311,7 +312,7 @@ impl ContextModule { }); let module_id = module_graph .module_identifier_by_dependency_id(dep_id) - .and_then(|module| ChunkGraph::get_module_id(&compilation.module_ids, *module)) + .and_then(|module| ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module)) .map(|s| s.to_string()); // module_id could be None in weak mode dep.map(|dep| (dep, module_id)) @@ -337,7 +338,7 @@ impl ContextModule { module.exports = webpackEmptyAsyncContext; "#, keys = returning_function(&compilation.options.output.environment, "[]", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }) .boxed() } @@ -355,7 +356,7 @@ impl ContextModule { module.exports = webpackEmptyContext; "#, keys = returning_function(&compilation.options.output.environment, "[]", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }) .boxed() } @@ -452,7 +453,7 @@ impl ContextModule { })?; let module_id = module_graph .module_identifier_by_dependency_id(d) - .and_then(|m| ChunkGraph::get_module_id(&compilation.module_ids, *m))?; + .and_then(|m| ChunkGraph::get_module_id(&compilation.module_ids_artifact, *m))?; Some((chunks, user_request, module_id.to_string())) }) .collect::>(); @@ -478,7 +479,7 @@ impl ContextModule { let chunk_id = compilation .chunk_by_ukey .expect_get(c) - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .expect("should have chunk id in code generation"); serde_json::json!(chunk_id) })) @@ -560,7 +561,7 @@ impl ContextModule { "#, map = json_stringify(&map), keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) })); source.boxed() } @@ -622,7 +623,7 @@ impl ContextModule { fake_map_init_statement = self.get_fake_map_init_statement(&fake_map), has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY, keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }; RawStringSource::from(source).boxed() } @@ -668,7 +669,7 @@ impl ContextModule { module_factories = RuntimeGlobals::MODULE_FACTORIES, has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY, keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }; RawStringSource::from(source).boxed() } @@ -709,7 +710,7 @@ impl ContextModule { module_factories = RuntimeGlobals::MODULE_FACTORIES, has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY, keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }; RawStringSource::from(source).boxed() } @@ -760,7 +761,7 @@ impl ContextModule { fake_map_init_statement = self.get_fake_map_init_statement(&fake_map), has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY, keys = returning_function(&compilation.options.output.environment, "Object.keys(map)", ""), - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }; RawStringSource::from(source).boxed() } @@ -797,7 +798,7 @@ impl ContextModule { map = json_stringify(&map), fake_map_init_statement = self.get_fake_map_init_statement(&fake_map), has_own_property = RuntimeGlobals::HAS_OWN_PROPERTY, - id = json_stringify(self.get_module_id(&compilation.module_ids)) + id = json_stringify(self.get_module_id(&compilation.module_ids_artifact)) }; RawStringSource::from(source).boxed() } diff --git a/crates/rspack_core/src/dependency/module_dependency.rs b/crates/rspack_core/src/dependency/module_dependency.rs index e5c60fe8259..8cc725cc7d1 100644 --- a/crates/rspack_core/src/dependency/module_dependency.rs +++ b/crates/rspack_core/src/dependency/module_dependency.rs @@ -36,10 +36,6 @@ pub trait ModuleDependency: Dependency { fn get_condition(&self) -> Option { None } - - fn is_export_all(&self) -> Option { - None - } } clone_trait_object!(ModuleDependency); diff --git a/crates/rspack_core/src/dependency/runtime_template.rs b/crates/rspack_core/src/dependency/runtime_template.rs index ae0b47259db..d0d4e227872 100644 --- a/crates/rspack_core/src/dependency/runtime_template.rs +++ b/crates/rspack_core/src/dependency/runtime_template.rs @@ -355,7 +355,8 @@ pub fn module_id( if let Some(module_identifier) = compilation .get_module_graph() .module_identifier_by_dependency_id(id) - && let Some(module_id) = ChunkGraph::get_module_id(&compilation.module_ids, *module_identifier) + && let Some(module_id) = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module_identifier) { module_id_expr(&compilation.options, request, module_id) } else if weak { @@ -584,14 +585,15 @@ pub fn block_promise( .iter() .map(|c| compilation.chunk_by_ukey.expect_get(c)) .filter(|c| { - !c.has_runtime(&compilation.chunk_group_by_ukey) && c.id(&compilation.chunk_ids).is_some() + !c.has_runtime(&compilation.chunk_group_by_ukey) + && c.id(&compilation.chunk_ids_artifact).is_some() }) .collect::>(); if chunks.len() == 1 { let chunk_id = serde_json::to_string( chunks[0] - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .expect("should have chunk.id"), ) .expect("should able to json stringify"); @@ -632,8 +634,11 @@ pub fn block_promise( .map(|c| format!( "{}({}{})", RuntimeGlobals::ENSURE_CHUNK, - serde_json::to_string(c.id(&compilation.chunk_ids).expect("should have chunk.id")) - .expect("should able to json stringify"), + serde_json::to_string( + c.id(&compilation.chunk_ids_artifact) + .expect("should have chunk.id") + ) + .expect("should able to json stringify"), fetch_priority .map(|x| format!(r#", "{x}""#)) .unwrap_or_default() @@ -656,7 +661,8 @@ pub fn module_raw( if let Some(module_identifier) = compilation .get_module_graph() .module_identifier_by_dependency_id(id) - && let Some(module_id) = ChunkGraph::get_module_id(&compilation.module_ids, *module_identifier) + && let Some(module_id) = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module_identifier) { runtime_requirements.insert(RuntimeGlobals::REQUIRE); format!( diff --git a/crates/rspack_core/src/external_module.rs b/crates/rspack_core/src/external_module.rs index e3a9e02add5..27373e278a9 100644 --- a/crates/rspack_core/src/external_module.rs +++ b/crates/rspack_core/src/external_module.rs @@ -313,7 +313,7 @@ impl ExternalModule { } } "amd" | "amd-require" | "umd" | "umd2" | "system" | "jsonp" => { - let id = ChunkGraph::get_module_id(&compilation.module_ids, self.identifier()) + let id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, self.identifier()) .map(|s| s.as_str()) .expect("should have module id"); format!( diff --git a/crates/rspack_core/src/incremental/mod.rs b/crates/rspack_core/src/incremental/mod.rs index f8d5959edaa..8d17f799b01 100644 --- a/crates/rspack_core/src/incremental/mod.rs +++ b/crates/rspack_core/src/incremental/mod.rs @@ -10,16 +10,17 @@ bitflags! { const INFER_ASYNC_MODULES = 1 << 1; const PROVIDED_EXPORTS = 1 << 2; const DEPENDENCIES_DIAGNOSTICS = 1 << 3; - const BUILD_CHUNK_GRAPH = 1 << 4; - const MODULE_IDS = 1 << 5; - const CHUNK_IDS = 1 << 6; - const MODULES_HASHES = 1 << 7; - const MODULES_CODEGEN = 1 << 8; - const MODULES_RUNTIME_REQUIREMENTS = 1 << 9; - const CHUNKS_RUNTIME_REQUIREMENTS = 1 << 10; - const CHUNKS_HASHES = 1 << 11; - const CHUNKS_RENDER = 1 << 12; - const EMIT_ASSETS = 1 << 13; + const SIDE_EFFECTS = 1 << 4; + const BUILD_CHUNK_GRAPH = 1 << 5; + const MODULE_IDS = 1 << 6; + const CHUNK_IDS = 1 << 7; + const MODULES_HASHES = 1 << 8; + const MODULES_CODEGEN = 1 << 9; + const MODULES_RUNTIME_REQUIREMENTS = 1 << 10; + const CHUNKS_RUNTIME_REQUIREMENTS = 1 << 11; + const CHUNKS_HASHES = 1 << 12; + const CHUNKS_RENDER = 1 << 13; + const EMIT_ASSETS = 1 << 14; } } diff --git a/crates/rspack_core/src/incremental/mutations.rs b/crates/rspack_core/src/incremental/mutations.rs index bf2c53de672..be50afd1496 100644 --- a/crates/rspack_core/src/incremental/mutations.rs +++ b/crates/rspack_core/src/incremental/mutations.rs @@ -358,7 +358,7 @@ fn compute_affected_modules_with_chunk_graph( }; for chunk in &chunk_group.chunks { let chunk = compilation.chunk_by_ukey.expect_get(chunk); - chunk.id(&compilation.chunk_ids).hash(&mut hasher); + chunk.id(&compilation.chunk_ids_artifact).hash(&mut hasher); } } hasher.finish() diff --git a/crates/rspack_core/src/lib.rs b/crates/rspack_core/src/lib.rs index 26ba6f22c71..55d6220a205 100644 --- a/crates/rspack_core/src/lib.rs +++ b/crates/rspack_core/src/lib.rs @@ -6,8 +6,8 @@ #![feature(hash_raw_entry)] use std::{fmt, sync::Arc}; -mod cgm_hash_results; -mod cgm_runtime_requirement_results; +mod artifacts; +pub use artifacts::*; mod dependencies_block; pub mod diagnostics; pub mod incremental; @@ -80,8 +80,6 @@ mod runtime; mod runtime_module; pub use runtime::*; pub use runtime_module::*; -mod code_generation_results; -pub use code_generation_results::*; mod entrypoint; pub use entrypoint::*; mod loader; diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index 19236df33c8..f3d2c5f9712 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -865,7 +865,7 @@ impl<'a> ModuleGraph<'a> { } pub fn is_async(compilation: &Compilation, module_id: &ModuleIdentifier) -> bool { - compilation.async_modules.contains(module_id) + compilation.async_modules_artifact.contains(module_id) } pub fn set_async( @@ -878,9 +878,9 @@ impl<'a> ModuleGraph<'a> { return false; } if original { - compilation.async_modules.remove(&module_id) + compilation.async_modules_artifact.remove(&module_id) } else { - compilation.async_modules.insert(module_id) + compilation.async_modules_artifact.insert(module_id) } } diff --git a/crates/rspack_core/src/old_cache/occasion/chunk_render.rs b/crates/rspack_core/src/old_cache/occasion/chunk_render.rs index bda2f35f7a4..978f256cc51 100644 --- a/crates/rspack_core/src/old_cache/occasion/chunk_render.rs +++ b/crates/rspack_core/src/old_cache/occasion/chunk_render.rs @@ -35,7 +35,7 @@ impl ChunkRenderOccasion { }; let Some(content_hash) = - chunk.content_hash_by_source_type(&compilation.chunk_hashes_results, source_type) + chunk.content_hash_by_source_type(&compilation.chunk_hashes_artifact, source_type) else { return generator().await; }; diff --git a/crates/rspack_core/src/stats/mod.rs b/crates/rspack_core/src/stats/mod.rs index bef027a6875..c1446a3cfda 100644 --- a/crates/rspack_core/src/stats/mod.rs +++ b/crates/rspack_core/src/stats/mod.rs @@ -121,7 +121,7 @@ impl Stats<'_> { .par_iter() .map(|chunk| { chunk - .id(&self.compilation.chunk_ids) + .id(&self.compilation.chunk_ids_artifact) .map(|id| id.to_string()) }) .collect(); @@ -143,7 +143,7 @@ impl Stats<'_> { .par_iter() .map(|chunk| { chunk - .id(&self.compilation.chunk_ids) + .id(&self.compilation.chunk_ids_artifact) .map(|id| id.to_string()) }) .collect(); @@ -345,7 +345,7 @@ impl Stats<'_> { let module_id = origin .module .map(|identifier| { - ChunkGraph::get_module_id(&self.compilation.module_ids, identifier) + ChunkGraph::get_module_id(&self.compilation.module_ids_artifact, identifier) .map(|s| s.to_string()) .unwrap_or_default() }) @@ -374,7 +374,9 @@ impl Stats<'_> { r#type: "chunk", files, auxiliary_files, - id: c.id(&self.compilation.chunk_ids).map(|id| id.to_string()), + id: c + .id(&self.compilation.chunk_ids_artifact) + .map(|id| id.to_string()), id_hints, names: c.name().map(|n| vec![n.to_owned()]).unwrap_or_default(), entry: c.has_entry_module(chunk_graph), @@ -392,7 +394,7 @@ impl Stats<'_> { origins, hash: c .rendered_hash( - &self.compilation.chunk_hashes_results, + &self.compilation.chunk_hashes_artifact, self.compilation.options.output.hash_digest_length, ) .map(ToOwned::to_owned), @@ -429,7 +431,7 @@ impl Stats<'_> { .compilation .chunk_by_ukey .expect_get(c) - .id(&self.compilation.chunk_ids) + .id(&self.compilation.chunk_ids_artifact) .map(|id| id.to_string()) }) .collect(); @@ -594,7 +596,10 @@ impl Stats<'_> { chunk_name: chunk.and_then(|c| c.name().map(ToOwned::to_owned)), chunk_entry: chunk.map(|c| c.has_runtime(&self.compilation.chunk_group_by_ukey)), chunk_initial: chunk.map(|c| c.can_be_initial(&self.compilation.chunk_group_by_ukey)), - chunk_id: chunk.and_then(|c| c.id(&self.compilation.chunk_ids).map(|id| id.to_string())), + chunk_id: chunk.and_then(|c| { + c.id(&self.compilation.chunk_ids_artifact) + .map(|id| id.to_string()) + }), details: d.details(), stack: d.stack(), module_trace, @@ -645,7 +650,10 @@ impl Stats<'_> { chunk_name: chunk.and_then(|c| c.name().map(ToOwned::to_owned)), chunk_entry: chunk.map(|c| c.has_runtime(&self.compilation.chunk_group_by_ukey)), chunk_initial: chunk.map(|c| c.can_be_initial(&self.compilation.chunk_group_by_ukey)), - chunk_id: chunk.and_then(|c| c.id(&self.compilation.chunk_ids).map(|id| id.to_string())), + chunk_id: chunk.and_then(|c| { + c.id(&self.compilation.chunk_ids_artifact) + .map(|id| id.to_string()) + }), details: d.details(), stack: d.stack(), module_trace, @@ -851,7 +859,8 @@ impl Stats<'_> { stats.id = if executed { None } else { - ChunkGraph::get_module_id(&self.compilation.module_ids, identifier).map(|s| s.as_str()) + ChunkGraph::get_module_id(&self.compilation.module_ids_artifact, identifier) + .map(|s| s.as_str()) }; stats.issuer_id = issuer_id.and_then(|i| i); @@ -869,7 +878,7 @@ impl Stats<'_> { .compilation .chunk_by_ukey .expect_get(k) - .id(&self.compilation.chunk_ids) + .id(&self.compilation.chunk_ids_artifact) .map(|id| id.to_string()) }) .collect() @@ -1142,7 +1151,7 @@ impl Stats<'_> { .compilation .chunk_by_ukey .expect_get(k) - .id(&self.compilation.chunk_ids) + .id(&self.compilation.chunk_ids_artifact) .map(|id| id.to_string()) }) .collect(); diff --git a/crates/rspack_core/src/stats/utils.rs b/crates/rspack_core/src/stats/utils.rs index 29e6d4a252c..68c06a0c176 100644 --- a/crates/rspack_core/src/stats/utils.rs +++ b/crates/rspack_core/src/stats/utils.rs @@ -43,7 +43,8 @@ pub fn get_stats_module_name_and_id<'s, 'c>( ) -> (Cow<'s, str>, Option<&'c str>) { let identifier = module.identifier(); let name = module.readable_identifier(&compilation.options.context); - let id = ChunkGraph::get_module_id(&compilation.module_ids, identifier).map(|s| s.as_str()); + let id = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, identifier).map(|s| s.as_str()); (name, id) } @@ -106,7 +107,7 @@ pub fn get_chunk_relations( if let Some(pg) = compilation.chunk_group_by_ukey.get(p) { for c in &pg.chunks { if let Some(c) = compilation.chunk_by_ukey.get(c) - && let Some(id) = c.id(&compilation.chunk_ids) + && let Some(id) = c.id(&compilation.chunk_ids_artifact) { parents.insert(id.to_string()); } @@ -118,7 +119,7 @@ pub fn get_chunk_relations( if let Some(pg) = compilation.chunk_group_by_ukey.get(p) { for c in &pg.chunks { if let Some(c) = compilation.chunk_by_ukey.get(c) - && let Some(id) = c.id(&compilation.chunk_ids) + && let Some(id) = c.id(&compilation.chunk_ids_artifact) { children.insert(id.to_string()); } @@ -128,8 +129,8 @@ pub fn get_chunk_relations( for c in &cg.chunks { if let Some(c) = compilation.chunk_by_ukey.get(c) - && c.id(&compilation.chunk_ids) != chunk.id(&compilation.chunk_ids) - && let Some(id) = c.id(&compilation.chunk_ids) + && c.id(&compilation.chunk_ids_artifact) != chunk.id(&compilation.chunk_ids_artifact) + && let Some(id) = c.id(&compilation.chunk_ids_artifact) { siblings.insert(id.to_string()); } @@ -173,7 +174,7 @@ pub fn get_module_trace( name: origin_module .readable_identifier(&options.context) .to_string(), - id: ChunkGraph::get_module_id(&compilation.module_ids, origin_module.identifier()) + id: ChunkGraph::get_module_id(&compilation.module_ids_artifact, origin_module.identifier()) .map(|s| s.to_string()), }; @@ -182,8 +183,11 @@ pub fn get_module_trace( name: current_module .readable_identifier(&options.context) .to_string(), - id: ChunkGraph::get_module_id(&compilation.module_ids, current_module.identifier()) - .map(|s| s.to_string()), + id: ChunkGraph::get_module_id( + &compilation.module_ids_artifact, + current_module.identifier(), + ) + .map(|s| s.to_string()), }; module_trace.push(StatsModuleTrace { diff --git a/crates/rspack_ids/src/deterministic_chunk_ids_plugin.rs b/crates/rspack_ids/src/deterministic_chunk_ids_plugin.rs index 76a3fcb5f36..f9cc98150a0 100644 --- a/crates/rspack_ids/src/deterministic_chunk_ids_plugin.rs +++ b/crates/rspack_ids/src/deterministic_chunk_ids_plugin.rs @@ -40,7 +40,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: let chunks = compilation .chunk_by_ukey .values() - .filter(|chunk| chunk.id(&compilation.chunk_ids).is_none()) + .filter(|chunk| chunk.id(&compilation.chunk_ids_artifact).is_none()) .collect::>(); let mut chunk_key_to_id = FxHashMap::with_capacity_and_hasher(chunks.len(), FxBuildHasher::default()); @@ -48,7 +48,15 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: assign_deterministic_ids( chunks, |chunk| get_full_chunk_name(chunk, chunk_graph, &module_graph, &context), - |a, b| compare_chunks_natural(chunk_graph, &module_graph, &compilation.module_ids, a, b), + |a, b| { + compare_chunks_natural( + chunk_graph, + &module_graph, + &compilation.module_ids_artifact, + a, + b, + ) + }, |chunk, id| { let size = used_ids.len(); used_ids.insert(id.to_string()); @@ -67,7 +75,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: chunk_key_to_id.into_iter().for_each(|(chunk_ukey, id)| { let chunk = compilation.chunk_by_ukey.expect_get_mut(&chunk_ukey); - chunk.set_id(&mut compilation.chunk_ids, id.to_string()); + chunk.set_id(&mut compilation.chunk_ids_artifact, id.to_string()); }); Ok(()) diff --git a/crates/rspack_ids/src/deterministic_module_ids_plugin.rs b/crates/rspack_ids/src/deterministic_module_ids_plugin.rs index 0e122db8bda..801b1dd39ba 100644 --- a/crates/rspack_ids/src/deterministic_module_ids_plugin.rs +++ b/crates/rspack_ids/src/deterministic_module_ids_plugin.rs @@ -18,7 +18,7 @@ pub struct DeterministicModuleIdsPlugin; fn module_ids(&self, compilation: &mut Compilation) -> Result<()> { let (mut used_ids, modules) = get_used_module_ids_and_modules(compilation, None); - let mut module_ids = std::mem::take(&mut compilation.module_ids); + let mut module_ids = std::mem::take(&mut compilation.module_ids_artifact); let context = compilation.options.context.as_ref(); let max_length = 3; let fail_on_conflict = false; @@ -49,7 +49,7 @@ fn module_ids(&self, compilation: &mut Compilation) -> Result<()> { used_ids_len, salt, ); - compilation.module_ids = module_ids; + compilation.module_ids_artifact = module_ids; if fail_on_conflict && conflicts > 0 { // TODO: better error msg panic!("Assigning deterministic module ids has lead to conflicts {conflicts}"); diff --git a/crates/rspack_ids/src/id_helpers.rs b/crates/rspack_ids/src/id_helpers.rs index b0daa2471a1..cd26c69a167 100644 --- a/crates/rspack_ids/src/id_helpers.rs +++ b/crates/rspack_ids/src/id_helpers.rs @@ -13,7 +13,7 @@ use regex::Regex; use rspack_collections::{DatabaseItem, IdentifierMap}; use rspack_core::{ compare_runtime, BoxModule, Chunk, ChunkGraph, ChunkUkey, Compilation, ModuleGraph, ModuleId, - ModuleIdentifier, + ModuleIdentifier, ModuleIdsArtifact, }; use rspack_util::itoa; use rspack_util::{ @@ -46,7 +46,8 @@ pub fn get_used_module_ids_and_modules( .values() .filter(|m| m.need_id()) .for_each(|module| { - let module_id = ChunkGraph::get_module_id(&compilation.module_ids, module.identifier()); + let module_id = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.identifier()); if let Some(module_id) = module_id { used_ids.insert(module_id.to_string()); } else { @@ -157,7 +158,7 @@ pub fn assign_deterministic_ids( pub fn assign_ascending_module_ids( used_ids: &FxHashSet, modules: Vec<&BoxModule>, - module_ids: &mut IdentifierMap, + module_ids: &mut ModuleIdsArtifact, ) { let mut next_id = 0; let mut assign_id = |module: &BoxModule| { @@ -319,7 +320,7 @@ pub fn request_to_id(request: &str) -> String { pub fn get_used_chunk_ids(compilation: &Compilation) -> FxHashSet { let mut used_ids = FxHashSet::default(); for chunk in compilation.chunk_by_ukey.values() { - if let Some(id) = chunk.id(&compilation.chunk_ids) { + if let Some(id) = chunk.id(&compilation.chunk_ids_artifact) { used_ids.insert(id.to_string()); } } @@ -333,19 +334,19 @@ pub fn assign_ascending_chunk_ids(chunks: &[ChunkUkey], compilation: &mut Compil if !used_ids.is_empty() { for chunk in chunks { let chunk = compilation.chunk_by_ukey.expect_get_mut(chunk); - if chunk.id(&compilation.chunk_ids).is_none() { + if chunk.id(&compilation.chunk_ids_artifact).is_none() { while used_ids.contains(&next_id.to_string()) { next_id += 1; } - chunk.set_id(&mut compilation.chunk_ids, next_id.to_string()); + chunk.set_id(&mut compilation.chunk_ids_artifact, next_id.to_string()); next_id += 1; } } } else { for chunk in chunks { let chunk = compilation.chunk_by_ukey.expect_get_mut(chunk); - if chunk.id(&compilation.chunk_ids).is_none() { - chunk.set_id(&mut compilation.chunk_ids, next_id.to_string()); + if chunk.id(&compilation.chunk_ids_artifact).is_none() { + chunk.set_id(&mut compilation.chunk_ids_artifact, next_id.to_string()); next_id += 1; } } @@ -355,7 +356,7 @@ pub fn assign_ascending_chunk_ids(chunks: &[ChunkUkey], compilation: &mut Compil fn compare_chunks_by_modules( chunk_graph: &ChunkGraph, module_graph: &ModuleGraph, - module_ids: &IdentifierMap, + module_ids: &ModuleIdsArtifact, a: &Chunk, b: &Chunk, ) -> Ordering { @@ -397,7 +398,7 @@ fn compare_chunks_by_modules( pub fn compare_chunks_natural( chunk_graph: &ChunkGraph, module_graph: &ModuleGraph, - module_ids: &IdentifierMap, + module_ids: &ModuleIdsArtifact, a: &Chunk, b: &Chunk, ) -> Ordering { diff --git a/crates/rspack_ids/src/named_chunk_ids_plugin.rs b/crates/rspack_ids/src/named_chunk_ids_plugin.rs index ca69d7fa0ef..8d808d34b90 100644 --- a/crates/rspack_ids/src/named_chunk_ids_plugin.rs +++ b/crates/rspack_ids/src/named_chunk_ids_plugin.rs @@ -3,8 +3,8 @@ use rspack_collections::{DatabaseItem, UkeyIndexSet, UkeyMap, UkeySet}; use rspack_core::{ chunk_graph_chunk::ChunkId, incremental::{IncrementalPasses, Mutation, Mutations}, - ApplyContext, ChunkGraph, ChunkUkey, Compilation, CompilationChunkIds, CompilerOptions, Logger, - Plugin, PluginContext, + ApplyContext, ChunkGraph, ChunkIdsArtifact, ChunkUkey, Compilation, CompilationChunkIds, + CompilerOptions, Logger, Plugin, PluginContext, }; use rspack_hook::{plugin, plugin_hook}; use rspack_util::itoa; @@ -18,7 +18,7 @@ fn assign_named_chunk_ids( compilation: &Compilation, delimiter: &str, used_ids: &mut FxHashMap, - chunk_ids: &mut UkeyMap, + chunk_ids: &mut ChunkIdsArtifact, mutations: &mut Option, ) -> Vec { let context: &str = compilation.options.context.as_ref(); @@ -101,7 +101,13 @@ fn assign_named_chunk_ids( items.sort_unstable_by(|a, b| { let a = compilation.chunk_by_ukey.expect_get(a); let b = compilation.chunk_by_ukey.expect_get(b); - compare_chunks_natural(chunk_graph, &module_graph, &compilation.module_ids, a, b) + compare_chunks_natural( + chunk_graph, + &module_graph, + &compilation.module_ids_artifact, + a, + b, + ) }); let mut i = 0; for item in items { @@ -126,7 +132,13 @@ fn assign_named_chunk_ids( unnamed_items.sort_unstable_by(|a, b| { let a = compilation.chunk_by_ukey.expect_get(a); let b = compilation.chunk_by_ukey.expect_get(b); - compare_chunks_natural(chunk_graph, &module_graph, &compilation.module_ids, a, b) + compare_chunks_natural( + chunk_graph, + &module_graph, + &compilation.module_ids_artifact, + a, + b, + ) }); unnamed_items } @@ -154,7 +166,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: for mutation in mutations.iter() { match mutation { Mutation::ChunkRemove { chunk } => { - compilation.chunk_ids.remove(chunk); + compilation.chunk_ids_artifact.remove(chunk); } Mutation::ModuleSetId { module } => { affected_chunks.extend(compilation.chunk_graph.get_module_chunks(*module)); @@ -163,7 +175,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: } } compilation - .chunk_ids + .chunk_ids_artifact .retain(|chunk, _| compilation.chunk_by_ukey.contains(chunk)); affected_chunks } else { @@ -173,7 +185,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: let mut chunks: UkeySet = compilation .chunk_by_ukey .values() - .filter(|chunk| chunk.id(&compilation.chunk_ids).is_none()) + .filter(|chunk| chunk.id(&compilation.chunk_ids_artifact).is_none()) .map(|chunk| chunk.ukey()) .collect(); chunks.extend(more_chunks); @@ -188,8 +200,11 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: chunks.retain(|chunk_ukey| { let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey); if let Some(chunk_name) = chunk.name() { - if ChunkGraph::set_chunk_id(&mut compilation.chunk_ids, *chunk_ukey, chunk_name.into()) - && let Some(mutations) = &mut mutations + if ChunkGraph::set_chunk_id( + &mut compilation.chunk_ids_artifact, + *chunk_ukey, + chunk_name.into(), + ) && let Some(mutations) = &mut mutations { mutations.add(Mutation::ChunkSetId { chunk: *chunk_ukey }); } @@ -199,7 +214,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: }); let named_chunks_len = chunks_len - chunks.len(); - let mut chunk_ids = std::mem::take(&mut compilation.chunk_ids); + let mut chunk_ids = std::mem::take(&mut compilation.chunk_ids_artifact); let mut used_ids: FxHashMap = chunk_ids .iter() .map(|(&chunk, id)| (id.clone(), chunk)) @@ -257,7 +272,7 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error: compilation_mutations.extend(mutations); } - compilation.chunk_ids = chunk_ids; + compilation.chunk_ids_artifact = chunk_ids; Ok(()) } diff --git a/crates/rspack_ids/src/named_module_ids_plugin.rs b/crates/rspack_ids/src/named_module_ids_plugin.rs index 71975d75087..14e6b38bfb6 100644 --- a/crates/rspack_ids/src/named_module_ids_plugin.rs +++ b/crates/rspack_ids/src/named_module_ids_plugin.rs @@ -3,7 +3,7 @@ use rspack_collections::{IdentifierIndexSet, IdentifierMap, IdentifierSet}; use rspack_core::{ incremental::{IncrementalPasses, Mutation, Mutations}, ApplyContext, ChunkGraph, CompilationModuleIds, CompilerOptions, Logger, ModuleGraph, ModuleId, - ModuleIdentifier, Plugin, PluginContext, + ModuleIdentifier, ModuleIdsArtifact, Plugin, PluginContext, }; use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; @@ -18,7 +18,7 @@ fn assign_named_module_ids( context: &str, module_graph: &ModuleGraph, used_ids: &mut FxHashMap, - module_ids: &mut IdentifierMap, + module_ids: &mut ModuleIdsArtifact, mutations: &mut Option, ) -> Vec { let item_name_pair: Vec<_> = modules @@ -123,7 +123,7 @@ pub struct NamedModuleIdsPlugin; #[plugin_hook(CompilationModuleIds for NamedModuleIdsPlugin)] fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { - let mut module_ids = std::mem::take(&mut compilation.module_ids); + let mut module_ids = std::mem::take(&mut compilation.module_ids_artifact); let mut used_ids: FxHashMap = module_ids .iter() .map(|(&module, id)| (id.clone(), module)) @@ -228,7 +228,7 @@ fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { compilation_mutations.extend(mutations); } - compilation.module_ids = module_ids; + compilation.module_ids_artifact = module_ids; Ok(()) } diff --git a/crates/rspack_ids/src/natural_chunk_ids_plugin.rs b/crates/rspack_ids/src/natural_chunk_ids_plugin.rs index b000b383533..42fb6c98b9a 100644 --- a/crates/rspack_ids/src/natural_chunk_ids_plugin.rs +++ b/crates/rspack_ids/src/natural_chunk_ids_plugin.rs @@ -13,7 +13,7 @@ pub struct NaturalChunkIdsPlugin; #[plugin_hook(CompilationChunkIds for NaturalChunkIdsPlugin)] fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> rspack_error::Result<()> { - let module_ids = &compilation.module_ids; + let module_ids = &compilation.module_ids_artifact; let chunk_graph = &compilation.chunk_graph; let module_graph = &compilation.get_module_graph(); diff --git a/crates/rspack_ids/src/natural_module_ids_plugin.rs b/crates/rspack_ids/src/natural_module_ids_plugin.rs index d2d1e7c3102..ed0ab877b1e 100644 --- a/crates/rspack_ids/src/natural_module_ids_plugin.rs +++ b/crates/rspack_ids/src/natural_module_ids_plugin.rs @@ -15,7 +15,7 @@ pub struct NaturalModuleIdsPlugin; fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { let (used_ids, mut modules_in_natural_order) = get_used_module_ids_and_modules(compilation, None); - let mut module_ids = std::mem::take(&mut compilation.module_ids); + let mut module_ids = std::mem::take(&mut compilation.module_ids_artifact); let module_graph = compilation.get_module_graph(); modules_in_natural_order @@ -28,7 +28,7 @@ fn module_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { assign_ascending_module_ids(&used_ids, modules_in_natural_order, &mut module_ids); - compilation.module_ids = module_ids; + compilation.module_ids_artifact = module_ids; Ok(()) } diff --git a/crates/rspack_ids/src/occurrence_chunk_ids_plugin.rs b/crates/rspack_ids/src/occurrence_chunk_ids_plugin.rs index 131070489c4..5653f29114e 100644 --- a/crates/rspack_ids/src/occurrence_chunk_ids_plugin.rs +++ b/crates/rspack_ids/src/occurrence_chunk_ids_plugin.rs @@ -68,7 +68,13 @@ fn chunk_ids(&self, compilation: &mut rspack_core::Compilation) -> Result<()> { return b_occurs.cmp(&a_occurs); } - compare_chunks_natural(chunk_graph, module_graph, &compilation.module_ids, a, b) + compare_chunks_natural( + chunk_graph, + module_graph, + &compilation.module_ids_artifact, + a, + b, + ) }) .map(|chunk| chunk.ukey()) .collect::>(); diff --git a/crates/rspack_plugin_asset/src/lib.rs b/crates/rspack_plugin_asset/src/lib.rs index 6d981fc187c..493b6310e20 100644 --- a/crates/rspack_plugin_asset/src/lib.rs +++ b/crates/rspack_plugin_asset/src/lib.rs @@ -250,7 +250,8 @@ impl AssetParserAndGenerator { .unwrap_or(&compilation.options.output.asset_module_filename); let path_data = PathData::default() .module_id_optional( - ChunkGraph::get_module_id(&compilation.module_ids, module.id()).map(|s| s.as_str()), + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.id()) + .map(|s| s.as_str()), ) .content_hash_optional(contenthash) .hash_optional(contenthash) @@ -288,7 +289,8 @@ impl AssetParserAndGenerator { template, PathData::default() .module_id_optional( - ChunkGraph::get_module_id(&compilation.module_ids, module.id()).map(|s| s.as_str()), + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.id()) + .map(|s| s.as_str()), ) .content_hash_optional(contenthash) .hash_optional(contenthash) diff --git a/crates/rspack_plugin_banner/src/lib.rs b/crates/rspack_plugin_banner/src/lib.rs index a6ca94191a3..9d8e8064cde 100644 --- a/crates/rspack_plugin_banner/src/lib.rs +++ b/crates/rspack_plugin_banner/src/lib.rs @@ -189,11 +189,15 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { &FilenameTemplate::from(banner), PathData::default() .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .hash(&hash) .filename(file), ) diff --git a/crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs b/crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs index ce64443b374..cab233fbce1 100644 --- a/crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs +++ b/crates/rspack_plugin_css/src/plugin/impl_plugin_for_css_plugin.rs @@ -103,7 +103,7 @@ impl CssPlugin { "chunk {}\nConflicting order between {} and {}", chunk.name().unwrap_or( chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .expect("should have chunk id") .as_str() ), @@ -287,7 +287,7 @@ async fn content_hash( compilation .code_generation_results .get_hash(&m.identifier(), Some(chunk.runtime())), - ChunkGraph::get_module_id(&compilation.module_ids, m.identifier()), + ChunkGraph::get_module_id(&compilation.module_ids_artifact, m.identifier()), ) }) .for_each(|(current, id)| { @@ -336,14 +336,18 @@ async fn render_manifest( let output_path = compilation.get_path_with_info( filename_template, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::Css, compilation.options.output.hash_digest_length, )) diff --git a/crates/rspack_plugin_css/src/runtime/mod.rs b/crates/rspack_plugin_css/src/runtime/mod.rs index a4ad4442a64..0ae459db288 100644 --- a/crates/rspack_plugin_css/src/runtime/mod.rs +++ b/crates/rspack_plugin_css/src/runtime/mod.rs @@ -54,7 +54,7 @@ impl RuntimeModule for CssLoadingRuntimeModule { let id = compilation .chunk_by_ukey .expect_get(chunk_ukey) - .expect_id(&compilation.chunk_ids) + .expect_id(&compilation.chunk_ids_artifact) .clone(); if chunk_has_css(chunk_ukey, compilation) { initial_chunk_ids.insert(id); diff --git a/crates/rspack_plugin_css/src/utils.rs b/crates/rspack_plugin_css/src/utils.rs index 3410b804bfc..a77fc967343 100644 --- a/crates/rspack_plugin_css/src/utils.rs +++ b/crates/rspack_plugin_css/src/utils.rs @@ -216,7 +216,7 @@ pub fn stringified_exports<'a>( .expect("should have css from module"); let from = serde_json::to_string( - ChunkGraph::get_module_id(&compilation.module_ids, from.module_identifier) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, from.module_identifier) .expect("should have module"), ) .expect("should json stringify module id"); @@ -290,7 +290,7 @@ pub fn css_modules_exports_to_concatenate_module_string<'a>( .expect("should have css from module"); let from = serde_json::to_string( - ChunkGraph::get_module_id(&compilation.module_ids, from.module_identifier) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, from.module_identifier) .expect("should have module"), ) .expect("should json stringify module id"); diff --git a/crates/rspack_plugin_devtool/src/module_filename_helpers.rs b/crates/rspack_plugin_devtool/src/module_filename_helpers.rs index 40b8a72aa63..83229c0b59c 100644 --- a/crates/rspack_plugin_devtool/src/module_filename_helpers.rs +++ b/crates/rspack_plugin_devtool/src/module_filename_helpers.rs @@ -74,9 +74,10 @@ impl ModuleFilenameHelpers { let short_identifier = module.readable_identifier(context).to_string(); let identifier = contextify(context, module_identifier); - let module_id = ChunkGraph::get_module_id(&compilation.module_ids, *module_identifier) - .map(|s| s.to_string()) - .unwrap_or_default(); + let module_id = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module_identifier) + .map(|s| s.to_string()) + .unwrap_or_default(); let absolute_resource_path = "".to_string(); let hash = get_hash(&identifier, output_options); diff --git a/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs b/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs index b7a41841953..458ace8ef80 100644 --- a/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs +++ b/crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs @@ -422,12 +422,18 @@ impl SourceMapDevToolPlugin { let data = PathData::default().filename(&filename); let data = match chunk { Some(chunk) => data - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional( + chunk.name_for_filename_template(&compilation.chunk_ids_artifact), + ) .content_hash_optional(Some(digest.encoded())), None => data, }; diff --git a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs index 0ec34663160..453ac7d379d 100644 --- a/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs +++ b/crates/rspack_plugin_dll/src/lib_manifest_plugin.rs @@ -72,12 +72,16 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { let target_path = compilation.get_path( &self.options.path, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)), + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)), )?; if use_paths.contains(&target_path) { @@ -91,14 +95,18 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { .get_path( name, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )), @@ -140,7 +148,7 @@ async fn emit(&self, compilation: &mut Compilation) -> Result<()> { _ => None, }; - let id = ChunkGraph::get_module_id(&compilation.module_ids, module.identifier()); + let id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.identifier()); let build_meta = module.build_meta(); diff --git a/crates/rspack_plugin_extract_css/src/plugin.rs b/crates/rspack_plugin_extract_css/src/plugin.rs index dad79b3c0c3..95136765496 100644 --- a/crates/rspack_plugin_extract_css/src/plugin.rs +++ b/crates/rspack_plugin_extract_css/src/plugin.rs @@ -330,7 +330,9 @@ despite it was not able to fulfill desired ordering with these modules: {}"#, chunk .name() - .or_else(|| chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .or_else(|| chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str())) .unwrap_or_default(), fallback_module.readable_identifier(&compilation.options.context), conflict @@ -526,7 +528,7 @@ fn runtime_requirement_in_tree( |_| false, move |chunk, compilation| { chunk - .content_hash(&compilation.chunk_hashes_results)? + .content_hash(&compilation.chunk_hashes_artifact)? .contains_key(&SOURCE_TYPE[0]) .then(|| { if chunk.can_be_initial(&compilation.chunk_group_by_ukey) { @@ -622,14 +624,18 @@ async fn render_manifest( let filename = compilation.get_path_with_info( filename_template, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SOURCE_TYPE[0], compilation.options.output.hash_digest_length, )), diff --git a/crates/rspack_plugin_extract_css/src/runtime.rs b/crates/rspack_plugin_extract_css/src/runtime.rs index ba777b5319b..50c5c7e10c7 100644 --- a/crates/rspack_plugin_extract_css/src/runtime.rs +++ b/crates/rspack_plugin_extract_css/src/runtime.rs @@ -153,7 +153,7 @@ impl RuntimeModule for CssLoadingRuntimeModule { "__INSTALLED_CHUNKS__", &format!( "{}: 0,\n", - serde_json::to_string(chunk.expect_id(&compilation.chunk_ids)) + serde_json::to_string(chunk.expect_id(&compilation.chunk_ids_artifact)) .expect("json stringify failed") ), ); @@ -172,7 +172,7 @@ impl RuntimeModule for CssLoadingRuntimeModule { .filter_map(|id| { let chunk = compilation.chunk_by_ukey.expect_get(id); - chunk.id(&compilation.chunk_ids).map(|id| { + chunk.id(&compilation.chunk_ids_artifact).map(|id| { format!( "{}: 1,\n", serde_json::to_string(id).expect("json stringify failed") diff --git a/crates/rspack_plugin_hmr/src/lib.rs b/crates/rspack_plugin_hmr/src/lib.rs index b6568886808..8dd3ed50acf 100644 --- a/crates/rspack_plugin_hmr/src/lib.rs +++ b/crates/rspack_plugin_hmr/src/lib.rs @@ -142,12 +142,18 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { let current_chunk = compilation .chunk_by_ukey .iter() - .find(|(_, chunk)| chunk.expect_id(&compilation.chunk_ids).eq(&chunk_id)) + .find(|(_, chunk)| { + chunk + .expect_id(&compilation.chunk_ids_artifact) + .eq(&chunk_id) + }) .map(|(_, chunk)| chunk); let current_chunk_ukey = current_chunk.map(|c| c.ukey()); if let Some(current_chunk) = current_chunk { - chunk_id = current_chunk.expect_id(&compilation.chunk_ids).clone(); + chunk_id = current_chunk + .expect_id(&compilation.chunk_ids_artifact) + .clone(); new_runtime = Default::default(); // intersectRuntime for old_runtime in all_old_runtime.iter() { @@ -191,7 +197,7 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { if !new_modules.is_empty() || !new_runtime_modules.is_empty() { let mut hot_update_chunk = Chunk::new(None, ChunkKind::HotUpdate); - hot_update_chunk.set_id(&mut compilation.chunk_ids, chunk_id.clone()); + hot_update_chunk.set_id(&mut compilation.chunk_ids_artifact, chunk_id.clone()); hot_update_chunk.set_runtime(if let Some(current_chunk) = current_chunk { current_chunk.runtime().clone() } else { @@ -268,11 +274,11 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> { PathData::default() .chunk_id_optional( hot_update_chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .map(|id| id.as_str()), ) .chunk_name_optional( - hot_update_chunk.name_for_filename_template(&compilation.chunk_ids), + hot_update_chunk.name_for_filename_template(&compilation.chunk_ids_artifact), ) .hash_optional( old_hash diff --git a/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs index 61e7c489d1c..41b7b3bb242 100644 --- a/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/commonjs/module_decorator_dependency.rs @@ -59,9 +59,10 @@ impl DependencyTemplate for ModuleDecoratorDependency { let module_argument = module.get_module_argument(); // ref: tests/webpack-test/cases/scope-hoisting/issue-5096 will return a `null` as module id - let module_id = ChunkGraph::get_module_id(&compilation.module_ids, module.identifier()) - .map(|s| s.to_string()) - .unwrap_or_default(); + let module_id = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.identifier()) + .map(|s| s.to_string()) + .unwrap_or_default(); init_fragments.push(Box::new(NormalInitFragment::new( format!( diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs index 38445f880b5..88a5f87a754 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_export_imported_specifier_dependency.rs @@ -1390,14 +1390,6 @@ impl ModuleDependency for ESMExportImportedSpecifierDependency { self.request = request.into(); } - fn is_export_all(&self) -> Option { - if self.export_all { - Some(true) - } else { - None - } - } - fn get_condition(&self) -> Option { let id = self.id; Some(DependencyCondition::Fn(Arc::new( diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs index f99d8737ba4..972d947a00b 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/esm_import_dependency.rs @@ -479,10 +479,6 @@ impl DependencyConditionFn for ESMImportSideEffectDependencyCondition { #[cacheable_dyn] impl ModuleDependency for ESMImportSideEffectDependency { - fn is_export_all(&self) -> Option { - Some(self.export_all) - } - fn request(&self) -> &str { &self.request } diff --git a/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs b/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs index cc2b19b2ff7..89566a7e503 100644 --- a/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs +++ b/crates/rspack_plugin_javascript/src/dependency/worker/mod.rs @@ -103,7 +103,7 @@ impl DependencyTemplate for WorkerDependency { }) .map(|entrypoint| entrypoint.get_entry_point_chunk()) .and_then(|ukey| compilation.chunk_by_ukey.get(&ukey)) - .and_then(|chunk| chunk.id(&compilation.chunk_ids)) + .and_then(|chunk| chunk.id(&compilation.chunk_ids_artifact)) .and_then(|chunk_id| serde_json::to_string(chunk_id).ok()) .expect("failed to get json stringified chunk id"); let worker_import_base_url = if !self.public_path.is_empty() { 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 fc95fe1f692..024175cb578 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 @@ -184,7 +184,7 @@ async fn content_hash( if chunk.has_runtime(&compilation.chunk_group_by_ukey) { self.update_hash_with_bootstrap(chunk_ukey, compilation, hasher)?; } else { - chunk.id(&compilation.chunk_ids).hash(&mut hasher); + chunk.id(&compilation.chunk_ids_artifact).hash(&mut hasher); } self.get_chunk_hash(chunk_ukey, compilation, hasher).await?; @@ -205,7 +205,7 @@ async fn content_hash( compilation .code_generation_results .get_hash(&mgm.identifier(), Some(chunk.runtime())), - ChunkGraph::get_module_id(&compilation.module_ids, mgm.identifier()), + ChunkGraph::get_module_id(&compilation.module_ids_artifact, mgm.identifier()), ) }) .for_each(|(current, id)| { @@ -277,13 +277,17 @@ async fn render_manifest( &filename_template, PathData::default() .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )) diff --git a/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs index c0bef3f124c..bc15c0ee949 100644 --- a/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/infer_async_modules_plugin.rs @@ -29,7 +29,7 @@ async fn finish_modules(&self, compilation: &mut Compilation) -> Result<()> { // we keep the state for the module only if the module revoke first, and then rebuild // otherwise we gc its state if !acc.contains(module) { - compilation.async_modules.remove(module); + compilation.async_modules_artifact.remove(module); } } _ => {} diff --git a/crates/rspack_plugin_javascript/src/plugin/mod.rs b/crates/rspack_plugin_javascript/src/plugin/mod.rs index 10a79f0fbeb..2f3d638c3f6 100644 --- a/crates/rspack_plugin_javascript/src/plugin/mod.rs +++ b/crates/rspack_plugin_javascript/src/plugin/mod.rs @@ -301,7 +301,7 @@ impl JsPlugin { compilation .chunk_by_ukey .expect_get(chunk_ukey) - .expect_id(&compilation.chunk_ids) + .expect_id(&compilation.chunk_ids_artifact) .to_string() }) .collect::>(); @@ -374,7 +374,7 @@ impl JsPlugin { buf2.push("// This entry module used 'module' so it can't be inlined".into()); } - let module_id = ChunkGraph::get_module_id(&compilation.module_ids, *module) + let module_id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module) .expect("should have module id"); let mut module_id_expr = serde_json::to_string(module_id).expect("invalid module_id"); if runtime_requirements.contains(RuntimeGlobals::ENTRY_MODULE_ID) { diff --git a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs index 808bfa2ba32..2aef5d11efc 100644 --- a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs @@ -3,11 +3,19 @@ use std::rc::Rc; use std::sync::LazyLock; use rayon::prelude::*; +use rspack_collections::IdentifierIndexSet; use rspack_collections::IdentifierMap; +use rspack_collections::IdentifierSet; +use rspack_core::incremental::IncrementalPasses; +use rspack_core::incremental::Mutation; use rspack_core::DependencyExtraMeta; use rspack_core::DependencyId; +use rspack_core::Logger; use rspack_core::MaybeDynamicTargetExportInfo; use rspack_core::ModuleGraphConnection; +use rspack_core::SideEffectsDoOptimize; +use rspack_core::SideEffectsDoOptimizeMoveTarget; +use rspack_core::SideEffectsOptimizeArtifact; use rspack_core::{ BoxModule, Compilation, CompilationOptimizeDependencies, ConnectionState, FactoryMeta, ModuleFactoryCreateData, ModuleGraph, ModuleIdentifier, NormalModuleCreateData, @@ -17,7 +25,6 @@ use rspack_error::Result; use rspack_hook::{plugin, plugin_hook}; use rspack_paths::AssertUtf8; use rspack_paths::Utf8Path; -use rspack_util::atom::Atom; use sugar_path::SugarPath; use swc_core::common::comments::Comments; use swc_core::common::{comments, Span, Spanned, SyntaxContext, GLOBALS}; @@ -658,6 +665,9 @@ async fn nmf_module( #[plugin_hook(CompilationOptimizeDependencies for SideEffectsFlagPlugin)] fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result> { + let logger = compilation.get_logger("rspack.SideEffectsFlagPlugin"); + let start = logger.time("update dependencies"); + let module_graph = compilation.get_module_graph(); let side_effects_state_map: IdentifierMap = module_graph .modules() @@ -670,26 +680,97 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result = module_graph - .modules() - .keys() - .par_bridge() + let modules: IdentifierSet = if let Some(mutations) = compilation + .incremental + .mutations_read(IncrementalPasses::SIDE_EFFECTS) + && !compilation.side_effects_optimize_artifact.is_empty() + { + let mut modules: IdentifierSet = mutations + .iter() + .filter_map(|mutation| match mutation { + Mutation::ModuleBuild { module } => Some(*module), + Mutation::ModuleRemove { module } => { + compilation.side_effects_optimize_artifact.remove(module); + None + } + _ => None, + }) + .collect(); + + let module_graph = compilation.get_module_graph(); + let mut stack = IdentifierIndexSet::from_iter(modules.iter().copied()); + while let Some(module) = stack.pop() { + for connection in module_graph.get_incoming_connections(&module) { + let Some(original_module) = connection.original_module_identifier else { + continue; + }; + if modules.contains(&original_module) { + continue; + } + let Some(dep) = module_graph.dependency_by_id(&connection.dependency_id) else { + continue; + }; + if dep.is::() { + modules.insert(original_module); + stack.insert(original_module); + } + } + } + + let logger = compilation.get_logger("rspack.incremental.sideEffects"); + logger.log(format!( + "{} modules are affected, {} in total", + modules.len(), + module_graph.modules().len() + )); + + modules + } else { + module_graph.modules().keys().copied().collect() + }; + + let module_graph = compilation.get_module_graph(); + let do_optimizes: SideEffectsOptimizeArtifact = modules + .par_iter() .filter(|module| side_effects_state_map[module] == ConnectionState::Bool(false)) - .flat_map_iter(|module| { - module_graph + .map(|module| { + let do_optimizes: Vec<_> = module_graph .get_incoming_connections(module) .filter_map(|connection| { can_optimize_connection(connection, &side_effects_state_map, &module_graph) }) + .collect(); + (*module, do_optimizes) }) .collect(); + let mut do_optimizes: Vec<_> = if compilation + .incremental + .can_read_mutations(IncrementalPasses::SIDE_EFFECTS) + { + compilation + .side_effects_optimize_artifact + .extend(do_optimizes); + compilation + .side_effects_optimize_artifact + .values() + .flat_map(|v| v) + .cloned() + .collect() + } else { + do_optimizes.into_values().flat_map(|v| v).collect() + }; + + let mut do_optimized_count = 0; while !do_optimizes.is_empty() { + do_optimized_count += do_optimizes.len(); + let mut module_graph = compilation.get_module_graph_mut(); let new_connections: Vec<_> = do_optimizes .into_iter() .map(|do_optimize| do_optimize_connection(do_optimize, &mut module_graph)) .collect(); + let module_graph = compilation.get_module_graph(); do_optimizes = new_connections .into_par_iter() @@ -701,41 +782,28 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result, - dependency: DependencyId, - target_module: ModuleIdentifier, - kind: DoOptimizeKind, -} - -enum DoOptimizeKind { - ExportImportedSpecifier { - export_info: MaybeDynamicTargetExportInfo, - target_export: Option>, - }, - ImportSpecifier, -} - #[tracing::instrument(skip_all)] fn do_optimize_connection( - do_optimize: DoOptimize, + do_optimize: SideEffectsDoOptimize, module_graph: &mut ModuleGraph, ) -> (DependencyId, ModuleIdentifier) { - let DoOptimize { + let SideEffectsDoOptimize { ids, dependency, target_module, - kind, + need_move_target, } = do_optimize; module_graph.do_update_module(&dependency, &target_module); module_graph.set_dependency_extra_meta(dependency, DependencyExtraMeta { ids }); - if let DoOptimizeKind::ExportImportedSpecifier { - export_info: MaybeDynamicTargetExportInfo::Static(export_info), + if let Some(SideEffectsDoOptimizeMoveTarget { + export_info, target_export, - } = kind + }) = need_move_target { export_info.do_move_target(module_graph, dependency, target_export); } @@ -747,7 +815,7 @@ fn can_optimize_connection( connection: &ModuleGraphConnection, side_effects_state_map: &IdentifierMap, module_graph: &ModuleGraph, -) -> Option { +) -> Option { let original_module = connection.original_module_identifier?; let dependency_id = connection.dependency_id; let dep = module_graph.dependency_by_id(&dependency_id)?; @@ -778,15 +846,19 @@ fn can_optimize_connection( ret }) .unwrap_or_else(|| ids.get(1..).unwrap_or_default().to_vec()); + let need_move_target = match export_info { + MaybeDynamicTargetExportInfo::Static(export_info) => Some(SideEffectsDoOptimizeMoveTarget { + export_info, + target_export: target.export, + }), + MaybeDynamicTargetExportInfo::Dynamic { .. } => None, + }; - return Some(DoOptimize { + return Some(SideEffectsDoOptimize { ids: processed_ids, dependency: dependency_id, target_module: target.module, - kind: DoOptimizeKind::ExportImportedSpecifier { - export_info, - target_export: target.export, - }, + need_move_target, }); } @@ -816,11 +888,11 @@ fn can_optimize_connection( }) .unwrap_or_else(|| ids[1..].to_vec()); - return Some(DoOptimize { + return Some(SideEffectsDoOptimize { ids: processed_ids, dependency: dependency_id, target_module: target.module, - kind: DoOptimizeKind::ImportSpecifier, + need_move_target: None, }); } diff --git a/crates/rspack_plugin_javascript/src/runtime.rs b/crates/rspack_plugin_javascript/src/runtime.rs index 40d2a04b2dc..c6ff43c84db 100644 --- a/crates/rspack_plugin_javascript/src/runtime.rs +++ b/crates/rspack_plugin_javascript/src/runtime.rs @@ -126,8 +126,9 @@ pub fn render_module( if need_require { args.push(RuntimeGlobals::REQUIRE.to_string()); } - let module_id = ChunkGraph::get_module_id(&compilation.module_ids, module.identifier()) - .expect("should have module_id in render_module"); + let module_id = + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.identifier()) + .expect("should have module_id in render_module"); sources.add(RawStringSource::from( serde_json::to_string(&module_id).map_err(|e| error!(e.to_string()))?, )); diff --git a/crates/rspack_plugin_lazy_compilation/src/module.rs b/crates/rspack_plugin_lazy_compilation/src/module.rs index cff20702a5c..d1ce2afbbce 100644 --- a/crates/rspack_plugin_lazy_compilation/src/module.rs +++ b/crates/rspack_plugin_lazy_compilation/src/module.rs @@ -204,7 +204,7 @@ impl Module for LazyCompilationProxyModule { let client = format!( "var client = __webpack_require__(\"{}\");\nvar data = \"{}\"", - ChunkGraph::get_module_id(&compilation.module_ids, *client_module) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *client_module) .expect("should have module id"), self.data ); @@ -258,7 +258,7 @@ impl Module for LazyCompilationProxyModule { "import()", false ), - ChunkGraph::get_module_id(&compilation.module_ids, *module) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module) .map(|s| s.as_str()) .expect("should have module id") .cow_replace('"', r#"\""#), diff --git a/crates/rspack_plugin_library/src/amd_library_plugin.rs b/crates/rspack_plugin_library/src/amd_library_plugin.rs index 95c206b1a5c..3a7e41937f7 100644 --- a/crates/rspack_plugin_library/src/amd_library_plugin.rs +++ b/crates/rspack_plugin_library/src/amd_library_plugin.rs @@ -133,14 +133,18 @@ fn render( .get_path( &FilenameTemplate::from(name.to_string()), PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )), diff --git a/crates/rspack_plugin_library/src/assign_library_plugin.rs b/crates/rspack_plugin_library/src/assign_library_plugin.rs index 6c1af3e1987..00f98739210 100644 --- a/crates/rspack_plugin_library/src/assign_library_plugin.rs +++ b/crates/rspack_plugin_library/src/assign_library_plugin.rs @@ -158,14 +158,20 @@ impl AssignLibraryPlugin { .get_path( &FilenameTemplate::from(v.to_owned()), PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional( + chunk.name_for_filename_template(&compilation.chunk_ids_artifact), + ) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )), diff --git a/crates/rspack_plugin_library/src/umd_library_plugin.rs b/crates/rspack_plugin_library/src/umd_library_plugin.rs index 2d3dd4b77a6..caff1d0a840 100644 --- a/crates/rspack_plugin_library/src/umd_library_plugin.rs +++ b/crates/rspack_plugin_library/src/umd_library_plugin.rs @@ -306,14 +306,18 @@ fn replace_keys(v: String, chunk: &Chunk, compilation: &Compilation) -> String { .get_path( &FilenameTemplate::from(v), PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )), diff --git a/crates/rspack_plugin_library/src/utils.rs b/crates/rspack_plugin_library/src/utils.rs index 37e5b342480..d53579cc105 100644 --- a/crates/rspack_plugin_library/src/utils.rs +++ b/crates/rspack_plugin_library/src/utils.rs @@ -28,7 +28,7 @@ fn inner_external_arguments(modules: &[&ExternalModule], compilation: &Compilati format!( "__WEBPACK_EXTERNAL_MODULE_{}__", to_identifier( - ChunkGraph::get_module_id(&compilation.module_ids, m.identifier()) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, m.identifier()) .map(|s| s.as_str()) .expect("should have module id") ) diff --git a/crates/rspack_plugin_mf/src/container/fallback_module.rs b/crates/rspack_plugin_mf/src/container/fallback_module.rs index 90efce81606..bd9061cc5be 100644 --- a/crates/rspack_plugin_mf/src/container/fallback_module.rs +++ b/crates/rspack_plugin_mf/src/container/fallback_module.rs @@ -161,7 +161,9 @@ impl Module for FallbackModule { .get_dependencies() .iter() .filter_map(|dep| module_graph.get_module_by_dependency_id(dep)) - .filter_map(|module| ChunkGraph::get_module_id(&compilation.module_ids, module.identifier())) + .filter_map(|module| { + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.identifier()) + }) .collect(); let code = format!( r#" diff --git a/crates/rspack_plugin_mf/src/container/remote_module.rs b/crates/rspack_plugin_mf/src/container/remote_module.rs index 1cde75b10eb..73e670ea249 100644 --- a/crates/rspack_plugin_mf/src/container/remote_module.rs +++ b/crates/rspack_plugin_mf/src/container/remote_module.rs @@ -178,8 +178,8 @@ impl Module for RemoteModule { let mut codegen = CodeGenerationResult::default(); let module_graph = compilation.get_module_graph(); let module = module_graph.get_module_by_dependency_id(&self.dependencies[0]); - let id = - module.and_then(|m| ChunkGraph::get_module_id(&compilation.module_ids, m.identifier())); + let id = module + .and_then(|m| ChunkGraph::get_module_id(&compilation.module_ids_artifact, m.identifier())); codegen.add(SourceType::Remote, RawStringSource::from_static("").boxed()); codegen.data.insert(CodeGenerationDataShareInit { items: vec![ShareInitData { diff --git a/crates/rspack_plugin_mf/src/container/remote_runtime_module.rs b/crates/rspack_plugin_mf/src/container/remote_runtime_module.rs index e1098aeb223..a92941920a5 100644 --- a/crates/rspack_plugin_mf/src/container/remote_runtime_module.rs +++ b/crates/rspack_plugin_mf/src/container/remote_runtime_module.rs @@ -56,16 +56,18 @@ impl RuntimeModule for RemoteRuntimeModule { continue; }; let name = m.internal_request.as_str(); - let id = ChunkGraph::get_module_id(&compilation.module_ids, m.identifier()) + let id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, m.identifier()) .expect("should have module_id at ::generate"); let share_scope = m.share_scope.as_str(); let dep = m.get_dependencies()[0]; let external_module = module_graph .get_module_by_dependency_id(&dep) .expect("should have module"); - let external_module_id = - ChunkGraph::get_module_id(&compilation.module_ids, external_module.identifier()) - .expect("should have module_id at ::generate"); + let external_module_id = ChunkGraph::get_module_id( + &compilation.module_ids_artifact, + external_module.identifier(), + ) + .expect("should have module_id at ::generate"); remotes.push(id.to_string()); id_to_remote_data_mapping.insert( id, @@ -83,7 +85,7 @@ impl RuntimeModule for RemoteRuntimeModule { let chunk = compilation.chunk_by_ukey.expect_get(&chunk); chunk_to_remotes_mapping.insert( chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .expect("should have chunkId at ::generate"), remotes, ); diff --git a/crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs b/crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs index 90f499bd224..ee53751405c 100644 --- a/crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs +++ b/crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs @@ -47,7 +47,7 @@ impl RuntimeModule for ConsumeSharedRuntimeModule { let mut module_id_to_consume_data_mapping = FxHashMap::default(); let mut initial_consumes = Vec::new(); let mut add_module = |module: ModuleIdentifier, chunk: &Chunk, ids: &mut Vec| { - let id = ChunkGraph::get_module_id(&compilation.module_ids, module) + let id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, module) .map(|s| s.to_string()) .expect("should have moduleId at ::generate"); ids.push(id.clone()); @@ -86,7 +86,7 @@ impl RuntimeModule for ConsumeSharedRuntimeModule { } chunk_to_module_mapping.insert( chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .map(ToOwned::to_owned) .expect("should have chunkId at ::generate"), ids, diff --git a/crates/rspack_plugin_runtime/src/array_push_callback_chunk_format.rs b/crates/rspack_plugin_runtime/src/array_push_callback_chunk_format.rs index 9f5b955e087..3495ff10838 100644 --- a/crates/rspack_plugin_runtime/src/array_push_callback_chunk_format.rs +++ b/crates/rspack_plugin_runtime/src/array_push_callback_chunk_format.rs @@ -112,7 +112,7 @@ fn render_chunk( "{}[{}]('{}', ", global_object, serde_json::to_string(hot_update_global).map_err(|e| error!(e.to_string()))?, - chunk.expect_id(&compilation.chunk_ids) + chunk.expect_id(&compilation.chunk_ids_artifact) ))); source.add(render_source.source.clone()); if has_runtime_modules { @@ -129,7 +129,7 @@ fn render_chunk( chunk_loading_global, global_object, chunk_loading_global, - serde_json::to_string(chunk.expect_id(&compilation.chunk_ids)) + serde_json::to_string(chunk.expect_id(&compilation.chunk_ids_artifact)) .expect("json stringify failed"), ))); source.add(render_source.source.clone()); diff --git a/crates/rspack_plugin_runtime/src/common_js_chunk_format.rs b/crates/rspack_plugin_runtime/src/common_js_chunk_format.rs index b83edc02798..51a8c3d633a 100644 --- a/crates/rspack_plugin_runtime/src/common_js_chunk_format.rs +++ b/crates/rspack_plugin_runtime/src/common_js_chunk_format.rs @@ -102,7 +102,7 @@ fn render_chunk( let mut sources = ConcatSource::default(); sources.add(RawStringSource::from(format!( "exports.ids = ['{}'];\n", - &chunk.expect_id(&compilation.chunk_ids) + &chunk.expect_id(&compilation.chunk_ids_artifact) ))); sources.add(RawStringSource::from_static("exports.modules = ")); sources.add(render_source.source.clone()); diff --git a/crates/rspack_plugin_runtime/src/helpers.rs b/crates/rspack_plugin_runtime/src/helpers.rs index 1e39030a408..823f289faa0 100644 --- a/crates/rspack_plugin_runtime/src/helpers.rs +++ b/crates/rspack_plugin_runtime/src/helpers.rs @@ -23,7 +23,7 @@ pub fn update_hash_for_entry_startup( .get_module_graph() .module_graph_module_by_identifier(module) .and_then(|module| { - ChunkGraph::get_module_id(&compilation.module_ids, module.module_identifier) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.module_identifier) }) { module_id.hash(hasher); @@ -41,7 +41,7 @@ pub fn update_hash_for_entry_startup( &compilation.chunk_group_by_ukey, ) { if let Some(chunk) = compilation.chunk_by_ukey.get(&chunk_ukey) { - chunk.id(&compilation.chunk_ids).hash(hasher); + chunk.id(&compilation.chunk_ids_artifact).hash(hasher); } } } @@ -149,7 +149,7 @@ pub fn generate_entry_startup( .get_module_graph() .module_graph_module_by_identifier(module) .map(|module| { - ChunkGraph::get_module_id(&compilation.module_ids, module.module_identifier) + ChunkGraph::get_module_id(&compilation.module_ids_artifact, module.module_identifier) .map(|s| s.as_str()) .unwrap_or("null") }) @@ -174,7 +174,7 @@ pub fn generate_entry_startup( .iter() .map(|chunk_ukey| { let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey); - chunk.expect_id(&compilation.chunk_ids).clone() + chunk.expect_id(&compilation.chunk_ids_artifact).clone() }) .collect::>(), ); @@ -247,7 +247,7 @@ pub fn get_relative_path(base_chunk_output_name: &str, other_chunk_output_name: pub fn get_chunk_output_name(chunk: &Chunk, compilation: &Compilation) -> Result { let hash = chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, ); let filename = get_js_chunk_filename_template( @@ -258,14 +258,18 @@ pub fn get_chunk_output_name(chunk: &Chunk, compilation: &Compilation) -> Result compilation.get_path( &filename, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )) diff --git a/crates/rspack_plugin_runtime/src/module_chunk_format.rs b/crates/rspack_plugin_runtime/src/module_chunk_format.rs index 7c19901177d..68feba0def6 100644 --- a/crates/rspack_plugin_runtime/src/module_chunk_format.rs +++ b/crates/rspack_plugin_runtime/src/module_chunk_format.rs @@ -109,7 +109,7 @@ fn render_chunk( let mut sources = ConcatSource::default(); sources.add(RawStringSource::from(format!( "export const ids = ['{}'];\n", - &chunk.expect_id(&compilation.chunk_ids) + &chunk.expect_id(&compilation.chunk_ids_artifact) ))); sources.add(RawStringSource::from_static("export const modules = ")); sources.add(render_source.source.clone()); @@ -144,8 +144,8 @@ fn render_chunk( let mut loaded_chunks = HashSet::default(); for (i, (module, entry)) in entries.iter().enumerate() { - let module_id = - ChunkGraph::get_module_id(&compilation.module_ids, *module).expect("should have module id"); + let module_id = ChunkGraph::get_module_id(&compilation.module_ids_artifact, *module) + .expect("should have module id"); let runtime_chunk = compilation .chunk_group_by_ukey .expect_get(entry) diff --git a/crates/rspack_plugin_runtime/src/runtime_module/auto_public_path.rs b/crates/rspack_plugin_runtime/src/runtime_module/auto_public_path.rs index 3eadf917650..985b9d0c4e0 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/auto_public_path.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/auto_public_path.rs @@ -45,14 +45,18 @@ impl RuntimeModule for AutoPublicPathRuntimeModule { let filename = compilation.get_path( &filename, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )), diff --git a/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_startup.rs b/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_startup.rs index e14e69c022b..4b8dfdccbea 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_startup.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/chunk_prefetch_startup.rs @@ -48,7 +48,7 @@ impl RuntimeModule for ChunkPrefetchStartupRuntimeModule { compilation .chunk_by_ukey .expect_get(c) - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) } else { None } @@ -61,7 +61,7 @@ impl RuntimeModule for ChunkPrefetchStartupRuntimeModule { compilation .chunk_by_ukey .expect_get(c) - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) }) .collect_vec(); 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 db090a175ab..4cb23da2395 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 @@ -180,7 +180,7 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { let chunk_id = "\" + chunkId + \""; let chunk_name = stringify_dynamic_chunk_map( |c| { - c.name_for_filename_template(&compilation.chunk_ids) + c.name_for_filename_template(&compilation.chunk_ids_artifact) .map(|s| s.to_string()) }, &chunks, @@ -191,7 +191,7 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { |c| { let hash = c .rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, ) .map(|hash| hash.to_string()); @@ -207,7 +207,7 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { let content_hash = stringify_dynamic_chunk_map( |c| { c.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &self.source_type, compilation.options.output.hash_digest_length, ) @@ -260,35 +260,35 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { let (fake_filename, hash_len_map) = get_filename_without_hash_length(filename_template); let chunk_id = chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .map(|chunk_id| unquoted_stringify(Some(chunk_id), chunk_id.as_str())); let chunk_name = match chunk.name() { Some(chunk_name) => Some(unquoted_stringify( - chunk.id(&compilation.chunk_ids), + chunk.id(&compilation.chunk_ids_artifact), chunk_name, )), None => chunk - .id(&compilation.chunk_ids) + .id(&compilation.chunk_ids_artifact) .map(|chunk_id| unquoted_stringify(Some(chunk_id), chunk_id.as_str())), }; let chunk_hash = chunk .rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, ) .map(|chunk_hash| { - let hash = unquoted_stringify(chunk.id(&compilation.chunk_ids), chunk_hash); + let hash = unquoted_stringify(chunk.id(&compilation.chunk_ids_artifact), chunk_hash); match hash_len_map.get("[chunkhash]") { Some(hash_len) => hash[..*hash_len].to_string(), None => hash, } }); let content_hash = chunk - .content_hash(&compilation.chunk_hashes_results) + .content_hash(&compilation.chunk_hashes_artifact) .and_then(|content_hash| content_hash.get(&self.source_type)) .map(|i| { let hash = unquoted_stringify( - chunk.id(&compilation.chunk_ids), + chunk.id(&compilation.chunk_ids_artifact), i.rendered(compilation.options.output.hash_digest_length), ); match hash_len_map.get("[contenthash]") { @@ -316,7 +316,11 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { .render( PathData::default() .chunk_name_optional(chunk.name()) - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())), + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ), None, )? .as_str(), @@ -332,7 +336,7 @@ impl RuntimeModule for GetChunkFilenameRuntimeModule { ) .always_ok(); - if let Some(chunk_id) = chunk.id(&compilation.chunk_ids) { + if let Some(chunk_id) = chunk.id(&compilation.chunk_ids_artifact) { static_urls .entry(filename) .or_insert(Vec::new()) diff --git a/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_update_filename.rs b/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_update_filename.rs index 3640d2f98d4..968dc989d1a 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_update_filename.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/get_chunk_update_filename.rs @@ -35,12 +35,12 @@ impl RuntimeModule for GetChunkUpdateFilenameRuntimeModule { &FilenameTemplate::from(compilation.options.output.hot_update_chunk_filename.clone()), PathData::default() .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )) diff --git a/crates/rspack_plugin_runtime/src/runtime_module/get_main_filename.rs b/crates/rspack_plugin_runtime/src/runtime_module/get_main_filename.rs index 1e082d1b9a5..c439e5e51db 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/get_main_filename.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/get_main_filename.rs @@ -36,14 +36,18 @@ impl RuntimeModule for GetMainFilenameRuntimeModule { let filename = compilation.get_path( &self.filename, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )) diff --git a/crates/rspack_plugin_runtime/src/runtime_module/startup_chunk_dependencies.rs b/crates/rspack_plugin_runtime/src/runtime_module/startup_chunk_dependencies.rs index f453454435e..34ac85d8b34 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/startup_chunk_dependencies.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/startup_chunk_dependencies.rs @@ -44,7 +44,7 @@ impl RuntimeModule for StartupChunkDependenciesRuntimeModule { compilation .chunk_by_ukey .expect_get(&chunk_ukey) - .expect_id(&compilation.chunk_ids) + .expect_id(&compilation.chunk_ids_artifact) .to_string() }) .collect::>(); diff --git a/crates/rspack_plugin_runtime/src/runtime_module/utils.rs b/crates/rspack_plugin_runtime/src/runtime_module/utils.rs index c8853c1a247..8bbd5c1243a 100644 --- a/crates/rspack_plugin_runtime/src/runtime_module/utils.rs +++ b/crates/rspack_plugin_runtime/src/runtime_module/utils.rs @@ -24,10 +24,10 @@ pub fn get_initial_chunk_ids( .filter(|key| !(chunk_ukey.eq(key) || filter_fn(key, compilation))) .map(|chunk_ukey| { let chunk = compilation.chunk_by_ukey.expect_get(chunk_ukey); - chunk.expect_id(&compilation.chunk_ids).clone() + chunk.expect_id(&compilation.chunk_ids_artifact).clone() }) .collect::>(); - js_chunks.insert(chunk.expect_id(&compilation.chunk_ids).clone()); + js_chunks.insert(chunk.expect_id(&compilation.chunk_ids_artifact).clone()); js_chunks } None => HashSet::default(), @@ -130,14 +130,18 @@ pub fn get_output_dir( let output_dir = compilation.get_path( &filename, PathData::default() - .chunk_id_optional(chunk.id(&compilation.chunk_ids).map(|id| id.as_str())) + .chunk_id_optional( + chunk + .id(&compilation.chunk_ids_artifact) + .map(|id| id.as_str()), + ) .chunk_hash_optional(chunk.rendered_hash( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, compilation.options.output.hash_digest_length, )) - .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids)) + .chunk_name_optional(chunk.name_for_filename_template(&compilation.chunk_ids_artifact)) .content_hash_optional(chunk.rendered_content_hash_by_source_type( - &compilation.chunk_hashes_results, + &compilation.chunk_hashes_artifact, &SourceType::JavaScript, compilation.options.output.hash_digest_length, )), @@ -189,7 +193,7 @@ where for chunk_ukey in chunks.iter() { if let Some(chunk) = chunk_map.get(chunk_ukey) { - if let Some(chunk_id) = chunk.id(&compilation.chunk_ids) { + if let Some(chunk_id) = chunk.id(&compilation.chunk_ids_artifact) { if let Some(value) = f(chunk) { if value.as_str() == chunk_id.as_str() { use_id = true; diff --git a/crates/rspack_plugin_wasm/src/parser_and_generator.rs b/crates/rspack_plugin_wasm/src/parser_and_generator.rs index 195435acf05..1a2f43a5b78 100644 --- a/crates/rspack_plugin_wasm/src/parser_and_generator.rs +++ b/crates/rspack_plugin_wasm/src/parser_and_generator.rs @@ -176,8 +176,11 @@ impl ParserAndGenerator for AsyncWasmParserAndGenerator { let import_var = format!("WEBPACK_IMPORTED_MODULE_{}", itoa!(dep_modules.len())); let val = ( import_var.clone(), - ChunkGraph::get_module_id(&compilation.module_ids, mgm.module_identifier) - .expect("should have module id"), + ChunkGraph::get_module_id( + &compilation.module_ids_artifact, + mgm.module_identifier, + ) + .expect("should have module id"), ); if ModuleGraph::is_async(compilation, &mgm.module_identifier) { diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index e7fd8761182..d4cbfe3fe18 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -2433,6 +2433,7 @@ export type Incremental = { inferAsyncModules?: boolean; providedExports?: boolean; dependenciesDiagnostics?: boolean; + sideEffects?: boolean; buildChunkGraph?: boolean; moduleIds?: boolean; chunkIds?: boolean; @@ -6436,6 +6437,7 @@ export const rspackOptions: z.ZodObject<{ inferAsyncModules: z.ZodOptional; providedExports: z.ZodOptional; dependenciesDiagnostics: z.ZodOptional; + sideEffects: z.ZodOptional; buildChunkGraph: z.ZodOptional; moduleIds: z.ZodOptional; chunkIds: z.ZodOptional; @@ -6451,6 +6453,7 @@ export const rspackOptions: z.ZodObject<{ make?: boolean | undefined; inferAsyncModules?: boolean | undefined; dependenciesDiagnostics?: boolean | undefined; + sideEffects?: boolean | undefined; buildChunkGraph?: boolean | undefined; moduleIds?: boolean | undefined; chunkIds?: boolean | undefined; @@ -6466,6 +6469,7 @@ export const rspackOptions: z.ZodObject<{ make?: boolean | undefined; inferAsyncModules?: boolean | undefined; dependenciesDiagnostics?: boolean | undefined; + sideEffects?: boolean | undefined; buildChunkGraph?: boolean | undefined; moduleIds?: boolean | undefined; chunkIds?: boolean | undefined; @@ -6551,6 +6555,7 @@ export const rspackOptions: z.ZodObject<{ make?: boolean | undefined; inferAsyncModules?: boolean | undefined; dependenciesDiagnostics?: boolean | undefined; + sideEffects?: boolean | undefined; buildChunkGraph?: boolean | undefined; moduleIds?: boolean | undefined; chunkIds?: boolean | undefined; @@ -6616,6 +6621,7 @@ export const rspackOptions: z.ZodObject<{ make?: boolean | undefined; inferAsyncModules?: boolean | undefined; dependenciesDiagnostics?: boolean | undefined; + sideEffects?: boolean | undefined; buildChunkGraph?: boolean | undefined; moduleIds?: boolean | undefined; chunkIds?: boolean | undefined; @@ -7180,6 +7186,7 @@ export const rspackOptions: z.ZodObject<{ }, "strict", z.ZodTypeAny, { usedExports?: boolean | "global" | undefined; providedExports?: boolean | undefined; + sideEffects?: boolean | "flag" | undefined; moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | "size" | "total-size" | undefined; minimize?: boolean | undefined; @@ -7237,7 +7244,6 @@ export const rspackOptions: z.ZodObject<{ removeAvailableModules?: boolean | undefined; removeEmptyChunks?: boolean | undefined; realContentHash?: boolean | undefined; - sideEffects?: boolean | "flag" | undefined; concatenateModules?: boolean | undefined; innerGraph?: boolean | undefined; mangleExports?: boolean | "deterministic" | "size" | undefined; @@ -7246,6 +7252,7 @@ export const rspackOptions: z.ZodObject<{ }, { usedExports?: boolean | "global" | undefined; providedExports?: boolean | undefined; + sideEffects?: boolean | "flag" | undefined; moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | "size" | "total-size" | undefined; minimize?: boolean | undefined; @@ -7303,7 +7310,6 @@ export const rspackOptions: z.ZodObject<{ removeAvailableModules?: boolean | undefined; removeEmptyChunks?: boolean | undefined; realContentHash?: boolean | undefined; - sideEffects?: boolean | "flag" | undefined; concatenateModules?: boolean | undefined; innerGraph?: boolean | undefined; mangleExports?: boolean | "deterministic" | "size" | undefined; @@ -8556,6 +8562,7 @@ export const rspackOptions: z.ZodObject<{ make?: boolean | undefined; inferAsyncModules?: boolean | undefined; dependenciesDiagnostics?: boolean | undefined; + sideEffects?: boolean | undefined; buildChunkGraph?: boolean | undefined; moduleIds?: boolean | undefined; chunkIds?: boolean | undefined; @@ -8869,6 +8876,7 @@ export const rspackOptions: z.ZodObject<{ optimization?: { usedExports?: boolean | "global" | undefined; providedExports?: boolean | undefined; + sideEffects?: boolean | "flag" | undefined; moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | "size" | "total-size" | undefined; minimize?: boolean | undefined; @@ -8926,7 +8934,6 @@ export const rspackOptions: z.ZodObject<{ removeAvailableModules?: boolean | undefined; removeEmptyChunks?: boolean | undefined; realContentHash?: boolean | undefined; - sideEffects?: boolean | "flag" | undefined; concatenateModules?: boolean | undefined; innerGraph?: boolean | undefined; mangleExports?: boolean | "deterministic" | "size" | undefined; @@ -9158,6 +9165,7 @@ export const rspackOptions: z.ZodObject<{ make?: boolean | undefined; inferAsyncModules?: boolean | undefined; dependenciesDiagnostics?: boolean | undefined; + sideEffects?: boolean | undefined; buildChunkGraph?: boolean | undefined; moduleIds?: boolean | undefined; chunkIds?: boolean | undefined; @@ -9471,6 +9479,7 @@ export const rspackOptions: z.ZodObject<{ optimization?: { usedExports?: boolean | "global" | undefined; providedExports?: boolean | undefined; + sideEffects?: boolean | "flag" | undefined; moduleIds?: "named" | "natural" | "deterministic" | undefined; chunkIds?: "named" | "natural" | "deterministic" | "size" | "total-size" | undefined; minimize?: boolean | undefined; @@ -9528,7 +9537,6 @@ export const rspackOptions: z.ZodObject<{ removeAvailableModules?: boolean | undefined; removeEmptyChunks?: boolean | undefined; realContentHash?: boolean | undefined; - sideEffects?: boolean | "flag" | undefined; concatenateModules?: boolean | undefined; innerGraph?: boolean | undefined; mangleExports?: boolean | "deterministic" | "size" | undefined; diff --git a/packages/rspack/src/config/defaults.ts b/packages/rspack/src/config/defaults.ts index dcb46752c18..9eaf761f96e 100644 --- a/packages/rspack/src/config/defaults.ts +++ b/packages/rspack/src/config/defaults.ts @@ -222,6 +222,7 @@ const applyExperimentsDefaults = ( D(experiments.incremental, "inferAsyncModules", false); D(experiments.incremental, "providedExports", false); D(experiments.incremental, "dependenciesDiagnostics", false); + D(experiments.incremental, "sideEffects", false); D(experiments.incremental, "buildChunkGraph", false); D(experiments.incremental, "moduleIds", false); D(experiments.incremental, "chunkIds", false); diff --git a/packages/rspack/src/config/normalization.ts b/packages/rspack/src/config/normalization.ts index 1351f9f233a..c9072f593cf 100644 --- a/packages/rspack/src/config/normalization.ts +++ b/packages/rspack/src/config/normalization.ts @@ -350,9 +350,10 @@ export const getNormalizedRspackOptions = ( options === true ? ({ make: true, - dependenciesDiagnostics: true, inferAsyncModules: true, providedExports: true, + dependenciesDiagnostics: true, + sideEffects: true, buildChunkGraph: true, moduleIds: true, chunkIds: true, diff --git a/packages/rspack/src/config/types.ts b/packages/rspack/src/config/types.ts index 0a1c3c4bb6e..514b9f8a28d 100644 --- a/packages/rspack/src/config/types.ts +++ b/packages/rspack/src/config/types.ts @@ -2495,6 +2495,11 @@ export type Incremental = { */ dependenciesDiagnostics?: boolean; + /** + * Enables incremental side effects optimization. + */ + sideEffects?: boolean; + /** * Enable incremental build chunk graph. */ diff --git a/packages/rspack/src/config/zod.ts b/packages/rspack/src/config/zod.ts index 9099f8ae6e7..5192d30a679 100644 --- a/packages/rspack/src/config/zod.ts +++ b/packages/rspack/src/config/zod.ts @@ -1346,6 +1346,7 @@ const incremental = z.strictObject({ inferAsyncModules: z.boolean().optional(), providedExports: z.boolean().optional(), dependenciesDiagnostics: z.boolean().optional(), + sideEffects: z.boolean().optional(), buildChunkGraph: z.boolean().optional(), moduleIds: z.boolean().optional(), chunkIds: z.boolean().optional(), From 3e89bd00a5daf6c99e7bdfc8233d7c9ef18d2535 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Mon, 23 Dec 2024 20:40:48 +0800 Subject: [PATCH 2/5] wip --- .../src/chunk_graph/chunk_graph_chunk.rs | 4 +- .../src/chunk_graph/chunk_graph_module.rs | 2 +- .../rspack_core/src/compiler/compilation.rs | 5 +- crates/rspack_core/src/context_module.rs | 2 +- .../rspack_core/src/incremental/mutations.rs | 13 +- crates/rspack_core/src/module_graph/mod.rs | 2 +- crates/rspack_ids/src/id_helpers.rs | 4 +- .../rspack_ids/src/named_chunk_ids_plugin.rs | 2 +- .../rspack_ids/src/named_module_ids_plugin.rs | 2 +- .../src/plugin/side_effects_flag_plugin.rs | 117 +++++++++++------- .../tests/__snapshots__/Defaults.test.js.snap | 1 + .../tests/defaultsCases/mode/production.js | 1 + .../tests/defaultsCases/mode/undefined.js | 1 + .../tests/statsAPICases/verbose-time.js | 4 + .../watchCases/side-effects/basic/0/index.js | 7 +- .../DeterministicChunkIdsPlugin.ts | 2 +- .../DeterministicModuleIdsPlugin.ts | 2 +- .../FlagDependencyUsagePlugin.ts | 6 +- .../src/builtin-plugin/MangleExportsPlugin.ts | 6 +- .../ModuleConcatenationPlugin.ts | 14 +-- .../builtin-plugin/NaturalChunkIdsPlugin.ts | 2 +- .../builtin-plugin/NaturalModuleIdsPlugin.ts | 2 +- 22 files changed, 126 insertions(+), 75 deletions(-) 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 174de5f00ce..cdda189fb16 100644 --- a/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs +++ b/crates/rspack_core/src/chunk_graph/chunk_graph_chunk.rs @@ -8,9 +8,7 @@ use hashlink::LinkedHashMap; use indexmap::IndexSet; use itertools::Itertools; use rspack_cacheable::cacheable; -use rspack_collections::{ - DatabaseItem, IdentifierLinkedMap, IdentifierMap, IdentifierSet, UkeyMap, -}; +use rspack_collections::{DatabaseItem, IdentifierLinkedMap, IdentifierMap, IdentifierSet}; use rustc_hash::{FxHashMap as HashMap, FxHashSet}; use serde::{Serialize, Serializer}; diff --git a/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs b/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs index ff3fab49f22..0521ba4b7d6 100644 --- a/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs +++ b/crates/rspack_core/src/chunk_graph/chunk_graph_module.rs @@ -5,7 +5,7 @@ use std::fmt; use std::hash::{Hash, Hasher}; use std::sync::Arc; -use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet}; +use rspack_collections::{IdentifierSet, UkeySet}; use rspack_hash::RspackHashDigest; use rspack_util::ext::DynHash; use rustc_hash::FxHasher; diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 553ee1b59fe..b78ddc54c4b 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -38,7 +38,6 @@ use super::{ use crate::{ build_chunk_graph::build_chunk_graph, cache::Cache, - chunk_graph_chunk::ChunkId, get_runtime_key, incremental::{Incremental, IncrementalPasses, Mutation}, is_source_equal, @@ -46,8 +45,8 @@ use crate::{ to_identifier, AsyncModulesArtifact, BoxDependency, BoxModule, CacheCount, CacheOptions, CgcRuntimeRequirementsArtifact, CgmHashArtifact, CgmRuntimeRequirementsArtifact, Chunk, ChunkByUkey, ChunkContentHash, ChunkGraph, ChunkGroupByUkey, ChunkGroupUkey, ChunkHashesArtifact, - ChunkHashesResult, ChunkIdsArtifact, ChunkKind, ChunkRenderArtifact, ChunkRenderResult, - ChunkUkey, CodeGenerationJob, CodeGenerationResult, CodeGenerationResults, CompilationLogger, + ChunkIdsArtifact, ChunkKind, ChunkRenderArtifact, ChunkRenderResult, ChunkUkey, + CodeGenerationJob, CodeGenerationResult, CodeGenerationResults, CompilationLogger, CompilationLogging, CompilerOptions, DependenciesDiagnosticsArtifact, DependencyId, DependencyType, Entry, EntryData, EntryOptions, EntryRuntime, Entrypoint, ExecuteModuleId, Filename, ImportVarMap, LocalFilenameFn, Logger, ModuleFactory, ModuleGraph, ModuleGraphPartial, diff --git a/crates/rspack_core/src/context_module.rs b/crates/rspack_core/src/context_module.rs index 2e87ab2c864..69e07b54261 100644 --- a/crates/rspack_core/src/context_module.rs +++ b/crates/rspack_core/src/context_module.rs @@ -9,7 +9,7 @@ use rspack_cacheable::{ cacheable, cacheable_dyn, with::{AsOption, AsPreset, AsVec, Unsupported}, }; -use rspack_collections::{Identifiable, Identifier, IdentifierMap}; +use rspack_collections::{Identifiable, Identifier}; use rspack_error::{impl_empty_diagnosable_trait, Diagnostic, Result}; use rspack_macros::impl_source_map_config; use rspack_paths::{ArcPath, Utf8PathBuf}; diff --git a/crates/rspack_core/src/incremental/mutations.rs b/crates/rspack_core/src/incremental/mutations.rs index be50afd1496..916ad279ff7 100644 --- a/crates/rspack_core/src/incremental/mutations.rs +++ b/crates/rspack_core/src/incremental/mutations.rs @@ -10,7 +10,7 @@ use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet}; use rustc_hash::FxHasher; use crate::{ - AffectType, ChunkGraph, ChunkUkey, Compilation, Logger, Module, ModuleGraph, + AffectType, ChunkGraph, ChunkUkey, Compilation, DependencyId, Logger, Module, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, }; @@ -50,6 +50,7 @@ pub enum Mutation { ModuleRemove { module: ModuleIdentifier }, ModuleSetAsync { module: ModuleIdentifier }, ModuleSetId { module: ModuleIdentifier }, + ConnectionUpdateModule { connection: DependencyId }, ChunkSetId { chunk: ChunkUkey }, ChunkAdd { chunk: ChunkUkey }, ChunkSplit { from: ChunkUkey, to: ChunkUkey }, @@ -64,6 +65,9 @@ impl fmt::Display for Mutation { Mutation::ModuleRemove { module } => write!(f, "remove module {}", module), Mutation::ModuleSetAsync { module } => write!(f, "set async module {}", module), Mutation::ModuleSetId { module } => write!(f, "set id module {}", module), + Mutation::ConnectionUpdateModule { connection } => { + write!(f, "update module for connection {}", **connection) + } Mutation::ChunkSetId { chunk } => write!(f, "set id chunk {}", chunk.as_u32()), Mutation::ChunkAdd { chunk } => write!(f, "add chunk {}", chunk.as_u32()), Mutation::ChunkSplit { from, to } => { @@ -166,6 +170,13 @@ impl Mutations { Mutation::ModuleSetAsync { module } => { affected_modules.insert(*module); } + Mutation::ConnectionUpdateModule { connection } => { + if let Some(connection) = module_graph.connection_by_dependency_id(connection) + && let Some(original_module) = connection.original_module_identifier + { + affected_modules.insert(original_module); + } + } Mutation::ModuleSetId { module } => { affected_modules.insert(*module); affected_modules.extend( diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index f3d2c5f9712..d3f5cb912e7 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -945,7 +945,7 @@ impl<'a> ModuleGraph<'a> { pub fn do_update_module(&mut self, dep_id: &DependencyId, module_id: &ModuleIdentifier) { let connection = self .connection_by_dependency_id_mut(dep_id) - .expect("should have connection"); + .unwrap_or_else(|| panic!("{:?}", dep_id)); let old_module_identifier = *connection.module_identifier(); connection.set_module_identifier(*module_id); diff --git a/crates/rspack_ids/src/id_helpers.rs b/crates/rspack_ids/src/id_helpers.rs index cd26c69a167..316b2878a49 100644 --- a/crates/rspack_ids/src/id_helpers.rs +++ b/crates/rspack_ids/src/id_helpers.rs @@ -10,9 +10,9 @@ use itertools::{ Itertools, }; use regex::Regex; -use rspack_collections::{DatabaseItem, IdentifierMap}; +use rspack_collections::DatabaseItem; use rspack_core::{ - compare_runtime, BoxModule, Chunk, ChunkGraph, ChunkUkey, Compilation, ModuleGraph, ModuleId, + compare_runtime, BoxModule, Chunk, ChunkGraph, ChunkUkey, Compilation, ModuleGraph, ModuleIdentifier, ModuleIdsArtifact, }; use rspack_util::itoa; diff --git a/crates/rspack_ids/src/named_chunk_ids_plugin.rs b/crates/rspack_ids/src/named_chunk_ids_plugin.rs index 8d808d34b90..7014f7ecd09 100644 --- a/crates/rspack_ids/src/named_chunk_ids_plugin.rs +++ b/crates/rspack_ids/src/named_chunk_ids_plugin.rs @@ -1,5 +1,5 @@ use rayon::iter::{IntoParallelIterator, ParallelBridge, ParallelIterator}; -use rspack_collections::{DatabaseItem, UkeyIndexSet, UkeyMap, UkeySet}; +use rspack_collections::{DatabaseItem, UkeyIndexSet, UkeySet}; use rspack_core::{ chunk_graph_chunk::ChunkId, incremental::{IncrementalPasses, Mutation, Mutations}, diff --git a/crates/rspack_ids/src/named_module_ids_plugin.rs b/crates/rspack_ids/src/named_module_ids_plugin.rs index 14e6b38bfb6..d616f3dd606 100644 --- a/crates/rspack_ids/src/named_module_ids_plugin.rs +++ b/crates/rspack_ids/src/named_module_ids_plugin.rs @@ -1,5 +1,5 @@ use rayon::iter::{IntoParallelIterator, ParallelBridge, ParallelIterator}; -use rspack_collections::{IdentifierIndexSet, IdentifierMap, IdentifierSet}; +use rspack_collections::{IdentifierIndexSet, IdentifierSet}; use rspack_core::{ incremental::{IncrementalPasses, Mutation, Mutations}, ApplyContext, ChunkGraph, CompilationModuleIds, CompilerOptions, Logger, ModuleGraph, ModuleId, diff --git a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs index 2aef5d11efc..74144f3ef49 100644 --- a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs @@ -6,8 +6,10 @@ use rayon::prelude::*; use rspack_collections::IdentifierIndexSet; use rspack_collections::IdentifierMap; use rspack_collections::IdentifierSet; +use rspack_collections::UkeyMap; use rspack_core::incremental::IncrementalPasses; use rspack_core::incremental::Mutation; +use rspack_core::incremental::Mutations; use rspack_core::DependencyExtraMeta; use rspack_core::DependencyId; use rspack_core::Logger; @@ -668,7 +670,10 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result = module_graph .modules() .par_iter() @@ -683,21 +688,29 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Some(*module), - Mutation::ModuleRemove { module } => { - compilation.side_effects_optimize_artifact.remove(module); - None + side_effects_optimize_artifact.retain(|dependency_id, _| { + module_graph + .connection_by_dependency_id(dependency_id) + .is_some() + }); + + let mut modules: IdentifierSet = mutations.iter().fold( + IdentifierSet::default(), + |mut modules, mutation| match mutation { + Mutation::ModuleBuild { module } => { + modules.extend( + module_graph + .get_outgoing_connections(module) + .map(|connection| *connection.module_identifier()), + ); + modules } - _ => None, - }) - .collect(); + _ => modules, + }, + ); - let module_graph = compilation.get_module_graph(); let mut stack = IdentifierIndexSet::from_iter(modules.iter().copied()); while let Some(module) = stack.pop() { for connection in module_graph.get_incoming_connections(&module) { @@ -729,38 +742,32 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result = module_graph + .flat_map_iter(|module| { + module_graph .get_incoming_connections(module) .filter_map(|connection| { can_optimize_connection(connection, &side_effects_state_map, &module_graph) }) - .collect(); - (*module, do_optimizes) }) .collect(); - let mut do_optimizes: Vec<_> = if compilation + let mut do_optimizes: UkeyMap = if compilation .incremental .can_read_mutations(IncrementalPasses::SIDE_EFFECTS) { - compilation - .side_effects_optimize_artifact - .extend(do_optimizes); - compilation - .side_effects_optimize_artifact - .values() - .flat_map(|v| v) - .cloned() - .collect() + side_effects_optimize_artifact.extend(do_optimizes); + side_effects_optimize_artifact.clone() } else { - do_optimizes.into_values().flat_map(|v| v).collect() + do_optimizes }; + let mut mutations = compilation + .incremental + .can_write_mutations() + .then(Mutations::default); let mut do_optimized_count = 0; while !do_optimizes.is_empty() { do_optimized_count += do_optimizes.len(); @@ -768,7 +775,14 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result = do_optimizes .into_iter() - .map(|do_optimize| do_optimize_connection(do_optimize, &mut module_graph)) + .map(|(dependency, do_optimize)| { + do_optimize_connection( + dependency, + do_optimize, + &mut module_graph, + mutations.as_mut(), + ) + }) .collect(); let module_graph = compilation.get_module_graph(); @@ -782,6 +796,13 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Result, ) -> (DependencyId, ModuleIdentifier) { let SideEffectsDoOptimize { ids, - dependency, target_module, need_move_target, } = do_optimize; module_graph.do_update_module(&dependency, &target_module); + if let Some(mutations) = mutations { + mutations.add(Mutation::ConnectionUpdateModule { + connection: dependency, + }); + } module_graph.set_dependency_extra_meta(dependency, DependencyExtraMeta { ids }); if let Some(SideEffectsDoOptimizeMoveTarget { export_info, @@ -815,7 +842,7 @@ fn can_optimize_connection( connection: &ModuleGraphConnection, side_effects_state_map: &IdentifierMap, module_graph: &ModuleGraph, -) -> Option { +) -> Option<(DependencyId, SideEffectsDoOptimize)> { let original_module = connection.original_module_identifier?; let dependency_id = connection.dependency_id; let dep = module_graph.dependency_by_id(&dependency_id)?; @@ -854,12 +881,14 @@ fn can_optimize_connection( MaybeDynamicTargetExportInfo::Dynamic { .. } => None, }; - return Some(SideEffectsDoOptimize { - ids: processed_ids, - dependency: dependency_id, - target_module: target.module, - need_move_target, - }); + return Some(( + dependency_id, + SideEffectsDoOptimize { + ids: processed_ids, + target_module: target.module, + need_move_target, + }, + )); } if let Some(dep) = dep.downcast_ref::() @@ -888,12 +917,14 @@ fn can_optimize_connection( }) .unwrap_or_else(|| ids[1..].to_vec()); - return Some(SideEffectsDoOptimize { - ids: processed_ids, - dependency: dependency_id, - target_module: target.module, - need_move_target: None, - }); + return Some(( + dependency_id, + SideEffectsDoOptimize { + ids: processed_ids, + target_module: target.module, + need_move_target: None, + }, + )); } None diff --git a/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap index ef0e2e2c85c..9a8c0188a2b 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap @@ -36,6 +36,7 @@ Object { modulesHashes: false, modulesRuntimeRequirements: false, providedExports: false, + sideEffects: false, }, layers: false, lazyCompilation: false, diff --git a/packages/rspack-test-tools/tests/defaultsCases/mode/production.js b/packages/rspack-test-tools/tests/defaultsCases/mode/production.js index fd598b9fd39..02908178300 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/mode/production.js +++ b/packages/rspack-test-tools/tests/defaultsCases/mode/production.js @@ -23,6 +23,7 @@ module.exports = { - "modulesHashes": false, - "modulesRuntimeRequirements": false, - "providedExports": false, + - "sideEffects": false, - }, + "incremental": false, @@ ... @@ diff --git a/packages/rspack-test-tools/tests/defaultsCases/mode/undefined.js b/packages/rspack-test-tools/tests/defaultsCases/mode/undefined.js index 94fa2c86262..0839d785b8a 100644 --- a/packages/rspack-test-tools/tests/defaultsCases/mode/undefined.js +++ b/packages/rspack-test-tools/tests/defaultsCases/mode/undefined.js @@ -23,6 +23,7 @@ module.exports = { - "modulesHashes": false, - "modulesRuntimeRequirements": false, - "providedExports": false, + - "sideEffects": false, - }, + "incremental": false, @@ ... @@ diff --git a/packages/rspack-test-tools/tests/statsAPICases/verbose-time.js b/packages/rspack-test-tools/tests/statsAPICases/verbose-time.js index b28bc634211..42f84adb206 100644 --- a/packages/rspack-test-tools/tests/statsAPICases/verbose-time.js +++ b/packages/rspack-test-tools/tests/statsAPICases/verbose-time.js @@ -59,6 +59,10 @@ module.exports = { LOG from rspack.RemoveEmptyChunksPlugin remove empty chunks: X ms + LOG from rspack.SideEffectsFlagPlugin + optimized 0 dependencies + update dependencies: X ms + LOG from rspack.SplitChunksPlugin prepare module group map: X ms ensure min size fit: X ms diff --git a/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js b/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js index 978b35bc46f..0c134a191a5 100644 --- a/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js +++ b/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js @@ -1,5 +1,10 @@ import {value} from "./module"; -it("should have correct export from re-exports", function () { +it("should have correct export from re-exports", () => { expect(value).toBe("foo"); + + const orphanModules = new Set(__STATS__.modules.filter(m => m.orphan).map(m => m.name)); + expect(orphanModules).toEqual(new Set(["./reexports.js", "./reexports-deep.js"])); + console.log(orphanModules); + // if }); diff --git a/packages/rspack/src/builtin-plugin/DeterministicChunkIdsPlugin.ts b/packages/rspack/src/builtin-plugin/DeterministicChunkIdsPlugin.ts index 94f5128e44a..98b941bbb4a 100644 --- a/packages/rspack/src/builtin-plugin/DeterministicChunkIdsPlugin.ts +++ b/packages/rspack/src/builtin-plugin/DeterministicChunkIdsPlugin.ts @@ -15,7 +15,7 @@ export class DeterministicChunkIdsPlugin extends RspackBuiltinPlugin { if (incremental.chunkIds) { incremental.chunkIds = false; logger.warn( - "`optimization.chunkIds = 'deterministic'` can't be used with `incremental.chunkIds` as deterministic chunk ids is a global effect. `incremental.chunkIds` has been overridden to false." + "`optimization.chunkIds = 'deterministic'` can't be used with `incremental.chunkIds` as deterministic chunk ids is a global effect. `incremental.chunkIds` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, undefined); diff --git a/packages/rspack/src/builtin-plugin/DeterministicModuleIdsPlugin.ts b/packages/rspack/src/builtin-plugin/DeterministicModuleIdsPlugin.ts index a67ac773e47..fb0da3a50c1 100644 --- a/packages/rspack/src/builtin-plugin/DeterministicModuleIdsPlugin.ts +++ b/packages/rspack/src/builtin-plugin/DeterministicModuleIdsPlugin.ts @@ -15,7 +15,7 @@ export class DeterministicModuleIdsPlugin extends RspackBuiltinPlugin { if (incremental.moduleIds) { incremental.moduleIds = false; logger.warn( - "`optimization.moduleIds = 'deterministic'` can't be used with `incremental.moduleIds` as deterministic module ids is a global effect. `incremental.moduleIds` has been overridden to false." + "`optimization.moduleIds = 'deterministic'` can't be used with `incremental.moduleIds` as deterministic module ids is a global effect. `incremental.moduleIds` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, undefined); diff --git a/packages/rspack/src/builtin-plugin/FlagDependencyUsagePlugin.ts b/packages/rspack/src/builtin-plugin/FlagDependencyUsagePlugin.ts index 24046887ce6..6c32dd080cc 100644 --- a/packages/rspack/src/builtin-plugin/FlagDependencyUsagePlugin.ts +++ b/packages/rspack/src/builtin-plugin/FlagDependencyUsagePlugin.ts @@ -19,19 +19,19 @@ export class FlagDependencyUsagePlugin extends RspackBuiltinPlugin { if (incremental.modulesHashes) { incremental.modulesHashes = false; logger.warn( - "`optimization.usedExports` can't be used with `incremental.modulesHashes` as export usage is a global effect. `incremental.modulesHashes` has been overridden to false." + "`optimization.usedExports` can't be used with `incremental.modulesHashes` as export usage is a global effect. `incremental.modulesHashes` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesCodegen) { incremental.modulesCodegen = false; logger.warn( - "`optimization.usedExports` can't be used with `incremental.modulesCodegen` as export usage is a global effect. `incremental.modulesCodegen` has been overridden to false." + "`optimization.usedExports` can't be used with `incremental.modulesCodegen` as export usage is a global effect. `incremental.modulesCodegen` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesRuntimeRequirements) { incremental.modulesRuntimeRequirements = false; logger.warn( - "`optimization.usedExports` can't be used with `incremental.modulesRuntimeRequirements` as export usage is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false." + "`optimization.usedExports` can't be used with `incremental.modulesRuntimeRequirements` as export usage is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, this.global); diff --git a/packages/rspack/src/builtin-plugin/MangleExportsPlugin.ts b/packages/rspack/src/builtin-plugin/MangleExportsPlugin.ts index c683fe93798..eb7a77690bc 100644 --- a/packages/rspack/src/builtin-plugin/MangleExportsPlugin.ts +++ b/packages/rspack/src/builtin-plugin/MangleExportsPlugin.ts @@ -19,19 +19,19 @@ export class MangleExportsPlugin extends RspackBuiltinPlugin { if (incremental.modulesHashes) { incremental.modulesHashes = false; logger.warn( - "`optimization.mangleExports` can't be used with `incremental.modulesHashes` as export mangling is a global effect. `incremental.modulesHashes` has been overridden to false." + "`optimization.mangleExports` can't be used with `incremental.modulesHashes` as export mangling is a global effect. `incremental.modulesHashes` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesCodegen) { incremental.modulesCodegen = false; logger.warn( - "`optimization.mangleExports` can't be used with `incremental.modulesCodegen` as export mangling is a global effect. `incremental.modulesCodegen` has been overridden to false." + "`optimization.mangleExports` can't be used with `incremental.modulesCodegen` as export mangling is a global effect. `incremental.modulesCodegen` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesRuntimeRequirements) { incremental.modulesRuntimeRequirements = false; logger.warn( - "`optimization.mangleExports` can't be used with `incremental.modulesRuntimeRequirements` as export mangling is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false." + "`optimization.mangleExports` can't be used with `incremental.modulesRuntimeRequirements` as export mangling is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, this.deterministic); diff --git a/packages/rspack/src/builtin-plugin/ModuleConcatenationPlugin.ts b/packages/rspack/src/builtin-plugin/ModuleConcatenationPlugin.ts index 4e4c9af2fda..53adafce20d 100644 --- a/packages/rspack/src/builtin-plugin/ModuleConcatenationPlugin.ts +++ b/packages/rspack/src/builtin-plugin/ModuleConcatenationPlugin.ts @@ -16,43 +16,43 @@ export class ModuleConcatenationPlugin extends RspackBuiltinPlugin { if (incremental.moduleIds) { incremental.moduleIds = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.moduleIds` as module concatenation is a global effect. `incremental.moduleIds` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.moduleIds` as module concatenation is a global effect. `incremental.moduleIds` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesHashes) { incremental.modulesHashes = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.modulesHashes` as module concatenation is a global effect. `incremental.modulesHashes` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.modulesHashes` as module concatenation is a global effect. `incremental.modulesHashes` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesCodegen) { incremental.modulesCodegen = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.modulesCodegen` as module concatenation is a global effect. `incremental.modulesCodegen` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.modulesCodegen` as module concatenation is a global effect. `incremental.modulesCodegen` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.modulesRuntimeRequirements) { incremental.modulesRuntimeRequirements = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.modulesRuntimeRequirements` as module concatenation is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.modulesRuntimeRequirements` as module concatenation is a global effect. `incremental.modulesRuntimeRequirements` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.chunksRuntimeRequirements) { incremental.chunksRuntimeRequirements = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.chunksRuntimeRequirements` as module concatenation is a global effect. `incremental.chunksRuntimeRequirements` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.chunksRuntimeRequirements` as module concatenation is a global effect. `incremental.chunksRuntimeRequirements` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.chunksHashes) { incremental.chunksHashes = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.chunksHashes` as module concatenation is a global effect. `incremental.chunksHashes` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.chunksHashes` as module concatenation is a global effect. `incremental.chunksHashes` has been overridden to false. We recommend enabling incremental only in the development mode." ); } if (incremental.chunksRender) { incremental.chunksRender = false; logger.warn( - "`optimization.concatenateModules` can't be used with `incremental.chunksRender` as module concatenation is a global effect. `incremental.chunksRender` has been overridden to false." + "`optimization.concatenateModules` can't be used with `incremental.chunksRender` as module concatenation is a global effect. `incremental.chunksRender` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, undefined); diff --git a/packages/rspack/src/builtin-plugin/NaturalChunkIdsPlugin.ts b/packages/rspack/src/builtin-plugin/NaturalChunkIdsPlugin.ts index b59b4e7c33a..17615c81419 100644 --- a/packages/rspack/src/builtin-plugin/NaturalChunkIdsPlugin.ts +++ b/packages/rspack/src/builtin-plugin/NaturalChunkIdsPlugin.ts @@ -15,7 +15,7 @@ export class NaturalChunkIdsPlugin extends RspackBuiltinPlugin { if (incremental.moduleIds) { incremental.moduleIds = false; logger.warn( - "`optimization.chunkIds = 'natural'` can't be used with `incremental.chunkIds` as natural chunk ids is a global effect. `incremental.chunkIds` has been overridden to false." + "`optimization.chunkIds = 'natural'` can't be used with `incremental.chunkIds` as natural chunk ids is a global effect. `incremental.chunkIds` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, undefined); diff --git a/packages/rspack/src/builtin-plugin/NaturalModuleIdsPlugin.ts b/packages/rspack/src/builtin-plugin/NaturalModuleIdsPlugin.ts index 4ddfcad98d6..4564ff5aac1 100644 --- a/packages/rspack/src/builtin-plugin/NaturalModuleIdsPlugin.ts +++ b/packages/rspack/src/builtin-plugin/NaturalModuleIdsPlugin.ts @@ -15,7 +15,7 @@ export class NaturalModuleIdsPlugin extends RspackBuiltinPlugin { if (incremental.moduleIds) { incremental.moduleIds = false; logger.warn( - "`optimization.moduleIds = 'natural'` can't be used with `incremental.moduleIds` as natural module ids is a global effect. `incremental.moduleIds` has been overridden to false." + "`optimization.moduleIds = 'natural'` can't be used with `incremental.moduleIds` as natural module ids is a global effect. `incremental.moduleIds` has been overridden to false. We recommend enabling incremental only in the development mode." ); } return createBuiltinPlugin(this.name, undefined); From 456b6d52fae99e92038b06933b1c4e7d49bd02d6 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Tue, 24 Dec 2024 15:11:47 +0800 Subject: [PATCH 3/5] fix --- .../rspack_core/src/incremental/mutations.rs | 13 +-------- .../src/plugin/side_effects_flag_plugin.rs | 29 +++++-------------- .../watchCases/side-effects/basic/0/index.js | 5 ---- .../side-effects/two-to-one/0/index.js | 7 +++++ .../side-effects/two-to-one/2/module.js | 2 ++ 5 files changed, 17 insertions(+), 39 deletions(-) create mode 100644 packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/2/module.js diff --git a/crates/rspack_core/src/incremental/mutations.rs b/crates/rspack_core/src/incremental/mutations.rs index 916ad279ff7..be50afd1496 100644 --- a/crates/rspack_core/src/incremental/mutations.rs +++ b/crates/rspack_core/src/incremental/mutations.rs @@ -10,7 +10,7 @@ use rspack_collections::{IdentifierMap, IdentifierSet, UkeySet}; use rustc_hash::FxHasher; use crate::{ - AffectType, ChunkGraph, ChunkUkey, Compilation, DependencyId, Logger, Module, ModuleGraph, + AffectType, ChunkGraph, ChunkUkey, Compilation, Logger, Module, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, }; @@ -50,7 +50,6 @@ pub enum Mutation { ModuleRemove { module: ModuleIdentifier }, ModuleSetAsync { module: ModuleIdentifier }, ModuleSetId { module: ModuleIdentifier }, - ConnectionUpdateModule { connection: DependencyId }, ChunkSetId { chunk: ChunkUkey }, ChunkAdd { chunk: ChunkUkey }, ChunkSplit { from: ChunkUkey, to: ChunkUkey }, @@ -65,9 +64,6 @@ impl fmt::Display for Mutation { Mutation::ModuleRemove { module } => write!(f, "remove module {}", module), Mutation::ModuleSetAsync { module } => write!(f, "set async module {}", module), Mutation::ModuleSetId { module } => write!(f, "set id module {}", module), - Mutation::ConnectionUpdateModule { connection } => { - write!(f, "update module for connection {}", **connection) - } Mutation::ChunkSetId { chunk } => write!(f, "set id chunk {}", chunk.as_u32()), Mutation::ChunkAdd { chunk } => write!(f, "add chunk {}", chunk.as_u32()), Mutation::ChunkSplit { from, to } => { @@ -170,13 +166,6 @@ impl Mutations { Mutation::ModuleSetAsync { module } => { affected_modules.insert(*module); } - Mutation::ConnectionUpdateModule { connection } => { - if let Some(connection) = module_graph.connection_by_dependency_id(connection) - && let Some(original_module) = connection.original_module_identifier - { - affected_modules.insert(original_module); - } - } Mutation::ModuleSetId { module } => { affected_modules.insert(*module); affected_modules.extend( diff --git a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs index 74144f3ef49..be554e29c13 100644 --- a/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/side_effects_flag_plugin.rs @@ -9,7 +9,6 @@ use rspack_collections::IdentifierSet; use rspack_collections::UkeyMap; use rspack_core::incremental::IncrementalPasses; use rspack_core::incremental::Mutation; -use rspack_core::incremental::Mutations; use rspack_core::DependencyExtraMeta; use rspack_core::DependencyId; use rspack_core::Logger; @@ -707,6 +706,12 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result { + for connection in module_graph.get_incoming_connections(module) { + side_effects_optimize_artifact.remove(&connection.dependency_id); + } + modules + } _ => modules, }, ); @@ -764,10 +769,6 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result Result = do_optimizes .into_iter() .map(|(dependency, do_optimize)| { - do_optimize_connection( - dependency, - do_optimize, - &mut module_graph, - mutations.as_mut(), - ) + do_optimize_connection(dependency, do_optimize, &mut module_graph) }) .collect(); @@ -797,11 +793,6 @@ fn optimize_dependencies(&self, compilation: &mut Compilation) -> Result, ) -> (DependencyId, ModuleIdentifier) { let SideEffectsDoOptimize { ids, @@ -821,11 +811,6 @@ fn do_optimize_connection( need_move_target, } = do_optimize; module_graph.do_update_module(&dependency, &target_module); - if let Some(mutations) = mutations { - mutations.add(Mutation::ConnectionUpdateModule { - connection: dependency, - }); - } module_graph.set_dependency_extra_meta(dependency, DependencyExtraMeta { ids }); if let Some(SideEffectsDoOptimizeMoveTarget { export_info, diff --git a/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js b/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js index 0c134a191a5..4004d4326aa 100644 --- a/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js +++ b/packages/rspack-test-tools/tests/watchCases/side-effects/basic/0/index.js @@ -2,9 +2,4 @@ import {value} from "./module"; it("should have correct export from re-exports", () => { expect(value).toBe("foo"); - - const orphanModules = new Set(__STATS__.modules.filter(m => m.orphan).map(m => m.name)); - expect(orphanModules).toEqual(new Set(["./reexports.js", "./reexports-deep.js"])); - console.log(orphanModules); - // if }); diff --git a/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js b/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js index 978b35bc46f..d5cd30c711c 100644 --- a/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js +++ b/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js @@ -2,4 +2,11 @@ import {value} from "./module"; it("should have correct export from re-exports", function () { expect(value).toBe("foo"); + + const orphanModules = new Set(__STATS__.modules.filter(m => m.orphan).map(m => m.name)); + if (WATCH_STEP === '1') { + expect(orphanModules).toEqual(new Set(["./module.js", "./reexports.js", "./reexports-deep.js"])); + } else { + expect(orphanModules).toEqual(new Set(["./reexports.js", "./reexports-deep.js"])); + } }); diff --git a/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/2/module.js b/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/2/module.js new file mode 100644 index 00000000000..a5970107246 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/2/module.js @@ -0,0 +1,2 @@ +import {v} from './reexports' +export const value = '' + v; From d95adb83bd037339da3109dc78da2879db17b1e9 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Tue, 24 Dec 2024 15:14:21 +0800 Subject: [PATCH 4/5] fix --- .../tests/watchCases/side-effects/two-to-one/0/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js b/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js index d5cd30c711c..83fc9bfee05 100644 --- a/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js +++ b/packages/rspack-test-tools/tests/watchCases/side-effects/two-to-one/0/index.js @@ -1,6 +1,6 @@ import {value} from "./module"; -it("should have correct export from re-exports", function () { +it("should have correct export from re-exports", () => { expect(value).toBe("foo"); const orphanModules = new Set(__STATS__.modules.filter(m => m.orphan).map(m => m.name)); From 11003c6fb50d58a4fe0da11ad17a0cc2d357ec0c Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Tue, 24 Dec 2024 15:25:22 +0800 Subject: [PATCH 5/5] fix --- .gitignore | 2 +- .../src/artifacts/cgm_hash_artifact.rs | 28 ++ .../cgm_runtime_requirement_artifact.rs | 33 ++ .../src/artifacts/code_generation_results.rs | 343 ++++++++++++++++++ crates/rspack_core/src/artifacts/mod.rs | 25 ++ .../side_effects_do_optimize_artifact.rs | 19 + 6 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 crates/rspack_core/src/artifacts/cgm_hash_artifact.rs create mode 100644 crates/rspack_core/src/artifacts/cgm_runtime_requirement_artifact.rs create mode 100644 crates/rspack_core/src/artifacts/code_generation_results.rs create mode 100644 crates/rspack_core/src/artifacts/mod.rs create mode 100644 crates/rspack_core/src/artifacts/side_effects_do_optimize_artifact.rs diff --git a/.gitignore b/.gitignore index d6d457a334d..b0c5fbecbfa 100644 --- a/.gitignore +++ b/.gitignore @@ -190,7 +190,7 @@ esbuild.cpuprofile !packages/rspack-test-tools/tests/treeShakingCases/node_modules # Binding artifacts -artifacts +/artifacts/ # Node Addons npm/**/*.node diff --git a/crates/rspack_core/src/artifacts/cgm_hash_artifact.rs b/crates/rspack_core/src/artifacts/cgm_hash_artifact.rs new file mode 100644 index 00000000000..5b010dde7a6 --- /dev/null +++ b/crates/rspack_core/src/artifacts/cgm_hash_artifact.rs @@ -0,0 +1,28 @@ +use rspack_collections::IdentifierMap; +use rspack_hash::RspackHashDigest; + +use crate::{ModuleIdentifier, RuntimeSpec, RuntimeSpecMap}; + +#[derive(Debug, Default)] +pub struct CgmHashArtifact { + module_to_hashes: IdentifierMap>, +} + +impl CgmHashArtifact { + pub fn is_empty(&self) -> bool { + self.module_to_hashes.is_empty() + } + + pub fn get(&self, module: &ModuleIdentifier, runtime: &RuntimeSpec) -> Option<&RspackHashDigest> { + let hashes = self.module_to_hashes.get(module)?; + hashes.get(runtime) + } + + pub fn set_hashes(&mut self, module: ModuleIdentifier, hashes: RuntimeSpecMap) { + self.module_to_hashes.insert(module, hashes); + } + + pub fn remove(&mut self, module: &ModuleIdentifier) -> Option> { + self.module_to_hashes.remove(module) + } +} diff --git a/crates/rspack_core/src/artifacts/cgm_runtime_requirement_artifact.rs b/crates/rspack_core/src/artifacts/cgm_runtime_requirement_artifact.rs new file mode 100644 index 00000000000..01bf7806494 --- /dev/null +++ b/crates/rspack_core/src/artifacts/cgm_runtime_requirement_artifact.rs @@ -0,0 +1,33 @@ +use rspack_collections::IdentifierMap; + +use crate::{ModuleIdentifier, RuntimeGlobals, RuntimeSpec, RuntimeSpecMap}; + +#[derive(Debug, Default)] +pub struct CgmRuntimeRequirementsArtifact { + module_to_runtime_requirements: IdentifierMap>, +} + +impl CgmRuntimeRequirementsArtifact { + pub fn is_empty(&self) -> bool { + self.module_to_runtime_requirements.is_empty() + } + + pub fn get(&self, module: &ModuleIdentifier, runtime: &RuntimeSpec) -> Option<&RuntimeGlobals> { + let requirements = self.module_to_runtime_requirements.get(module)?; + requirements.get(runtime) + } + + pub fn set_runtime_requirements( + &mut self, + module: ModuleIdentifier, + runtime_requirements_map: RuntimeSpecMap, + ) { + self + .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/artifacts/code_generation_results.rs b/crates/rspack_core/src/artifacts/code_generation_results.rs new file mode 100644 index 00000000000..68ef01bd572 --- /dev/null +++ b/crates/rspack_core/src/artifacts/code_generation_results.rs @@ -0,0 +1,343 @@ +use std::collections::hash_map::Entry; +use std::hash::Hash; +use std::ops::{Deref, DerefMut}; +use std::sync::atomic::AtomicU32; + +use anymap::CloneAny; +use rspack_collections::IdentifierMap; +use rspack_hash::{HashDigest, HashFunction, HashSalt, RspackHash, RspackHashDigest}; +use rspack_sources::BoxSource; +use rspack_util::atom::Atom; +use rustc_hash::{FxHashMap as HashMap, FxHashSet}; +use serde::Serialize; + +use crate::{ + AssetInfo, ChunkInitFragments, ConcatenationScope, ModuleIdentifier, PublicPath, RuntimeGlobals, + RuntimeMode, RuntimeSpec, RuntimeSpecMap, SourceType, +}; + +#[derive(Clone, Debug)] +pub struct CodeGenerationDataUrl { + inner: String, +} + +impl CodeGenerationDataUrl { + pub fn new(inner: String) -> Self { + Self { inner } + } + + pub fn inner(&self) -> &str { + &self.inner + } +} + +#[derive(Clone, Debug)] +pub struct CodeGenerationDataFilename { + filename: String, + public_path: PublicPath, +} + +impl CodeGenerationDataFilename { + pub fn new(filename: String, public_path: PublicPath) -> Self { + Self { + filename, + public_path, + } + } + + pub fn filename(&self) -> &str { + &self.filename + } + + pub fn public_path(&self) -> &PublicPath { + &self.public_path + } +} + +#[derive(Clone, Debug)] +pub struct CodeGenerationDataAssetInfo { + inner: AssetInfo, +} + +impl CodeGenerationDataAssetInfo { + pub fn new(inner: AssetInfo) -> Self { + Self { inner } + } + + pub fn inner(&self) -> &AssetInfo { + &self.inner + } +} + +#[derive(Clone, Debug)] +pub struct CodeGenerationDataTopLevelDeclarations { + inner: FxHashSet, +} + +impl CodeGenerationDataTopLevelDeclarations { + pub fn new(inner: FxHashSet) -> Self { + Self { inner } + } + + pub fn inner(&self) -> &FxHashSet { + &self.inner + } +} + +#[derive(Clone, Debug)] +pub struct CodeGenerationExportsFinalNames { + inner: HashMap, +} + +impl CodeGenerationExportsFinalNames { + pub fn new(inner: HashMap) -> Self { + Self { inner } + } + + pub fn inner(&self) -> &HashMap { + &self.inner + } +} + +#[derive(Debug, Default, Clone)] +pub struct CodeGenerationData { + inner: anymap::Map, +} + +impl Deref for CodeGenerationData { + type Target = anymap::Map; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl DerefMut for CodeGenerationData { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.inner + } +} + +#[derive(Debug, Default, Clone)] +pub struct CodeGenerationResult { + pub inner: HashMap, + /// [definition in webpack](https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/Module.js#L75) + pub data: CodeGenerationData, + pub chunk_init_fragments: ChunkInitFragments, + pub runtime_requirements: RuntimeGlobals, + pub hash: Option, + pub id: CodeGenResultId, + pub concatenation_scope: Option, +} + +impl CodeGenerationResult { + pub fn with_javascript(mut self, generation_result: BoxSource) -> Self { + self.inner.insert(SourceType::JavaScript, generation_result); + self + } + + pub fn with_css(mut self, generation_result: BoxSource) -> Self { + self.inner.insert(SourceType::Css, generation_result); + self + } + + pub fn with_asset(mut self, generation_result: BoxSource) -> Self { + self.inner.insert(SourceType::Asset, generation_result); + self + } + + pub fn inner(&self) -> &HashMap { + &self.inner + } + + pub fn get(&self, source_type: &SourceType) -> Option<&BoxSource> { + self.inner.get(source_type) + } + + pub fn add(&mut self, source_type: SourceType, generation_result: BoxSource) { + let result = self.inner.insert(source_type, generation_result); + debug_assert!(result.is_none()); + } + + pub fn set_hash( + &mut self, + hash_function: &HashFunction, + hash_digest: &HashDigest, + hash_salt: &HashSalt, + ) { + let mut hasher = RspackHash::with_salt(hash_function, hash_salt); + for (source_type, source) in &self.inner { + source_type.hash(&mut hasher); + source.hash(&mut hasher); + } + self.chunk_init_fragments.hash(&mut hasher); + self.runtime_requirements.hash(&mut hasher); + self.hash = Some(hasher.digest(hash_digest)); + } +} + +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)] +pub struct CodeGenResultId(u32); + +impl Default for CodeGenResultId { + fn default() -> Self { + Self(CODE_GEN_RESULT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) + } +} + +pub static CODE_GEN_RESULT_ID: AtomicU32 = AtomicU32::new(0); + +#[derive(Debug, Default, Clone)] +pub struct CodeGenerationResults { + module_generation_result_map: HashMap, + map: IdentifierMap>, +} + +impl CodeGenerationResults { + pub fn is_empty(&self) -> bool { + self.module_generation_result_map.is_empty() && self.map.is_empty() + } + + pub fn get_one(&self, module_identifier: &ModuleIdentifier) -> Option<&CodeGenerationResult> { + self + .map + .get(module_identifier) + .and_then(|spec| match spec.mode { + RuntimeMode::Empty => None, + RuntimeMode::SingleEntry => spec + .single_value + .and_then(|result_id| self.module_generation_result_map.get(&result_id)), + RuntimeMode::Map => spec + .map + .values() + .next() + .and_then(|result_id| self.module_generation_result_map.get(result_id)), + }) + } + + 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() { + self.module_generation_result_map.remove(result)?; + } + Some(()) + } + + pub fn get( + &self, + module_identifier: &ModuleIdentifier, + runtime: Option<&RuntimeSpec>, + ) -> &CodeGenerationResult { + if let Some(entry) = self.map.get(module_identifier) { + if let Some(runtime) = runtime { + entry + .get(runtime) + .and_then(|m| { + self.module_generation_result_map.get(m) + }) + .unwrap_or_else(|| { + panic!( + "Failed to code generation result for {module_identifier} with runtime {runtime:?} \n {entry:?}" + ) + }) + } else { + if entry.size() > 1 { + let results = entry.get_values(); + if results.len() != 1 { + panic!( + "No unique code generation entry for unspecified runtime for {module_identifier} ", + ); + } + + return results + .first() + .copied() + .and_then(|m| self.module_generation_result_map.get(m)) + .unwrap_or_else(|| panic!("Expected value exists")); + } + + entry + .get_values() + .first() + .copied() + .and_then(|m| self.module_generation_result_map.get(m)) + .unwrap_or_else(|| panic!("Expected value exists")) + } + } else { + panic!( + "No code generation entry for {} (existing entries: {:?})", + module_identifier, + self.map.keys().collect::>() + ) + } + } + + pub fn add( + &mut self, + module_identifier: ModuleIdentifier, + runtime: RuntimeSpec, + result: CodeGenResultId, + ) { + match self.map.entry(module_identifier) { + Entry::Occupied(mut record) => { + record.get_mut().set(runtime, result); + } + Entry::Vacant(record) => { + let mut spec_map = RuntimeSpecMap::default(); + spec_map.set(runtime, result); + record.insert(spec_map); + } + }; + } + + pub fn get_runtime_requirements( + &self, + module_identifier: &ModuleIdentifier, + runtime: Option<&RuntimeSpec>, + ) -> RuntimeGlobals { + self.get(module_identifier, runtime).runtime_requirements + } + + #[allow(clippy::unwrap_in_result)] + pub fn get_hash( + &self, + module_identifier: &ModuleIdentifier, + runtime: Option<&RuntimeSpec>, + ) -> Option<&RspackHashDigest> { + let code_generation_result = self.get(module_identifier, runtime); + + code_generation_result.hash.as_ref() + } + + pub fn into_inner( + self, + ) -> ( + IdentifierMap>, + HashMap, + ) { + (self.map, self.module_generation_result_map) + } +} + +#[derive(Debug)] +pub struct CodeGenerationJob { + pub module: ModuleIdentifier, + pub hash: RspackHashDigest, + pub runtime: RuntimeSpec, + pub runtimes: Vec, +} diff --git a/crates/rspack_core/src/artifacts/mod.rs b/crates/rspack_core/src/artifacts/mod.rs new file mode 100644 index 00000000000..487e11ed0fe --- /dev/null +++ b/crates/rspack_core/src/artifacts/mod.rs @@ -0,0 +1,25 @@ +use rspack_collections::{IdentifierMap, IdentifierSet, UkeyMap}; +use rspack_error::Diagnostic; + +use crate::{ + chunk_graph_chunk::ChunkId, ChunkHashesResult, ChunkRenderResult, ChunkUkey, ModuleId, + RuntimeGlobals, +}; + +mod cgm_hash_artifact; +mod cgm_runtime_requirement_artifact; +mod code_generation_results; +mod side_effects_do_optimize_artifact; + +pub use cgm_hash_artifact::*; +pub use cgm_runtime_requirement_artifact::*; +pub use code_generation_results::*; +pub use side_effects_do_optimize_artifact::*; + +pub type AsyncModulesArtifact = IdentifierSet; +pub type DependenciesDiagnosticsArtifact = IdentifierMap>; +pub type ModuleIdsArtifact = IdentifierMap; +pub type ChunkIdsArtifact = UkeyMap; +pub type CgcRuntimeRequirementsArtifact = UkeyMap; +pub type ChunkHashesArtifact = UkeyMap; +pub type ChunkRenderArtifact = UkeyMap; diff --git a/crates/rspack_core/src/artifacts/side_effects_do_optimize_artifact.rs b/crates/rspack_core/src/artifacts/side_effects_do_optimize_artifact.rs new file mode 100644 index 00000000000..af7c0d82735 --- /dev/null +++ b/crates/rspack_core/src/artifacts/side_effects_do_optimize_artifact.rs @@ -0,0 +1,19 @@ +use rspack_collections::UkeyMap; +use rspack_util::atom::Atom; + +use crate::{DependencyId, ExportInfo, ModuleIdentifier}; + +#[derive(Debug, Clone)] +pub struct SideEffectsDoOptimize { + pub ids: Vec, + pub target_module: ModuleIdentifier, + pub need_move_target: Option, +} + +#[derive(Debug, Clone)] +pub struct SideEffectsDoOptimizeMoveTarget { + pub export_info: ExportInfo, + pub target_export: Option>, +} + +pub type SideEffectsOptimizeArtifact = UkeyMap;