@meituan-nocode/vite-plugin-nocode-compiler 0.2.3-beta.1 → 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/dist/index.cjs +80 -0
- package/dist/index.d.cts +4 -9
- package/dist/index.d.ts +4 -9
- package/dist/index.js +80 -0
- package/package.json +30 -31
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,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.js
CHANGED
|
@@ -1,15 +1,95 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
import { resolve } from "path";
|
|
2
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();
|
|
3
25
|
function componentCompiler(options = {}) {
|
|
4
26
|
options = {
|
|
5
27
|
enableLogging: false,
|
|
28
|
+
enableOverride: true,
|
|
6
29
|
...options
|
|
7
30
|
};
|
|
8
31
|
const vueCompiler = new VueCompiler(options);
|
|
9
32
|
const jsxCompiler = new JSXCompiler(options);
|
|
33
|
+
let server;
|
|
10
34
|
return {
|
|
11
35
|
name: "vite-plugin-nocode-compiler",
|
|
12
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
|
+
},
|
|
13
93
|
async transform(code, id) {
|
|
14
94
|
if (id.includes("node_modules")) {
|
|
15
95
|
return code;
|
package/package.json
CHANGED
|
@@ -1,33 +1,32 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
},
|
|
11
|
-
"./package.json": "./package.json"
|
|
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"
|
|
12
10
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
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
|
+
}
|