Skip to content

Commit

Permalink
revert: import namespace optimize (#1606)
Browse files Browse the repository at this point in the history
* Revert "fix(tree-shaking): object spread transform should go before deconstructing (#1598)"

This reverts commit 9434d99.

* Revert "feat(tree-shaking): optimize import namespace used all exports to partial used of source modules (#1584)"

This reverts commit 81a52f8.
  • Loading branch information
stormslowly authored Sep 23, 2024
1 parent 457f3ed commit a485358
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 363 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ jobs:
- name: LS
run: ls -l ./packages/mako
- name: Test E2E
env:
RUST_BACKTRACE: full
run: pnpm ${{ matrix.script }}

lint:
Expand Down
1 change: 0 additions & 1 deletion crates/mako/src/plugins/tree_shaking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::compiler::Context;
use crate::module_graph::ModuleGraph;
use crate::plugin::{Plugin, PluginTransformJsParam};

mod collect_explicit_prop;
mod module;
mod module_side_effects_flag;
mod remove_useless_stmts;
Expand Down
222 changes: 0 additions & 222 deletions crates/mako/src/plugins/tree_shaking/collect_explicit_prop.rs

This file was deleted.

79 changes: 3 additions & 76 deletions crates/mako/src/plugins/tree_shaking/remove_useless_stmts.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
use std::collections::HashSet;

use swc_core::common::util::take::Take;
use swc_core::common::SyntaxContext;
use swc_core::ecma::ast::{
Decl, ExportDecl, ExportSpecifier, Id, ImportDecl, ImportSpecifier, Module as SwcModule,
Module, ModuleExportName,
Decl, ExportDecl, ExportSpecifier, ImportDecl, ImportSpecifier, Module as SwcModule,
ModuleExportName,
};
use swc_core::ecma::transforms::compat::es2015::destructuring;
use swc_core::ecma::transforms::compat::es2018::object_rest_spread;
use swc_core::ecma::visit::{VisitMut, VisitMutWith, VisitWith};

use super::collect_explicit_prop::IdExplicitPropAccessCollector;
use crate::plugins::tree_shaking::module::TreeShakeModule;
use crate::plugins::tree_shaking::statement_graph::analyze_imports_and_exports::{
analyze_imports_and_exports, StatementInfo,
Expand Down Expand Up @@ -112,10 +105,10 @@ pub fn remove_useless_stmts(

// remove from the end to the start
stmts_to_remove.reverse();

for stmt in stmts_to_remove {
swc_module.body.remove(stmt);
}
optimize_import_namespace(&mut used_import_infos, swc_module);

(used_import_infos, used_export_from_infos)
}
Expand Down Expand Up @@ -238,72 +231,6 @@ impl VisitMut for UselessExportStmtRemover {
}
}

fn optimize_import_namespace(import_infos: &mut [ImportInfo], module: &mut Module) {
let namespaces = import_infos
.iter()
.filter_map(|import_info| {
let ns = import_info
.specifiers
.iter()
.filter_map(|sp| match sp {
ImportSpecifierInfo::Namespace(ns) => Some(ns.clone()),
_ => None,
})
.collect::<Vec<String>>();
if ns.is_empty() {
None
} else {
Some(ns)
}
})
.flatten()
.collect::<Vec<String>>();

let ids = namespaces
.iter()
.map(|ns| {
let (sym, ctxt) = ns.rsplit_once('#').unwrap();
(sym.into(), SyntaxContext::from_u32(ctxt.parse().unwrap()))
})
.collect::<HashSet<Id>>();

if !ids.is_empty() {
let mut v = IdExplicitPropAccessCollector::new(ids);
let mut shadow = module.clone();

shadow.visit_mut_with(&mut object_rest_spread(Default::default()));
shadow.visit_mut_with(&mut destructuring(Default::default()));
shadow.visit_with(&mut v);

let explicit_prop_accessed_ids = v.explicit_accessed_props();

import_infos.iter_mut().for_each(|ii| {
ii.specifiers = ii
.specifiers
.take()
.into_iter()
.flat_map(|specifier_info| {
if let ImportSpecifierInfo::Namespace(ref ns) = specifier_info {
if let Some(visited_fields) = explicit_prop_accessed_ids.get(ns) {
return visited_fields
.iter()
.map(|v| {
let imported_name = format!("{v}#0");
ImportSpecifierInfo::Named {
imported: Some(imported_name.clone()),
local: imported_name,
}
})
.collect::<Vec<_>>();
}
}
vec![specifier_info]
})
.collect::<Vec<_>>();
})
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
20 changes: 9 additions & 11 deletions crates/mako/src/plugins/tree_shaking/shake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use anyhow::Result;
use rayon::prelude::*;
use swc_core::common::util::take::Take;
use swc_core::common::GLOBALS;
use swc_core::ecma::transforms::base::helpers::{Helpers, HELPERS};

use self::skip_module::skip_module_optimize;
use crate::compiler::Context;
Expand Down Expand Up @@ -135,24 +134,23 @@ pub fn optimize_modules(module_graph: &mut ModuleGraph, context: &Arc<Context>)
let mut current_index: usize = 0;
let len = tree_shake_modules_ids.len();

GLOBALS.set(&context.meta.script.globals, || {
{
mako_profile_scope!("tree-shake");

while current_index < len {
mako_profile_scope!(
"tree-shake-module",
&tree_shake_modules_ids[current_index].id
);
HELPERS.set(&Helpers::new(true), || {
current_index = shake_module(
module_graph,
&tree_shake_modules_ids,
&tree_shake_modules_map,
current_index,
);
});

current_index = shake_module(
module_graph,
&tree_shake_modules_ids,
&tree_shake_modules_map,
current_index,
);
}
});
}

{
mako_profile_scope!("update ast");
Expand Down
Loading

0 comments on commit a485358

Please sign in to comment.