@meituan-nocode/vite-plugin-nocode-compiler 0.2.3-beta.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,236 +1,127 @@
1
1
  # @meituan-nocode/vite-plugin-nocode-compiler
2
2
 
3
- Vite 插件,用于在构建过程中为 Vue React 组件自动添加属性,支持无代码平台的可视化编辑。
3
+ 一个专门为 Vite 开发的 nocode 编译插件,用于自动为 JSX/TSX 元素添加 nocode 所需的标识属性。该插件基于 `@meituan-nocode/nocode-compiler-core` 实现,提供了高性能的代码转换能力。
4
4
 
5
- ## 🚀 特性
5
+ ## 特性
6
6
 
7
- - 支持 Vue 单文件组件 (`.vue`)
8
- - 支持 React JSX/TSX 组件 (`.jsx`, `.tsx`)
9
- - 支持 Vue 文件中的 JSX 语法
10
- - 支持外部模板文件 (`<template src="xxx.html">`)
11
- - 兼容 Vite 4.x 和 5.x 版本
12
- - ✅ 可配置的日志输出
13
- - ✅ 支持 ESM 和 CommonJS 导入方式
14
- - ✅ 自动跳过 `node_modules` 文件
7
+ - 🎯 **智能元素标记**: 自动为 JSX/TSX 元素添加 nocode 相关属性
8
+ - 🔍 **上下文感知**: 智能识别数组映射上下文,添加相应的数组和索引信息
9
+ - ⚡️ **高性能**: 基于 Babel AST 实现,支持增量编译
10
+ - 🛡️ **类型安全**: 使用 TypeScript 编写,提供完整的类型定义
11
+ - 📦 **零配置**: 开箱即用,支持自定义配置
15
12
 
16
- ## 📦 安装
13
+ ## 安装
17
14
 
18
15
  ```bash
19
- # npm
20
16
  npm install @meituan-nocode/vite-plugin-nocode-compiler --save-dev
21
-
22
- # yarn
23
- yarn add @meituan-nocode/vite-plugin-nocode-compiler --dev
24
-
25
- # pnpm
26
- pnpm add @meituan-nocode/vite-plugin-nocode-compiler -D
17
+ # 或
18
+ yarn add -D @meituan-nocode/vite-plugin-nocode-compiler
19
+ #
20
+ pnpm add -D @meituan-nocode/vite-plugin-nocode-compiler
27
21
  ```
28
22
 
29
- ## 🔧 快速开始
23
+ ## 使用
30
24
 
31
25
  ### 基础配置
32
26
 
33
- 在项目的 `vite.config.js` 或 `vite.config.ts` 中配置插件:
27
+ 在你的 `vite.config.ts` 中添加插件:
34
28
 
35
29
  ```typescript
36
- // vite.config.ts
37
30
  import { defineConfig } from 'vite';
38
- import componentCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
31
+ import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
39
32
 
40
33
  export default defineConfig({
41
- plugins: [
42
- componentCompiler({
43
- enableLogging: true, // 开发时建议开启日志
44
- }),
45
- ],
46
- });
47
- ```
48
-
49
- ### CommonJS 配置
50
-
51
- ```javascript
52
- // vite.config.js
53
- const { defineConfig } = require('vite');
54
- const componentCompiler = require('@meituan-nocode/vite-plugin-nocode-compiler').default;
55
-
56
- module.exports = defineConfig({
57
- plugins: [
58
- componentCompiler({
59
- enableLogging: true,
60
- }),
61
- ],
34
+ plugins: [componentCompiler()],
62
35
  });
63
36
  ```
64
37
 
65
- ## ⚙️ 配置选项
66
-
67
- | 选项 | 类型 | 默认值 | 描述 |
68
- | --------------- | --------- | ------- | ------------------------------------------------------ |
69
- | `enableLogging` | `boolean` | `false` | 是否启用日志输出,开启后会输出详细的处理过程和调试信息 |
70
- | `rootPath` | `string` | - | 项目根路径,用于生成相对路径标识 |
71
-
72
- ### 配置示例
38
+ ### 自定义配置
73
39
 
74
40
  ```typescript
75
- // 完整配置示例
41
+ import { defineConfig } from 'vite';
42
+ import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
43
+
76
44
  export default defineConfig({
77
45
  plugins: [
78
46
  componentCompiler({
79
- enableLogging: process.env.NODE_ENV === 'development',
80
- rootPath: process.cwd(),
47
+ enableLogging: true, // 启用调试日志
48
+ rootPath: process.cwd(), // 自定义项目根路径
81
49
  }),
82
50
  ],
83
51
  });
84
52
  ```
85
53
 
86
- ## 📁 处理的文件类型
87
-
88
- ### Vue 文件
54
+ ## 工作原理
89
55
 
90
- - `.vue` 单文件组件的 `<template>` 部分
91
- - Vue 文件中的 JSX/TSX `<script>` 部分
92
- - 外部模板文件 `<template src="xxx.html">`
56
+ 插件会在构建过程中拦截并处理 `.jsx`、`.tsx`、`.js` `.ts` 文件,通过 AST 分析为符合条件的 JSX 元素添加以下属性:
93
57
 
94
- ### React 文件
58
+ - `data-nocode-id`: 元素的唯一标识,格式为 `文件路径:行号:列号`
59
+ - `data-nocode-name`: 元素的标签名称
60
+ - `data-nocode-array`: 当元素在数组 map 中时,记录数组名称
61
+ - `data-nocode-index`: 当元素在数组 map 中时,记录当前索引
95
62
 
96
- - `.jsx` 文件
97
- - `.tsx` 文件
98
- - 包含 JSX 语法的 `.js` 和 `.ts` 文件
99
-
100
- ### 文件处理逻辑
101
-
102
- 插件会根据文件扩展名和 Vite 查询参数智能判断文件类型:
103
-
104
- ```typescript
105
- // Vue 文件中的 JSX 处理
106
- // App.vue?type=script&lang.tsx
107
- // App.vue?isJsx
63
+ ### 示例
108
64
 
109
- // Vue 模板处理
110
- // App.vue?type=template
65
+ 输入代码:
111
66
 
112
- // 外部模板处理
113
- // template.html?type=template&vue
67
+ ```jsx
68
+ function TodoList({ items }) {
69
+ return (
70
+ <div className='todo-list'>
71
+ {items.map((item, index) => (
72
+ <div key={item.id} className='todo-item'>
73
+ <span>{item.text}</span>
74
+ <button onClick={() => handleDelete(item.id)}>删除</button>
75
+ </div>
76
+ ))}
77
+ </div>
78
+ );
79
+ }
114
80
  ```
115
81
 
116
- ## 🎯 工作原理
117
-
118
- 插件会在构建过程中为符合条件的元素添加以下属性:
119
-
120
- ```html
121
- <!-- 处理前 -->
122
- <div class="container">
123
- <button>Click me</button>
124
- </div>
125
-
126
- <!-- 处理后 -->
127
- <div class="container" data-nocode-id="src/App.vue:1:0" data-nocode-path="src/App.vue" data-nocode-line="1" data-nocode-col="0">
128
- <button data-nocode-id="src/App.vue:2:2" data-nocode-path="src/App.vue" data-nocode-line="2" data-nocode-col="2">Click me</button>
129
- </div>
82
+ 编译后:
83
+
84
+ ```jsx
85
+ function TodoList({ items }) {
86
+ return (
87
+ <div data-nocode-id='TodoList.jsx:2:8' data-nocode-name='div' className='todo-list'>
88
+ {items.map((item, index) => (
89
+ <div data-nocode-id='TodoList.jsx:4:12' data-nocode-name='div' data-nocode-array='items' data-nocode-index='${index}' key={item.id} className='todo-item'>
90
+ <span data-nocode-id='TodoList.jsx:5:16' data-nocode-name='span' data-nocode-array='items' data-nocode-index='${index}'>
91
+ {item.text}
92
+ </span>
93
+ <button data-nocode-id='TodoList.jsx:6:16' data-nocode-name='button' data-nocode-array='items' data-nocode-index='${index}' onClick={() => handleDelete(item.id)}>
94
+ 删除
95
+ </button>
96
+ </div>
97
+ ))}
98
+ </div>
99
+ );
100
+ }
130
101
  ```
131
102
 
132
- ### 添加的属性说明
133
-
134
- - `data-nocode-id`: 元素的唯一标识符(格式:`文件路径:行号:列号`)
135
- - `data-nocode-path`: 相对文件路径
136
- - `data-nocode-line`: 元素在文件中的行号
137
- - `data-nocode-col`: 元素在文件中的列号
138
- - `data-nocode-array`: (可选)当元素在 `v-for` 或 `map` 循环中时,标识数组名称
103
+ ## 技术细节
139
104
 
140
- ## 🏗️ Monorepo 项目中使用
105
+ - 使用 `@babel/parser` 进行代码解析
106
+ - 使用 `@babel/traverse` 进行 AST 遍历
107
+ - 使用 `magic-string` 进行高效的代码修改
108
+ - 支持 TypeScript、装饰器等高级语法特性
109
+ - 智能跳过 `node_modules` 中的文件
141
110
 
142
- monorepo 项目中,建议在共享配置中定义插件:
111
+ ## 版本要求
143
112
 
144
- ```typescript
145
- // packages/shared/vite-config.ts
146
- import { defineConfig } from 'vite';
147
- import componentCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
148
-
149
- export const createViteConfig = (options: { enableLogging?: boolean } = {}) => {
150
- return defineConfig({
151
- plugins: [
152
- componentCompiler({
153
- enableLogging: options.enableLogging ?? process.env.NODE_ENV === 'development',
154
- }),
155
- ],
156
- });
157
- };
158
-
159
- // 子项目中使用
160
- // packages/app-a/vite.config.ts
161
- import { createViteConfig } from '@shared/vite-config';
162
-
163
- export default createViteConfig({ enableLogging: true });
164
- ```
113
+ - Vite: ^5.4.0
114
+ - Node.js: ^20.0.0
165
115
 
166
- ## 🐛 调试和故障排除
116
+ ## 开发
167
117
 
168
- ### 启用调试日志
169
-
170
- ```typescript
171
- // vite.config.ts
172
- export default defineConfig({
173
- plugins: [
174
- componentCompiler({
175
- enableLogging: true, // 开启详细日志
176
- }),
177
- ],
178
- });
179
- ```
180
-
181
- 开启后,构建时会输出类似日志:
182
-
183
- ```
184
- [vite-plugin-nocode-compiler] Processing Vue file: src/App.vue
185
- [vue-compiler] File processing complete 5
186
- [vite-plugin-nocode-compiler] Processing JSX file: src/components/Button.tsx
187
- [jsx-compiler] File processing complete 3
188
- ```
189
-
190
- ### 常见问题
191
-
192
- #### Q: 插件没有生效怎么办?
193
-
194
- A: 检查以下几点:
195
-
196
- 1. 确认插件已正确安装和配置
197
- 2. 确保 `enforce: 'pre'` 设置正确(插件已内置)
198
- 3. 检查文件是否在 `node_modules` 中(插件会自动跳过)
199
- 4. 开启 `enableLogging` 查看是否有处理日志
200
-
201
- #### Q: 某些文件没有被处理?
202
-
203
- A: 可能的原因:
204
-
205
- - 文件在 `node_modules` 目录下
206
- - 文件类型不支持(如纯 CSS 文件)
207
- - Vue 文件的 `<style>` 部分(插件只处理模板和脚本)
208
-
209
- #### Q: 如何排除特定文件?
118
+ ```bash
119
+ # 安装依赖
120
+ pnpm install
210
121
 
211
- A: 目前插件没有内置排除选项,但可以通过 Vite 的 `optimizeDeps` 或自定义插件来实现:
122
+ # 开发模式
123
+ pnpm run dev
212
124
 
213
- ```typescript
214
- export default defineConfig({
215
- plugins: [
216
- componentCompiler(),
217
- // 自定义插件排除特定文件
218
- {
219
- name: 'exclude-files',
220
- transform(code, id) {
221
- if (id.includes('excluded-folder')) {
222
- return null; // 跳过处理
223
- }
224
- },
225
- },
226
- ],
227
- });
125
+ # 构建
126
+ pnpm run build
228
127
  ```
229
-
230
- ## 📚 依赖关系
231
-
232
- 该插件依赖于以下核心包:
233
-
234
- - `@meituan-nocode/nocode-compiler-core`: 提供核心编译逻辑
235
- - `VueCompiler`: Vue 组件编译器
236
- - `JSXCompiler`: JSX 组件编译器
package/dist/index.d.ts CHANGED
@@ -1,15 +1,9 @@
1
- interface NocodeCompilerOptions {
1
+ export interface NocodeCompilerOptions {
2
2
  enableLogging?: boolean;
3
3
  rootPath?: string;
4
4
  }
5
- /**
6
- * Vite插件
7
- * 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
8
- */
9
- declare function componentCompiler(options?: NocodeCompilerOptions): {
5
+ export declare function componentCompiler(options?: NocodeCompilerOptions): {
10
6
  name: string;
11
7
  enforce: "pre";
12
- transform(code: string, id: string): Promise<string>;
8
+ transform(code: string, id: string): Promise<string | null>;
13
9
  };
14
-
15
- export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
package/dist/index.js CHANGED
@@ -1,36 +1,28 @@
1
- // src/index.ts
2
- import { JSXCompiler, VueCompiler, detectCompileScenario } from "@meituan-nocode/nocode-compiler-core";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.componentCompiler = componentCompiler;
4
+ const nocode_compiler_core_1 = require("@meituan-nocode/nocode-compiler-core");
3
5
  function componentCompiler(options = {}) {
4
- options = {
5
- enableLogging: false,
6
- ...options
7
- };
8
- const vueCompiler = new VueCompiler(options);
9
- const jsxCompiler = new JSXCompiler(options);
10
- return {
11
- name: "vite-plugin-nocode-compiler",
12
- enforce: "pre",
13
- async transform(code, id) {
14
- if (id.includes("node_modules")) {
15
- return code;
16
- }
17
- const [filePath, query] = id.split("?", 2);
18
- const { useJSXCompiler, useVueCompiler } = detectCompileScenario({
19
- filePath,
20
- query
21
- });
22
- if (useJSXCompiler) {
23
- return jsxCompiler.compile(code, filePath) || code;
24
- }
25
- if (useVueCompiler) {
26
- return vueCompiler.compile(code, filePath) || code;
27
- }
28
- return code;
29
- }
30
- };
6
+ const { enableLogging = false, rootPath = process.cwd() } = options;
7
+ return {
8
+ name: 'vite-plugin-nocode-compiler',
9
+ enforce: 'pre',
10
+ async transform(code, id) {
11
+ // 检查文件是否在 node_modules
12
+ if (id.includes('node_modules')) {
13
+ return code;
14
+ }
15
+ // 检查文件扩展名
16
+ if (!/\.(jsx?|tsx?)$/.test(id)) {
17
+ return code;
18
+ }
19
+ // 使用 jsxcompiler 编译
20
+ const jsxCompiler = new nocode_compiler_core_1.JSXCompiler({
21
+ enableLogging,
22
+ rootPath,
23
+ });
24
+ const result = jsxCompiler.compile(code, id);
25
+ return result;
26
+ },
27
+ };
31
28
  }
32
- var index_default = componentCompiler;
33
- export {
34
- componentCompiler,
35
- index_default as default
36
- };
package/package.json CHANGED
@@ -1,33 +1,24 @@
1
1
  {
2
2
  "name": "@meituan-nocode/vite-plugin-nocode-compiler",
3
- "version": "0.2.3-beta.1",
3
+ "version": "0.2.3",
4
4
  "description": "Vite plugin for nocode compiler",
5
- "type": "module",
6
- "exports": {
7
- ".": {
8
- "import": "./dist/index.js",
9
- "require": "./dist/index.cjs"
10
- },
11
- "./package.json": "./package.json"
12
- },
13
- "main": "./dist/index.cjs",
14
- "types": "./dist/index.d.ts",
15
- "module": "./dist/index.js",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
16
8
  "files": [
17
9
  "dist"
18
10
  ],
19
11
  "scripts": {
20
- "dev": "tsup --watch",
21
- "build": "tsup",
12
+ "dev": "tsc --watch",
13
+ "build": "tsc",
22
14
  "prepublishOnly": "npm run build"
23
15
  },
24
16
  "devDependencies": {
25
17
  "@types/node": "^20.0.0",
26
18
  "typescript": "^5.0.0",
27
- "vite": "^4.5.14",
28
- "tsup": "8.5.0"
19
+ "vite": "^5.4.0"
29
20
  },
30
21
  "dependencies": {
31
- "@meituan-nocode/nocode-compiler-core": "0.2.3-beta.1"
22
+ "@meituan-nocode/nocode-compiler-core": "0.2.3"
32
23
  }
33
24
  }