@meituan-nocode/vite-plugin-nocode-compiler 0.1.0-beta.8 → 0.2.0-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 +58 -55
- package/dist/index.d.ts +6 -2
- package/dist/index.js +5 -4
- package/package.json +3 -6
package/README.md
CHANGED
|
@@ -1,44 +1,43 @@
|
|
|
1
1
|
# @meituan-nocode/vite-plugin-nocode-compiler
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
一个专门为 Vite 开发的 nocode 编译插件,用于自动为 JSX/TSX 元素添加 nocode 所需的标识属性。该插件基于 `@meituan-nocode/nocode-compiler-core` 实现,提供了高性能的代码转换能力。
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## 特性
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
7
|
+
- 🎯 **智能元素标记**: 自动为 JSX/TSX 元素添加 nocode 相关属性
|
|
8
|
+
- 🔍 **上下文感知**: 智能识别数组映射上下文,添加相应的数组和索引信息
|
|
9
|
+
- ⚡️ **高性能**: 基于 Babel AST 实现,支持增量编译
|
|
10
|
+
- 🛡️ **类型安全**: 使用 TypeScript 编写,提供完整的类型定义
|
|
11
|
+
- 📦 **零配置**: 开箱即用,支持自定义配置
|
|
12
12
|
|
|
13
13
|
## 安装
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
16
|
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
|
|
17
21
|
```
|
|
18
22
|
|
|
19
23
|
## 使用
|
|
20
24
|
|
|
21
|
-
###
|
|
25
|
+
### 基础配置
|
|
22
26
|
|
|
23
|
-
在你的 `vite.config.
|
|
27
|
+
在你的 `vite.config.ts` 中添加插件:
|
|
24
28
|
|
|
25
|
-
```
|
|
29
|
+
```typescript
|
|
26
30
|
import { defineConfig } from 'vite';
|
|
27
31
|
import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
28
32
|
|
|
29
33
|
export default defineConfig({
|
|
30
|
-
plugins: [
|
|
31
|
-
componentCompiler(), // 使用默认配置
|
|
32
|
-
// 其他插件...
|
|
33
|
-
],
|
|
34
|
+
plugins: [componentCompiler()],
|
|
34
35
|
});
|
|
35
36
|
```
|
|
36
37
|
|
|
37
|
-
###
|
|
38
|
-
|
|
39
|
-
如果需要查看编译过程的详细日志:
|
|
38
|
+
### 自定义配置
|
|
40
39
|
|
|
41
|
-
```
|
|
40
|
+
```typescript
|
|
42
41
|
import { defineConfig } from 'vite';
|
|
43
42
|
import { componentCompiler } from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
44
43
|
|
|
@@ -46,79 +45,83 @@ export default defineConfig({
|
|
|
46
45
|
plugins: [
|
|
47
46
|
componentCompiler({
|
|
48
47
|
enableLogging: true, // 启用调试日志
|
|
48
|
+
rootPath: process.cwd(), // 自定义项目根路径
|
|
49
49
|
}),
|
|
50
|
-
// 其他插件...
|
|
51
50
|
],
|
|
52
51
|
});
|
|
53
52
|
```
|
|
54
53
|
|
|
55
54
|
## 工作原理
|
|
56
55
|
|
|
57
|
-
|
|
56
|
+
插件会在构建过程中拦截并处理 `.jsx`、`.tsx`、`.js` 和 `.ts` 文件,通过 AST 分析为符合条件的 JSX 元素添加以下属性:
|
|
58
57
|
|
|
59
|
-
- `data-nocode-id`:
|
|
60
|
-
- `data-nocode-
|
|
61
|
-
- `data-nocode-array`:
|
|
58
|
+
- `data-nocode-id`: 元素的唯一标识,格式为 `文件路径:行号:列号`
|
|
59
|
+
- `data-nocode-name`: 元素的标签名称
|
|
60
|
+
- `data-nocode-array`: 当元素在数组 map 中时,记录数组名称
|
|
61
|
+
- `data-nocode-index`: 当元素在数组 map 中时,记录当前索引
|
|
62
62
|
|
|
63
|
-
|
|
63
|
+
### 示例
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
输入代码:
|
|
66
66
|
|
|
67
67
|
```jsx
|
|
68
|
-
function
|
|
68
|
+
function TodoList({ items }) {
|
|
69
69
|
return (
|
|
70
|
-
<div>
|
|
71
|
-
<button>Click me</button>
|
|
70
|
+
<div className='todo-list'>
|
|
72
71
|
{items.map((item, index) => (
|
|
73
|
-
<
|
|
72
|
+
<div key={item.id} className='todo-item'>
|
|
73
|
+
<span>{item.text}</span>
|
|
74
|
+
<button onClick={() => handleDelete(item.id)}>删除</button>
|
|
75
|
+
</div>
|
|
74
76
|
))}
|
|
75
77
|
</div>
|
|
76
78
|
);
|
|
77
79
|
}
|
|
78
80
|
```
|
|
79
81
|
|
|
80
|
-
|
|
82
|
+
编译后:
|
|
81
83
|
|
|
82
84
|
```jsx
|
|
83
|
-
function
|
|
85
|
+
function TodoList({ items }) {
|
|
84
86
|
return (
|
|
85
|
-
<div data-nocode-id='
|
|
86
|
-
<button data-nocode-id='App.jsx:4:6' data-nocode-name='button'>
|
|
87
|
-
Click me
|
|
88
|
-
</button>
|
|
87
|
+
<div data-nocode-id='TodoList.jsx:2:8' data-nocode-name='div' className='todo-list'>
|
|
89
88
|
{items.map((item, index) => (
|
|
90
|
-
<
|
|
91
|
-
|
|
92
|
-
|
|
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>
|
|
93
97
|
))}
|
|
94
98
|
</div>
|
|
95
99
|
);
|
|
96
100
|
}
|
|
97
101
|
```
|
|
98
102
|
|
|
99
|
-
##
|
|
103
|
+
## 技术细节
|
|
100
104
|
|
|
101
|
-
|
|
105
|
+
- 使用 `@babel/parser` 进行代码解析
|
|
106
|
+
- 使用 `@babel/traverse` 进行 AST 遍历
|
|
107
|
+
- 使用 `magic-string` 进行高效的代码修改
|
|
108
|
+
- 支持 TypeScript、装饰器等高级语法特性
|
|
109
|
+
- 智能跳过 `node_modules` 中的文件
|
|
102
110
|
|
|
103
|
-
|
|
104
|
-
- **Vue**: ✅ 已实现
|
|
111
|
+
## 版本要求
|
|
105
112
|
|
|
106
|
-
|
|
113
|
+
- Vite: ^5.4.0
|
|
114
|
+
- Node.js: ^20.0.0
|
|
107
115
|
|
|
108
116
|
## 开发
|
|
109
117
|
|
|
110
118
|
```bash
|
|
111
|
-
#
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
# 开发模式(监听文件变化)
|
|
115
|
-
npm run dev
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
## 扩展支持
|
|
119
|
-
|
|
120
|
-
如需添加新框架支持,请参考架构文档中的扩展指南。
|
|
119
|
+
# 安装依赖
|
|
120
|
+
pnpm install
|
|
121
121
|
|
|
122
|
-
|
|
122
|
+
# 开发模式
|
|
123
|
+
pnpm run dev
|
|
123
124
|
|
|
124
|
-
|
|
125
|
+
# 构建
|
|
126
|
+
pnpm run build
|
|
127
|
+
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import type { Plugin } from 'vite';
|
|
2
1
|
export interface NocodeCompilerOptions {
|
|
3
2
|
enableLogging?: boolean;
|
|
3
|
+
rootPath?: string;
|
|
4
4
|
}
|
|
5
|
-
export declare function componentCompiler(options?: NocodeCompilerOptions):
|
|
5
|
+
export declare function componentCompiler(options?: NocodeCompilerOptions): {
|
|
6
|
+
name: string;
|
|
7
|
+
enforce: "pre";
|
|
8
|
+
transform(code: string, id: string): Promise<string | null>;
|
|
9
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -3,22 +3,23 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.componentCompiler = componentCompiler;
|
|
4
4
|
const nocode_compiler_core_1 = require("@meituan-nocode/nocode-compiler-core");
|
|
5
5
|
function componentCompiler(options = {}) {
|
|
6
|
-
const { enableLogging = false } = options;
|
|
6
|
+
const { enableLogging = false, rootPath = process.cwd() } = options;
|
|
7
7
|
return {
|
|
8
8
|
name: 'vite-plugin-nocode-compiler',
|
|
9
9
|
enforce: 'pre',
|
|
10
10
|
async transform(code, id) {
|
|
11
11
|
// 检查文件是否在 node_modules 中
|
|
12
12
|
if (id.includes('node_modules')) {
|
|
13
|
-
return
|
|
13
|
+
return code;
|
|
14
14
|
}
|
|
15
15
|
// 检查文件扩展名
|
|
16
16
|
if (!/\.(jsx?|tsx?)$/.test(id)) {
|
|
17
|
-
return
|
|
17
|
+
return code;
|
|
18
18
|
}
|
|
19
19
|
// 使用 jsxcompiler 编译
|
|
20
20
|
const jsxCompiler = new nocode_compiler_core_1.JSXCompiler({
|
|
21
|
-
enableLogging
|
|
21
|
+
enableLogging,
|
|
22
|
+
rootPath,
|
|
22
23
|
});
|
|
23
24
|
const result = jsxCompiler.compile(code, id);
|
|
24
25
|
return result;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meituan-nocode/vite-plugin-nocode-compiler",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "nocode compiler
|
|
3
|
+
"version": "0.2.0-beta.1",
|
|
4
|
+
"description": "Vite plugin for nocode compiler",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -13,15 +13,12 @@
|
|
|
13
13
|
"build": "tsc",
|
|
14
14
|
"prepublishOnly": "npm run build"
|
|
15
15
|
},
|
|
16
|
-
"peerDependencies": {
|
|
17
|
-
"vite": "^5.4.0"
|
|
18
|
-
},
|
|
19
16
|
"devDependencies": {
|
|
20
17
|
"@types/node": "^20.0.0",
|
|
21
18
|
"typescript": "^5.0.0",
|
|
22
19
|
"vite": "^5.4.0"
|
|
23
20
|
},
|
|
24
21
|
"dependencies": {
|
|
25
|
-
"@meituan-nocode/nocode-compiler-core": "0.
|
|
22
|
+
"@meituan-nocode/nocode-compiler-core": "0.2.0"
|
|
26
23
|
}
|
|
27
24
|
}
|