Skip to content

Commit

Permalink
chore: 🤖 lint
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Sep 20, 2023
1 parent c94d3f9 commit 70f16d6
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 101 deletions.
3 changes: 2 additions & 1 deletion crates/rspack/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/chunk_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
20 changes: 10 additions & 10 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -251,3 +252,8 @@ impl TryFrom<&str> for ModuleType {
pub type ChunkByUkey = Database<Chunk>;
pub type ChunkGroupByUkey = Database<ChunkGroup>;
pub(crate) type SharedPluginDriver = Arc<PluginDriver>;

pub static IS_NEW_TREESHAKING: Lazy<bool> = Lazy::new(|| -> bool {
// std::env::set_var(key, value);
std::env::var("IS_NEW_TREESHAKING").is_ok()
});
15 changes: 12 additions & 3 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
93 changes: 48 additions & 45 deletions crates/rspack_core/src/module_graph_module.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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, &[]);
}
}

Expand Down Expand Up @@ -543,7 +542,6 @@ impl ModuleDependency for HarmonyExportImportedSpecifierDependency {
&self.id,
runtime,
);
dbg!(&mode, &self.ids);
match mode.ty {
ExportModeType::Missing
| ExportModeType::Unused
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -57,7 +55,7 @@ impl HarmonyImportDependency {
pub fn harmony_import_dependency_apply<T: ModuleDependency>(
module_dependency: &T,
code_generatable_context: &mut TemplateContext,
specifiers: &Vec<Specifier>,
specifiers: &[Specifier],
) {
let compilation = &code_generatable_context.compilation;
let module = &code_generatable_context.module;
Expand All @@ -71,7 +69,6 @@ pub fn harmony_import_dependency_apply<T: ModuleDependency>(
.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,
Expand All @@ -81,7 +78,7 @@ pub fn harmony_import_dependency_apply<T: ModuleDependency>(
// .and_then(|item| item.as_module_dependency())
// .map(|item| item.dependency_debug_name())
// );
ret
con.is_target_active(&compilation.module_graph, None)
} else {
true
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -251,7 +238,6 @@ impl ModuleDependency for HarmonyImportSpecifierDependency {
_runtime: Option<&RuntimeSpec>,
) -> Vec<ExtendedReferencedExport> {
// namespace import
dbg!(&self.ids);
if self.ids.is_empty() {
return self.get_referenced_exports_in_destructuring(None);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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::<Vec<_>>(),
ProcessModuleReferencedExports::ExtendRef(extend_ref) => extend_ref,
Expand Down

0 comments on commit 70f16d6

Please sign in to comment.