Skip to content

Commit

Permalink
feat: Support dynamic imports from transform-imports plugin (#384)
Browse files Browse the repository at this point in the history
Closes #376
  • Loading branch information
kdy1 authored Dec 31, 2024
1 parent 5415fb1 commit a7a22a2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/perfect-vans-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@swc/plugin-transform-imports": patch
---

feat: Support dynamic imports from `transform-imports` plugin
44 changes: 43 additions & 1 deletion packages/transform-imports/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use swc_atoms::Atom;
use swc_cached::regex::CachedRegex;
use swc_common::DUMMY_SP;
use swc_ecma_ast::{ImportDecl, ImportSpecifier, ModuleExportName, *};
use swc_ecma_visit::{fold_pass, noop_fold_type, Fold};
use swc_ecma_visit::{fold_pass, noop_fold_type, Fold, FoldWith};

static DUP_SLASH_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"//").unwrap());

Expand Down Expand Up @@ -323,11 +323,52 @@ impl FoldImports {
}
None
}

fn handle_dynamic_import(&mut self, call: &CallExpr) -> Option<Atom> {
let first_arg = call.args.first()?;
if first_arg.spread.is_some() {
return None;
}

match &*first_arg.expr {
Expr::Lit(Lit::Str(s)) => {
let rewriter = self.should_rewrite(&s.value)?;

let new_module = rewriter.new_path(None);
Some(new_module)
}

Expr::Tpl(tpl) => {
if tpl.exprs.is_empty() {
let cooked = tpl.quasis[0].cooked.as_ref()?;
let rewriter = self.should_rewrite(cooked)?;

let new_module = rewriter.new_path(None);
Some(new_module)
} else {
None
}
}
_ => None,
}
}
}

impl Fold for FoldImports {
noop_fold_type!();

fn fold_call_expr(&mut self, mut call: CallExpr) -> CallExpr {
call = call.fold_children_with(self);

if call.callee.is_import() {
if let Some(new_module) = self.handle_dynamic_import(&call) {
call.args.first_mut().unwrap().expr = new_module.into();
}
}

call
}

fn fold_module(&mut self, mut module: Module) -> Module {
let mut new_items: Vec<ModuleItem> = vec![];
for item in module.body {
Expand Down Expand Up @@ -392,6 +433,7 @@ impl Fold for FoldImports {
}
}
}
let new_items = new_items.fold_children_with(self);
module.body = new_items;
module
}
Expand Down
10 changes: 10 additions & 0 deletions packages/transform-imports/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ fn modularize_imports_fixture(input: PathBuf) {
handle_namespace_import: true,
},
),
(
"^(\\..*)(\\.tsx?)$".to_string(),
PackageConfig {
transform: "{{matches.[1]}}.js".into(),
prevent_full_import: false,
skip_default_conversion: false,
handle_default_import: false,
handle_namespace_import: false,
},
),
]
.into_iter()
.collect(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
await import('./stringLiteral.tsx');
await import(`./templateLiteral.ts`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
await import("./stringLiteral.js");
await import("./templateLiteral.js");

0 comments on commit a7a22a2

Please sign in to comment.