Skip to content

Commit

Permalink
fix: import/export minify object pat
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 committed Jul 19, 2024
1 parent 2a47305 commit d05eca8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
33 changes: 30 additions & 3 deletions crates/plugin_minify/src/imports_minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use farmfe_core::{
plugin::ResolveKind,
swc_common::{Mark, DUMMY_SP},
swc_ecma_ast::{
CallExpr, Callee, ExportNamedSpecifier, ExportSpecifier, Expr, ExprOrSpread, ExprStmt, Id,
Ident, KeyValueProp, Lit, MemberExpr, MemberProp, ModuleDecl, ModuleExportName, ModuleItem,
NamedExport, ObjectLit, Prop, PropName, PropOrSpread, Stmt, Str,
BindingIdent, CallExpr, Callee, ExportNamedSpecifier, ExportSpecifier, Expr, ExprOrSpread,
ExprStmt, Id, Ident, KeyValuePatProp, KeyValueProp, Lit, MemberExpr, MemberProp, ModuleDecl,
ModuleExportName, ModuleItem, NamedExport, ObjectLit, Pat, Prop, PropName, PropOrSpread, Stmt,
Str,
},
};
use farmfe_toolkit::swc_ecma_visit::{VisitMut, VisitMutWith};
Expand Down Expand Up @@ -510,6 +511,32 @@ impl<'a> VisitMut for IdentReplacer {
}
}
}

fn visit_mut_object_pat(&mut self, pat: &mut farmfe_core::swc_ecma_ast::ObjectPat) {
for n in &mut pat.props {
match n {
farmfe_core::swc_ecma_ast::ObjectPatProp::KeyValue(key) => key.value.visit_mut_with(self),
farmfe_core::swc_ecma_ast::ObjectPatProp::Assign(a) => {
if let Some(value) = &mut a.value {
value.visit_mut_with(self);
} else if let Some(replaced) = self.id_to_replace.get(&a.key.id.to_id()) {
*n = farmfe_core::swc_ecma_ast::ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(a.key.id.clone()),
value: Box::new(Pat::Ident(BindingIdent {
id: Ident::new(
replaced.as_str().into(),
DUMMY_SP.apply_mark(a.key.id.span.ctxt().outer()),
),
type_ann: None,
})),
})
}
}
farmfe_core::swc_ecma_ast::ObjectPatProp::Rest(r) => r.visit_mut_with(self),
}
}
}

fn visit_mut_ident(&mut self, n: &mut farmfe_core::swc_ecma_ast::Ident) {
if let Some(replaced) = self.id_to_replace.get(&n.to_id()) {
n.sym = replaced.as_str().into();
Expand Down
15 changes: 11 additions & 4 deletions crates/plugin_minify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,17 @@ impl Plugin for FarmPluginMinify {
let meta = module.meta.as_script_mut();
let ast = &mut meta.ast;

if let Some(id_to_replace) = id_to_replace.lock().remove(&module.id) {
let mut ident_replacer = IdentReplacer::new(id_to_replace);
ast.visit_mut_with(&mut ident_replacer);
}
let (cm, _) = create_swc_source_map(Source {
path: PathBuf::from(module.id.to_string()),
content: module.content.clone(),
});
try_with(cm, &context.meta.script.globals, || {
if let Some(id_to_replace) = id_to_replace.lock().remove(&module.id) {
let mut ident_replacer = IdentReplacer::new(id_to_replace);
ast.visit_mut_with(&mut ident_replacer);
}
})
.unwrap();
});

// update used exports of the module
Expand Down
2 changes: 1 addition & 1 deletion examples/react-antd/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default defineConfig({
},
presetEnv: false,
sourcemap: true,
persistentCache: true
persistentCache: false,
},
server: {
writeToDisk: false,
Expand Down

0 comments on commit d05eca8

Please sign in to comment.