@meituan-nocode/vite-plugin-nocode-compiler 0.2.4-beta.0 → 0.2.4-beta.10
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/dist/index.cjs +85 -8
- package/dist/index.d.cts +6 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +87 -10
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -25,18 +25,19 @@ __export(index_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
27
|
var import_path = require("path");
|
|
28
|
+
var import_fs = require("fs");
|
|
28
29
|
var import_nocode_compiler_core = require("@meituan-nocode/nocode-compiler-core");
|
|
29
30
|
|
|
30
31
|
// src/utils.ts
|
|
31
32
|
function readJsonBody(req) {
|
|
32
|
-
return new Promise((
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
33
34
|
let body = "";
|
|
34
35
|
req.on("data", (chunk) => {
|
|
35
36
|
body += chunk.toString();
|
|
36
37
|
});
|
|
37
38
|
req.on("end", () => {
|
|
38
39
|
try {
|
|
39
|
-
|
|
40
|
+
resolve(JSON.parse(body));
|
|
40
41
|
} catch (e) {
|
|
41
42
|
reject(e);
|
|
42
43
|
}
|
|
@@ -45,30 +46,88 @@ function readJsonBody(req) {
|
|
|
45
46
|
});
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
// package.json
|
|
50
|
+
var version = "0.2.4-beta.10";
|
|
51
|
+
|
|
48
52
|
// src/index.ts
|
|
49
53
|
var overrideMap = /* @__PURE__ */ new Map();
|
|
54
|
+
function resolveFileToAbsPath(file, viteRoot) {
|
|
55
|
+
if ((0, import_path.isAbsolute)(file)) {
|
|
56
|
+
return (0, import_fs.existsSync)(file) ? (0, import_path.normalize)(file) : null;
|
|
57
|
+
}
|
|
58
|
+
const fromRoot = (0, import_path.join)(viteRoot, file);
|
|
59
|
+
if ((0, import_fs.existsSync)(fromRoot)) {
|
|
60
|
+
return (0, import_path.normalize)(fromRoot);
|
|
61
|
+
}
|
|
62
|
+
let dir = viteRoot;
|
|
63
|
+
while (true) {
|
|
64
|
+
const candidate = (0, import_path.join)(dir, file);
|
|
65
|
+
if ((0, import_fs.existsSync)(candidate)) {
|
|
66
|
+
return (0, import_path.normalize)(candidate);
|
|
67
|
+
}
|
|
68
|
+
const parent = (0, import_path.dirname)(dir);
|
|
69
|
+
if (parent === dir) break;
|
|
70
|
+
dir = parent;
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
50
74
|
function componentCompiler(options = {}) {
|
|
51
75
|
options = {
|
|
52
76
|
enableLogging: false,
|
|
53
|
-
|
|
77
|
+
enableReporting: true,
|
|
54
78
|
...options
|
|
55
79
|
};
|
|
80
|
+
if (options.enableReporting) {
|
|
81
|
+
console.log("[vite compiler] init");
|
|
82
|
+
(0, import_nocode_compiler_core.initReporter)({
|
|
83
|
+
appName: options.reporterAppName || "nocode-compiler-plugin",
|
|
84
|
+
enabled: true,
|
|
85
|
+
framework: "vite",
|
|
86
|
+
pluginVersion: version
|
|
87
|
+
});
|
|
88
|
+
}
|
|
56
89
|
const vueCompiler = new import_nocode_compiler_core.VueCompiler(options);
|
|
57
90
|
const jsxCompiler = new import_nocode_compiler_core.JSXCompiler(options);
|
|
58
91
|
let server;
|
|
92
|
+
let pkgRegistry;
|
|
59
93
|
return {
|
|
60
94
|
name: "vite-plugin-nocode-compiler",
|
|
61
95
|
enforce: "pre",
|
|
96
|
+
/**
|
|
97
|
+
* 配置解析完成后,初始化 PackageVersionRegistry
|
|
98
|
+
* 使用 Vite 配置的 root 作为项目根目录
|
|
99
|
+
*/
|
|
100
|
+
configResolved(config) {
|
|
101
|
+
pkgRegistry = new import_nocode_compiler_core.PackageVersionRegistry(config.root);
|
|
102
|
+
},
|
|
62
103
|
/**
|
|
63
104
|
* 配置开发服务器,添加 Override 中间件
|
|
64
105
|
*/
|
|
65
106
|
configureServer(_server) {
|
|
66
|
-
if (!options.enableOverride) return;
|
|
67
107
|
server = _server;
|
|
108
|
+
server.ws.on("connection", () => {
|
|
109
|
+
if (overrideMap.size === 0) return;
|
|
110
|
+
const reporter = (0, import_nocode_compiler_core.getReporter)();
|
|
111
|
+
const overrideCount = overrideMap.size;
|
|
112
|
+
for (const filePath of overrideMap.keys()) {
|
|
113
|
+
const mods = server.moduleGraph.getModulesByFile(filePath);
|
|
114
|
+
if (mods && mods.size > 0) {
|
|
115
|
+
for (const mod of mods) {
|
|
116
|
+
server.moduleGraph.invalidateModule(mod);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
overrideMap.clear();
|
|
121
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
122
|
+
action: "clear",
|
|
123
|
+
overrideCount
|
|
124
|
+
});
|
|
125
|
+
});
|
|
68
126
|
server.middlewares.use("/__nocode_file_override", async (req, res, next) => {
|
|
69
127
|
if (req.method !== "POST") {
|
|
70
128
|
return next();
|
|
71
129
|
}
|
|
130
|
+
const reporter = (0, import_nocode_compiler_core.getReporter)();
|
|
72
131
|
try {
|
|
73
132
|
const { file, code } = await readJsonBody(req);
|
|
74
133
|
if (!file || typeof code !== "string") {
|
|
@@ -76,7 +135,17 @@ function componentCompiler(options = {}) {
|
|
|
76
135
|
res.end(JSON.stringify({ error: "file and code are required" }));
|
|
77
136
|
return;
|
|
78
137
|
}
|
|
79
|
-
const absolutePath = (
|
|
138
|
+
const absolutePath = resolveFileToAbsPath(file, server.config.root);
|
|
139
|
+
if (!absolutePath) {
|
|
140
|
+
console.warn("[nocode-compiler] file not found:", file, server.config.root);
|
|
141
|
+
res.statusCode = 404;
|
|
142
|
+
res.end(JSON.stringify({ error: "file not found" }));
|
|
143
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
144
|
+
action: "notfound",
|
|
145
|
+
filePath: file
|
|
146
|
+
});
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
80
149
|
overrideMap.set(absolutePath, code);
|
|
81
150
|
const mods = server.moduleGraph.getModulesByFile(absolutePath);
|
|
82
151
|
if (mods && mods.size > 0) {
|
|
@@ -95,9 +164,18 @@ function componentCompiler(options = {}) {
|
|
|
95
164
|
});
|
|
96
165
|
}
|
|
97
166
|
}
|
|
167
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
168
|
+
action: "set",
|
|
169
|
+
filePath: absolutePath,
|
|
170
|
+
overrideCount: overrideMap.size
|
|
171
|
+
});
|
|
98
172
|
res.statusCode = 200;
|
|
99
173
|
res.end(JSON.stringify({ success: true }));
|
|
100
174
|
} catch (error) {
|
|
175
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
176
|
+
action: "error",
|
|
177
|
+
error: error instanceof Error ? error : new Error(String(error))
|
|
178
|
+
});
|
|
101
179
|
res.statusCode = 500;
|
|
102
180
|
res.end(JSON.stringify({ error: String(error) }));
|
|
103
181
|
}
|
|
@@ -107,7 +185,6 @@ function componentCompiler(options = {}) {
|
|
|
107
185
|
* 加载模块时,如果有 override 则返回 override 内容
|
|
108
186
|
*/
|
|
109
187
|
load(id) {
|
|
110
|
-
if (!options.enableOverride) return;
|
|
111
188
|
if (id.includes("node_modules")) return;
|
|
112
189
|
const [filePath] = id.split("?", 2);
|
|
113
190
|
if (overrideMap.has(filePath)) {
|
|
@@ -125,10 +202,10 @@ function componentCompiler(options = {}) {
|
|
|
125
202
|
query
|
|
126
203
|
});
|
|
127
204
|
if (useJSXCompiler) {
|
|
128
|
-
return jsxCompiler.compile(code, filePath) || code;
|
|
205
|
+
return jsxCompiler.compile(code, filePath, pkgRegistry) || code;
|
|
129
206
|
}
|
|
130
207
|
if (useVueCompiler) {
|
|
131
|
-
return vueCompiler.compile(code, filePath) || code;
|
|
208
|
+
return vueCompiler.compile(code, filePath, pkgRegistry) || code;
|
|
132
209
|
}
|
|
133
210
|
return code;
|
|
134
211
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
|
|
3
3
|
interface NocodeCompilerOptions {
|
|
4
|
+
/** 是否启用日志 */
|
|
4
5
|
enableLogging?: boolean;
|
|
6
|
+
/** 项目根目录 */
|
|
5
7
|
rootPath?: string;
|
|
6
|
-
|
|
8
|
+
/** 是否启用埋点上报,默认 true */
|
|
9
|
+
enableReporting?: boolean;
|
|
10
|
+
/** 自定义 Cat appName */
|
|
11
|
+
reporterAppName?: string;
|
|
7
12
|
}
|
|
8
13
|
declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
|
|
9
14
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { Plugin } from 'vite';
|
|
2
2
|
|
|
3
3
|
interface NocodeCompilerOptions {
|
|
4
|
+
/** 是否启用日志 */
|
|
4
5
|
enableLogging?: boolean;
|
|
6
|
+
/** 项目根目录 */
|
|
5
7
|
rootPath?: string;
|
|
6
|
-
|
|
8
|
+
/** 是否启用埋点上报,默认 true */
|
|
9
|
+
enableReporting?: boolean;
|
|
10
|
+
/** 自定义 Cat appName */
|
|
11
|
+
reporterAppName?: string;
|
|
7
12
|
}
|
|
8
13
|
declare function componentCompiler(options?: NocodeCompilerOptions): Plugin;
|
|
9
14
|
|
package/dist/index.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { normalize, dirname, isAbsolute, join } from "path";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
import { JSXCompiler, VueCompiler, detectCompileScenario, PackageVersionRegistry, initReporter, getReporter } from "@meituan-nocode/nocode-compiler-core";
|
|
4
5
|
|
|
5
6
|
// src/utils.ts
|
|
6
7
|
function readJsonBody(req) {
|
|
7
|
-
return new Promise((
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
8
9
|
let body = "";
|
|
9
10
|
req.on("data", (chunk) => {
|
|
10
11
|
body += chunk.toString();
|
|
11
12
|
});
|
|
12
13
|
req.on("end", () => {
|
|
13
14
|
try {
|
|
14
|
-
|
|
15
|
+
resolve(JSON.parse(body));
|
|
15
16
|
} catch (e) {
|
|
16
17
|
reject(e);
|
|
17
18
|
}
|
|
@@ -20,30 +21,88 @@ function readJsonBody(req) {
|
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
23
|
|
|
24
|
+
// package.json
|
|
25
|
+
var version = "0.2.4-beta.10";
|
|
26
|
+
|
|
23
27
|
// src/index.ts
|
|
24
28
|
var overrideMap = /* @__PURE__ */ new Map();
|
|
29
|
+
function resolveFileToAbsPath(file, viteRoot) {
|
|
30
|
+
if (isAbsolute(file)) {
|
|
31
|
+
return existsSync(file) ? normalize(file) : null;
|
|
32
|
+
}
|
|
33
|
+
const fromRoot = join(viteRoot, file);
|
|
34
|
+
if (existsSync(fromRoot)) {
|
|
35
|
+
return normalize(fromRoot);
|
|
36
|
+
}
|
|
37
|
+
let dir = viteRoot;
|
|
38
|
+
while (true) {
|
|
39
|
+
const candidate = join(dir, file);
|
|
40
|
+
if (existsSync(candidate)) {
|
|
41
|
+
return normalize(candidate);
|
|
42
|
+
}
|
|
43
|
+
const parent = dirname(dir);
|
|
44
|
+
if (parent === dir) break;
|
|
45
|
+
dir = parent;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
25
49
|
function componentCompiler(options = {}) {
|
|
26
50
|
options = {
|
|
27
51
|
enableLogging: false,
|
|
28
|
-
|
|
52
|
+
enableReporting: true,
|
|
29
53
|
...options
|
|
30
54
|
};
|
|
55
|
+
if (options.enableReporting) {
|
|
56
|
+
console.log("[vite compiler] init");
|
|
57
|
+
initReporter({
|
|
58
|
+
appName: options.reporterAppName || "nocode-compiler-plugin",
|
|
59
|
+
enabled: true,
|
|
60
|
+
framework: "vite",
|
|
61
|
+
pluginVersion: version
|
|
62
|
+
});
|
|
63
|
+
}
|
|
31
64
|
const vueCompiler = new VueCompiler(options);
|
|
32
65
|
const jsxCompiler = new JSXCompiler(options);
|
|
33
66
|
let server;
|
|
67
|
+
let pkgRegistry;
|
|
34
68
|
return {
|
|
35
69
|
name: "vite-plugin-nocode-compiler",
|
|
36
70
|
enforce: "pre",
|
|
71
|
+
/**
|
|
72
|
+
* 配置解析完成后,初始化 PackageVersionRegistry
|
|
73
|
+
* 使用 Vite 配置的 root 作为项目根目录
|
|
74
|
+
*/
|
|
75
|
+
configResolved(config) {
|
|
76
|
+
pkgRegistry = new PackageVersionRegistry(config.root);
|
|
77
|
+
},
|
|
37
78
|
/**
|
|
38
79
|
* 配置开发服务器,添加 Override 中间件
|
|
39
80
|
*/
|
|
40
81
|
configureServer(_server) {
|
|
41
|
-
if (!options.enableOverride) return;
|
|
42
82
|
server = _server;
|
|
83
|
+
server.ws.on("connection", () => {
|
|
84
|
+
if (overrideMap.size === 0) return;
|
|
85
|
+
const reporter = getReporter();
|
|
86
|
+
const overrideCount = overrideMap.size;
|
|
87
|
+
for (const filePath of overrideMap.keys()) {
|
|
88
|
+
const mods = server.moduleGraph.getModulesByFile(filePath);
|
|
89
|
+
if (mods && mods.size > 0) {
|
|
90
|
+
for (const mod of mods) {
|
|
91
|
+
server.moduleGraph.invalidateModule(mod);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
overrideMap.clear();
|
|
96
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
97
|
+
action: "clear",
|
|
98
|
+
overrideCount
|
|
99
|
+
});
|
|
100
|
+
});
|
|
43
101
|
server.middlewares.use("/__nocode_file_override", async (req, res, next) => {
|
|
44
102
|
if (req.method !== "POST") {
|
|
45
103
|
return next();
|
|
46
104
|
}
|
|
105
|
+
const reporter = getReporter();
|
|
47
106
|
try {
|
|
48
107
|
const { file, code } = await readJsonBody(req);
|
|
49
108
|
if (!file || typeof code !== "string") {
|
|
@@ -51,7 +110,17 @@ function componentCompiler(options = {}) {
|
|
|
51
110
|
res.end(JSON.stringify({ error: "file and code are required" }));
|
|
52
111
|
return;
|
|
53
112
|
}
|
|
54
|
-
const absolutePath =
|
|
113
|
+
const absolutePath = resolveFileToAbsPath(file, server.config.root);
|
|
114
|
+
if (!absolutePath) {
|
|
115
|
+
console.warn("[nocode-compiler] file not found:", file, server.config.root);
|
|
116
|
+
res.statusCode = 404;
|
|
117
|
+
res.end(JSON.stringify({ error: "file not found" }));
|
|
118
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
119
|
+
action: "notfound",
|
|
120
|
+
filePath: file
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
55
124
|
overrideMap.set(absolutePath, code);
|
|
56
125
|
const mods = server.moduleGraph.getModulesByFile(absolutePath);
|
|
57
126
|
if (mods && mods.size > 0) {
|
|
@@ -70,9 +139,18 @@ function componentCompiler(options = {}) {
|
|
|
70
139
|
});
|
|
71
140
|
}
|
|
72
141
|
}
|
|
142
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
143
|
+
action: "set",
|
|
144
|
+
filePath: absolutePath,
|
|
145
|
+
overrideCount: overrideMap.size
|
|
146
|
+
});
|
|
73
147
|
res.statusCode = 200;
|
|
74
148
|
res.end(JSON.stringify({ success: true }));
|
|
75
149
|
} catch (error) {
|
|
150
|
+
reporter == null ? void 0 : reporter.reportOverride({
|
|
151
|
+
action: "error",
|
|
152
|
+
error: error instanceof Error ? error : new Error(String(error))
|
|
153
|
+
});
|
|
76
154
|
res.statusCode = 500;
|
|
77
155
|
res.end(JSON.stringify({ error: String(error) }));
|
|
78
156
|
}
|
|
@@ -82,7 +160,6 @@ function componentCompiler(options = {}) {
|
|
|
82
160
|
* 加载模块时,如果有 override 则返回 override 内容
|
|
83
161
|
*/
|
|
84
162
|
load(id) {
|
|
85
|
-
if (!options.enableOverride) return;
|
|
86
163
|
if (id.includes("node_modules")) return;
|
|
87
164
|
const [filePath] = id.split("?", 2);
|
|
88
165
|
if (overrideMap.has(filePath)) {
|
|
@@ -100,10 +177,10 @@ function componentCompiler(options = {}) {
|
|
|
100
177
|
query
|
|
101
178
|
});
|
|
102
179
|
if (useJSXCompiler) {
|
|
103
|
-
return jsxCompiler.compile(code, filePath) || code;
|
|
180
|
+
return jsxCompiler.compile(code, filePath, pkgRegistry) || code;
|
|
104
181
|
}
|
|
105
182
|
if (useVueCompiler) {
|
|
106
|
-
return vueCompiler.compile(code, filePath) || code;
|
|
183
|
+
return vueCompiler.compile(code, filePath, pkgRegistry) || code;
|
|
107
184
|
}
|
|
108
185
|
return code;
|
|
109
186
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meituan-nocode/vite-plugin-nocode-compiler",
|
|
3
|
-
"version": "0.2.4-beta.
|
|
3
|
+
"version": "0.2.4-beta.10",
|
|
4
4
|
"description": "Vite plugin for nocode compiler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"tsup": "8.5.0"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@meituan-nocode/nocode-compiler-core": "0.
|
|
26
|
+
"@meituan-nocode/nocode-compiler-core": "0.2.5-beta.4"
|
|
27
27
|
},
|
|
28
28
|
"scripts": {
|
|
29
29
|
"dev": "tsup --watch",
|