Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 🎨 idiomatic and concise SWC ast generate #1372

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading