@meituan-nocode/vite-plugin-nocode-compiler 0.1.0-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 ADDED
@@ -0,0 +1,236 @@
1
+ # @meituan-nocode/vite-plugin-nocode-compiler
2
+
3
+ Vite 插件,用于在构建过程中为 Vue 和 React 组件自动添加属性,支持无代码平台的可视化编辑。
4
+
5
+ ## 🚀 特性
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` 文件
15
+
16
+ ## 📦 安装
17
+
18
+ ```bash
19
+ # npm
20
+ 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
27
+ ```
28
+
29
+ ## 🔧 快速开始
30
+
31
+ ### 基础配置
32
+
33
+ 在项目的 `vite.config.js` 或 `vite.config.ts` 中配置插件:
34
+
35
+ ```typescript
36
+ // vite.config.ts
37
+ import { defineConfig } from 'vite';
38
+ import componentCompiler from '@meituan-nocode/vite-plugin-nocode-compiler';
39
+
40
+ 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
+ ],
62
+ });
63
+ ```
64
+
65
+ ## ⚙️ 配置选项
66
+
67
+ | 选项 | 类型 | 默认值 | 描述 |
68
+ | --------------- | --------- | ------- | ------------------------------------------------------ |
69
+ | `enableLogging` | `boolean` | `false` | 是否启用日志输出,开启后会输出详细的处理过程和调试信息 |
70
+ | `rootPath` | `string` | - | 项目根路径,用于生成相对路径标识 |
71
+
72
+ ### 配置示例
73
+
74
+ ```typescript
75
+ // 完整配置示例
76
+ export default defineConfig({
77
+ plugins: [
78
+ componentCompiler({
79
+ enableLogging: process.env.NODE_ENV === 'development',
80
+ rootPath: process.cwd(),
81
+ }),
82
+ ],
83
+ });
84
+ ```
85
+
86
+ ## 📁 处理的文件类型
87
+
88
+ ### Vue 文件
89
+
90
+ - `.vue` 单文件组件的 `<template>` 部分
91
+ - Vue 文件中的 JSX/TSX `<script>` 部分
92
+ - 外部模板文件 `<template src="xxx.html">`
93
+
94
+ ### React 文件
95
+
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
108
+
109
+ // Vue 模板处理
110
+ // App.vue?type=template
111
+
112
+ // 外部模板处理
113
+ // template.html?type=template&vue
114
+ ```
115
+
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>
130
+ ```
131
+
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` 循环中时,标识数组名称
139
+
140
+ ## 🏗️ 在 Monorepo 项目中使用
141
+
142
+ 在 monorepo 项目中,建议在共享配置中定义插件:
143
+
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
+ ```
165
+
166
+ ## 🐛 调试和故障排除
167
+
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: 如何排除特定文件?
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
+ });
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,141 @@
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_path = require("path");
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();
50
+ function componentCompiler(options = {}) {
51
+ options = {
52
+ enableLogging: false,
53
+ enableOverride: true,
54
+ ...options
55
+ };
56
+ const vueCompiler = new import_nocode_compiler_core.VueCompiler(options);
57
+ const jsxCompiler = new import_nocode_compiler_core.JSXCompiler(options);
58
+ let server;
59
+ return {
60
+ name: "vite-plugin-nocode-compiler",
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
+ },
118
+ async transform(code, id) {
119
+ if (id.includes("node_modules")) {
120
+ return code;
121
+ }
122
+ const [filePath, query] = id.split("?", 2);
123
+ const { useJSXCompiler, useVueCompiler } = (0, import_nocode_compiler_core.detectCompileScenario)({
124
+ filePath,
125
+ query
126
+ });
127
+ if (useJSXCompiler) {
128
+ return jsxCompiler.compile(code, filePath) || code;
129
+ }
130
+ if (useVueCompiler) {
131
+ return vueCompiler.compile(code, filePath) || code;
132
+ }
133
+ return code;
134
+ }
135
+ };
136
+ }
137
+ var index_default = componentCompiler;
138
+ // Annotate the CommonJS export names for ESM import in node:
139
+ 0 && (module.exports = {
140
+ componentCompiler
141
+ });
@@ -0,0 +1,10 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface NocodeCompilerOptions {
4
+ enableLogging?: boolean;
5
+ rootPath?: string;
6
+ enableOverride?: boolean;
7
+ }
8
+ declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
9
+
10
+ export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
@@ -0,0 +1,10 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ interface NocodeCompilerOptions {
4
+ enableLogging?: boolean;
5
+ rootPath?: string;
6
+ enableOverride?: boolean;
7
+ }
8
+ declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
9
+
10
+ export { type NocodeCompilerOptions, componentCompiler, componentCompiler as default };
package/dist/index.js ADDED
@@ -0,0 +1,116 @@
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();
25
+ function componentCompiler(options = {}) {
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
+ });
71
+ }
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
+ };
111
+ }
112
+ var index_default = componentCompiler;
113
+ export {
114
+ componentCompiler,
115
+ index_default as default
116
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@meituan-nocode/vite-plugin-nocode-compiler",
3
+ "version": "0.1.0-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"
10
+ },
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
+ }