Skip to content

Commit

Permalink
feat: implment importkind
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Nov 5, 2023
1 parent 9f606a3 commit 2d0bbc3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
6 changes: 3 additions & 3 deletions crates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use swc_core::plugin::proxies::TransformPluginProgramMetadata;
use swc_core::{
ecma::{ast::Program, visit::FoldWith},
ecma::{ast::Program, visit::as_folder, visit::FoldWith},
plugin::plugin_transform,
};

Expand All @@ -15,6 +15,6 @@ pub fn external_globals_plugin(program: Program, meta: TransformPluginProgramMet
.get_transform_plugin_config()
.expect("failed to get plugin config for 'external_globals_plugin'"),
)
.expect("invalid config for external_globals_plugin");
program
.expect("invalid config for 'external_globals_plugin'");
program.fold_with(&mut as_folder(visitor::ExternalGlobals::init(opts)))
}
72 changes: 72 additions & 0 deletions crates/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::Deserialize;
use std::collections::HashMap;
use swc_core::ecma::ast::*;
use swc_core::ecma::visit::VisitMut;

#[derive(Debug, Clone, Deserialize)]
pub struct ModuleInfo {
Expand All @@ -12,3 +14,73 @@ pub struct Options {
pub dependency: HashMap<String, ModuleInfo>,
pub dependency_with_alias: HashMap<String, String>,
}

pub struct ExternalGlobals {
dependency: HashMap<String, ModuleInfo>,
dependency_with_alias: HashMap<String, String>,
import_references: HashMap<String, String>,
imports_vec: Vec<usize>,
}

impl ExternalGlobals {
pub fn init(optins: Options) -> Self {
Self {
dependency: optins.dependency,
dependency_with_alias: optins.dependency_with_alias,
imports_vec: Default::default(),
import_references: Default::default(),
}
}
fn scan_import_decl(&mut self, import_decl: &ImportDecl, index: usize) {
let val = &mut import_decl.src.value.to_string();
match self.dependency_with_alias.get(val) {
Some(aliase) => {
if let Some(global_name) = self.dependency.get(aliase).map(|dep| &dep.global) {
self.imports_vec.push(index);
import_decl.specifiers.iter().for_each(|spec| match spec {
ImportSpecifier::Named(s) => {
// swc imoorted type is null
let s_local_name = s.local.sym.to_string();
let name = s_local_name.clone();
let mut ref_name = name.to_string();
ref_name.insert_str(0, global_name);

self.import_references.insert(name, ref_name);
}
ImportSpecifier::Default(s) => {
let s_local_name = s.local.sym.to_string();
let name = s_local_name.clone();
let mut ref_name = name.to_string();
ref_name.insert_str(0, &global_name);
self.import_references.insert(name, ref_name);
}
ImportSpecifier::Namespace(s) => {
let s_local_name = s.local.sym.to_string();
let name = s_local_name.clone();
let mut ref_name = name.to_string();
ref_name.insert_str(0, &global_name);
self.import_references.insert(name, ref_name);
}
})
}
}
None => {}
}
}
}

impl VisitMut for ExternalGlobals {
fn visit_mut_module(&mut self, n: &mut Module) {
for (index, item) in n.body.iter().enumerate() {
if let ModuleItem::ModuleDecl(decl) = item {
if let ModuleDecl::Import(import_decl) = decl {
self.scan_import_decl(import_decl, index);
}
}
}
self.imports_vec.iter().for_each(|i| {
let s = *i;
n.body.remove(s);
});
}
}

0 comments on commit 2d0bbc3

Please sign in to comment.