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

feat: enhance dynamic import with template argument #266

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
57 changes: 55 additions & 2 deletions src/dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ast::BinExpr;
use ast::Module;
use serde::Deserialize;
use serde::Serialize;
use swc_ecma_ast::{Ident, IdentName, MemberExpr, MemberProp};

use crate::swc::ast;
use crate::swc::ast::Callee;
Expand Down Expand Up @@ -161,6 +162,8 @@ pub enum DynamicArgument {
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DynamicTemplatePart {
String(Atom),
/// An member expression that may be analyzed.
MemberExpr(Atom, Atom, Atom),
/// An expression that could not be analyzed.
Expr,
}
Expand Down Expand Up @@ -304,8 +307,36 @@ impl<'a> Visit for DependencyCollector<'a> {
if cooked.len() > 0 {
parts.push(DynamicTemplatePart::String(cooked.clone()));
}
if tpl.exprs.get(i).is_some() {
parts.push(DynamicTemplatePart::Expr);

let mut is_member_expr = false;

if let Some(expr) = tpl.exprs.get(i) {
if let Expr::Member(MemberExpr {
prop: MemberProp::Ident(IdentName { sym: ref sym2, .. }),
ref obj,
..
}) = **expr
{
if let Expr::Member(MemberExpr {
prop: MemberProp::Ident(IdentName { sym: ref sym1, .. }),
obj: ref obj1,
..
}) = **obj
{
if let Expr::Ident(Ident { ref sym, .. }) = **obj1 {
parts.push(DynamicTemplatePart::MemberExpr(
sym.clone(),
sym1.clone(),
sym2.clone(),
));
is_member_expr = true;
}
}
}

if !is_member_expr {
parts.push(DynamicTemplatePart::Expr);
}
}
}
DynamicArgument::Template(parts)
Expand Down Expand Up @@ -881,6 +912,7 @@ const d9 = await import("./foo/" + value + ".ts");
const d10 = await import(value + ".ts");
const d11 = await import("./foo/" - value);
const d12 = await import(expr);
const d13 = await import(`${Deno.build.target}/test`);
"#;
let (start_pos, dependencies) = helper("file:///test.ts", source);
assert_eq!(
Expand Down Expand Up @@ -1008,6 +1040,27 @@ const d12 = await import(expr);
import_attributes: ImportAttributes::None,
}
.into(),
DynamicDependencyDescriptor {
leading_comments: Vec::new(),
range: SourceRange {
start: start_pos + 543,
end: start_pos + 578,
},
argument: DynamicArgument::Template(vec![
DynamicTemplatePart::MemberExpr(
JsWord::from("Deno"),
JsWord::from("build"),
JsWord::from("target")
),
DynamicTemplatePart::String(JsWord::from("/test")),
]),
argument_range: SourceRange {
start: start_pos + 550,
end: start_pos + 577,
},
import_attributes: ImportAttributes::None,
}
.into(),
]
);
}
Expand Down