# @eficy/plugin-unocss UnoCSS plugin for @eficy/core-jsx - Automatically extracts and generates CSS styles from className attributes in Eficy components. ## 📖 概述 `@eficy/plugin-unocss` 是为 Eficy 框架 v3 设计的 UnoCSS 集成插件。它通过拦截组件渲染过程,自动提取 `className` 中的样式类,并使用 UnoCSS 生成对应的 CSS 样式,最终注入到页面中。该插件支持实时样式生成、缓存优化和自定义配置。 ## ✨ 核心特性 ### 🔍 自动样式提取 - **智能识别**: 自动从组件 props 中提取 `className` 属性 - **深度扫描**: 支持字符串、数组等多种 className 格式 - **实时收集**: 在组件渲染过程中实时收集样式类 - **去重处理**: 自动去除重复的样式类 ### ⚡ 性能优化 - **智能缓存**: 基于样式类哈希的缓存机制,避免重复生成 - **按需生成**: 只生成实际使用的样式 - **异步处理**: 使用 `@eficy/reactive-async` 进行异步样式生成 - **防抖优化**: 1ms 防抖处理,避免频繁更新 ### 🎨 UnoCSS 集成 - **完整支持**: 支持 UnoCSS 的所有特性和语法 - **预设集成**: 内置 Uno 和 Attributify 预设 - **自定义配置**: 支持完全自定义的 UnoCSS 配置 - **错误处理**: 优雅处理 CSS 生成失败的情况 ### 🔌 插件化架构 - **生命周期钩子**: 集成到 Eficy 的插件生命周期系统 - **优先级控制**: 支持插件执行优先级设置 - **依赖注入**: 基于 tsyringe 的依赖注入支持 - **热插拔**: 支持插件的动态安装和卸载 ## 📦 安装 ```bash npm install @eficy/plugin-unocss @unocss/core @unocss/preset-uno @unocss/preset-attributify # 或 yarn add @eficy/plugin-unocss @unocss/core @unocss/preset-uno @unocss/preset-attributify # 或 pnpm add @eficy/plugin-unocss @unocss/core @unocss/preset-uno @unocss/preset-attributify ``` ## 🚀 快速开始 ### 基础使用 ```tsx import React from 'react'; import { createRoot } from 'react-dom/client'; import { Eficy, EficyProvider } from '@eficy/core-jsx'; import { UnocssPlugin } from '@eficy/plugin-unocss'; import { signal } from '@eficy/reactive'; // 创建 Eficy 实例 const core = new Eficy(); // 安装 UnoCSS 插件 await core.install(UnocssPlugin); // 应用组件 const App = () => { const count = signal(0); return (

Eficy + UnoCSS

Count: {count}

); }; // 渲染应用 const root = createRoot(document.getElementById('root')); root.render( , ); ``` ### 自定义配置 ```tsx import { UnocssPlugin } from '@eficy/plugin-unocss'; import { presetWind } from '@unocss/preset-wind'; // 使用自定义 UnoCSS 配置 await core.install(UnocssPlugin, { config: { presets: [ presetWind(), // 使用 Tailwind CSS 兼容预设 ], rules: [ // 自定义规则 [ 'btn-custom', { padding: '12px 24px', borderRadius: '8px', fontWeight: '500', transition: 'all 0.2s ease-in-out', }, ], ], shortcuts: [ // 自定义快捷方式 ['btn-primary', 'btn-custom bg-blue-500 text-white hover:bg-blue-600'], ['btn-secondary', 'btn-custom bg-gray-200 text-gray-800 hover:bg-gray-300'], ], theme: { colors: { primary: { 50: '#eff6ff', 500: '#3b82f6', 900: '#1e3a8a', }, }, }, }, }); ``` ## 🛠️ API 文档 ### UnocssPlugin 类 主要的插件类,实现了 `ILifecyclePlugin` 接口。 ```typescript @injectable() export class UnocssPlugin implements ILifecyclePlugin { public readonly name = 'unocss-plugin'; public readonly version = '1.0.0'; public readonly enforce = 'pre'; // 插件配置 async initialize(config?: UnocssPluginConfig): Promise; // 渲染钩子 onRender(context: IRenderContext, next: () => ComponentType): ComponentType; // 根组件挂载钩子 onRootMount(context: IRenderContext, next: () => ComponentType): ComponentType; // 获取 UnoCSS 生成器实例 getGenerator(): UnoGenerator | null; // 获取收集到的样式类 getCollectedClasses(): Set; // 清理资源 destroy(): void; } ``` ### UnocssPluginConfig 接口 ```typescript interface UnocssPluginConfig { config?: UserConfig; // UnoCSS 自定义配置 } ``` ### 配置选项 UnoCSS 配置支持所有官方配置选项: ```typescript { config: { // 预设 presets: [ presetUno(), presetAttributify(), presetWind(), // ... 其他预设 ], // 自定义规则 rules: [ ['btn', { padding: '0.5rem 1rem' }], [/^m-(\d+)$/, ([, d]) => ({ margin: `${d}px` })], ], // 快捷方式 shortcuts: [ ['btn', 'px-4 py-2 rounded'], ['btn-primary', 'btn bg-blue-500 text-white'], ], // 主题 theme: { colors: { primary: '#3b82f6', secondary: '#64748b', }, }, // 变体 variants: [ // 自定义变体 ], // 预检样式 preflights: [ // 自定义预检样式 ], } } ``` ## 🔧 工作原理 ### 1. 初始化阶段 ```typescript @Initialize() async initialize(config: UnocssPluginConfig = {}) { // 创建 UnoCSS 生成器 const generator = await createGenerator({ presets: [ presetUno({ preflight: false }), presetAttributify(), ], ...config.config, }); // 初始化异步信号 this.reactiveAsync = asyncSignal( () => this.generateCSS(), { manual: true, debounceWait: 1 } ); } ``` ### 2. 样式收集阶段 ```typescript @Render(5) // 优先级为 5,确保早期执行 onRender(context: IRenderContext, next: () => ComponentType) { // 收集 className 中的样式类 if (context.props.className) { this.collectClassNames(context.props.className); } return next(); } ``` ### 3. CSS 生成阶段 ```typescript private async generateCSS(): Promise { const classArray = Array.from(this.collectedClasses).sort(); const currentClassHash = classArray.join('|'); // 检查缓存 if (this.cssCache.has(currentClassHash)) { return this.cssCache.get(currentClassHash); } // 生成新的 CSS const result = await this.generator.generate(classArray.join(' ')); const css = result.css; // 更新缓存 if (css) { this.cssCache.set(currentClassHash, css); } return css; } ``` ### 4. 样式注入阶段 ```typescript @RootMount() onRootMount() { // 触发 CSS 生成 this.reactiveAsync.run(); } // Unocss 组件负责实际的样式注入 export const Unocss = ({ generateCSS }) => { const inlineStyle = useObserver(() => generateCSS.data); if (!inlineStyle) { return null; } return