From 70f16d62336f5be555dfdac12870acd247e4c3f5 Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Thu, 21 Sep 2023 00:18:02 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20lint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rspack/tests/fixtures.rs | 3 +- crates/rspack_core/src/chunk_graph/mod.rs | 2 +- .../rspack_core/src/compiler/compilation.rs | 20 ++-- crates/rspack_core/src/lib.rs | 6 ++ crates/rspack_core/src/module_graph/mod.rs | 15 ++- crates/rspack_core/src/module_graph_module.rs | 93 ++++++++++--------- ...ny_export_imported_specifier_dependency.rs | 4 +- .../harmony_export_specifier_dependency.rs | 2 +- .../esm/harmony_import_dependency.rs | 9 +- .../harmony_import_specifier_dependency.rs | 30 ++---- .../plugin/flag_dependency_usage_plugin.rs | 18 ++-- 11 files changed, 101 insertions(+), 101 deletions(-) diff --git a/crates/rspack/tests/fixtures.rs b/crates/rspack/tests/fixtures.rs index 10297c6b27a3..71c691a94d95 100644 --- a/crates/rspack/tests/fixtures.rs +++ b/crates/rspack/tests/fixtures.rs @@ -22,9 +22,10 @@ fn samples(fixture_path: PathBuf) { #[fixture("tests/tree-shaking/*", exclude("node_modules"))] fn tree_shaking(fixture_path: PathBuf) { + std::env::set_var("IS_NEW_TREESHAKING", "1"); // For each test case // First test is old version tree shaking snapshot test - // test_fixture(&fixture_path, Box::new(|_, _| {}), None); + test_fixture(&fixture_path, Box::new(|_, _| {}), None); // second test is webpack based tree shaking test_fixture( &fixture_path, diff --git a/crates/rspack_core/src/chunk_graph/mod.rs b/crates/rspack_core/src/chunk_graph/mod.rs index 156c0f1b6ea3..3d7f9c7fdebd 100644 --- a/crates/rspack_core/src/chunk_graph/mod.rs +++ b/crates/rspack_core/src/chunk_graph/mod.rs @@ -1,7 +1,7 @@ use rspack_identifier::IdentifierMap; use rustc_hash::FxHashMap as HashMap; -use crate::{ChunkGroupUkey, ChunkUkey, CompilerOptions}; +use crate::{ChunkGroupUkey, ChunkUkey}; pub mod chunk_graph_chunk; pub mod chunk_graph_module; diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index ed1fb4c66bd7..151b1f3f8d9c 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -32,7 +32,7 @@ use super::{ use crate::{ build_chunk_graph::build_chunk_graph, cache::{use_code_splitting_cache, Cache, CodeSplittingCache}, - debug_exports_info, is_source_equal, + is_source_equal, tree_shaking::{optimizer, visitor::SymbolRef, BailoutFlag, OptimizeDependencyResult}, AddQueue, AddTask, AddTaskResult, AdditionalChunkRuntimeRequirementsArgs, BoxDependency, BoxModule, BuildQueue, BuildTask, BuildTaskResult, CacheCount, CacheOptions, Chunk, ChunkByUkey, @@ -1158,15 +1158,15 @@ impl Compilation { while plugin_driver.optimize_dependencies(self).await?.is_some() {} logger.time_end(start); - if self.options.is_new_tree_shaking() { - // for module in self.module_graph.module_graph_modules().values() {} - // self - // .module_graph - // .module_graph_modules() - // .values() - // .foreach(|item| {}); - debug_exports_info(&self.module_graph); - } + // if self.options.is_new_tree_shaking() { + // // for module in self.module_graph.module_graph_modules().values() {} + // // self + // // .module_graph + // // .module_graph_modules() + // // .values() + // // .foreach(|item| {}); + // debug_exports_info(&self.module_graph); + // } let start = logger.time("create chunks"); use_code_splitting_cache(self, |compilation| async { diff --git a/crates/rspack_core/src/lib.rs b/crates/rspack_core/src/lib.rs index 85cab0df48da..f0ff66a2b03a 100644 --- a/crates/rspack_core/src/lib.rs +++ b/crates/rspack_core/src/lib.rs @@ -8,6 +8,7 @@ mod fake_namespace_object; pub use fake_namespace_object::*; mod module_profile; pub use module_profile::*; +use once_cell::sync::Lazy; use rspack_database::Database; pub mod external_module; pub use external_module::*; @@ -251,3 +252,8 @@ impl TryFrom<&str> for ModuleType { pub type ChunkByUkey = Database; pub type ChunkGroupByUkey = Database; pub(crate) type SharedPluginDriver = Arc; + +pub static IS_NEW_TREESHAKING: Lazy = Lazy::new(|| -> bool { + // std::env::set_var(key, value); + std::env::var("IS_NEW_TREESHAKING").is_ok() +}); diff --git a/crates/rspack_core/src/module_graph/mod.rs b/crates/rspack_core/src/module_graph/mod.rs index af2f748336b9..fc4cdff01e06 100644 --- a/crates/rspack_core/src/module_graph/mod.rs +++ b/crates/rspack_core/src/module_graph/mod.rs @@ -7,6 +7,7 @@ use rspack_hash::RspackHashDigest; use rspack_identifier::IdentifierMap; use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet}; +use crate::IS_NEW_TREESHAKING; mod connection; pub use connection::*; @@ -117,9 +118,13 @@ impl ModuleGraph { ) -> Result<()> { let is_module_dependency = dependency.as_module_dependency().is_some(); let dependency_id = *dependency.id(); - let condition = dependency - .as_module_dependency() - .and_then(|dep| dep.get_condition()); + let condition = if *IS_NEW_TREESHAKING { + dependency + .as_module_dependency() + .and_then(|dep| dep.get_condition()) + } else { + None + }; self.add_dependency(dependency); self .dependency_id_to_module_identifier @@ -660,6 +665,10 @@ mod test { } impl ModuleDependency for $ident { + fn dependency_debug_name(&self) -> &'static str { + stringify!($ident) + } + fn request(&self) -> &str { &*self.1 } diff --git a/crates/rspack_core/src/module_graph_module.rs b/crates/rspack_core/src/module_graph_module.rs index 623d57ab83c5..bda334858506 100644 --- a/crates/rspack_core/src/module_graph_module.rs +++ b/crates/rspack_core/src/module_graph_module.rs @@ -1,13 +1,13 @@ use rspack_error::{internal_error, Result}; use rustc_hash::FxHashSet as HashSet; -use crate::ExportsInfoId; use crate::{ is_async_dependency, module_graph::ConnectionId, BuildInfo, BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, ChunkGraph, ChunkGroupOptionsKindRef, DependencyId, ExportsArgument, ExportsType, FactoryMeta, ModuleArgument, ModuleGraph, ModuleGraphConnection, ModuleIdentifier, ModuleIssuer, ModuleProfile, ModuleSyntax, ModuleType, }; +use crate::{ExportsInfoId, IS_NEW_TREESHAKING}; #[derive(Debug)] pub struct ModuleGraphModule { @@ -123,51 +123,54 @@ impl ModuleGraphModule { // } pub fn depended_modules<'a>(&self, module_graph: &'a ModuleGraph) -> Vec<&'a ModuleIdentifier> { + if *IS_NEW_TREESHAKING { + self + .outgoing_connections_unordered(module_graph) + .expect("should have outgoing connections") + .filter_map(|con: &ModuleGraphConnection| { + // TODO: runtime opt + let active_state = con.get_active_state(module_graph, None); + // dbg!(&con, &active_state,); + // dbg!(&module_graph + // .dependency_by_id(&con.dependency_id) + // .and_then(|dep| dep + // .as_module_dependency() + // .map(|item| item.dependency_debug_name()))); + match active_state { + crate::ConnectionState::Bool(false) => None, + _ => Some(con.dependency_id), + } + }) + .filter(|id| { + if let Some(dep) = module_graph + .dependency_by_id(id) + .expect("should have id") + .as_module_dependency() + { + return !is_async_dependency(dep) && !dep.weak(); + } + false + }) + .filter_map(|id| module_graph.module_identifier_by_dependency_id(&id)) + .collect() + } else { + self + .dependencies + .iter() + .filter(|id| { + if let Some(dep) = module_graph + .dependency_by_id(id) + .expect("should have id") + .as_module_dependency() + { + return !is_async_dependency(dep) && !dep.weak(); + } + false + }) + .filter_map(|id| module_graph.module_identifier_by_dependency_id(id)) + .collect() + } // dbg!(&self.module_identifier); - self - .outgoing_connections_unordered(module_graph) - .unwrap() - .filter_map(|con: &ModuleGraphConnection| { - // TODO: runtime opt - let active_state = con.get_active_state(module_graph, None); - // dbg!(&con, &active_state,); - // dbg!(&module_graph - // .dependency_by_id(&con.dependency_id) - // .and_then(|dep| dep - // .as_module_dependency() - // .map(|item| item.dependency_debug_name()))); - match active_state { - crate::ConnectionState::Bool(false) => None, - _ => Some(con.dependency_id), - } - }) - .filter(|id| { - if let Some(dep) = module_graph - .dependency_by_id(id) - .expect("should have id") - .as_module_dependency() - { - return !is_async_dependency(dep) && !dep.weak(); - } - false - }) - .filter_map(|id| module_graph.module_identifier_by_dependency_id(&id)) - .collect() - // self - // .dependencies - // .iter() - // .filter(|id| { - // if let Some(dep) = module_graph - // .dependency_by_id(id) - // .expect("should have id") - // .as_module_dependency() - // { - // return !is_async_dependency(dep) && !dep.weak(); - // } - // false - // }) - // .filter_map(|id| module_graph.module_identifier_by_dependency_id(id)) - // .collect() } pub fn dynamic_depended_modules<'a>( diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs index a8fd459defd8..5ce11799b577 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_imported_specifier_dependency.rs @@ -402,7 +402,6 @@ impl DependencyTemplate for HarmonyExportImportedSpecifierDependency { None }; - dbg!(&is_new_tree_shaking); if is_new_tree_shaking { // TODO: runtime opt let mode = self.get_mode( @@ -417,7 +416,7 @@ impl DependencyTemplate for HarmonyExportImportedSpecifierDependency { None, ); if !matches!(mode.ty, ExportModeType::Unused | ExportModeType::EmptyStar) { - harmony_import_dependency_apply(self, code_generatable_context, &vec![]); + harmony_import_dependency_apply(self, code_generatable_context, &[]); } } @@ -543,7 +542,6 @@ impl ModuleDependency for HarmonyExportImportedSpecifierDependency { &self.id, runtime, ); - dbg!(&mode, &self.ids); match mode.ty { ExportModeType::Missing | ExportModeType::Unused diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_specifier_dependency.rs index 7d65f629e49a..0bcdaf2db542 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_export_specifier_dependency.rs @@ -1,7 +1,7 @@ use rspack_core::{ AsModuleDependency, Dependency, DependencyCategory, DependencyId, DependencyTemplate, DependencyType, ExportNameOrSpec, ExportsOfExportsSpec, ExportsSpec, HarmonyExportInitFragment, - ModuleDependency, TemplateContext, TemplateReplaceSource, UsedName, + TemplateContext, TemplateReplaceSource, UsedName, }; use swc_core::ecma::atoms::JsWord; diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs index 117f9a2a4432..97cecb6f4e26 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_dependency.rs @@ -1,5 +1,3 @@ -use std::ops::ControlFlow; - use rspack_core::tree_shaking::symbol::{self, IndirectTopLevelSymbol}; use rspack_core::tree_shaking::visitor::SymbolRef; use rspack_core::{ @@ -57,7 +55,7 @@ impl HarmonyImportDependency { pub fn harmony_import_dependency_apply( module_dependency: &T, code_generatable_context: &mut TemplateContext, - specifiers: &Vec, + specifiers: &[Specifier], ) { let compilation = &code_generatable_context.compilation; let module = &code_generatable_context.module; @@ -71,7 +69,6 @@ pub fn harmony_import_dependency_apply( .connection_by_dependency(module_dependency.id()); if let Some(con) = connection { // TODO: runtime opt - let ret = con.is_target_active(&compilation.module_graph, None); // dbg!( // &con, // &ret, @@ -81,7 +78,7 @@ pub fn harmony_import_dependency_apply( // .and_then(|item| item.as_module_dependency()) // .map(|item| item.dependency_debug_name()) // ); - ret + con.is_target_active(&compilation.module_graph, None) } else { true } @@ -244,7 +241,7 @@ impl Dependency for HarmonyImportDependency { ) -> ConnectionState { if let Some(module) = module_graph .module_identifier_by_dependency_id(&self.id) - .and_then(|module_identifier| module_graph.module_by_identifier(&module_identifier)) + .and_then(|module_identifier| module_graph.module_by_identifier(module_identifier)) { module.get_side_effects_connection_state(module_graph, module_chain) } else { diff --git a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs index 98bcb7218826..e627026ab5c1 100644 --- a/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs +++ b/crates/rspack_plugin_javascript/src/dependency/esm/harmony_import_specifier_dependency.rs @@ -133,28 +133,19 @@ impl DependencyTemplate for HarmonyImportSpecifierDependency { .expect("should have ref module"); let compilation = &code_generatable_context.compilation; - let ref_mgm = compilation - .module_graph - .module_graph_module_by_dependency_id(&self.id) - .expect("should have ref module"); - let is_target_active = if compilation.options.is_new_tree_shaking() { + if compilation.options.is_new_tree_shaking() { let connection = compilation.module_graph.connection_by_dependency(&self.id); - if let Some(con) = connection { + let is_target_active = if let Some(con) = connection { // TODO: runtime opt - let ret = con.is_target_active(&compilation.module_graph, None); - - ret + con.is_target_active(&compilation.module_graph, None) } else { true + }; + + if !is_target_active { + return; } - } else { - compilation - .include_module_ids - .contains(&ref_mgm.module_identifier) }; - if !is_target_active { - return; - } let used = self.check_used(reference_mgm, compilation); if !used { @@ -175,11 +166,7 @@ impl DependencyTemplate for HarmonyImportSpecifierDependency { // TODO: scope hoist if compilation.options.is_new_tree_shaking() { - harmony_import_dependency_apply( - self, - code_generatable_context, - &vec![self.specifier.clone()], - ); + harmony_import_dependency_apply(self, code_generatable_context, &[self.specifier.clone()]); } let export_expr = export_from_import( code_generatable_context, @@ -251,7 +238,6 @@ impl ModuleDependency for HarmonyImportSpecifierDependency { _runtime: Option<&RuntimeSpec>, ) -> Vec { // namespace import - dbg!(&self.ids); if self.ids.is_empty() { return self.get_referenced_exports_in_destructuring(None); } diff --git a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs index deafaadda1cf..f17aee64926f 100644 --- a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_usage_plugin.rs @@ -121,14 +121,14 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> { } else { continue; }; - dbg!( - &connection, - dep - .as_module_dependency() - .map(|item| item.dependency_debug_name()), - &referenced_exports, - &old_referenced_exports - ); + // dbg!( + // &connection, + // dep + // .as_module_dependency() + // .map(|item| item.dependency_debug_name()), + // &referenced_exports, + // &old_referenced_exports + // ); if old_referenced_exports.is_none() || matches!(old_referenced_exports.as_ref().expect("should be some"), ProcessModuleReferencedExports::ExtendRef(v) if is_no_exports_referenced(v)) @@ -203,7 +203,7 @@ impl<'a> FlagDependencyUsagePluginProxy<'a> { } for (module_id, referenced_exports) in map { - dbg!(&module_id, &referenced_exports); + // dbg!(&module_id, &referenced_exports); let normalized_refs = match referenced_exports { ProcessModuleReferencedExports::Map(map) => map.into_values().collect::>(), ProcessModuleReferencedExports::ExtendRef(extend_ref) => extend_ref,