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

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,127 +1,236 @@
1
1
  # @meituan-nocode/vite-plugin-nocode-compiler
2
2
 
3
- 一个专门为 Vite 开发的 nocode 编译插件,用于自动为 JSX/TSX 元素添加 nocode 所需的标识属性。该插件基于 `@meituan-nocode/nocode-compiler-core` 实现,提供了高性能的代码转换能力。
3
+ Vite 插件,用于在构建过程中为 Vue React 组件自动添加属性,支持无代码平台的可视化编辑。
4
4
 
5
- ## 特性
5
+ ## 🚀 特性
6
6
 
7
- - 🎯 **智能元素标记**: 自动为 JSX/TSX 元素添加 nocode 相关属性
8
- - 🔍 **上下文感知**: 智能识别数组映射上下文,添加相应的数组和索引信息
9
- - ⚡️ **高性能**: 基于 Babel AST 实现,支持增量编译
10
- - 🛡️ **类型安全**: 使用 TypeScript 编写,提供完整的类型定义
11
- - 📦 **零配置**: 开箱即用,支持自定义配置
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` 文件
12
15
 
13
- ## 安装
16
+ ## 📦 安装
14
17
 
15
18
  ```bash
19
+ # npm
16
20
  npm install @meituan-nocode/vite-plugin-nocode-compiler --save-dev
17
- # 或
18
- yarn add -D @meituan-nocode/vite-plugin-nocode-compiler
19
- #
20
- pnpm add -D @meituan-nocode/vite-plugin-nocode-compiler
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
21
27
  ```
22
28
 
23
- ## 使用
29
+ ## 🔧 快速开始
24
30
 
25
31
  ### 基础配置
26
32
 
27
- 在你的 `vite.config.ts` 中添加插件:
33
+ 在项目的 `vite.config.js` 或 `vite.config.ts` 中配置插件:
28
34
 
29
35
  ```typescript
36
+ // vite.config.ts
30
37
  import { defineConfig } from 'vite';
31
- import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
38
+ import componentCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
32
39
 
33
40
  export default defineConfig({
34
- plugins: [componentCompiler()],
41
+ plugins: [
42
+ componentCompiler({
43
+ enableLogging: true, // 开发时建议开启日志
44
+ }),
45
+ ],
35
46
  });
36
47
  ```
37
48
 
38
- ### 自定义配置
49
+ ### CommonJS 配置
39
50
 
40
- ```typescript
41
- import { defineConfig } from 'vite';
42
- import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
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
+ ],
62
+ });
63
+ ```
64
+
65
+ ## ⚙️ 配置选项
43
66
 
67
+ | 选项 | 类型 | 默认值 | 描述 |
68
+ | --------------- | --------- | ------- | ------------------------------------------------------ |
69
+ | `enableLogging` | `boolean` | `false` | 是否启用日志输出,开启后会输出详细的处理过程和调试信息 |
70
+ | `rootPath` | `string` | - | 项目根路径,用于生成相对路径标识 |
71
+
72
+ ### 配置示例
73
+
74
+ ```typescript
75
+ // 完整配置示例
44
76
  export default defineConfig({
45
77
  plugins: [
46
78
  componentCompiler({
47
- enableLogging: true, // 启用调试日志
48
- rootPath: process.cwd(), // 自定义项目根路径
79
+ enableLogging: process.env.NODE_ENV === 'development',
80
+ rootPath: process.cwd(),
49
81
  }),
50
82
  ],
51
83
  });
52
84
  ```
53
85
 
54
- ## 工作原理
86
+ ## 📁 处理的文件类型
87
+
88
+ ### Vue 文件
55
89
 
56
- 插件会在构建过程中拦截并处理 `.jsx`、`.tsx`、`.js` `.ts` 文件,通过 AST 分析为符合条件的 JSX 元素添加以下属性:
90
+ - `.vue` 单文件组件的 `<template>` 部分
91
+ - Vue 文件中的 JSX/TSX `<script>` 部分
92
+ - 外部模板文件 `<template src="xxx.html">`
57
93
 
58
- - `data-nocode-id`: 元素的唯一标识,格式为 `文件路径:行号:列号`
59
- - `data-nocode-name`: 元素的标签名称
60
- - `data-nocode-array`: 当元素在数组 map 中时,记录数组名称
61
- - `data-nocode-index`: 当元素在数组 map 中时,记录当前索引
94
+ ### React 文件
62
95
 
63
- ### 示例
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
64
108
 
65
- 输入代码:
109
+ // Vue 模板处理
110
+ // App.vue?type=template
66
111
 
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
- }
112
+ // 外部模板处理
113
+ // template.html?type=template&vue
80
114
  ```
81
115
 
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
- }
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>
101
130
  ```
102
131
 
103
- ## 技术细节
132
+ ### 添加的属性说明
104
133
 
105
- - 使用 `@babel/parser` 进行代码解析
106
- - 使用 `@babel/traverse` 进行 AST 遍历
107
- - 使用 `magic-string` 进行高效的代码修改
108
- - 支持 TypeScript、装饰器等高级语法特性
109
- - 智能跳过 `node_modules` 中的文件
134
+ - `data-nocode-id`: 元素的唯一标识符(格式:`文件路径:行号:列号`)
135
+ - `data-nocode-path`: 相对文件路径
136
+ - `data-nocode-line`: 元素在文件中的行号
137
+ - `data-nocode-col`: 元素在文件中的列号
138
+ - `data-nocode-array`: (可选)当元素在 `v-for` 或 `map` 循环中时,标识数组名称
110
139
 
111
- ## 版本要求
140
+ ## 🏗️ 在 Monorepo 项目中使用
112
141
 
113
- - Vite: ^5.4.0
114
- - Node.js: ^20.0.0
142
+ monorepo 项目中,建议在共享配置中定义插件:
115
143
 
116
- ## 开发
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
+ ```
117
165
 
118
- ```bash
119
- # 安装依赖
120
- pnpm install
166
+ ## 🐛 调试和故障排除
121
167
 
122
- # 开发模式
123
- pnpm run dev
168
+ ### 启用调试日志
124
169
 
125
- # 构建
126
- pnpm run build
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: 如何排除特定文件?
210
+
211
+ A: 目前插件没有内置排除选项,但可以通过 Vite 的 `optimizeDeps` 或自定义插件来实现:
212
+
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
+ });
127
228
  ```
229
+
230
+ ## 📚 依赖关系
231
+
232
+ 该插件依赖于以下核心包:
233
+
234
+ - `@meituan-nocode/nocode-compiler-core`: 提供核心编译逻辑
235
+ - `VueCompiler`: Vue 组件编译器
236
+ - `JSXCompiler`: JSX 组件编译器
package/dist/index.cjs ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ componentCompiler: () => componentCompiler,
24
+ default: () => index_default
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+ var import_nocode_compiler_core = require("@meituan-nocode/nocode-compiler-core");
28
+ function componentCompiler(options = {}) {
29
+ options = {
30
+ enableLogging: false,
31
+ ...options
32
+ };
33
+ const vueCompiler = new import_nocode_compiler_core.VueCompiler(options);
34
+ const jsxCompiler = new import_nocode_compiler_core.JSXCompiler(options);
35
+ return {
36
+ name: "vite-plugin-nocode-compiler",
37
+ enforce: "pre",
38
+ async transform(code, id) {
39
+ if (id.includes("node_modules")) {
40
+ return code;
41
+ }
42
+ const [filePath, query] = id.split("?", 2);
43
+ const { useJSXCompiler, useVueCompiler } = (0, import_nocode_compiler_core.detectCompileScenario)({
44
+ filePath,
45
+ query
46
+ });
47
+ if (useJSXCompiler) {
48
+ return jsxCompiler.compile(code, filePath) || code;
49
+ }
50
+ if (useVueCompiler) {
51
+ return vueCompiler.compile(code, filePath) || code;
52
+ }
53
+ return code;
54
+ }
55
+ };
56
+ }
57
+ var index_default = componentCompiler;
58
+ // Annotate the CommonJS export names for ESM import in node:
59
+ 0 && (module.exports = {
60
+ componentCompiler
61
+ });
@@ -0,0 +1,15 @@
1
+ interface NocodeCompilerOptions {
2
+ enableLogging?: boolean;
3
+ rootPath?: string;
4
+ }
5
+ /**
6
+ * Vite插件
7
+ * 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
8
+ */
9
+ declare function componentCompiler(options?: NocodeCompilerOptions): {
10
+ name: string;
11
+ enforce: "pre";
12
+ transform(code: string, id: string): Promise<string>;
13
+ };
14
+
15
+ export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,15 @@
1
- export interface NocodeCompilerOptions {
1
+ interface NocodeCompilerOptions {
2
2
  enableLogging?: boolean;
3
3
  rootPath?: string;
4
4
  }
5
- export declare function componentCompiler(options?: NocodeCompilerOptions): {
5
+ /**
6
+ * Vite插件
7
+ * 用于在构建过程中处理Vue和React组件,添加nocode相关的标记
8
+ */
9
+ declare function componentCompiler(options?: NocodeCompilerOptions): {
6
10
  name: string;
7
11
  enforce: "pre";
8
- transform(code: string, id: string): Promise<string | null>;
12
+ transform(code: string, id: string): Promise<string>;
9
13
  };
14
+
15
+ export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
package/dist/index.js CHANGED
@@ -1,28 +1,36 @@
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");
1
+ // src/index.ts
2
+ import { JSXCompiler, VueCompiler, detectCompileScenario } from "@meituan-nocode/nocode-compiler-core";
5
3
  function componentCompiler(options = {}) {
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
- };
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
+ };
28
31
  }
32
+ var index_default = componentCompiler;
33
+ export {
34
+ componentCompiler,
35
+ index_default as default
36
+ };
package/package.json CHANGED
@@ -1,24 +1,33 @@
1
1
  {
2
2
  "name": "@meituan-nocode/vite-plugin-nocode-compiler",
3
- "version": "0.2.2",
3
+ "version": "0.2.3-beta.1",
4
4
  "description": "Vite plugin for nocode compiler",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
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",
8
16
  "files": [
9
17
  "dist"
10
18
  ],
11
19
  "scripts": {
12
- "dev": "tsc --watch",
13
- "build": "tsc",
20
+ "dev": "tsup --watch",
21
+ "build": "tsup",
14
22
  "prepublishOnly": "npm run build"
15
23
  },
16
24
  "devDependencies": {
17
25
  "@types/node": "^20.0.0",
18
26
  "typescript": "^5.0.0",
19
- "vite": "^5.4.0"
27
+ "vite": "^4.5.14",
28
+ "tsup": "8.5.0"
20
29
  },
21
30
  "dependencies": {
22
- "@meituan-nocode/nocode-compiler-core": "0.2.1"
31
+ "@meituan-nocode/nocode-compiler-core": "0.2.3-beta.1"
23
32
  }
24
33
  }