Mitosis开发JS库时还可以做哪些定制化?

在开发Mitosis组件时,有时候你可能会有独特的需求,如果你想要Mitosis生成的代码符合你的需求,你可以:

  • 在写Mistosis组件的文件顶头加上特定的import
  • 为特定的目标框架删除特定的样式属性(比如,你想要你的react-native去掉一些样式和属性)
  • 只修改某些组件变成动态输入 这些功能多亏了Mitosis强有力的插件系统。

插件

在你的项目目录的mitosis.config.js,你可以给每个代码生成器提供一个插件数组,有多种不同的插件可选:

js 复制代码
export type Plugin = {
  json?: {
    // Happens before any modifiers
    pre?: (json: MitosisComponent) => MitosisComponent | void;
    // Happens after built in modifiers
    post?: (json: MitosisComponent) => MitosisComponent | void;
  };
  code?: {
    // Happens before formatting
    pre?: (code: string) => string;
    // Happens after formatting
    post?: (code: string) => string;
  };
};

我们会在4个节点跑这些插件:

  • preJSON:在任何改编器运行在Mitosis JSON之前
  • postJSON:在所有改编器运行了Mitosis JSON之后
  • preCode:在Mitosis输出做任何格式化之前(我们使用prettier做格式化)
  • postCode:在Mitosis输出做了所有格式化之后(我们使用prettier做格式化) JSON插件接收Mitosis组件的完整JSON对象作为输入,同样地,code插件接收代码string作为输入。我们甚至在内部用插件生成Mitosis组件!以下是我们的一个react-native插件的:
ts 复制代码
//mitosis/packages/core/src/generators/react-native.ts
// Plugin to convert DOM tags to <View /> and <Text />
function processReactNative() {
    return () => ({
        json: {
            pre: (json: MitosisComponent) => {
                traverse(json).forEach((node) => {
                    if (isMitosisNode(node)) {
                        // TODO: handle TextInput, Image, etc
                        if (node.name.toLowerCase() === node.name) {
                            node.name = 'View';
                        }
                        
                        if (
                            node.properties._text?.trim().length ||
                            node.bindings._text?.trim()?.length
                        ) {
                            node.name = 'Text';
                        }
                        
                        if (node.properties.class) {
                            delete node.properties.class;
                        }
                        if (node.properties.className) {
                            delete node.properties.className;
                        }
                        if (node.bindings.class) {
                            delete node.bindings.class;
                        }
                        if (node.bindings.className) {
                            delete node.bindings.className;
                        }
                    }
                })
            }
        }
    })
}

在代码中,你会看到,我们遍历了JSON节点和每个Mitosis节点,我们删除了class和className值和对应的绑定关系。因为react-native在手机上不支持class-name。

useMetaData

那么如果你想要一个插件仅仅应用在某类组件呢?或者你想要为你的插件提供一些元数据,而这些元数据将依赖于一个已经编译好了的组件? 这时就可以使用useMetaData,你需要做的就是引入并使用这个hook(你可以在Mitosis组件任何地方使用,甚至是根层级):

js 复制代码
import { useMetadata } from '@builder.io/mitosis';

useMetadata({ mySpecialComponentType: 'ABC' });

export default function SmileReviews(props: SmileReviewsProps) {
  return <div>{/**/}</div>;
}

元数据将会被存储在Mitosis组件JSON中,在json.meta.useMetadata.mySpecialComponentType下面,你可以使用你的JSON pre/post插件:

js 复制代码
const plugin = {
  json: {
    pre: (json: MitosisComponent) => {
      const myComponentType = json.meta.useMetadata?.mySpecialComponentType;
      if (myComponentType === 'ABC') {
        //...
      }
    },
  },
};

组件JSON

Mitosis引擎的工作方式是:

  • 你写一个.lite.jsx组件
  • Mitosis的JSX解析器会把它转换成MitosisComponentJSON
  • JSON会喂给你选好的生成器,生成器也有你提供好的插件

关于JSON还包含什么,更多信息查看以下文档:

  • MitosisComponent
  • MitosisNode:每个MitosisComponent都会有多个MitosisNode在component.children下面。每个MitosisNode都代表一个DOM/JSX节点。

参考

github.com/BuilderIO/m...

相关推荐
独孤九剑打醒他15 小时前
【原创开源】双层Master‑Worker软硬协同调度架构:从根源解决分布式数据一致性难题
前端·嵌入式硬件·架构·前端框架·硬件工程
ebok.1 天前
国产大模型落地业务系统的优选载体:京微智枢信创 AI 业务支撑平台
大数据·人工智能·低代码·ai
Highcharts.js3 天前
Highcharts 主流前端框架无缝集成指南
前端·vue.js·前端框架·highcharts·可视化图表
杰夫(简道云个人搭建)3 天前
SAAS系统应该先解决业务问题,再优化体验
数据库·人工智能·低代码·excel·个人开发
Devlab3 天前
LVGL设计大师——网页版anyui来了!!
嵌入式硬件·物联网·低代码·ui·iot
jonyleek3 天前
JVS-Logic 实践指南:基于私有化与柔性配置的企业级逻辑引擎落地方法
人工智能·低代码·私有化部署·企业集成·逻辑引擎·流程编排·jvs
2601_953988074 天前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·后端·物联网·tcp/ip·数学建模·前端框架
console.log('npc')4 天前
大型前端项目的文档驱动协作实践:从混乱到有序
前端·前端框架
console.log('npc')4 天前
React 19 + Vite 企业级前端项目:从零搭建到规范交付
前端·react.js·前端框架
光影少年4 天前
react离线缓存、图片缓存方案
开发语言·前端·javascript·react native·react.js·缓存·前端框架