Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Jan 2, 2024
1 parent d643f49 commit 58dbd2f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 46 deletions.
11 changes: 10 additions & 1 deletion crates/mako/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ impl Compiler {
let t_tree_shaking = t_tree_shaking.elapsed();
println!("basic optimize in {}ms.", t_tree_shaking.as_millis());
}
TreeShakeStrategy::Advanced => {}
TreeShakeStrategy::Advanced => {
mako_core::mako_profile_scope!("advanced tree shake");
let shaking_module_ids = self.tree_shaking();
let t_tree_shaking = t_tree_shaking.elapsed();
println!(
"{} modules removed in {}ms.",
shaking_module_ids.len(),
t_tree_shaking.as_millis()
);
}
TreeShakeStrategy::None => {}
}
}
Expand Down
39 changes: 25 additions & 14 deletions crates/mako/src/plugins/minifish/inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use mako_core::regex::Regex;
use mako_core::swc_common::{Mark, Span, SyntaxContext, DUMMY_SP};
use mako_core::swc_ecma_ast::{
ExportSpecifier, Ident, ImportDecl, ImportDefaultSpecifier, ImportNamedSpecifier,
ImportSpecifier, ImportStarAsSpecifier, MemberExpr, ModuleDecl, ModuleItem, NamedExport, Stmt,
VarDeclKind,
ImportSpecifier, ImportStarAsSpecifier, MemberExpr, ModuleDecl, ModuleItem, NamedExport,
Program, Stmt, VarDeclKind,
};
use mako_core::swc_ecma_utils::{quote_ident, quote_str, ExprFactory};
use mako_core::swc_ecma_visit::{VisitMut, VisitMutWith};
Expand Down Expand Up @@ -63,18 +63,29 @@ impl VisitMut for MyInjector<'_> {
}
}

fn visit_mut_module(&mut self, n: &mut mako_core::swc_ecma_ast::Module) {
fn visit_mut_program(&mut self, n: &mut Program) {
n.visit_mut_children_with(self);

let stmts = self.will_inject.iter().map(|&(inject, ctxt)| {
if self.is_cjs || inject.prefer_require {
inject.clone().into_require_with(ctxt, self.unresolved_mark)
} else {
inject.clone().into_with(ctxt)
match n {
Program::Module(n) => {
let stmts = self.will_inject.iter().map(|&(inject, ctxt)| {
if self.is_cjs || inject.prefer_require {
inject
.clone()
.into_require_with(ctxt, self.unresolved_mark)
.into()
} else {
inject.clone().into_with(ctxt)
}
});
n.body.splice(0..0, stmts);
}
});

n.body.splice(0..0, stmts);
Program::Script(n) => {
let stmts = self.will_inject.iter().map(|&(inject, ctxt)| {
inject.clone().into_require_with(ctxt, self.unresolved_mark)
});
n.body.splice(0..0, stmts);
}
}
}

fn visit_mut_module_items(&mut self, module_items: &mut Vec<ModuleItem>) {
Expand Down Expand Up @@ -114,7 +125,7 @@ impl Hash for Inject {
}

impl Inject {
fn into_require_with(self, ctxt: SyntaxContext, unresolved_mark: Mark) -> ModuleItem {
fn into_require_with(self, ctxt: SyntaxContext, unresolved_mark: Mark) -> Stmt {
let name_span = Span { ctxt, ..DUMMY_SP };

let require_source_expr = quote_ident!(DUMMY_SP.apply_mark(unresolved_mark), "require")
Expand Down Expand Up @@ -156,7 +167,7 @@ impl Inject {
}
};

stmt.into()
stmt
}

fn into_with(self, ctxt: SyntaxContext) -> ModuleItem {
Expand Down
8 changes: 4 additions & 4 deletions crates/mako/src/test_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use mako_core::swc_common::sync::Lrc;
use mako_core::swc_common::SourceMap;
use mako_core::swc_ecma_ast::Module as SwcModule;
use mako_core::swc_ecma_ast::Program;
use mako_core::swc_ecma_codegen::text_writer::JsWriter;
use mako_core::swc_ecma_codegen::Emitter;
use mako_core::swc_ecma_visit::{VisitMut, VisitMutWith};
Expand Down Expand Up @@ -113,15 +113,15 @@ pub fn setup_logger() {
}

pub fn transform_ast_with(
module: &mut SwcModule,
module: &mut Program,
visitor: &mut dyn VisitMut,
cm: &Lrc<SourceMap>,
) -> String {
module.visit_mut_with(visitor);
emit_js(module, cm)
}

fn emit_js(module: &SwcModule, cm: &Arc<SourceMap>) -> String {
fn emit_js(module: &Program, cm: &Arc<SourceMap>) -> String {
let mut buf = Vec::new();

{
Expand All @@ -133,7 +133,7 @@ fn emit_js(module: &SwcModule, cm: &Arc<SourceMap>) -> String {
wr: writer,
};
// This may return an error if it fails to write
emitter.emit_module(module).unwrap();
emitter.emit_program(module).unwrap();
}

String::from_utf8(buf).unwrap().trim().to_string()
Expand Down
9 changes: 3 additions & 6 deletions crates/mako/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,9 @@ App;
assert_eq!(
code,
r#"
Object.defineProperty(exports, "__esModule", {
value: true
});
var _jsxdevruntime = __mako_require__("react/jsx-dev-runtime");
const App = ()=>(0, _jsxdevruntime.jsxDEV)(_jsxdevruntime.Fragment, {
children: (0, _jsxdevruntime.jsxDEV)("h1", {
const { jsxDEV: _jsxDEV, Fragment: _Fragment } = __mako_require__("react/jsx-dev-runtime");
const App = ()=>_jsxDEV(_Fragment, {
children: _jsxDEV("h1", {
children: "Hello World"
}, void 0, false, {
fileName: "test.tsx",
Expand Down
4 changes: 2 additions & 2 deletions crates/mako/src/transformers/transform_env_replacer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use mako_core::swc_common::sync::Lrc;
use mako_core::swc_common::DUMMY_SP;
use mako_core::swc_ecma_ast::{
ArrayLit, Bool, ComputedPropName, Expr, ExprOrSpread, Id, Ident, KeyValueProp, Lit, MemberExpr,
MemberProp, MetaPropExpr, MetaPropKind, Module, Null, Number, ObjectLit, Prop, PropName,
MemberProp, MetaPropExpr, MetaPropKind, Null, Number, ObjectLit, Program, Prop, PropName,
PropOrSpread, Stmt, Str,
};
use mako_core::swc_ecma_utils::{collect_decls, quote_ident, ExprExt};
Expand Down Expand Up @@ -61,7 +61,7 @@ impl EnvReplacer {
}
}
impl VisitMut for EnvReplacer {
fn visit_mut_module(&mut self, module: &mut Module) {
fn visit_mut_program(&mut self, module: &mut Program) {
self.bindings = Lrc::new(collect_decls(&*module));
module.visit_mut_children_with(self);
}
Expand Down
28 changes: 19 additions & 9 deletions crates/mako/src/transformers/transform_provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use mako_core::indexmap::IndexMap;
use mako_core::swc_common::collections::AHashSet;
use mako_core::swc_common::sync::Lrc;
use mako_core::swc_common::DUMMY_SP;
use mako_core::swc_ecma_ast::{Expr, Id, Ident, MemberExpr, Module, ModuleItem, VarDeclKind};
use mako_core::swc_ecma_ast::{Expr, Id, Ident, MemberExpr, Program, Stmt, VarDeclKind};
use mako_core::swc_ecma_utils::{collect_decls, quote_ident, quote_str, ExprFactory};
use mako_core::swc_ecma_visit::{VisitMut, VisitMutWith};

use crate::config::Providers;
pub struct Provide {
bindings: Lrc<AHashSet<Id>>,
providers: Providers,
var_decls: IndexMap<String, ModuleItem>,
var_decls: IndexMap<String, Stmt>,
}
impl Provide {
pub fn new(providers: Providers) -> Self {
Expand All @@ -22,12 +22,22 @@ impl Provide {
}
}
impl VisitMut for Provide {
fn visit_mut_module(&mut self, module: &mut Module) {
self.bindings = Lrc::new(collect_decls(&*module));
module.visit_mut_children_with(self);
module
.body
.splice(0..0, self.var_decls.iter().map(|(_, var)| var.clone()));
fn visit_mut_program(&mut self, program: &mut Program) {
self.bindings = Lrc::new(collect_decls(&*program));
program.visit_mut_children_with(self);
match program {
Program::Module(module) => {
module.body.splice(
0..0,
self.var_decls.iter().map(|(_, var)| var.clone().into()),
);
}
Program::Script(script) => {
script
.body
.splice(0..0, self.var_decls.iter().map(|(_, var)| var.clone()));
}
}
}
fn visit_mut_expr(&mut self, expr: &mut Expr) {
if let Expr::Ident(Ident { ref sym, span, .. }) = expr {
Expand All @@ -36,7 +46,7 @@ impl VisitMut for Provide {
let provider = self.providers.get(name);
if !has_binding && provider.is_some() {
if let Some((from, key)) = provider {
let require_decl: ModuleItem = {
let require_decl = {
if key.is_empty() {
// eg: const process = require('process');
quote_ident!("__mako_require__")
Expand Down
19 changes: 9 additions & 10 deletions crates/mako/src/transformers/transform_react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ struct Emotion {
}

impl VisitMut for Emotion {
fn visit_mut_module(&mut self, module: &mut Module) {
fn visit_mut_program(&mut self, program: &mut Program) {
let is_dev = matches!(self.mode, Mode::Development);
let pos = self.cm.lookup_char_pos(module.span.lo);
let span = match program {
Program::Module(module) => module.span,
Program::Script(script) => script.span,
};
let pos = self.cm.lookup_char_pos(span.lo);
let hash = pos.file.src_hash as u32;
let mut folder = emotion(
EmotionOptions {
Expand All @@ -124,9 +128,8 @@ impl VisitMut for Emotion {
self.cm.clone(),
NoopComments,
);
module.body = folder.fold_module(module.clone()).body;

module.visit_mut_children_with(self);
*program = folder.fold_program(program.clone());
program.visit_mut_children_with(self);
}
}

Expand Down Expand Up @@ -359,11 +362,7 @@ mod tests {
)
));

transform_ast_with(
&mut ast.ast.as_module().unwrap(),
&mut visitor,
&context.meta.script.cm,
)
transform_ast_with(&mut ast.ast, &mut visitor, &context.meta.script.cm)
})
}
}

0 comments on commit 58dbd2f

Please sign in to comment.