As part of Toast I needed to port a babel plugin that included rewriting some imports from one target to another. This is a simple version of the transformation I needed to make happed in a JS file:
import Thing from "preact";
to
import Thing from "not-preact";
Here's the code I used to do it in Rust
use std::path::Path;use string_cache::Atom;use swc_ecma_ast::{ImportDecl, Str};use swc_ecma_visit::{noop_fold_type, Fold};pub struct SWCImportMapRewrite<'a> {pub import_map: &'a Path,}impl Fold for SWCImportMapRewrite<'_> {noop_fold_type!();fn fold_import_decl(&mut self,imports: ImportDecl,) -> ImportDecl {ImportDecl {src: Str {value: Atom::from("not-preact"),..imports.src},..imports}}}
program.fold_with(&mut SWCImportMapRewrite {import_map: Path::new("thing"),})