Skip to content

Commit

Permalink
refactor: remove is_new_treeshaking in module_graph
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrykingxyz committed Apr 23, 2024
1 parent b6fe628 commit b2cf6b6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 33 deletions.
5 changes: 2 additions & 3 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,12 @@ impl Compilation {
cache: Arc<Cache>,
module_executor: Option<ModuleExecutor>,
) -> 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(),
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/compiler/make/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl UpdateModuleGraph {
})
.collect::<Vec<_>>();

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);
Expand Down
13 changes: 12 additions & 1 deletion crates/rspack_core/src/compiler/make/tasks/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Task<MakeTaskContext> 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() {
Expand All @@ -41,6 +42,7 @@ impl Task<MakeTaskContext> for AddTask {
self.original_module_identifier,
self.dependencies,
*issuer,
is_new_treeshaking,
)?;

// reused module
Expand All @@ -56,6 +58,7 @@ impl Task<MakeTaskContext> for AddTask {
self.original_module_identifier,
self.dependencies,
module_identifier,
is_new_treeshaking,
)?;

// reused module
Expand All @@ -69,6 +72,7 @@ impl Task<MakeTaskContext> for AddTask {
self.original_module_identifier,
self.dependencies,
module_identifier,
is_new_treeshaking,
)?;

if self.is_entry {
Expand Down Expand Up @@ -97,9 +101,16 @@ fn set_resolved_module(
original_module_identifier: Option<ModuleIdentifier>,
dependencies: Vec<DependencyId>,
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(())
}
29 changes: 3 additions & 26 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DependencyId, Option<ModuleIdentifier>>,

/// Module identifier to its module
Expand Down Expand Up @@ -71,16 +68,6 @@ pub struct ModuleGraphPartial {
dep_meta_map: HashMap<DependencyId, DependencyExtraMeta>,
}

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>,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -705,13 +680,15 @@ impl<'a> ModuleGraph<'a> {
original_module_identifier: Option<ModuleIdentifier>,
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())
Expand Down
3 changes: 1 addition & 2 deletions crates/rspack_plugin_javascript/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b2cf6b6

Please sign in to comment.