Skip to content

Commit

Permalink
refactor: 🎨 idiomatic swc ast generate
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly committed Jul 3, 2024
1 parent e271894 commit 03bc35f
Showing 1 changed file with 15 additions and 34 deletions.
49 changes: 15 additions & 34 deletions crates/mako/src/visitors/dynamic_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use swc_core::ecma::utils::{
};
use swc_core::ecma::visit::{VisitMut, VisitMutWith};

use crate::ast::utils::{
id, is_dynamic_import, member_call, member_prop, promise_all, require_ensure,
};
use crate::ast::utils::{is_dynamic_import, promise_all, require_ensure};
use crate::compiler::Context;
use crate::generate::chunk::ChunkId;
use crate::visitors::dep_replacer::DependenciesToReplace;
Expand Down Expand Up @@ -133,45 +131,28 @@ impl<'a> VisitMut for DynamicImport<'a> {
.collect::<Vec<_>>();
let load_promise = promise_all(ExprOrSpread {
spread: None,
expr: Box::new(Expr::Array(ArrayLit {
expr: ArrayLit {
span: DUMMY_SP,
elems: to_ensure_elems,
})),
}
.into(),
});

let require_call = member_call(
Expr::Ident(id("__mako_require__")),
member_prop("bind"),
vec![
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Ident(id("__mako_require__"))),
},
ExprOrSpread {
spread: None,
expr: Box::new(Expr::Lit(Lit::Str(resolved_source.into()))),
},
],
);
let lazy_require_call = member_expr!(DUMMY_SP, __mako_require__.bind)
.as_call(
DUMMY_SP,
vec![
quote_ident!("__mako_require__").as_arg(),
quote_str!(resolved_source.clone()).as_arg(),
],
);
let dr_call = member_expr!(DUMMY_SP, __mako_require__.dr).as_call(
DUMMY_SP,
vec![
self.interop.clone().as_arg(),
ExprOrSpread {
spread: None,
expr: Box::new(require_call),
},
],
vec![self.interop.clone().as_arg(), lazy_require_call.as_arg()],
);

member_call(
load_promise,
member_prop("then"),
vec![ExprOrSpread {
spread: None,
expr: dr_call.into(),
}],
)
member_expr!(@EXT, DUMMY_SP, load_promise.into(), then)
.as_call(call_expr.span, vec![dr_call.as_arg()])
};
}
}
Expand Down

0 comments on commit 03bc35f

Please sign in to comment.