Skip to content

Commit

Permalink
feat: 设置半编译预处理config
Browse files Browse the repository at this point in the history
  • Loading branch information
ZEJIA-LIU committed Oct 23, 2024
1 parent f00b14a commit 6e7a0ca
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 77 deletions.
10 changes: 2 additions & 8 deletions crates/swc_plugin_compile_mode_pre_process/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@ mod tests;
mod utils;
mod visitors;
use visitors::entry::EntryVisitor;
struct SerdeDefault;
impl SerdeDefault {
fn platform_default() -> String {
String::from("WEAPP")
}
}

#[derive(Deserialize, Debug)]
pub struct PluginConfig {
#[serde(default = "SerdeDefault::platform_default")]
pub platform: String,
#[serde(default)]
sub_render_fn: String,
}

#[plugin_transform]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn tr() -> impl Fold {
let config = serde_json::from_str::<PluginConfig>(
r#"
{
"sub_render_fn": "subRenderFn"
}"#,
)
.unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub const COMPILE_MODE: &str = "compileMode";
pub const COMPILE_MODE_SUB_RENDER_FN_VAL: &str = "subRenderFn";
pub const DEFAULT_COMPONENT: &str = "Default_Component";
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

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

pub struct CollectRenderFnVisitor {
pub struct CollectRenderFnVisitor<'a> {
pub raw_render_fn_map: HashMap<String, RenderFn>,
sub_component_name: Option<String>,
sub_component_params: Option<Vec<Pat>>,
in_outmost_block_scope: bool,
config: &'a PluginConfig,
}

impl CollectRenderFnVisitor {
pub fn new() -> Self {
impl<'a> CollectRenderFnVisitor<'a> {
pub fn new(config: &'a PluginConfig) -> Self {
CollectRenderFnVisitor {
raw_render_fn_map: HashMap::new(),
sub_component_name: None,
sub_component_params: None,
in_outmost_block_scope: true,
config,
}
}
}
//只在最外层找就可以了,因为这个函数是一个 react 组件的入口
impl VisitMut for CollectRenderFnVisitor {
impl<'a> VisitMut for CollectRenderFnVisitor<'a> {
fn visit_mut_block_stmt(&mut self, n: &mut BlockStmt) {
if !self.in_outmost_block_scope {
return;
Expand Down Expand Up @@ -120,8 +122,7 @@ impl VisitMut for CollectRenderFnVisitor {
Some(sub_component_name),
Some(sub_component_params),
) => {
if jsx_attr_name.sym == COMPILE_MODE && value == COMPILE_MODE_SUB_RENDER_FN_VAL
{
if jsx_attr_name.sym == COMPILE_MODE && *value == self.config.sub_render_fn {
self.raw_render_fn_map.insert(
sub_component_name.clone(),
RenderFn::new(sub_component_params.clone(), *jsx_element.clone()),
Expand Down Expand Up @@ -149,7 +150,7 @@ impl VisitMut for CollectRenderFnVisitor {
Some(sub_component_name),
Some(sub_component_params),
) => {
if jsx_attr_name.sym == COMPILE_MODE && value == COMPILE_MODE_SUB_RENDER_FN_VAL {
if jsx_attr_name.sym == COMPILE_MODE && *value == self.config.sub_render_fn {
self.raw_render_fn_map.insert(
sub_component_name.clone(),
RenderFn::new(sub_component_params.clone(), *jsx_element.clone()),
Expand Down
10 changes: 7 additions & 3 deletions crates/swc_plugin_compile_mode_pre_process/src/visitors/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use super::generate_deps::GenerateDepsVisitor;
use super::transform::component_entry::ComponentEntryVisitor;
pub struct EntryVisitor {
visitor_context: VisitorContext,
config: PluginConfig,
}

struct VisitorContext {
Expand All @@ -31,6 +32,7 @@ impl EntryVisitor {
react_component_formatted_render_fn_map: HashMap::new(),
react_component_list: vec![],
},
config,
}
}
}
Expand Down Expand Up @@ -75,7 +77,7 @@ impl EntryVisitor {
.clone()
.into_iter()
.for_each(|mut react_component| {
let mut collect_render_fn_visitor = CollectRenderFnVisitor::new();
let mut collect_render_fn_visitor = CollectRenderFnVisitor::new(&self.config);
react_component
.block_stmt
.visit_mut_with(&mut collect_render_fn_visitor);
Expand Down Expand Up @@ -121,8 +123,10 @@ impl EntryVisitor {
}

fn transform(&mut self, n: &mut Module) {
let mut transform_visitor =
ComponentEntryVisitor::new(&self.visitor_context.react_component_formatted_render_fn_map);
let mut transform_visitor = ComponentEntryVisitor::new(
&self.visitor_context.react_component_formatted_render_fn_map,
&self.config,
);
n.visit_mut_children_with(&mut transform_visitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,26 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

use crate::utils::{constant::DEFAULT_COMPONENT, render_fn::RenderFn};
use crate::{
utils::{constant::DEFAULT_COMPONENT, render_fn::RenderFn},
PluginConfig,
};

use super::process::TransformProcessVisitor;

pub struct ComponentEntryVisitor<'a> {
format_render_fn_map: &'a HashMap<String, HashMap<String, RenderFn>>,
config: &'a PluginConfig,
}

impl<'a> ComponentEntryVisitor<'a> {
pub fn new(format_render_fn_map: &'a HashMap<String, HashMap<String, RenderFn>>) -> Self {
pub fn new(
format_render_fn_map: &'a HashMap<String, HashMap<String, RenderFn>>,
config: &'a PluginConfig,
) -> Self {
ComponentEntryVisitor {
format_render_fn_map,
config: config,
}
}

Expand All @@ -42,7 +50,7 @@ impl<'a> VisitMut for ComponentEntryVisitor<'a> {
let component_name = ident.sym.to_string();
let render_fn_map = self.get_format_render_fn_map_by_component_name(&component_name);
if let Some(render_fn_map) = render_fn_map {
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map);
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map, &self.config);
decl.init.visit_mut_children_with(&mut process_visitor);
}
}
Expand All @@ -55,7 +63,8 @@ impl<'a> VisitMut for ComponentEntryVisitor<'a> {
let component_name = ident.sym.to_string();
let render_fn_map = self.get_format_render_fn_map_by_component_name(&component_name);
if let Some(render_fn_map) = render_fn_map {
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map);
let mut process_visitor =
TransformProcessVisitor::new(&render_fn_map, &self.config);
decl.init.visit_mut_children_with(&mut process_visitor);
}
}
Expand All @@ -72,7 +81,7 @@ impl<'a> VisitMut for ComponentEntryVisitor<'a> {
let component_name = n.ident.sym.to_string();
let render_fn_map = self.get_format_render_fn_map_by_component_name(&component_name);
if let Some(render_fn_map) = render_fn_map {
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map);
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map, &self.config);
n.function
.body
.visit_mut_children_with(&mut process_visitor);
Expand All @@ -87,7 +96,7 @@ impl<'a> VisitMut for ComponentEntryVisitor<'a> {
let render_fn_map =
self.get_format_render_fn_map_by_component_name(&DEFAULT_COMPONENT.to_string());
if let Some(render_fn_map) = render_fn_map {
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map);
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map, &self.config);
fn_decl
.function
.body
Expand All @@ -107,7 +116,7 @@ impl<'a> VisitMut for ComponentEntryVisitor<'a> {
let render_fn_map =
self.get_format_render_fn_map_by_component_name(&DEFAULT_COMPONENT.to_string());
if let Some(render_fn_map) = render_fn_map {
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map);
let mut process_visitor = TransformProcessVisitor::new(&render_fn_map, &self.config);
n.expr.visit_mut_children_with(&mut process_visitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ use swc_core::ecma::{
visit::{VisitMut, VisitMutWith},
};

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

pub struct TransformProcessVisitor<'a> {
render_fn_map: &'a HashMap<String, RenderFn>,
in_compile_mode_jsx: bool,
config: &'a PluginConfig,
}

impl<'a> TransformProcessVisitor<'a> {
pub fn new(render_fn_map: &'a HashMap<String, RenderFn>) -> Self {
pub fn new(render_fn_map: &'a HashMap<String, RenderFn>, config: &'a PluginConfig) -> Self {
TransformProcessVisitor {
render_fn_map,
in_compile_mode_jsx: false,
config,
}
}
}
Expand Down Expand Up @@ -48,7 +50,7 @@ impl<'a> VisitMut for TransformProcessVisitor<'a> {
(
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),
) => !(jsx_attr_name.sym == COMPILE_MODE && *value == self.config.sub_render_fn),
_ => true,
},
_ => true,
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export enum PLATFORM_TYPE {

export const COMPILE_MODE_IDENTIFIER_PREFIX = 'f'

export const COMPILE_MODE_SUB_RENDER_FN = 'subRenderFn'

export const PLATFORM_CONFIG_MAP = {
h5: {
type: PLATFORM_TYPE.WEB
Expand Down
1 change: 1 addition & 0 deletions packages/taro-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@stencil/core": "2.22.3",
"hammerjs": "^2.0.8",
"@tarojs/runtime": "workspace:*",
"@tarojs/shared": "workspace:*",
"@tarojs/components-advanced": "workspace:*",
"@tarojs/taro": "workspace:*",
"classnames": "^2.2.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/taro-components/types/common.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CSSProperties, LegacyRef, ReactNode } from 'react'
import { COMPILE_MODE_SUB_RENDER_FN } from '@tarojs/shared'

type COMPILE_MODE_SUB_RENDER_FN = 'subRenderFn'
export interface StandardProps<T = any, TouchEvent extends BaseTouchEvent<any> = ITouchEvent> extends EventProps<TouchEvent> {
/** 组件的唯一标示, 保持整个页面唯一 */
id?: string
Expand Down Expand Up @@ -28,10 +28,10 @@ export interface StandardProps<T = any, TouchEvent extends BaseTouchEvent<any> =
__html: string
}
/**
* 是否开启编译模式
* 是否开启编译模式 或者 是否是编译模式的子渲染函数
* @supported weapp, harmony
*/
compileMode?: boolean | string | COMPILE_MODE_SUB_RENDER_FN
compileMode?: boolean | string | typeof COMPILE_MODE_SUB_RENDER_FN
/**
* 自定义容器组件的方向
* @supported harmony
Expand Down
Binary file modified packages/taro-helper/swc-backup/swc_plugin_compile_mode.wasm
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { swc } from '@tarojs/helper'
import { COMPILE_MODE_IDENTIFIER_PREFIX, getComponentsAlias } from '@tarojs/shared'
import { COMPILE_MODE_IDENTIFIER_PREFIX, COMPILE_MODE_SUB_RENDER_FN, getComponentsAlias } from '@tarojs/shared'
import { isUrlRequest, urlToRequest } from 'loader-utils'

import { templatesCache, XMLDependency } from '../plugins/MiniCompileModePlugin'
Expand Down Expand Up @@ -56,7 +56,9 @@ export default async function (this: LoaderContext<IOptions>, source) {
plugins: [
[
'@tarojs/helper/swc/swc_plugin_compile_mode_pre_process.wasm',
{}
{
sub_render_fn: COMPILE_MODE_SUB_RENDER_FN,
}
],
[
'@tarojs/helper/swc/swc_plugin_compile_mode.wasm',
Expand Down
Loading

0 comments on commit 6e7a0ca

Please sign in to comment.