Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree-shaking):skip-module according to side-effects #807

Merged
merged 22 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8565544
feat: ✨ POC of import reexport
stormslowly Dec 14, 2023
30ae591
feat: ✨ enable if let some chain
stormslowly Dec 19, 2023
cd66d39
refactor: 🎨 use &string instead of &Task
stormslowly Dec 19, 2023
4ea9da4
refactor: 🔥 remove unused
stormslowly Dec 19, 2023
40a2966
test: ✅ fix minifish test when dep analyzes api change
stormslowly Dec 19, 2023
31da7a9
refactor: 🔥 remove useless find statment fn
stormslowly Dec 19, 2023
fc8bdbb
refactor: 🎨 extract skip module to shake/ folder
stormslowly Dec 19, 2023
1e4df91
Merge remote-tracking branch 'origin/master' into feat/re_export_with…
stormslowly Dec 19, 2023
ebf11c9
fix: 🐛 when reexport namespace export
stormslowly Dec 20, 2023
a82cef0
refactor: 🔥 revert field to private
stormslowly Dec 20, 2023
01c69c8
test: ✅ fix export namespace test case
stormslowly Dec 20, 2023
df74fef
chore: 🚨 lint happy for toml
stormslowly Dec 20, 2023
a1ef95e
test: ✅ add e2e test
stormslowly Dec 20, 2023
5cf5f33
feat: ✨ skip module enabled by config
stormslowly Dec 20, 2023
08636e4
refactor: 🎨 change config name optimizations to optimization
stormslowly Dec 20, 2023
7879c36
test: ✅ e2e turn off skip for decorator test case
stormslowly Dec 20, 2023
2a9a814
chore: 🔧 boime ignore dist-webpack dist-mako
stormslowly Dec 20, 2023
d02f336
refactor: 🎨 rename config name to skipModules
stormslowly Dec 21, 2023
bf8a494
Merge remote-tracking branch 'origin/master' into feat/re_export_with…
stormslowly Dec 21, 2023
5e0fd6d
chore: 🔧 update tsd
stormslowly Dec 22, 2023
58e3191
Merge remote-tracking branch 'origin/master' into feat/re_export_with…
stormslowly Dec 22, 2023
e2b422b
release: @okamjs/[email protected]
stormslowly Dec 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ members = ["crates/*"]
serde = { version = "1.0.171", features = ["derive"] }
serde_json = "1.0.100"
cached = "0.46.1"
swc_core = { version = "=0.83.19", default-features = false }
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"

[dependencies]
tokio = { version = "1", features = ["full"] }
swc_core = { version = "=0.83.19", features = [
swc_core = { workspace = true, features = [
"__ecma",
"__visit",
"__utils",
Expand Down
1 change: 1 addition & 0 deletions crates/mako/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mako_core = { path = "../core" }
serde = { workspace = true }
serde_json = { workspace = true }
cached = { workspace = true }
swc_core = { workspace = true, features = ["swc_ecma_quote_macros"] }
stormslowly marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
insta = { version = "1.30.0", features = ["yaml"] }
Expand Down
5 changes: 2 additions & 3 deletions crates/mako/src/analyze_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use mako_core::anyhow::{anyhow, Result};
use crate::compiler::Context;
use crate::module::{Dependency, ModuleAst};
use crate::plugin::PluginDepAnalyzeParam;
use crate::task::Task;

pub fn analyze_deps(
ast: &ModuleAst,
task: &Task,
file_path: &String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个没特别的理由不要调整。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有,skip modules 之后,ast 会变化,ast 需要重新分析,这个时候分析deps 是没有 task 这个概念的。

context: &Arc<Context>,
) -> Result<Vec<Dependency>> {
mako_core::mako_profile_function!();
Expand All @@ -32,7 +31,7 @@ pub fn analyze_deps(
return Err(anyhow!(
"webpack loader syntax is not supported, since found dep {:?} in {:?}",
dep.source,
task.path,
file_path
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ module.exports = new Promise((resolve, reject) => {{

// 在此之前需要把所有依赖都和模块关联起来,并且需要使用 resolved source
// analyze deps
let deps = analyze_deps(&ast, &task, context)?;
let deps = analyze_deps(&ast, &task.path, context)?;

// resolve
let mut dep_resolve_err = None;
Expand Down
7 changes: 7 additions & 0 deletions crates/mako/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ pub struct MinifishConfig {
pub meta_path: Option<PathBuf>,
pub inject: Option<HashMap<String, InjectItem>>,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OptimizationConfig {
pub skip_module: Option<bool>,
stormslowly marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -301,6 +306,7 @@ pub struct Config {
pub optimize_package_imports: bool,
pub emotion: bool,
pub flex_bugs: bool,
pub optimization: Option<OptimizationConfig>,
}

pub(crate) fn hash_config(c: &Config) -> u64 {
Expand Down Expand Up @@ -457,6 +463,7 @@ const DEFAULT_CONFIG: &str = r#"
"optimizePackageImports": false,
"emotion": false,
"flexBugs": false,
"optimization": { "skipModule": true }
}
"#;

Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(box_patterns)]
#![feature(hasher_prefixfree_extras)]
#![feature(let_chains)]

mod analyze_deps;
mod ast;
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(box_patterns)]
#![feature(let_chains)]

use std::sync::Arc;

Expand Down
8 changes: 8 additions & 0 deletions crates/mako/src/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ impl ModuleGraph {
targets
}

pub fn remove_dependencies(&mut self, module_id: &ModuleId) {
let mut edges = self.get_edges(module_id, Direction::Outgoing);

while let Some((edge_index, _)) = edges.next(&self.graph) {
self.graph.remove_edge(edge_index);
}
}

pub fn get_dependency_module_by_source(
&self,
module_id: &ModuleId,
Expand Down
33 changes: 28 additions & 5 deletions crates/mako/src/plugins/farm_tree_shake/module.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod import_exports;

use std::collections::{HashMap, HashSet};

use mako_core::swc_common::SyntaxContext;
Expand Down Expand Up @@ -129,6 +131,22 @@ pub struct TreeShakeModule {
}

impl TreeShakeModule {
pub fn update_stmt_graph(&mut self, module: &SwcModule) {
let stmt_graph = StatementGraph::new(module, self.unresolved_ctxt);

self.stmt_graph = stmt_graph;
}

pub fn has_side_effect(&self) -> bool {
if let Some(described_side_effects) = self.described_side_effects {
if !described_side_effects {
return false;
}
}

self.side_effects
}

pub fn update_side_effect(&mut self) -> bool {
let mut side_effect_stmts = vec![];

Expand Down Expand Up @@ -236,8 +254,6 @@ impl TreeShakeModule {
pub fn new(module: &Module, order: usize, _module_graph: &ModuleGraph) -> Self {
let module_info = module.info.as_ref().unwrap();

let side_effect_map: HashMap<String, bool> = Default::default();

let mut unresolved_ctxt = SyntaxContext::empty();
// 1. generate statement graph
let mut module_system = ModuleSystem::CommonJS;
Expand All @@ -251,7 +267,7 @@ impl TreeShakeModule {
if is_esm {
module_system = ModuleSystem::ESModule;
unresolved_ctxt = unresolved_ctxt.apply_mark(module.unresolved_mark);
StatementGraph::new(&module.ast, &side_effect_map, unresolved_ctxt)
StatementGraph::new(&module.ast, unresolved_ctxt)
} else {
StatementGraph::empty()
}
Expand Down Expand Up @@ -396,7 +412,7 @@ impl TreeShakeModule {
for export_info in self.exports() {
for sp in export_info.specifiers {
match sp {
ExportSpecifierInfo::Default => {
ExportSpecifierInfo::Default(_) => {
used_idents.push((UsedIdent::Default, export_info.stmt_id));
}
ExportSpecifierInfo::Named { local, .. } => {
Expand Down Expand Up @@ -443,7 +459,7 @@ impl TreeShakeModule {
for export_info in export_infos {
for sp in export_info.specifiers {
match sp {
ExportSpecifierInfo::Default => {
ExportSpecifierInfo::Default(_) => {
if ident == "default" {
used_idents.push((UsedIdent::Default, export_info.stmt_id));
}
Expand Down Expand Up @@ -508,3 +524,10 @@ pub fn is_ident_equal(ident1: &str, ident2: &str) -> bool {
split1[0] == split2[0]
}
}

pub fn is_ident_sym_equal(ident1: &str, ident2: &str) -> bool {
let split1 = ident1.split('#').collect::<Vec<_>>();
let split2 = ident2.split('#').collect::<Vec<_>>();

split1[0] == split2[0]
}
Loading