Skip to content

Commit

Permalink
Merge pull request #4 from tajo/split
Browse files Browse the repository at this point in the history
Add support for import split transformation
  • Loading branch information
tajo committed Jul 6, 2023
2 parents 3553ed1 + fde4b78 commit 2a40432
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 70 deletions.
18 changes: 10 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/fusion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swc-plugin-fusion",
"version": "1.0.4",
"version": "1.0.8",
"description": "SWC plugin for fusion",
"main": "plugin_fusion.wasm",
"publishConfig": {
Expand Down
4 changes: 4 additions & 0 deletions packages/fusion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ fn fusion_asseturl(mut program: Program, data: TransformPluginProgramMetadata) -

program.visit_mut_with(&mut pass);

let mut pass = fusion::split_macro(file_name.clone());

program.visit_mut_with(&mut pass);

debug!("Running i18n macro");

let mut pass = fusion::i18n_macro(file_name.clone());
Expand Down
2 changes: 2 additions & 0 deletions packages/fusion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ base64 = "0.13"
byteorder = "1"
fxhash = "0.2.1"
once_cell = "1.13.0"
percent-encoding = "2.3.0"
radix_fmt = "1"
regex = "1.5"
serde = "1"
Expand All @@ -27,6 +28,7 @@ swc_core = { features = [
"trace_macro",
], version = "0.76.6" }
tracing = { version = "0.1.37" }
url = "2.2.2"

[dev-dependencies]
serde_json = "1"
Expand Down
31 changes: 0 additions & 31 deletions packages/fusion/transform/src/dirname/analyzer.rs

This file was deleted.

19 changes: 0 additions & 19 deletions packages/fusion/transform/src/dirname/mod.rs

This file was deleted.

11 changes: 7 additions & 4 deletions packages/fusion/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ pub use crate::{
asseturl_utils::{analyzer as asseturlAnalyzer, State as asseturlState},
gql_utils::{analyzer as gqlAnalyzer, State as gqlState},
i18n::{i18n_analyze_imports, i18n_analyze_use_translation, State as i18n_state},
visitors::{asseturl::asseturl, dirname::dirname, gql::gql, i18n::i18n_report_ids},
visitors::{
asseturl::asseturl, dirname::dirname, gql::gql, i18n::i18n_report_ids, split::split,
},
};

mod asseturl_utils;
Expand Down Expand Up @@ -96,8 +98,9 @@ pub fn gql_macro(config: Config) -> impl Fold + VisitMut {
}

pub fn dirname_macro(file_name: FileName) -> impl Fold + VisitMut {
// let state: Rc<RefCell<gqlState>> = Default::default();
// let config = Rc::new(config);

dirname(file_name)
}

pub fn split_macro(file_name: FileName) -> impl Fold + VisitMut {
split(file_name)
}
1 change: 1 addition & 0 deletions packages/fusion/transform/src/visitors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod asseturl;
pub mod dirname;
pub mod gql;
pub mod i18n;
pub mod split;
176 changes: 176 additions & 0 deletions packages/fusion/transform/src/visitors/split.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use swc_core::{
common::{errors::HANDLER, FileName, DUMMY_SP},
ecma::{
ast::*,
visit::{as_folder, noop_visit_mut_type, Fold, VisitMut},
},
};

pub fn split(file_name: FileName) -> impl VisitMut + Fold {
as_folder(DisplayNameAndId { file_name })
}

#[derive(Debug)]
struct DisplayNameAndId {
file_name: FileName,
}

impl VisitMut for DisplayNameAndId {
noop_visit_mut_type!();

fn visit_mut_call_expr(&mut self, call_expr: &mut CallExpr) {
match &call_expr.callee {
Callee::Import(_) => match call_expr.args.first() {
Some(arg) => match arg {
ExprOrSpread { expr, .. } => match &**expr {
&Expr::Lit(ref lit) => match lit {
Lit::Str(lit_str) => {
let obj_ident = Expr::Ident(Ident::new("Object".into(), DUMMY_SP));
let define_props_ident = MemberProp::Ident(Ident::new(
"defineProperties".into(),
DUMMY_SP,
));
let new_callee = Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: Box::new(obj_ident),
prop: define_props_ident,
});
let import_arg = Expr::Lit(Lit::Str(Str {
value: lit_str.value.clone(),
span: DUMMY_SP,
raw: Default::default(),
}));

let import_callee =
Expr::Ident(Ident::new("import".into(), DUMMY_SP));
let import_call = Expr::Call(CallExpr {
span: DUMMY_SP,
callee: Callee::Expr(Box::new(import_callee)),
args: vec![ExprOrSpread {
spread: None,
expr: Box::new(import_arg.clone()),
}],
type_args: Default::default(),
});

let encoded_file_name = utf8_percent_encode(
&self.file_name.to_string(),
NON_ALPHANUMERIC,
)
.to_string();

let encoded_import = utf8_percent_encode(
&lit_str.value.clone().to_string(),
NON_ALPHANUMERIC,
)
.to_string();

let module_id = format!(
"{}{}{}{}",
"virtual:fusion-vite-split-loader?importer=",
encoded_file_name,
"&specifier=",
encoded_import
);

let obj_lit = Expr::Object(ObjectLit {
span: DUMMY_SP,
props: vec![
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("__CHUNK_IDS".into(), DUMMY_SP)),
value: Box::new(Expr::Object(ObjectLit {
span: DUMMY_SP,
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("value".into(), DUMMY_SP)),
value: Box::new(Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: vec![],
})),
})))],
})),
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("__MODULE_ID".into(), DUMMY_SP)),
value: Box::new(Expr::Object(ObjectLit {
span: DUMMY_SP,
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("value".into(), DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: module_id.clone().into(),
raw: Default::default(),
}))),
})))],
})),
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("__FUSION_DYNAMIC_IMPORT_METADATA__".into(), DUMMY_SP)),
value: Box::new(Expr::Object(ObjectLit {
span: DUMMY_SP,
props: vec![PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("value".into(), DUMMY_SP)),
value: Box::new(Expr::Object(ObjectLit {
span: DUMMY_SP,
props: vec![
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("version".into(), DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: 0.0,
raw: Default::default(),
}))),
}))),
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new("moduleId".into(), DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: module_id.clone().into(),
raw: Default::default(),
}))),
}))),
],
})),
})))],
})),
}))),
],
});

let new_args = vec![
ExprOrSpread {
spread: None,
expr: Box::new(import_call),
},
ExprOrSpread {
spread: None,
expr: Box::new(obj_lit),
},
];

*call_expr = CallExpr {
span: call_expr.span,
callee: Callee::Expr(Box::new(new_callee)),
args: new_args,
type_args: Default::default(),
};
}
_ => HANDLER.with(|handler| {
handler.err(&format!(
"Only string literal is supported in dynamic import"
));
}),
},
_ => HANDLER.with(|handler| {
handler.err(&format!(
"Only string literal is supported in dynamic import"
));
}),
},
},
_ => {}
},
_ => (),
}
}
}
3 changes: 2 additions & 1 deletion packages/fusion/transform/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{fs::read_to_string, path::PathBuf};

use fusion::{asseturl_macro, dirname_macro, gql_macro, i18n_macro, Config};
use fusion::{asseturl_macro, dirname_macro, gql_macro, i18n_macro, split_macro, Config};
use swc_core::{
common::{chain, FileName, Mark},
ecma::{
Expand All @@ -29,6 +29,7 @@ fn fixture(input: PathBuf) {
asseturl_macro(config.clone()),
gql_macro(config.clone()),
dirname_macro(FileName::Real(PathBuf::from("/path/to/file.js"))),
split_macro(FileName::Real(PathBuf::from("/path/to/file.js"))),
i18n_macro(FileName::Real(PathBuf::from("/path/to/file.js"))),
)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"transpileTemplateLiterals": false,
"ssr": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"transpileTemplateLiterals": false,
"ssr": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"transpileTemplateLiterals": false,
"ssr": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"transpileTemplateLiterals": false,
"ssr": true
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"transpileTemplateLiterals": false,
"ssr": true
}

0 comments on commit 2a40432

Please sign in to comment.