@meituan-nocode/vite-plugin-nocode-compiler 0.2.3 → 0.2.4-beta.0
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 +188 -79
- package/dist/index.cjs +80 -0
- package/dist/index.d.cts +4 -9
- package/dist/index.d.ts +7 -6
- package/dist/index.js +113 -25
- package/package.json +30 -22
package/README.md
CHANGED
|
@@ -1,127 +1,236 @@
|
|
|
1
1
|
# @meituan-nocode/vite-plugin-nocode-compiler
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vite 插件,用于在构建过程中为 Vue 和 React 组件自动添加属性,支持无代码平台的可视化编辑。
|
|
4
4
|
|
|
5
|
-
## 特性
|
|
5
|
+
## 🚀 特性
|
|
6
6
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
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
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
33
|
+
在项目的 `vite.config.js` 或 `vite.config.ts` 中配置插件:
|
|
28
34
|
|
|
29
35
|
```typescript
|
|
36
|
+
// vite.config.ts
|
|
30
37
|
import { defineConfig } from 'vite';
|
|
31
|
-
import
|
|
38
|
+
import componentCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
|
|
32
39
|
|
|
33
40
|
export default defineConfig({
|
|
34
|
-
plugins: [
|
|
41
|
+
plugins: [
|
|
42
|
+
componentCompiler({
|
|
43
|
+
enableLogging: true, // 开发时建议开启日志
|
|
44
|
+
}),
|
|
45
|
+
],
|
|
35
46
|
});
|
|
36
47
|
```
|
|
37
48
|
|
|
38
|
-
###
|
|
49
|
+
### CommonJS 配置
|
|
39
50
|
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
|
|
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:
|
|
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
|
-
|
|
90
|
+
- `.vue` 单文件组件的 `<template>` 部分
|
|
91
|
+
- Vue 文件中的 JSX/TSX `<script>` 部分
|
|
92
|
+
- 外部模板文件 `<template src="xxx.html">`
|
|
57
93
|
|
|
58
|
-
|
|
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
|
-
|
|
68
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
-
|
|
106
|
-
-
|
|
107
|
-
-
|
|
108
|
-
-
|
|
109
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
119
|
-
# 安装依赖
|
|
120
|
-
pnpm install
|
|
166
|
+
## 🐛 调试和故障排除
|
|
121
167
|
|
|
122
|
-
|
|
123
|
-
pnpm run dev
|
|
168
|
+
### 启用调试日志
|
|
124
169
|
|
|
125
|
-
|
|
126
|
-
|
|
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
CHANGED
|
@@ -24,17 +24,97 @@ __export(index_exports, {
|
|
|
24
24
|
default: () => index_default
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var import_path = require("path");
|
|
27
28
|
var import_nocode_compiler_core = require("@meituan-nocode/nocode-compiler-core");
|
|
29
|
+
|
|
30
|
+
// src/utils.ts
|
|
31
|
+
function readJsonBody(req) {
|
|
32
|
+
return new Promise((resolve2, reject) => {
|
|
33
|
+
let body = "";
|
|
34
|
+
req.on("data", (chunk) => {
|
|
35
|
+
body += chunk.toString();
|
|
36
|
+
});
|
|
37
|
+
req.on("end", () => {
|
|
38
|
+
try {
|
|
39
|
+
resolve2(JSON.parse(body));
|
|
40
|
+
} catch (e) {
|
|
41
|
+
reject(e);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
req.on("error", reject);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/index.ts
|
|
49
|
+
var overrideMap = /* @__PURE__ */ new Map();
|
|
28
50
|
function componentCompiler(options = {}) {
|
|
29
51
|
options = {
|
|
30
52
|
enableLogging: false,
|
|
53
|
+
enableOverride: true,
|
|
31
54
|
...options
|
|
32
55
|
};
|
|
33
56
|
const vueCompiler = new import_nocode_compiler_core.VueCompiler(options);
|
|
34
57
|
const jsxCompiler = new import_nocode_compiler_core.JSXCompiler(options);
|
|
58
|
+
let server;
|
|
35
59
|
return {
|
|
36
60
|
name: "vite-plugin-nocode-compiler",
|
|
37
61
|
enforce: "pre",
|
|
62
|
+
/**
|
|
63
|
+
* 配置开发服务器,添加 Override 中间件
|
|
64
|
+
*/
|
|
65
|
+
configureServer(_server) {
|
|
66
|
+
if (!options.enableOverride) return;
|
|
67
|
+
server = _server;
|
|
68
|
+
server.middlewares.use("/__nocode_file_override", async (req, res, next) => {
|
|
69
|
+
if (req.method !== "POST") {
|
|
70
|
+
return next();
|
|
71
|
+
}
|
|
72
|
+
try {
|
|
73
|
+
const { file, code } = await readJsonBody(req);
|
|
74
|
+
if (!file || typeof code !== "string") {
|
|
75
|
+
res.statusCode = 400;
|
|
76
|
+
res.end(JSON.stringify({ error: "file and code are required" }));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const absolutePath = (0, import_path.resolve)(server.config.root, file);
|
|
80
|
+
overrideMap.set(absolutePath, code);
|
|
81
|
+
const mods = server.moduleGraph.getModulesByFile(absolutePath);
|
|
82
|
+
if (mods && mods.size > 0) {
|
|
83
|
+
for (const mod of mods) {
|
|
84
|
+
server.moduleGraph.invalidateModule(mod);
|
|
85
|
+
server.ws.send({
|
|
86
|
+
type: "update",
|
|
87
|
+
updates: [
|
|
88
|
+
{
|
|
89
|
+
type: "js-update",
|
|
90
|
+
path: mod.url,
|
|
91
|
+
acceptedPath: mod.url,
|
|
92
|
+
timestamp: Date.now()
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
res.statusCode = 200;
|
|
99
|
+
res.end(JSON.stringify({ success: true }));
|
|
100
|
+
} catch (error) {
|
|
101
|
+
res.statusCode = 500;
|
|
102
|
+
res.end(JSON.stringify({ error: String(error) }));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
/**
|
|
107
|
+
* 加载模块时,如果有 override 则返回 override 内容
|
|
108
|
+
*/
|
|
109
|
+
load(id) {
|
|
110
|
+
if (!options.enableOverride) return;
|
|
111
|
+
if (id.includes("node_modules")) return;
|
|
112
|
+
const [filePath] = id.split("?", 2);
|
|
113
|
+
if (overrideMap.has(filePath)) {
|
|
114
|
+
return overrideMap.get(filePath);
|
|
115
|
+
}
|
|
116
|
+
return void 0;
|
|
117
|
+
},
|
|
38
118
|
async transform(code, id) {
|
|
39
119
|
if (id.includes("node_modules")) {
|
|
40
120
|
return code;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
1
3
|
interface NocodeCompilerOptions {
|
|
2
4
|
enableLogging?: boolean;
|
|
3
5
|
rootPath?: string;
|
|
6
|
+
enableOverride?: boolean;
|
|
4
7
|
}
|
|
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
|
-
};
|
|
8
|
+
declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
|
|
14
9
|
|
|
15
10
|
export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
interface NocodeCompilerOptions {
|
|
2
4
|
enableLogging?: boolean;
|
|
3
5
|
rootPath?: string;
|
|
6
|
+
enableOverride?: boolean;
|
|
4
7
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
transform(code: string, id: string): Promise<string | null>;
|
|
9
|
-
};
|
|
8
|
+
declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
|
|
9
|
+
|
|
10
|
+
export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
|
package/dist/index.js
CHANGED
|
@@ -1,28 +1,116 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { JSXCompiler, VueCompiler, detectCompileScenario } from "@meituan-nocode/nocode-compiler-core";
|
|
4
|
+
|
|
5
|
+
// src/utils.ts
|
|
6
|
+
function readJsonBody(req) {
|
|
7
|
+
return new Promise((resolve2, reject) => {
|
|
8
|
+
let body = "";
|
|
9
|
+
req.on("data", (chunk) => {
|
|
10
|
+
body += chunk.toString();
|
|
11
|
+
});
|
|
12
|
+
req.on("end", () => {
|
|
13
|
+
try {
|
|
14
|
+
resolve2(JSON.parse(body));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
req.on("error", reject);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var overrideMap = /* @__PURE__ */ new Map();
|
|
5
25
|
function componentCompiler(options = {}) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
26
|
+
options = {
|
|
27
|
+
enableLogging: false,
|
|
28
|
+
enableOverride: true,
|
|
29
|
+
...options
|
|
30
|
+
};
|
|
31
|
+
const vueCompiler = new VueCompiler(options);
|
|
32
|
+
const jsxCompiler = new JSXCompiler(options);
|
|
33
|
+
let server;
|
|
34
|
+
return {
|
|
35
|
+
name: "vite-plugin-nocode-compiler",
|
|
36
|
+
enforce: "pre",
|
|
37
|
+
/**
|
|
38
|
+
* 配置开发服务器,添加 Override 中间件
|
|
39
|
+
*/
|
|
40
|
+
configureServer(_server) {
|
|
41
|
+
if (!options.enableOverride) return;
|
|
42
|
+
server = _server;
|
|
43
|
+
server.middlewares.use("/__nocode_file_override", async (req, res, next) => {
|
|
44
|
+
if (req.method !== "POST") {
|
|
45
|
+
return next();
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
const { file, code } = await readJsonBody(req);
|
|
49
|
+
if (!file || typeof code !== "string") {
|
|
50
|
+
res.statusCode = 400;
|
|
51
|
+
res.end(JSON.stringify({ error: "file and code are required" }));
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const absolutePath = resolve(server.config.root, file);
|
|
55
|
+
overrideMap.set(absolutePath, code);
|
|
56
|
+
const mods = server.moduleGraph.getModulesByFile(absolutePath);
|
|
57
|
+
if (mods && mods.size > 0) {
|
|
58
|
+
for (const mod of mods) {
|
|
59
|
+
server.moduleGraph.invalidateModule(mod);
|
|
60
|
+
server.ws.send({
|
|
61
|
+
type: "update",
|
|
62
|
+
updates: [
|
|
63
|
+
{
|
|
64
|
+
type: "js-update",
|
|
65
|
+
path: mod.url,
|
|
66
|
+
acceptedPath: mod.url,
|
|
67
|
+
timestamp: Date.now()
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
});
|
|
18
71
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
72
|
+
}
|
|
73
|
+
res.statusCode = 200;
|
|
74
|
+
res.end(JSON.stringify({ success: true }));
|
|
75
|
+
} catch (error) {
|
|
76
|
+
res.statusCode = 500;
|
|
77
|
+
res.end(JSON.stringify({ error: String(error) }));
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* 加载模块时,如果有 override 则返回 override 内容
|
|
83
|
+
*/
|
|
84
|
+
load(id) {
|
|
85
|
+
if (!options.enableOverride) return;
|
|
86
|
+
if (id.includes("node_modules")) return;
|
|
87
|
+
const [filePath] = id.split("?", 2);
|
|
88
|
+
if (overrideMap.has(filePath)) {
|
|
89
|
+
return overrideMap.get(filePath);
|
|
90
|
+
}
|
|
91
|
+
return void 0;
|
|
92
|
+
},
|
|
93
|
+
async transform(code, id) {
|
|
94
|
+
if (id.includes("node_modules")) {
|
|
95
|
+
return code;
|
|
96
|
+
}
|
|
97
|
+
const [filePath, query] = id.split("?", 2);
|
|
98
|
+
const { useJSXCompiler, useVueCompiler } = detectCompileScenario({
|
|
99
|
+
filePath,
|
|
100
|
+
query
|
|
101
|
+
});
|
|
102
|
+
if (useJSXCompiler) {
|
|
103
|
+
return jsxCompiler.compile(code, filePath) || code;
|
|
104
|
+
}
|
|
105
|
+
if (useVueCompiler) {
|
|
106
|
+
return vueCompiler.compile(code, filePath) || code;
|
|
107
|
+
}
|
|
108
|
+
return code;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
28
111
|
}
|
|
112
|
+
var index_default = componentCompiler;
|
|
113
|
+
export {
|
|
114
|
+
componentCompiler,
|
|
115
|
+
index_default as default
|
|
116
|
+
};
|
package/package.json
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"dev": "tsc --watch",
|
|
13
|
-
"build": "tsc",
|
|
14
|
-
"prepublishOnly": "npm run build"
|
|
2
|
+
"name": "@meituan-nocode/vite-plugin-nocode-compiler",
|
|
3
|
+
"version": "0.2.4-beta.0",
|
|
4
|
+
"description": "Vite plugin for nocode compiler",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"require": "./dist/index.cjs"
|
|
15
10
|
},
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.cjs",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/node": "^20.0.0",
|
|
21
|
+
"typescript": "^5.0.0",
|
|
22
|
+
"vite": "^4.5.14",
|
|
23
|
+
"tsup": "8.5.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@meituan-nocode/nocode-compiler-core": "0.1.0-beta.24-z"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"dev": "tsup --watch",
|
|
30
|
+
"build": "tsup"
|
|
31
|
+
}
|
|
32
|
+
}
|