From 58dbd2f4e0f7e07da0890e2b4cbe4a90ae99abf0 Mon Sep 17 00:00:00 2001 From: sorrycc Date: Tue, 2 Jan 2024 11:33:08 +0800 Subject: [PATCH] chore: update --- crates/mako/src/generate.rs | 11 +++++- crates/mako/src/plugins/minifish/inject.rs | 39 ++++++++++++------- crates/mako/src/test_helper.rs | 8 ++-- crates/mako/src/transform.rs | 9 ++--- .../transformers/transform_env_replacer.rs | 4 +- .../src/transformers/transform_provide.rs | 28 ++++++++----- .../mako/src/transformers/transform_react.rs | 19 +++++---- 7 files changed, 72 insertions(+), 46 deletions(-) diff --git a/crates/mako/src/generate.rs b/crates/mako/src/generate.rs index 598e4766e..146f7abc8 100644 --- a/crates/mako/src/generate.rs +++ b/crates/mako/src/generate.rs @@ -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 => {} } } diff --git a/crates/mako/src/plugins/minifish/inject.rs b/crates/mako/src/plugins/minifish/inject.rs index 59065a40a..7d7ccfca3 100644 --- a/crates/mako/src/plugins/minifish/inject.rs +++ b/crates/mako/src/plugins/minifish/inject.rs @@ -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}; @@ -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) { @@ -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") @@ -156,7 +167,7 @@ impl Inject { } }; - stmt.into() + stmt } fn into_with(self, ctxt: SyntaxContext) -> ModuleItem { diff --git a/crates/mako/src/test_helper.rs b/crates/mako/src/test_helper.rs index 013b1e38c..973ebb045 100644 --- a/crates/mako/src/test_helper.rs +++ b/crates/mako/src/test_helper.rs @@ -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}; @@ -113,7 +113,7 @@ pub fn setup_logger() { } pub fn transform_ast_with( - module: &mut SwcModule, + module: &mut Program, visitor: &mut dyn VisitMut, cm: &Lrc, ) -> String { @@ -121,7 +121,7 @@ pub fn transform_ast_with( emit_js(module, cm) } -fn emit_js(module: &SwcModule, cm: &Arc) -> String { +fn emit_js(module: &Program, cm: &Arc) -> String { let mut buf = Vec::new(); { @@ -133,7 +133,7 @@ fn emit_js(module: &SwcModule, cm: &Arc) -> 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() diff --git a/crates/mako/src/transform.rs b/crates/mako/src/transform.rs index 099f99461..d71182db8 100644 --- a/crates/mako/src/transform.rs +++ b/crates/mako/src/transform.rs @@ -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", diff --git a/crates/mako/src/transformers/transform_env_replacer.rs b/crates/mako/src/transformers/transform_env_replacer.rs index ec29bd4e7..9cf6449af 100644 --- a/crates/mako/src/transformers/transform_env_replacer.rs +++ b/crates/mako/src/transformers/transform_env_replacer.rs @@ -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}; @@ -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); } diff --git a/crates/mako/src/transformers/transform_provide.rs b/crates/mako/src/transformers/transform_provide.rs index a92aec214..93eba5fad 100644 --- a/crates/mako/src/transformers/transform_provide.rs +++ b/crates/mako/src/transformers/transform_provide.rs @@ -2,7 +2,7 @@ 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}; @@ -10,7 +10,7 @@ use crate::config::Providers; pub struct Provide { bindings: Lrc>, providers: Providers, - var_decls: IndexMap, + var_decls: IndexMap, } impl Provide { pub fn new(providers: Providers) -> Self { @@ -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 { @@ -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__") diff --git a/crates/mako/src/transformers/transform_react.rs b/crates/mako/src/transformers/transform_react.rs index 5eb8c3f2f..4acbd206c 100644 --- a/crates/mako/src/transformers/transform_react.rs +++ b/crates/mako/src/transformers/transform_react.rs @@ -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 { @@ -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); } } @@ -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) }) } }