ms-vite-plugin 0.2.9 → 0.3.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/dist/build.js +67 -1
- package/package.json +1 -1
package/dist/build.js
CHANGED
|
@@ -130,9 +130,58 @@ function detectPython314() {
|
|
|
130
130
|
}
|
|
131
131
|
return { ok: false };
|
|
132
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* 处理 Python 构建产物:
|
|
135
|
+
* - 将所有 ".cpython-314.pyc" 重命名为 ".pyc"
|
|
136
|
+
* - 将 __pycache__/name.cpython-314.pyc 移动到上级目录为 name.pyc
|
|
137
|
+
* - 删除所有 ".py" 源文件
|
|
138
|
+
*/
|
|
139
|
+
async function normalizePythonArtifacts(rootDir) {
|
|
140
|
+
async function walk(dir) {
|
|
141
|
+
const entries = await fsExtra.readdir(dir, { withFileTypes: true });
|
|
142
|
+
for (const entry of entries) {
|
|
143
|
+
const full = path.join(dir, entry.name);
|
|
144
|
+
if (entry.isDirectory()) {
|
|
145
|
+
if (entry.name === "__pycache__") {
|
|
146
|
+
const cacheEntries = await fsExtra.readdir(full, {
|
|
147
|
+
withFileTypes: true,
|
|
148
|
+
});
|
|
149
|
+
for (const ce of cacheEntries) {
|
|
150
|
+
const cacheFull = path.join(full, ce.name);
|
|
151
|
+
if (ce.isFile() && ce.name.endsWith(".cpython-314.pyc")) {
|
|
152
|
+
const parent = path.dirname(full);
|
|
153
|
+
const base = ce.name.replace(/\.cpython-314\.pyc$/, ".pyc");
|
|
154
|
+
const target = path.join(parent, base);
|
|
155
|
+
await fsExtra.move(cacheFull, target, { overwrite: true });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// 清理 __pycache__ 目录
|
|
159
|
+
try {
|
|
160
|
+
await fsExtra.remove(full);
|
|
161
|
+
}
|
|
162
|
+
catch { }
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
await walk(full);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else if (entry.isFile()) {
|
|
169
|
+
if (entry.name.endsWith(".cpython-314.pyc")) {
|
|
170
|
+
const target = full.replace(".cpython-314.pyc", ".pyc");
|
|
171
|
+
await fsExtra.move(full, target, { overwrite: true });
|
|
172
|
+
}
|
|
173
|
+
else if (entry.name.endsWith(".py")) {
|
|
174
|
+
await fsExtra.remove(full);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
await walk(rootDir);
|
|
180
|
+
}
|
|
133
181
|
const buildAll = async (isDev = true, workspacePath) => {
|
|
134
182
|
const isJs = await fsExtra.pathExists(path.join(workspacePath, "scripts", "main.js"));
|
|
135
183
|
const isPy = await fsExtra.pathExists(path.join(workspacePath, "scripts", "main.py"));
|
|
184
|
+
const isLua = await fsExtra.pathExists(path.join(workspacePath, "scripts", "main.lua"));
|
|
136
185
|
if (isJs) {
|
|
137
186
|
console.log("🚀 开始构建JavaScript项目...");
|
|
138
187
|
const obfuscatorConfigPath = path.join(workspacePath, "obfuscator.json");
|
|
@@ -182,12 +231,29 @@ const buildAll = async (isDev = true, workspacePath) => {
|
|
|
182
231
|
}
|
|
183
232
|
const outPath = path.join(workspacePath, "msbundle");
|
|
184
233
|
await fsExtra.ensureDir(outPath);
|
|
185
|
-
if (isPy) {
|
|
234
|
+
if (isPy || isLua) {
|
|
186
235
|
await fsExtra.copy(path.join(workspacePath, "scripts"), path.join(outPath, "scripts"));
|
|
236
|
+
}
|
|
237
|
+
if (isPy) {
|
|
187
238
|
// 获取系统有没有 3.14 版本的 python
|
|
188
239
|
const py = detectPython314();
|
|
189
240
|
if (py.ok) {
|
|
190
241
|
console.log(`✅ 检测到 ${py.version} (${py.path})`);
|
|
242
|
+
// python -m compileall scripts
|
|
243
|
+
const r = (0, child_process_1.spawnSync)(py.path, ["-m", "compileall", "scripts"], {
|
|
244
|
+
encoding: "utf8",
|
|
245
|
+
cwd: outPath,
|
|
246
|
+
});
|
|
247
|
+
if (r.status === 0) {
|
|
248
|
+
console.log("✅ 成功编译所有 Python 文件");
|
|
249
|
+
// 修改所有 .cpython-314.pyc 为 .pyc,并删除所有 .py 文件
|
|
250
|
+
await normalizePythonArtifacts(path.join(outPath, "scripts"));
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
console.error("❌ 编译 Python 文件失败");
|
|
254
|
+
console.error(r.stderr);
|
|
255
|
+
process.exit(1);
|
|
256
|
+
}
|
|
191
257
|
}
|
|
192
258
|
else {
|
|
193
259
|
console.warn("⚠️ 未检测到 Python 3.14,请确保在目标运行环境安装 Python 3.14");
|