Skip to content

Commit

Permalink
feat: 接入半编译预处理 swc 插件
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEJIA-LIU committed Sep 24, 2024
1 parent 54f1a1b commit e435c31
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 120 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"./crates/taro_init/Cargo.toml",
"./crates/native_binding/Cargo.toml",
"./crates/swc_plugin_compile_mode/Cargo.toml",
"./crates/swc_plugin_define_config/Cargo.toml"
"./crates/swc_plugin_define_config/Cargo.toml",
"./crates/swc_plugin_compile_mode_pre_process/Cargo.toml"
],
"rust-analyzer.showUnlinkedFileNotification": false
}
2 changes: 1 addition & 1 deletion crates/swc_plugin_compile_mode/src/tests/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ test!(
)
}
"#
);
);
1 change: 1 addition & 0 deletions crates/swc_plugin_compile_mode_pre_process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use swc_core::{
plugin::{plugin_transform, proxies::TransformPluginProgramMetadata},
};

#[cfg(test)]
mod tests;
mod utils;
mod visitors;
Expand Down
30 changes: 15 additions & 15 deletions crates/swc_plugin_compile_mode_pre_process/src/tests/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test!(
r#"
export default function Index () {
function renderA () {
return <View compileModeSubComponent> ComponentA </View>
return <View compileMode="subRenderFn"> ComponentA </View>
}
return (
Expand All @@ -26,17 +26,17 @@ test!(
|_| tr(),
should_support_default_export_arrow_fn,
r#"
export default () => {
function renderA () {
return <View compileModeSubComponent> ComponentA </View>
}
return (
<View compileMode>
{renderA()}
</View>
)
export default () => {
function renderA () {
return <View compileMode="subRenderFn"> ComponentA </View>
}
return (
<View compileMode>
{renderA()}
</View>
)
}
"#
);

Expand All @@ -47,7 +47,7 @@ test!(
r#"
export const ComponentA = () => {
function renderA () {
return <View compileModeSubComponent> ComponentA </View>
return <View compileMode="subRenderFn"> ComponentA </View>
}
return (
Expand All @@ -66,7 +66,7 @@ test!(
r#"
export function ComponentA () {
function renderA () {
return <View compileModeSubComponent> ComponentA </View>
return <View compileMode="subRenderFn"> ComponentA </View>
}
return (
Expand All @@ -85,7 +85,7 @@ test!(
r#"
function ComponentA () {
function renderA () {
return <View compileModeSubComponent> ComponentA </View>
return <View compileMode="subRenderFn"> ComponentA </View>
}
return (
Expand All @@ -104,7 +104,7 @@ test!(
r#"
const ComponentA = () => {
function renderA () {
return <View compileModeSubComponent> ComponentA </View>
return <View compileMode="subRenderFn"> ComponentA </View>
}
return (
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_plugin_compile_mode_pre_process/src/tests/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test!(
export default function Index () {
const list = [1, 2, 3];
function renderA (listA) {
return <View compileModeSubComponent> {
return <View compileMode="subRenderFn"> {
listA.map((item, index) => {
return <View key={index}> {item} </View>
})
Expand All @@ -34,7 +34,7 @@ test!(
export default function Index () {
const list = [1, 2, 3];
function renderA (listA) {
return <View compileModeSubComponent> {
return <View compileMode="subRenderFn"> {
listA.map((item, index) => {
return <View key={index}> {item} </View>
})
Expand Down
18 changes: 9 additions & 9 deletions crates/swc_plugin_compile_mode_pre_process/src/tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test!(
}
function renderA () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentA</View>
{renderB()}
</View>
Expand All @@ -38,26 +38,26 @@ test!(
r#"
export default function Index () {
function renderB () {
return <View compileModeSubComponent> ComponentB </View>
return <View compileMode="subRenderFn"> ComponentB </View>
}
function renderC () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentC</View>
{renderB()}
{renderA()}
</View>
}
function renderD () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentD</View>
{renderB()}
</View>
}
function renderA () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentA</View>
{renderB()}
</View>
Expand All @@ -84,28 +84,28 @@ test!(
r#"
export default function Index () {
function renderB () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentB </View>
{renderA()}
</View>
}
function renderC () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentC</View>
{renderA()}
</View>
}
function renderD () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentD</View>
{renderC()}
</View>
}
function renderA () {
return <View compileModeSubComponent>
return <View compileMode="subRenderFn">
<View>ComponentA</View>
{renderB()}
</View>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub const COMPILE_MODE: &str = "compileMode";
pub const COMPILE_MODE_SUB_RENDER_FN_VAL: &str = "subRenderFn";
pub const DEFAULT_COMPONENT: &str = "Default_Component";
pub const COMPILE_MODE_SUB_COMPONENT: &str = "compileModeSubComponent";
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

use crate::utils::{constant::COMPILE_MODE_SUB_COMPONENT, render_fn::RenderFn};
use crate::utils::{
constant::{COMPILE_MODE, COMPILE_MODE_SUB_RENDER_FN_VAL},
render_fn::RenderFn,
};

pub struct CollectRenderFnVisitor {
pub raw_render_fn_map: HashMap<String, RenderFn>,
Expand Down Expand Up @@ -105,20 +108,27 @@ impl VisitMut for CollectRenderFnVisitor {
if let Expr::JSXElement(jsx_element) = &*paren_expr.expr {
for attr in &jsx_element.opening.attrs {
if let JSXAttrOrSpread::JSXAttr(jsx_attr) = attr {
if let JSXAttrName::Ident(jsx_attr_name) = &jsx_attr.name {
match (
jsx_attr_name.sym == COMPILE_MODE_SUB_COMPONENT,
&self.sub_component_name,
&self.sub_component_params,
) {
(true, Some(sub_component_name), Some(sub_component_params)) => {
match (
&jsx_attr.name,
&jsx_attr.value,
&self.sub_component_name,
&self.sub_component_params,
) {
(
JSXAttrName::Ident(jsx_attr_name),
Some(JSXAttrValue::Lit(Lit::Str(Str { value, .. }))),
Some(sub_component_name),
Some(sub_component_params),
) => {
if jsx_attr_name.sym == COMPILE_MODE && value == COMPILE_MODE_SUB_RENDER_FN_VAL
{
self.raw_render_fn_map.insert(
sub_component_name.clone(),
RenderFn::new(sub_component_params.clone(), *jsx_element.clone()),
);
}
_ => {}
}
_ => {}
}
}
}
Expand All @@ -127,20 +137,26 @@ impl VisitMut for CollectRenderFnVisitor {
Expr::JSXElement(jsx_element) => {
for attr in &jsx_element.opening.attrs {
if let JSXAttrOrSpread::JSXAttr(jsx_attr) = attr {
if let JSXAttrName::Ident(jsx_attr_name) = &jsx_attr.name {
match (
jsx_attr_name.sym == COMPILE_MODE_SUB_COMPONENT,
&self.sub_component_name,
&self.sub_component_params,
) {
(true, Some(sub_component_name), Some(sub_component_params)) => {
match (
&jsx_attr.name,
&jsx_attr.value,
&self.sub_component_name,
&self.sub_component_params,
) {
(
JSXAttrName::Ident(jsx_attr_name),
Some(JSXAttrValue::Lit(Lit::Str(Str { value, .. }))),
Some(sub_component_name),
Some(sub_component_params),
) => {
if jsx_attr_name.sym == COMPILE_MODE && value == COMPILE_MODE_SUB_RENDER_FN_VAL {
self.raw_render_fn_map.insert(
sub_component_name.clone(),
RenderFn::new(sub_component_params.clone(), *jsx_element.clone()),
);
}
_ => {}
}
_ => {}
}
}
}
Expand Down
64 changes: 0 additions & 64 deletions crates/swc_plugin_compile_mode_pre_process/src/visitors/common.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use swc_core::ecma::{
};

use crate::utils::{
constant::{COMPILE_MODE, COMPILE_MODE_SUB_COMPONENT},
constant::{COMPILE_MODE, COMPILE_MODE_SUB_RENDER_FN_VAL},
render_fn::RenderFn,
};

Expand All @@ -30,19 +30,28 @@ impl<'a> VisitMut for TransformProcessVisitor<'a> {
if !self.in_compile_mode_jsx {
for attr in &mut el.opening.attrs {
if let JSXAttrOrSpread::JSXAttr(jsx_attr) = attr {
if let JSXAttrName::Ident(jsx_attr_name) = &jsx_attr.name {
if jsx_attr_name.sym == COMPILE_MODE {
self.in_compile_mode_jsx = true;
break;
match (&jsx_attr.name, &jsx_attr.value) {
(
JSXAttrName::Ident(jsx_attr_name),
None
) => {
if jsx_attr_name.sym == COMPILE_MODE {
self.in_compile_mode_jsx = true;
break;
}
}
_ => {}
}
}
}
}

el.opening.attrs.retain(|attr| match &attr {
JSXAttrOrSpread::JSXAttr(JSXAttr { name, .. }) => match name {
JSXAttrName::Ident(jsx_attr_name) => jsx_attr_name.sym != COMPILE_MODE_SUB_COMPONENT,
JSXAttrOrSpread::JSXAttr(JSXAttr { name, value, .. }) => match (name, value) {
(
JSXAttrName::Ident(jsx_attr_name),
Some(JSXAttrValue::Lit(Lit::Str(Str { value, .. }))),
) => !(jsx_attr_name.sym == COMPILE_MODE && value == COMPILE_MODE_SUB_RENDER_FN_VAL),
_ => true,
},
_ => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default (()=>{
}
return <View compileMode>

{<View> ComponentA </View>}
{<View> ComponentA </View>}

</View>;
</View>;
});
Loading

0 comments on commit e435c31

Please sign in to comment.