Skip to content

Commit

Permalink
feat: treeshaking in module level (#382)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 authored Dec 4, 2023
1 parent 47b4b6d commit 6f8b03f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/module/normal_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::{Module, ModuleRenderContext, ModuleVec};

#[derive(Debug)]
pub struct NormalModule {
pub is_included: bool,
pub exec_order: u32,
pub id: ModuleId,
pub is_user_defined_entry: bool,
Expand Down Expand Up @@ -62,8 +63,7 @@ impl NormalModule {

#[allow(clippy::needless_pass_by_value)]
pub fn render(&self, ctx: ModuleRenderContext<'_>) -> Option<MagicString<'static>> {
// FIXME: Remove this when we support removing module that rendered nothing
if self.id == ctx.graph.runtime.id() && self.stmt_infos.iter().all(|info| !info.is_included) {
if !self.is_included {
return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl NormalModuleBuilder {
module_type: self.module_type,
is_user_defined_entry: self.is_user_defined_entry.unwrap(),
pretty_path: self.pretty_path.unwrap(),
is_included: false,
}
}
}
5 changes: 5 additions & 0 deletions crates/rolldown/src/bundler/stages/link_stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl LinkStage {
modules: &'a ModuleVec,
symbols: &'a Symbols,
is_included_vec: &'a mut IndexVec<ModuleId, IndexVec<StmtInfoId, bool>>,
is_module_included_vec: &'a mut IndexVec<ModuleId, bool>,
}

fn include_symbol(ctx: &mut Context, symbol_ref: SymbolRef) {
Expand All @@ -262,6 +263,7 @@ impl LinkStage {
}

fn include_statement(ctx: &mut Context, module: &NormalModule, stmt_info_id: StmtInfoId) {
ctx.is_module_included_vec[module.id] = true;
let is_included = &mut ctx.is_included_vec[module.id][stmt_info_id];
if *is_included {
return;
Expand Down Expand Up @@ -290,11 +292,13 @@ impl LinkStage {
Module::External(_) => IndexVec::default(),
})
.collect::<IndexVec<ModuleId, _>>();
let mut is_module_included_vec = index_vec::index_vec![false; self.modules.len()];

let context = &mut Context {
modules: &self.modules,
symbols: &self.symbols,
is_included_vec: &mut is_included_vec,
is_module_included_vec: &mut is_module_included_vec,
};

for module in &self.modules {
Expand Down Expand Up @@ -323,6 +327,7 @@ impl LinkStage {
let Module::Normal(module) = module else {
return;
};
module.is_included = is_module_included_vec[module.id];
is_included_vec[module.id].iter_enumerated().for_each(|(stmt_info_id, is_included)| {
module.stmt_infos.get_mut(stmt_info_id).is_included = *is_included;
});
Expand Down

0 comments on commit 6f8b03f

Please sign in to comment.