From b2cf6b645fe81c30c6a574ffc3265ddfc3d7b8d3 Mon Sep 17 00:00:00 2001 From: jerrykingxyz Date: Tue, 23 Apr 2024 17:45:16 +0800 Subject: [PATCH] refactor: remove is_new_treeshaking in module_graph --- .../rspack_core/src/compiler/compilation.rs | 5 ++-- crates/rspack_core/src/compiler/make/mod.rs | 2 +- .../src/compiler/make/tasks/add.rs | 13 ++++++++- crates/rspack_core/src/module_graph/mod.rs | 29 ++----------------- .../rspack_plugin_javascript/src/runtime.rs | 3 +- 5 files changed, 19 insertions(+), 33 deletions(-) diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 158a72f44fa..3200ef304fb 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -219,13 +219,12 @@ impl Compilation { cache: Arc, module_executor: Option, ) -> Self { - let make_module_graph = ModuleGraphPartial::new(options.is_new_tree_shaking()); Self { id: CompilationId::new(), hot_index: 0, records, options, - make_module_graph, + make_module_graph: Default::default(), other_module_graph: None, dependency_factories: Default::default(), make_failed_dependencies: HashSet::default(), @@ -891,7 +890,7 @@ impl Compilation { #[instrument(name = "compilation:seal", skip_all)] pub async fn seal(&mut self, plugin_driver: SharedPluginDriver) -> Result<()> { - self.other_module_graph = Some(ModuleGraphPartial::new(self.options.is_new_tree_shaking())); + self.other_module_graph = Some(ModuleGraphPartial::default()); let logger = self.get_logger("rspack.Compilation"); // https://github.com/webpack/webpack/blob/main/lib/Compilation.js#L2809 diff --git a/crates/rspack_core/src/compiler/make/mod.rs b/crates/rspack_core/src/compiler/make/mod.rs index 96ce30e13b7..d9e6647d3de 100644 --- a/crates/rspack_core/src/compiler/make/mod.rs +++ b/crates/rspack_core/src/compiler/make/mod.rs @@ -138,7 +138,7 @@ impl UpdateModuleGraph { }) .collect::>(); - let mut make_module_graph = ModuleGraphPartial::new(true); + let mut make_module_graph = ModuleGraphPartial::default(); compilation.swap_make_module_graph(&mut make_module_graph); let mut ctx = MakeTaskContext::new(compilation, make_module_graph); let res = run_task_loop(&mut ctx, init_tasks); diff --git a/crates/rspack_core/src/compiler/make/tasks/add.rs b/crates/rspack_core/src/compiler/make/tasks/add.rs index d58ff830b16..756d5b8f91d 100644 --- a/crates/rspack_core/src/compiler/make/tasks/add.rs +++ b/crates/rspack_core/src/compiler/make/tasks/add.rs @@ -27,6 +27,7 @@ impl Task for AddTask { } let module_identifier = self.module.identifier(); + let is_new_treeshaking = context.compiler_options.is_new_tree_shaking(); let module_graph = &mut MakeTaskContext::get_module_graph(&mut context.module_graph_partial); if self.module.as_self_module().is_some() { @@ -41,6 +42,7 @@ impl Task for AddTask { self.original_module_identifier, self.dependencies, *issuer, + is_new_treeshaking, )?; // reused module @@ -56,6 +58,7 @@ impl Task for AddTask { self.original_module_identifier, self.dependencies, module_identifier, + is_new_treeshaking, )?; // reused module @@ -69,6 +72,7 @@ impl Task for AddTask { self.original_module_identifier, self.dependencies, module_identifier, + is_new_treeshaking, )?; if self.is_entry { @@ -97,9 +101,16 @@ fn set_resolved_module( original_module_identifier: Option, dependencies: Vec, module_identifier: ModuleIdentifier, + // TODO: removed when new treeshaking is stable + is_new_treeshaking: bool, ) -> Result<()> { for dependency in dependencies { - module_graph.set_resolved_module(original_module_identifier, dependency, module_identifier)?; + module_graph.set_resolved_module( + original_module_identifier, + dependency, + module_identifier, + is_new_treeshaking, + )?; } Ok(()) } diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index b60e04aa1dd..45521d3ed35 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -41,9 +41,6 @@ pub struct DependencyParents { #[derive(Debug, Default)] pub struct ModuleGraphPartial { - // TODO: removed when new treeshaking is stable - is_new_treeshaking: bool, - dependency_id_to_module_identifier: HashMap>, /// Module identifier to its module @@ -71,16 +68,6 @@ pub struct ModuleGraphPartial { dep_meta_map: HashMap, } -impl ModuleGraphPartial { - // TODO remove and use default() after new_treeshaking stable - pub fn new(is_new_treeshaking: bool) -> Self { - Self { - is_new_treeshaking, - ..Default::default() - } - } -} - #[derive(Debug, Default)] pub struct ModuleGraph<'a> { partials: Vec<&'a ModuleGraphPartial>, @@ -138,18 +125,6 @@ impl<'a> ModuleGraph<'a> { f_mut(active_partial) } - // TODO: removed when new treeshaking is stable - pub fn is_new_treeshaking(&self) -> bool { - if let Some(active) = &self.active { - return active.is_new_treeshaking; - } - if let Some(p) = self.partials.last() { - p.is_new_treeshaking - } else { - panic!("can not get any partial module graph") - } - } - /// Return an unordered iterator of modules pub fn modules(&self) -> IdentifierMap<&BoxModule> { let mut res = IdentifierMap::default(); @@ -705,13 +680,15 @@ impl<'a> ModuleGraph<'a> { original_module_identifier: Option, dependency_id: DependencyId, module_identifier: ModuleIdentifier, + // TODO: removed when new treeshaking is stable + is_new_treeshaking: bool, ) -> Result<()> { let dependency = self .dependency_by_id(&dependency_id) .expect("should have dependency"); let is_module_dependency = dependency.as_module_dependency().is_some() || dependency.as_context_dependency().is_some(); - let condition = if self.is_new_treeshaking() { + let condition = if is_new_treeshaking { dependency .as_module_dependency() .and_then(|dep| dep.get_condition()) diff --git a/crates/rspack_plugin_javascript/src/runtime.rs b/crates/rspack_plugin_javascript/src/runtime.rs index 21574a184c4..1c49d68ec65 100644 --- a/crates/rspack_plugin_javascript/src/runtime.rs +++ b/crates/rspack_plugin_javascript/src/runtime.rs @@ -28,8 +28,7 @@ pub fn render_chunk_modules( let mut module_code_array = ordered_modules .par_iter() .filter(|module| { - compilation.get_module_graph().is_new_treeshaking() - || include_module_ids.contains(&module.identifier()) + compilation.options.is_new_tree_shaking() || include_module_ids.contains(&module.identifier()) }) .filter_map(|module| { let code_gen_result = compilation