-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from tajo/split
Add support for import split transformation
- Loading branch information
Showing
19 changed files
with
221 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod asseturl; | |
pub mod dirname; | ||
pub mod gql; | ||
pub mod i18n; | ||
pub mod split; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
)); | ||
}), | ||
}, | ||
}, | ||
_ => {} | ||
}, | ||
_ => (), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/fusion/transform/tests/fixtures/asseturl-use-file-name/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"transpileTemplateLiterals": false, | ||
"ssr": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"transpileTemplateLiterals": false, | ||
"ssr": true | ||
} |
1 change: 0 additions & 1 deletion
1
packages/fusion/transform/tests/fixtures/gql-use-file-name/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"transpileTemplateLiterals": false, | ||
"ssr": true | ||
} |
1 change: 0 additions & 1 deletion
1
packages/fusion/transform/tests/fixtures/i18n-component/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"transpileTemplateLiterals": false, | ||
"ssr": true | ||
} |
1 change: 0 additions & 1 deletion
1
packages/fusion/transform/tests/fixtures/i18n-use-translate/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
{ | ||
"transpileTemplateLiterals": false, | ||
"ssr": true | ||
} |
Oops, something went wrong.