diff --git a/.vscode/settings.json b/.vscode/settings.json index ebd73d16e806..5c4932a411f0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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 } diff --git a/crates/swc_plugin_compile_mode/src/tests/condition.rs b/crates/swc_plugin_compile_mode/src/tests/condition.rs index 591f1ba2da88..625799d8282b 100644 --- a/crates/swc_plugin_compile_mode/src/tests/condition.rs +++ b/crates/swc_plugin_compile_mode/src/tests/condition.rs @@ -72,4 +72,4 @@ test!( ) } "# -); \ No newline at end of file +); diff --git a/crates/swc_plugin_compile_mode_pre_process/src/lib.rs b/crates/swc_plugin_compile_mode_pre_process/src/lib.rs index aa16002450f1..5b1111a16139 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/lib.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/lib.rs @@ -7,6 +7,7 @@ use swc_core::{ plugin::{plugin_transform, proxies::TransformPluginProgramMetadata}, }; +#[cfg(test)] mod tests; mod utils; mod visitors; diff --git a/crates/swc_plugin_compile_mode_pre_process/src/tests/entry.rs b/crates/swc_plugin_compile_mode_pre_process/src/tests/entry.rs index 2ca11301a05a..0f78ca3f78be 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/tests/entry.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/tests/entry.rs @@ -9,7 +9,7 @@ test!( r#" export default function Index () { function renderA () { - return ComponentA + return ComponentA } return ( @@ -26,17 +26,17 @@ test!( |_| tr(), should_support_default_export_arrow_fn, r#" - export default () => { - function renderA () { - return ComponentA - } - - return ( - - {renderA()} - - ) + export default () => { + function renderA () { + return ComponentA } + + return ( + + {renderA()} + + ) + } "# ); @@ -47,7 +47,7 @@ test!( r#" export const ComponentA = () => { function renderA () { - return ComponentA + return ComponentA } return ( @@ -66,7 +66,7 @@ test!( r#" export function ComponentA () { function renderA () { - return ComponentA + return ComponentA } return ( @@ -85,7 +85,7 @@ test!( r#" function ComponentA () { function renderA () { - return ComponentA + return ComponentA } return ( @@ -104,7 +104,7 @@ test!( r#" const ComponentA = () => { function renderA () { - return ComponentA + return ComponentA } return ( diff --git a/crates/swc_plugin_compile_mode_pre_process/src/tests/props.rs b/crates/swc_plugin_compile_mode_pre_process/src/tests/props.rs index 7a8a99e0932b..fbe5b16a8c92 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/tests/props.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/tests/props.rs @@ -10,7 +10,7 @@ test!( export default function Index () { const list = [1, 2, 3]; function renderA (listA) { - return { + return { listA.map((item, index) => { return {item} }) @@ -34,7 +34,7 @@ test!( export default function Index () { const list = [1, 2, 3]; function renderA (listA) { - return { + return { listA.map((item, index) => { return {item} }) diff --git a/crates/swc_plugin_compile_mode_pre_process/src/tests/render.rs b/crates/swc_plugin_compile_mode_pre_process/src/tests/render.rs index 38e4434b6d11..5315b4d5ae54 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/tests/render.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/tests/render.rs @@ -13,7 +13,7 @@ test!( } function renderA () { - return + return ComponentA {renderB()} @@ -38,11 +38,11 @@ test!( r#" export default function Index () { function renderB () { - return ComponentB + return ComponentB } function renderC () { - return + return ComponentC {renderB()} {renderA()} @@ -50,14 +50,14 @@ test!( } function renderD () { - return + return ComponentD {renderB()} } function renderA () { - return + return ComponentA {renderB()} @@ -84,28 +84,28 @@ test!( r#" export default function Index () { function renderB () { - return + return ComponentB {renderA()} } function renderC () { - return + return ComponentC {renderA()} } function renderD () { - return + return ComponentD {renderC()} } function renderA () { - return + return ComponentA {renderB()} diff --git a/crates/swc_plugin_compile_mode_pre_process/src/utils/constant.rs b/crates/swc_plugin_compile_mode_pre_process/src/utils/constant.rs index 9ecaa25db695..8b9abc98522d 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/utils/constant.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/utils/constant.rs @@ -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"; diff --git a/crates/swc_plugin_compile_mode_pre_process/src/visitors/collect_render_fn.rs b/crates/swc_plugin_compile_mode_pre_process/src/visitors/collect_render_fn.rs index d0b0cfd3dddf..30a5e3098c49 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/visitors/collect_render_fn.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/visitors/collect_render_fn.rs @@ -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, @@ -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()), ); } - _ => {} } + _ => {} } } } @@ -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()), ); } - _ => {} } + _ => {} } } } diff --git a/crates/swc_plugin_compile_mode_pre_process/src/visitors/common.rs b/crates/swc_plugin_compile_mode_pre_process/src/visitors/common.rs deleted file mode 100644 index 238643ca0bfc..000000000000 --- a/crates/swc_plugin_compile_mode_pre_process/src/visitors/common.rs +++ /dev/null @@ -1,64 +0,0 @@ -use swc_core::ecma::{ast::*, visit::VisitMutWith}; - -use super::is_compile_mode_component::IsCompileModeVisitor; - -pub const COMPILE_MODE: &str = "compileMode"; -pub const DEFAULT_COMPONENT: &str = "Default_Component"; -pub const COMPILE_MODE_SUB_COMPONENT: &str = "compileModeSubComponent"; -pub struct RenderFn { - pub params: Vec, - pub jsx_element: JSXElement, -} - -impl RenderFn { - pub fn new(params: Vec, jsx_element: JSXElement) -> Self { - RenderFn { - params, - jsx_element, - } - } -} - -impl Clone for RenderFn { - fn clone(&self) -> Self { - RenderFn { - params: self.params.clone(), - jsx_element: self.jsx_element.clone(), - } - } -} - -pub struct ReactComponent { - pub name: String, - pub block_stmt: Option, -} - -impl Clone for ReactComponent { - fn clone(&self) -> Self { - ReactComponent { - name: self.name.clone(), - block_stmt: self.block_stmt.clone(), - } - } -} - -impl ReactComponent { - pub fn new(name: String, block_stmt: Option) -> Self { - ReactComponent { name, block_stmt } - } - - pub fn get_name(&self) -> String { - self.name.clone() - } - - pub fn is_valid(&mut self) -> bool { - // 1. 名称是否是大写字母开头 - let is_first_char_uppercase = self.get_name().chars().next().unwrap().is_uppercase(); - // 2. 返回的 JSX 里面有没有 compilerMode - let mut is_compile_mode_component: IsCompileModeVisitor = IsCompileModeVisitor::new(); - if let Some(block_stmt) = &mut self.block_stmt { - block_stmt.visit_mut_with(&mut is_compile_mode_component); - } - is_first_char_uppercase && is_compile_mode_component.valid - } -} diff --git a/crates/swc_plugin_compile_mode_pre_process/src/visitors/transform/process.rs b/crates/swc_plugin_compile_mode_pre_process/src/visitors/transform/process.rs index 48a8f89af900..98c133589ae5 100644 --- a/crates/swc_plugin_compile_mode_pre_process/src/visitors/transform/process.rs +++ b/crates/swc_plugin_compile_mode_pre_process/src/visitors/transform/process.rs @@ -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, }; @@ -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, diff --git a/crates/swc_plugin_compile_mode_pre_process/tests/__swc_snapshots__/src/tests/entry.rs/should_support_default_export_arrow_fn.js b/crates/swc_plugin_compile_mode_pre_process/tests/__swc_snapshots__/src/tests/entry.rs/should_support_default_export_arrow_fn.js index d240d7d47deb..58a9f263b4df 100644 --- a/crates/swc_plugin_compile_mode_pre_process/tests/__swc_snapshots__/src/tests/entry.rs/should_support_default_export_arrow_fn.js +++ b/crates/swc_plugin_compile_mode_pre_process/tests/__swc_snapshots__/src/tests/entry.rs/should_support_default_export_arrow_fn.js @@ -4,7 +4,7 @@ export default (()=>{ } return - { ComponentA } + { ComponentA } - ; + ; }); diff --git a/crates/swc_plugin_define_config/.editorconfig b/crates/swc_plugin_define_config/.editorconfig new file mode 100644 index 000000000000..7b227545b443 --- /dev/null +++ b/crates/swc_plugin_define_config/.editorconfig @@ -0,0 +1,6 @@ +[*.rs] +indent_style = space +indent_size = 4 + +[*.js] +trim_trailing_whitespace = false diff --git a/packages/taro-helper/scripts/constants.js b/packages/taro-helper/scripts/constants.js index eec7c023a1b6..52ec33c72d36 100644 --- a/packages/taro-helper/scripts/constants.js +++ b/packages/taro-helper/scripts/constants.js @@ -1,4 +1,5 @@ module.exports.plugins = [ 'swc_plugin_compile_mode', 'swc_plugin_define_config', + 'swc_plugin_compile_mode_pre_process', ] diff --git a/packages/taro-helper/swc-backup/swc_plugin_compile_mode.wasm b/packages/taro-helper/swc-backup/swc_plugin_compile_mode.wasm old mode 100644 new mode 100755 index 15a0276b8a81..cebb1d234e2c Binary files a/packages/taro-helper/swc-backup/swc_plugin_compile_mode.wasm and b/packages/taro-helper/swc-backup/swc_plugin_compile_mode.wasm differ diff --git a/packages/taro-helper/swc-backup/swc_plugin_compile_mode_pre_process.wasm b/packages/taro-helper/swc-backup/swc_plugin_compile_mode_pre_process.wasm new file mode 100755 index 000000000000..1541d374b646 Binary files /dev/null and b/packages/taro-helper/swc-backup/swc_plugin_compile_mode_pre_process.wasm differ diff --git a/packages/taro-webpack5-runner/src/loaders/miniCompilerLoader.ts b/packages/taro-webpack5-runner/src/loaders/miniCompilerLoader.ts index 73823e2ce230..5dba960d2cb5 100644 --- a/packages/taro-webpack5-runner/src/loaders/miniCompilerLoader.ts +++ b/packages/taro-webpack5-runner/src/loaders/miniCompilerLoader.ts @@ -53,6 +53,10 @@ export default async function (this: LoaderContext, source) { }, experimental: { plugins: [ + [ + '@tarojs/helper/swc/swc_plugin_compile_mode_pre_process.wasm', + {} + ], [ '@tarojs/helper/swc/swc_plugin_compile_mode.wasm', { diff --git a/rustfmt.toml b/rustfmt.toml index 3caaa87af730..cab5731edaff 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,2 @@ tab_spaces = 2 - edition = "2021"