@lark-apaas/miaoda-cli 0.1.33-alpha.b340b4a → 0.1.33-alpha.d63e9fb
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 +8 -7
- package/dist/cli/commands/app/index.js +39 -22
- package/dist/cli/handlers/app/index.js +6 -4
- package/dist/cli/handlers/app/pack.js +256 -0
- package/dist/services/app/{export-standalone → pack}/archive.js +4 -2
- package/dist/services/app/{export-standalone → pack}/copy-tree.js +1 -1
- package/dist/services/app/{export-standalone → pack}/download.js +6 -2
- package/dist/services/app/{export-standalone → pack}/index.js +8 -2
- package/dist/services/app/pack/inline-babel.js +91 -0
- package/dist/services/app/{export-standalone → pack}/rewrite.js +8 -0
- package/dist/services/app/{export-standalone → pack}/scan.js +23 -0
- package/dist/services/app/pack/strategies.js +264 -0
- package/dist/services/app/pack/upload.js +110 -0
- package/dist/utils/exclude-patterns.js +1 -1
- package/package.json +1 -1
- package/dist/cli/handlers/app/export-standalone.js +0 -165
- package/dist/services/app/export-standalone/strategies.js +0 -127
- /package/dist/services/app/{export-standalone → pack}/naming.js +0 -0
package/README.md
CHANGED
|
@@ -52,18 +52,19 @@ miaoda file ls --output json
|
|
|
52
52
|
|
|
53
53
|
完整命令通过 `miaoda --help` 或 `miaoda <domain> --help` 查看。
|
|
54
54
|
|
|
55
|
-
##
|
|
55
|
+
## 打包可本地访问的静态产物
|
|
56
56
|
|
|
57
|
-
`miaoda app
|
|
57
|
+
`miaoda app pack`(默认 `--mode standalone`)把当前应用构建成一份自包含静态产物,可脱离妙搭平台本地打开。
|
|
58
58
|
|
|
59
59
|
```bash
|
|
60
|
-
miaoda app
|
|
61
|
-
miaoda app
|
|
62
|
-
miaoda app
|
|
60
|
+
miaoda app pack # → dist/standalone.zip(中间目录打完即删)
|
|
61
|
+
miaoda app pack --out dist/share
|
|
62
|
+
miaoda app pack --json
|
|
63
|
+
miaoda app pack --upload-url 'https://tos.example.com/x.zip?sign=...'
|
|
63
64
|
```
|
|
64
65
|
|
|
65
66
|
支持 `design-html` / `html` / `vite-react` / `nestjs-react-fullstack`。页面与代码里字面以
|
|
66
|
-
`/spark/app`
|
|
67
|
+
`/spark/app` 开头的存储图片会经沙箱网关(localhost:8080)下载到 `images/` 并改写成相对路径。
|
|
67
68
|
|
|
68
69
|
默认输出落在 `dist/` 下是刻意的:`dist` 已在部署扫描的 EXCLUDES 与各模板 `.gitignore` 中,
|
|
69
70
|
产物不会污染部署包、不会被误提交;代价是下一次构建会清掉它。
|
|
@@ -71,7 +72,7 @@ miaoda app export-standalone --json
|
|
|
71
72
|
**只保证纯静态可看** —— 接口调用会失败,不做数据快照。多路由 SPA 在 `file://` 下不可用
|
|
72
73
|
(`BrowserRouter` 匹配不上路径),需用本地 HTTP server 打开,命令会在 `warnings` 里提示。
|
|
73
74
|
|
|
74
|
-
> 与 `miaoda app export`
|
|
75
|
+
> 与 `miaoda app export` **语义无关**:后者拉的是**其他应用**的源码归档(创意应用升级链路的只读参考物)。
|
|
75
76
|
|
|
76
77
|
## 组件 registry
|
|
77
78
|
|
|
@@ -22,7 +22,7 @@ function registerAppCommands(program, opts = {}) {
|
|
|
22
22
|
registerAppGet(appCmd);
|
|
23
23
|
registerAppUpdate(appCmd);
|
|
24
24
|
registerAppExport(appCmd);
|
|
25
|
-
|
|
25
|
+
registerAppPack(appCmd);
|
|
26
26
|
if (opts.includeInit) {
|
|
27
27
|
registerAppInit(appCmd);
|
|
28
28
|
registerAppSync(appCmd);
|
|
@@ -128,9 +128,9 @@ function registerAppExport(parent) {
|
|
|
128
128
|
.option('--out <dir>', '输出目录(相对当前目录)', 'source_package/creative')
|
|
129
129
|
.option('--checkpoint <n>', '按检查点版本导出;缺省为最新提交(HEAD)')
|
|
130
130
|
.addHelpText('after', `
|
|
131
|
-
与 'miaoda app
|
|
132
|
-
app export
|
|
133
|
-
app
|
|
131
|
+
与 'miaoda app pack' 的区别(**两个命令语义无关,别选错**)
|
|
132
|
+
app export 拉「其他应用」的源码归档(本命令)
|
|
133
|
+
app pack 把「当前应用」打包成产物(默认 standalone 形态)
|
|
134
134
|
|
|
135
135
|
说明
|
|
136
136
|
从服务端导出源应用(可以不是当前应用)的源码归档并解压到 --out 目录,导出完成后写
|
|
@@ -178,30 +178,41 @@ JSON 输出
|
|
|
178
178
|
});
|
|
179
179
|
}));
|
|
180
180
|
}
|
|
181
|
-
function
|
|
181
|
+
function registerAppPack(parent) {
|
|
182
182
|
const cmd = parent
|
|
183
|
-
.command('
|
|
184
|
-
.description('
|
|
183
|
+
.command('pack')
|
|
184
|
+
.description('打包应用产物。默认 standalone 模式:自包含静态产物(目录 + zip),可脱离妙搭平台本地访问')
|
|
185
185
|
.option('--dir <path>', '项目目录,默认 cwd(需含 .spark/meta.json)')
|
|
186
|
-
.option('--
|
|
187
|
-
.option('--
|
|
186
|
+
.option('--mode <mode>', `打包形态(${index_1.PACK_MODES.join(' / ')})`, index_1.DEFAULT_PACK_MODE)
|
|
187
|
+
.option('--out <dir>', '产物路径(相对项目根),实际产出 <out>.zip', index_1.DEFAULT_PACK_OUT)
|
|
188
|
+
.option('--upload-url <url>', '打包后把 zip 以 PUT 上传到该预签名 URL(http/https);上传成功后清掉本地产物')
|
|
188
189
|
.addHelpText('after', `
|
|
189
190
|
与 'miaoda app export' 的区别(**两个命令语义无关,别选错**)
|
|
190
|
-
app export
|
|
191
|
-
app
|
|
191
|
+
app export 拉「其他应用」的源码归档(创意应用升级链路的只读参考物)
|
|
192
|
+
app pack 把「当前应用」打包成产物(本命令)
|
|
193
|
+
|
|
194
|
+
打包形态(--mode)
|
|
195
|
+
standalone(默认) 自包含静态产物,可脱离妙搭平台本地访问
|
|
196
|
+
后续新增形态时在此列出;不传 --mode 的行为永远等同 standalone。
|
|
192
197
|
|
|
193
198
|
支持的 stack
|
|
194
199
|
design-html / html / vite-react / nestjs-react-fullstack
|
|
195
200
|
|
|
196
201
|
产物
|
|
197
|
-
<out
|
|
198
|
-
|
|
202
|
+
<out>.zip **唯一交付物**。内含 index.html + assets/ + images/
|
|
203
|
+
(+ static/,vite 系有 shared/static 时)
|
|
204
|
+
<out>/ 打包用的中间目录,**打完包即删除**——它与 zip 内容重复,
|
|
205
|
+
留着占双份磁盘。要预览请先解压。
|
|
206
|
+
传了 --upload-url 时,zip 会以 PUT 裸 body(Content-Type: application/zip)
|
|
207
|
+
上传到该地址——预签名 URL 的标准形态,鉴权由 URL 自带签名承担,不加额外 header。
|
|
208
|
+
**上传成功后本地 zip 也会被清掉**(目录本就已删),交付物以远端为准,
|
|
209
|
+
JSON 里 localRemoved=true。上传失败则保留 zip,便于修正后重跑或手动上传。
|
|
199
210
|
默认 <out> 是 dist/standalone —— 落在 dist/ 下是刻意的:dist 已在部署扫描的
|
|
200
211
|
EXCLUDES 与各模板 .gitignore 中,产物不会污染部署包、不会被误提交。
|
|
201
212
|
代价:下一次构建会 rm -rf dist,导出产物是易失的。
|
|
202
213
|
|
|
203
214
|
图片本地化
|
|
204
|
-
只处理页面/代码里字面以 /spark/app 开头的 URL
|
|
215
|
+
只处理页面/代码里字面以 /spark/app 开头的 URL,经沙箱网关(localhost:8080)下载到
|
|
205
216
|
<out>/images/ 并改写成相对路径。其它挂载前缀(/spark/p/、/app/<id>、反代)不处理。
|
|
206
217
|
接口运行时返回的 download_url 属动态 URL,静态扫描覆盖不到。
|
|
207
218
|
|
|
@@ -215,25 +226,31 @@ JSON 输出
|
|
|
215
226
|
{"data": {"appId": "...", "stack": "...", "out": "dist/standalone", "zip": "dist/standalone.zip",
|
|
216
227
|
"fileCount": N, "bytes": N,
|
|
217
228
|
"images": {"matched": N, "downloaded": N, "failed": [{"url": "...", "reason": "..."}]},
|
|
229
|
+
"localRemoved": false,
|
|
218
230
|
"rewrittenFiles": [...],
|
|
219
231
|
"warnings": [{"code": "...", "message": "..."}]}}
|
|
220
232
|
|
|
221
233
|
失败
|
|
222
234
|
单张图片下载失败 → 跳过并计入 images.failed + warnings,退出码 0。
|
|
223
|
-
命中 > 0 但全部失败 →
|
|
224
|
-
stack 不支持 →
|
|
225
|
-
构建失败 →
|
|
235
|
+
命中 > 0 但全部失败 → PACK_IMAGE_ALL_FAILED,退出码 1(系统性问题)。
|
|
236
|
+
stack 不支持 → PACK_STACK_UNSUPPORTED。
|
|
237
|
+
构建失败 → PACK_BUILD_FAILED。
|
|
238
|
+
上传失败(非 2xx / 网络异常)→ PACK_UPLOAD_FAILED,退出码 1。
|
|
239
|
+
本地产物保留,可修正 URL 后重跑或手动上传。
|
|
240
|
+
--upload-url 非 http/https → ARGS_INVALID,退出码 2。
|
|
226
241
|
|
|
227
242
|
示例
|
|
228
|
-
$ miaoda app
|
|
229
|
-
$ miaoda app
|
|
230
|
-
$ miaoda app
|
|
243
|
+
$ miaoda app pack
|
|
244
|
+
$ miaoda app pack --out dist/share
|
|
245
|
+
$ miaoda app pack --json
|
|
246
|
+
$ miaoda app pack --upload-url 'https://tos.example.com/x.zip?sign=...'
|
|
231
247
|
`);
|
|
232
248
|
cmd.action((0, shared_1.withHelp)(cmd, async (rawOpts) => {
|
|
233
|
-
await (0, index_1.
|
|
249
|
+
await (0, index_1.handleAppPack)({
|
|
234
250
|
dir: rawOpts.dir,
|
|
235
251
|
out: rawOpts.out,
|
|
236
|
-
|
|
252
|
+
uploadUrl: rawOpts.uploadUrl,
|
|
253
|
+
mode: rawOpts.mode,
|
|
237
254
|
});
|
|
238
255
|
}));
|
|
239
256
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PACK_MODES = exports.DEFAULT_PACK_MODE = exports.DEFAULT_PACK_OUT = exports.handleAppPack = exports.SUPPORTED_STACKS = exports.handleAppMigrate = exports.UPGRADE_MANIFEST_FILENAME = exports.DEFAULT_EXPORT_OUT = exports.handleAppExport = exports.handleAppUpgrade = exports.handleAppSync = exports.handleAppInit = exports.handleAppUpdate = exports.handleAppGet = void 0;
|
|
4
4
|
var get_1 = require("./get");
|
|
5
5
|
Object.defineProperty(exports, "handleAppGet", { enumerable: true, get: function () { return get_1.handleAppGet; } });
|
|
6
6
|
var update_1 = require("./update");
|
|
@@ -19,6 +19,8 @@ Object.defineProperty(exports, "handleAppMigrate", { enumerable: true, get: func
|
|
|
19
19
|
// commands 层渲染 help 时需要这份枚举;从 handler barrel 转发,避免 commands → services 越界
|
|
20
20
|
var index_1 = require("../../../services/app/init/index");
|
|
21
21
|
Object.defineProperty(exports, "SUPPORTED_STACKS", { enumerable: true, get: function () { return index_1.SUPPORTED_STACKS; } });
|
|
22
|
-
var
|
|
23
|
-
Object.defineProperty(exports, "
|
|
24
|
-
Object.defineProperty(exports, "
|
|
22
|
+
var pack_1 = require("./pack");
|
|
23
|
+
Object.defineProperty(exports, "handleAppPack", { enumerable: true, get: function () { return pack_1.handleAppPack; } });
|
|
24
|
+
Object.defineProperty(exports, "DEFAULT_PACK_OUT", { enumerable: true, get: function () { return pack_1.DEFAULT_PACK_OUT; } });
|
|
25
|
+
Object.defineProperty(exports, "DEFAULT_PACK_MODE", { enumerable: true, get: function () { return pack_1.DEFAULT_PACK_MODE; } });
|
|
26
|
+
Object.defineProperty(exports, "PACK_MODES", { enumerable: true, get: function () { return pack_1.PACK_MODES; } });
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.DEFAULT_PACK_OUT = exports.DEFAULT_PACK_MODE = exports.PACK_MODES = void 0;
|
|
7
|
+
exports.handleAppPack = handleAppPack;
|
|
8
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const exclude_patterns_1 = require("../../../utils/exclude-patterns");
|
|
11
|
+
const spark_meta_1 = require("../../../utils/spark-meta");
|
|
12
|
+
const error_1 = require("../../../utils/error");
|
|
13
|
+
const output_1 = require("../../../utils/output");
|
|
14
|
+
const logger_1 = require("../../../utils/logger");
|
|
15
|
+
const index_1 = require("../../../services/app/pack/index");
|
|
16
|
+
/**
|
|
17
|
+
* 支持的打包形态。当前只有 `standalone`;留成枚举是为了让扩展点显式可见 ——
|
|
18
|
+
* 新增形态时只需往这里加,命令 help 会自动列出。
|
|
19
|
+
*
|
|
20
|
+
* ⚠️ 不传 `--mode` 的行为永远等同 `standalone`,这是对调用方的稳定契约:
|
|
21
|
+
* 将来加了新形态也不能改默认值,否则存量调用会静默换语义。
|
|
22
|
+
*/
|
|
23
|
+
exports.PACK_MODES = ['standalone'];
|
|
24
|
+
exports.DEFAULT_PACK_MODE = 'standalone';
|
|
25
|
+
/** standalone 形态的默认输出目录。新增形态时各自用自己的目录名。 */
|
|
26
|
+
exports.DEFAULT_PACK_OUT = 'dist/standalone';
|
|
27
|
+
/** 校验相对输出路径:禁绝对路径、禁含 ".." 段(与 app export 的 normalizeOutDir 同口径)。 */
|
|
28
|
+
function normalizeOutDir(rel) {
|
|
29
|
+
if (node_path_1.default.isAbsolute(rel) || rel.startsWith('/')) {
|
|
30
|
+
throw new error_1.AppError('ARGS_INVALID', `输出目录不能是绝对路径:${rel}`);
|
|
31
|
+
}
|
|
32
|
+
const posix = rel.split(node_path_1.default.sep).join('/').replace(/^\.\//, '');
|
|
33
|
+
if (posix.split('/').includes('..')) {
|
|
34
|
+
throw new error_1.AppError('ARGS_INVALID', `输出目录不能包含 "..":${rel}`);
|
|
35
|
+
}
|
|
36
|
+
return posix;
|
|
37
|
+
}
|
|
38
|
+
/** 沙箱网关地址:它才是代理 `/spark/app` 的那个服务(响应头 `Server: openresty`)。 */
|
|
39
|
+
const SANDBOX_GATEWAY_BASE = 'http://localhost:8080';
|
|
40
|
+
/** 遍历产物树里的文本文件,返回 posix 相对路径。 */
|
|
41
|
+
function* walkText(dir, rel = '') {
|
|
42
|
+
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
43
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
44
|
+
const abs = node_path_1.default.join(dir, entry.name);
|
|
45
|
+
if (entry.isDirectory())
|
|
46
|
+
yield* walkText(abs, childRel);
|
|
47
|
+
else if (index_1.TEXT_EXTENSIONS.has(node_path_1.default.extname(entry.name).toLowerCase()))
|
|
48
|
+
yield childRel;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** 统计产物树的文件数与总字节数。 */
|
|
52
|
+
function measure(dir) {
|
|
53
|
+
let fileCount = 0;
|
|
54
|
+
let bytes = 0;
|
|
55
|
+
const walk = (cur) => {
|
|
56
|
+
for (const entry of node_fs_1.default.readdirSync(cur, { withFileTypes: true })) {
|
|
57
|
+
const abs = node_path_1.default.join(cur, entry.name);
|
|
58
|
+
if (entry.isDirectory())
|
|
59
|
+
walk(abs);
|
|
60
|
+
else {
|
|
61
|
+
fileCount += 1;
|
|
62
|
+
bytes += node_fs_1.default.statSync(abs).size;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
walk(dir);
|
|
67
|
+
return { fileCount, bytes };
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* 扫**项目源码**里是否用了 BrowserRouter。
|
|
71
|
+
*
|
|
72
|
+
* preset 在 standalone 下会把它编译期换成 HashRouter(`standaloneRouterPlugin`),
|
|
73
|
+
* 换完 file:// 下路由完全可用。这里保留检测是为了**覆盖不到时给提示**:插件只替换
|
|
74
|
+
* 用户源码里字面的 `BrowserRouter` / `createBrowserRouter`,碰不到的情况有——
|
|
75
|
+
* 用户二次封装(`export const Router = BrowserRouter`)、非 react-router 的路由库、
|
|
76
|
+
* 代码里硬编码 `window.location.href = '/about'`。
|
|
77
|
+
*
|
|
78
|
+
* 命中时只是提示"若路由异常请用本地 server",不代表一定坏。
|
|
79
|
+
*
|
|
80
|
+
* ⚠️ **必须扫源码,不能扫产物**:编译型 stack 的产物经过 minify,`BrowserRouter`
|
|
81
|
+
* 这个标识符会被改名,扫产物永远命中不了 —— 而那恰恰是最需要这条警告的场景。
|
|
82
|
+
* 真机踩过:vite-react 应用源码里明明有 BrowserRouter、产物首屏渲染成 404,
|
|
83
|
+
* 但因为扫的是产物,warnings 是空的。
|
|
84
|
+
*
|
|
85
|
+
* 扫描面套 EXCLUDES(跳过 node_modules / dist 等),四个 stack 统一按项目根扫。
|
|
86
|
+
*/
|
|
87
|
+
function detectBrowserRouter(projectDir) {
|
|
88
|
+
const walk = (dir) => {
|
|
89
|
+
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
90
|
+
if (exclude_patterns_1.EXCLUDES.has(entry.name))
|
|
91
|
+
continue;
|
|
92
|
+
const abs = node_path_1.default.join(dir, entry.name);
|
|
93
|
+
let isDir;
|
|
94
|
+
try {
|
|
95
|
+
isDir = node_fs_1.default.statSync(abs).isDirectory();
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (isDir) {
|
|
101
|
+
if (walk(abs))
|
|
102
|
+
return true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (!index_1.TEXT_EXTENSIONS.has(node_path_1.default.extname(entry.name).toLowerCase()))
|
|
106
|
+
continue;
|
|
107
|
+
if (/\b(BrowserRouter|createBrowserRouter)\b/.test(node_fs_1.default.readFileSync(abs, 'utf-8'))) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
};
|
|
113
|
+
return walk(projectDir);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* miaoda app pack [--mode standalone] [--out <dir>]
|
|
117
|
+
*
|
|
118
|
+
* 把当前应用构建成自包含静态产物:按 stack 走各自的 strategy 产出可静态服务的目录树,
|
|
119
|
+
* 再扫出 `/spark/app` 图片经 dev server 代理下载到 `images/` 并改写成相对路径,最后打 zip。
|
|
120
|
+
*
|
|
121
|
+
* 设计文档:docs/superpowers/specs/2026-08-19-app-pack-design.md
|
|
122
|
+
*/
|
|
123
|
+
async function handleAppPack(opts) {
|
|
124
|
+
const mode = opts.mode ?? exports.DEFAULT_PACK_MODE;
|
|
125
|
+
if (!exports.PACK_MODES.includes(mode)) {
|
|
126
|
+
throw new error_1.AppError('ARGS_INVALID', `不支持的打包形态:${mode}`, {
|
|
127
|
+
next_actions: [`可用形态:${exports.PACK_MODES.join(', ')}`],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const projectDir = node_path_1.default.resolve(opts.dir ?? process.cwd());
|
|
131
|
+
const meta = (0, spark_meta_1.readSparkMeta)(projectDir);
|
|
132
|
+
if (!meta.stack) {
|
|
133
|
+
throw new error_1.AppError('ARGS_INVALID', '.spark/meta.json 缺少 stack —— 请先跑 miaoda app init', {
|
|
134
|
+
next_actions: ['cd 到应用根目录后重试,或先执行 miaoda app init'],
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
const strategy = (0, index_1.resolveStrategy)(meta.stack);
|
|
138
|
+
// 参数校验一律前置:URL 写错不该等到构建 + 下载 + 打包跑完才报(那是几十秒)
|
|
139
|
+
if (opts.uploadUrl)
|
|
140
|
+
(0, index_1.assertValidUploadUrl)(opts.uploadUrl);
|
|
141
|
+
const outRel = normalizeOutDir(opts.out ?? exports.DEFAULT_PACK_OUT);
|
|
142
|
+
const outDir = node_path_1.default.join(projectDir, outRel);
|
|
143
|
+
const zipRel = `${outRel}.zip`;
|
|
144
|
+
const zipPath = node_path_1.default.join(projectDir, zipRel);
|
|
145
|
+
// 1. stack-specific:产出可静态服务的目录树
|
|
146
|
+
strategy({ projectDir, outDir });
|
|
147
|
+
if (!node_fs_1.default.existsSync(outDir) || node_fs_1.default.readdirSync(outDir).length === 0) {
|
|
148
|
+
throw new error_1.AppError('PACK_EMPTY_OUTPUT', '构建产物为空', {
|
|
149
|
+
next_actions: ['先手动跑一次该 stack 的构建命令确认工程本身可构建'],
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
// 2. 扫描
|
|
153
|
+
const urls = [];
|
|
154
|
+
const seen = new Set();
|
|
155
|
+
for (const rel of walkText(outDir)) {
|
|
156
|
+
for (const url of (0, index_1.scanSparkAppUrls)(node_fs_1.default.readFileSync(node_path_1.default.join(outDir, rel), 'utf-8'))) {
|
|
157
|
+
if (seen.has(url))
|
|
158
|
+
continue;
|
|
159
|
+
seen.add(url);
|
|
160
|
+
urls.push(url);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// 3. 下载
|
|
164
|
+
const devServerBase = SANDBOX_GATEWAY_BASE;
|
|
165
|
+
(0, logger_1.log)('pack', `Matched ${String(urls.length)} image url(s), fetching via ${devServerBase}`);
|
|
166
|
+
const { mapping, failed } = await (0, index_1.downloadImages)(urls, {
|
|
167
|
+
imagesDir: node_path_1.default.join(outDir, 'images'),
|
|
168
|
+
devServerBase,
|
|
169
|
+
});
|
|
170
|
+
// 命中 > 0 且全部失败 = 系统性问题(代理挂了 / 路径口径错),静默成功会误导调用方
|
|
171
|
+
if (urls.length > 0 && mapping.size === 0) {
|
|
172
|
+
throw new error_1.AppError('PACK_IMAGE_ALL_FAILED', `命中 ${String(urls.length)} 张图片但全部下载失败`, {
|
|
173
|
+
next_actions: [
|
|
174
|
+
'确认 dev server 正在运行且能代理 /spark/app 路径',
|
|
175
|
+
`首个失败:${failed[0]?.url ?? '?'} → ${failed[0]?.reason ?? 'unknown'}`,
|
|
176
|
+
...(failed.length > 1
|
|
177
|
+
? [`其余 ${String(failed.length - 1)} 条见 --verbose(每条请求地址都会打到 stderr)`]
|
|
178
|
+
: []),
|
|
179
|
+
`实际请求前缀:${devServerBase}(沙箱网关)`,
|
|
180
|
+
],
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
// 4. 改写
|
|
184
|
+
const rewrittenFiles = (0, index_1.rewriteTree)(outDir, mapping);
|
|
185
|
+
// 5. 统计。**必须在打包前做**——目录随后就被删掉,那时量不到了。
|
|
186
|
+
const { fileCount, bytes } = measure(outDir);
|
|
187
|
+
// 6. 打包,随后删掉目录。目录是**中间态**,zip 才是交付物;两者内容重复,
|
|
188
|
+
// 留着等于占双份磁盘。预览需先解压。
|
|
189
|
+
(0, index_1.zipDir)(outDir, zipPath);
|
|
190
|
+
node_fs_1.default.rmSync(outDir, { recursive: true, force: true });
|
|
191
|
+
(0, logger_1.log)('pack', `Packed ${zipRel}, staging dir removed`);
|
|
192
|
+
// 7. 上传(可选)。放在 emit 之前——传了 --upload-url 就说明上传是目的,
|
|
193
|
+
// 失败要让整个命令失败(退出码 1),不能先 emit 成功再抛。
|
|
194
|
+
// JSON 输出里**不回显 URL**(凭证类信息),只给状态码与服务端 log_id。
|
|
195
|
+
const upload = opts.uploadUrl ? await (0, index_1.uploadZip)(zipPath, opts.uploadUrl) : undefined;
|
|
196
|
+
const warnings = [];
|
|
197
|
+
// 8. 上传成功后清掉本地产物:交付物已在远端,沙箱磁盘不必留一份目录 + 一份 zip。
|
|
198
|
+
//
|
|
199
|
+
// **只在上传成功后清**。上传失败时产物必须留着——错误提示里明确写了「本地产物已保留,
|
|
200
|
+
// 可修正 --upload-url 后重跑,或手动上传」,清掉会让那句话变成谎话,且用户得重跑
|
|
201
|
+
// 整轮构建。失败路径走 uploadZip 抛错,根本到不了这里。
|
|
202
|
+
//
|
|
203
|
+
// 清理失败不算命令失败:上传已经成功,目的达到了,删不掉只是留了点垃圾 → 降级为 warning。
|
|
204
|
+
let localRemoved = false;
|
|
205
|
+
if (upload) {
|
|
206
|
+
try {
|
|
207
|
+
// 目录在打包后已删,这里只需清 zip
|
|
208
|
+
node_fs_1.default.rmSync(zipPath, { force: true });
|
|
209
|
+
localRemoved = true;
|
|
210
|
+
(0, logger_1.log)('pack', `Uploaded, local zip removed (${zipRel})`);
|
|
211
|
+
}
|
|
212
|
+
catch (err) {
|
|
213
|
+
warnings.push({
|
|
214
|
+
code: 'LOCAL_CLEANUP_FAILED',
|
|
215
|
+
message: `上传成功但本地产物清理失败:${err instanceof Error ? err.message : String(err)}`,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (failed.length > 0) {
|
|
220
|
+
warnings.push({
|
|
221
|
+
code: 'IMAGE_DOWNLOAD_PARTIAL_FAILURE',
|
|
222
|
+
message: `${String(failed.length)} 张图片下载失败,产物中保留原始绝对路径(离线打不开)`,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (detectBrowserRouter(projectDir)) {
|
|
226
|
+
warnings.push({
|
|
227
|
+
code: 'SPA_ROUTER_FILE_PROTOCOL',
|
|
228
|
+
message: '检测到 BrowserRouter:standalone 构建已自动换成 HashRouter,路由形如 index.html#/about。' +
|
|
229
|
+
'若你对 router 做过二次封装或用了非 react-router 的方案,替换可能覆盖不到 —— ' +
|
|
230
|
+
'此时产物在 file:// 下路由会失效(首屏落 404 页),请改用本地 HTTP server 并从 / 访问。',
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
(0, output_1.emit)({
|
|
234
|
+
data: {
|
|
235
|
+
appId: meta.app_id ?? '',
|
|
236
|
+
stack: meta.stack,
|
|
237
|
+
mode,
|
|
238
|
+
out: outRel,
|
|
239
|
+
zip: zipRel,
|
|
240
|
+
// ⚠️ out 指向的**目录在打包后即被删除**(它只是中间态),永远不要去读它。
|
|
241
|
+
// zip 是本地交付物;localRemoved=true 时连 zip 也被清了(已上传到远端)。
|
|
242
|
+
localRemoved,
|
|
243
|
+
fileCount,
|
|
244
|
+
bytes,
|
|
245
|
+
images: {
|
|
246
|
+
matched: urls.length,
|
|
247
|
+
downloaded: mapping.size,
|
|
248
|
+
failed,
|
|
249
|
+
},
|
|
250
|
+
rewrittenFiles,
|
|
251
|
+
// 只在传了 --upload-url 时出现:{ status, logId? }。刻意不含 URL。
|
|
252
|
+
...(upload ? { upload } : {}),
|
|
253
|
+
warnings,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
}
|
|
@@ -19,7 +19,7 @@ const error_1 = require("../../../utils/error");
|
|
|
19
19
|
*/
|
|
20
20
|
function zipDir(dir, zipPath) {
|
|
21
21
|
if (!node_fs_1.default.existsSync(dir)) {
|
|
22
|
-
throw new error_1.AppError('
|
|
22
|
+
throw new error_1.AppError('PACK_ARCHIVE_FAILED', `待打包目录不存在:${dir}`);
|
|
23
23
|
}
|
|
24
24
|
node_fs_1.default.rmSync(zipPath, { force: true });
|
|
25
25
|
node_fs_1.default.mkdirSync(node_path_1.default.dirname(zipPath), { recursive: true });
|
|
@@ -28,6 +28,8 @@ function zipDir(dir, zipPath) {
|
|
|
28
28
|
(0, node_child_process_1.execFileSync)('zip', ['-qr', zipPath, '.'], { cwd: dir, stdio: 'pipe' });
|
|
29
29
|
}
|
|
30
30
|
catch (err) {
|
|
31
|
-
throw new error_1.AppError('
|
|
31
|
+
throw new error_1.AppError('PACK_ARCHIVE_FAILED', `zip failed: ${err.message}`, {
|
|
32
|
+
next_actions: ['确认运行环境有 zip 命令(沙箱镜像应自带)'],
|
|
33
|
+
});
|
|
32
34
|
}
|
|
33
35
|
}
|
|
@@ -18,7 +18,7 @@ const error_1 = require("../../../utils/error");
|
|
|
18
18
|
*/
|
|
19
19
|
function copyTree(root, out, excludes) {
|
|
20
20
|
if (!node_fs_1.default.existsSync(root)) {
|
|
21
|
-
throw new error_1.AppError('
|
|
21
|
+
throw new error_1.AppError('PACK_EMPTY_OUTPUT', `源目录不存在:${root}`, {
|
|
22
22
|
next_actions: ['确认已在应用根目录下执行,且项目结构完整'],
|
|
23
23
|
});
|
|
24
24
|
}
|
|
@@ -26,19 +26,23 @@ async function downloadImages(urls, opts) {
|
|
|
26
26
|
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
27
27
|
for (const url of urls) {
|
|
28
28
|
const target = `${opts.devServerBase}${url}`;
|
|
29
|
+
(0, logger_1.debug)(`pack: GET ${target}`);
|
|
29
30
|
try {
|
|
30
31
|
// 刻意串行(见函数注释):图片量级通常个位数到几十,并发收益有限;
|
|
31
32
|
// 串行能让每张图的失败归因清晰,且不对沙箱 dev server 造成并发压力。
|
|
32
33
|
const resp = await fetch(target, { signal: AbortSignal.timeout(timeoutMs) });
|
|
33
34
|
if (!resp.ok) {
|
|
34
|
-
|
|
35
|
+
// resp.url 是**跟随重定向后**的最终地址。沙箱代理对存储图返回 302 指向签名 CDN,
|
|
36
|
+
// 失败发生在哪一跳,只看状态码分不出来,必须把最终地址带出来。
|
|
37
|
+
const finalUrl = resp.url && resp.url !== target ? ` (final: ${resp.url})` : '';
|
|
38
|
+
failed.push({ url, reason: `HTTP ${String(resp.status)}${finalUrl}` });
|
|
35
39
|
continue;
|
|
36
40
|
}
|
|
37
41
|
const buf = Buffer.from(await resp.arrayBuffer());
|
|
38
42
|
const name = (0, naming_1.localImageName)(url, resp.headers.get('content-type') ?? undefined);
|
|
39
43
|
node_fs_1.default.writeFileSync(node_path_1.default.join(opts.imagesDir, name), buf);
|
|
40
44
|
mapping.set(url, name);
|
|
41
|
-
(0, logger_1.debug)(`
|
|
45
|
+
(0, logger_1.debug)(`pack: downloaded ${url} -> images/${name} (${String(buf.length)}B)`);
|
|
42
46
|
}
|
|
43
47
|
catch (err) {
|
|
44
48
|
failed.push({ url, reason: err instanceof Error ? err.message : String(err) });
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.PACK_SUPPORTED_STACKS = exports.resolveStrategy = exports.buildStandaloneEnv = exports.uploadZip = exports.redactUrl = exports.assertValidUploadUrl = exports.scanSparkAppUrls = exports.TEXT_EXTENSIONS = exports.rewriteTree = exports.localImageName = exports.extFromContentType = exports.inlineBabelScripts = exports.downloadImages = exports.copyTree = exports.zipDir = void 0;
|
|
4
4
|
var archive_1 = require("./archive");
|
|
5
5
|
Object.defineProperty(exports, "zipDir", { enumerable: true, get: function () { return archive_1.zipDir; } });
|
|
6
6
|
var copy_tree_1 = require("./copy-tree");
|
|
7
7
|
Object.defineProperty(exports, "copyTree", { enumerable: true, get: function () { return copy_tree_1.copyTree; } });
|
|
8
8
|
var download_1 = require("./download");
|
|
9
9
|
Object.defineProperty(exports, "downloadImages", { enumerable: true, get: function () { return download_1.downloadImages; } });
|
|
10
|
+
var inline_babel_1 = require("./inline-babel");
|
|
11
|
+
Object.defineProperty(exports, "inlineBabelScripts", { enumerable: true, get: function () { return inline_babel_1.inlineBabelScripts; } });
|
|
10
12
|
var naming_1 = require("./naming");
|
|
11
13
|
Object.defineProperty(exports, "extFromContentType", { enumerable: true, get: function () { return naming_1.extFromContentType; } });
|
|
12
14
|
Object.defineProperty(exports, "localImageName", { enumerable: true, get: function () { return naming_1.localImageName; } });
|
|
@@ -15,7 +17,11 @@ Object.defineProperty(exports, "rewriteTree", { enumerable: true, get: function
|
|
|
15
17
|
Object.defineProperty(exports, "TEXT_EXTENSIONS", { enumerable: true, get: function () { return rewrite_1.TEXT_EXTENSIONS; } });
|
|
16
18
|
var scan_1 = require("./scan");
|
|
17
19
|
Object.defineProperty(exports, "scanSparkAppUrls", { enumerable: true, get: function () { return scan_1.scanSparkAppUrls; } });
|
|
20
|
+
var upload_1 = require("./upload");
|
|
21
|
+
Object.defineProperty(exports, "assertValidUploadUrl", { enumerable: true, get: function () { return upload_1.assertValidUploadUrl; } });
|
|
22
|
+
Object.defineProperty(exports, "redactUrl", { enumerable: true, get: function () { return upload_1.redactUrl; } });
|
|
23
|
+
Object.defineProperty(exports, "uploadZip", { enumerable: true, get: function () { return upload_1.uploadZip; } });
|
|
18
24
|
var strategies_1 = require("./strategies");
|
|
19
25
|
Object.defineProperty(exports, "buildStandaloneEnv", { enumerable: true, get: function () { return strategies_1.buildStandaloneEnv; } });
|
|
20
26
|
Object.defineProperty(exports, "resolveStrategy", { enumerable: true, get: function () { return strategies_1.resolveStrategy; } });
|
|
21
|
-
Object.defineProperty(exports, "
|
|
27
|
+
Object.defineProperty(exports, "PACK_SUPPORTED_STACKS", { enumerable: true, get: function () { return strategies_1.PACK_SUPPORTED_STACKS; } });
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.inlineBabelScripts = inlineBabelScripts;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const logger_1 = require("../../../utils/logger");
|
|
10
|
+
/**
|
|
11
|
+
* 匹配**完整的 `<script>` 元素**(含闭合标签),而不是只匹配开标签。
|
|
12
|
+
*
|
|
13
|
+
* 属性区用 `(?:"[^"]*"|'[^']*'|[^>])*` 而非 `[^>]*` —— 后者会被属性值里的 `>` 提前截断。
|
|
14
|
+
* 元素体用非贪婪 `[\s\S]*?` 配到最近的 `</script>`:带 `src` 的标签体按规范会被忽略,
|
|
15
|
+
* 整体替换掉最直白,不必去拼接原闭合标签。
|
|
16
|
+
*/
|
|
17
|
+
const SCRIPT_EL_RE = /<script((?:"[^"]*"|'[^']*'|[^>])*)>([\s\S]*?)<\/script\s*>/gi;
|
|
18
|
+
/** Babel Standalone 认这两种 type。 */
|
|
19
|
+
const BABEL_TYPE_RE = /\btype\s*=\s*["']text\/(?:babel|jsx)["']/i;
|
|
20
|
+
/** 取 src 属性值(带引号形式;Vite/手写产物里都是带引号的)。 */
|
|
21
|
+
const SRC_ATTR_RE = /\bsrc\s*=\s*["']([^"']+)["']/i;
|
|
22
|
+
/** 远端 / 内联数据源:不是本地文件,不内联。 */
|
|
23
|
+
const REMOTE_SRC_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
24
|
+
/** 需要处理的 HTML 扩展名。 */
|
|
25
|
+
const HTML_EXT = new Set(['.html', '.htm']);
|
|
26
|
+
function* walkHtml(dir, rel = '') {
|
|
27
|
+
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
28
|
+
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
29
|
+
const abs = node_path_1.default.join(dir, entry.name);
|
|
30
|
+
if (entry.isDirectory())
|
|
31
|
+
yield* walkHtml(abs, childRel);
|
|
32
|
+
else if (HTML_EXT.has(node_path_1.default.extname(entry.name).toLowerCase()))
|
|
33
|
+
yield childRel;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 把 `<script type="text/babel" src="x.jsx"></script>` 的外链内容内联进 HTML。
|
|
38
|
+
*
|
|
39
|
+
* **为什么必须做**:Babel Standalone 是用 **XHR** 去取 `src` 指向的文件的。`file://` 下
|
|
40
|
+
* 文档 origin 是 `null`,浏览器只允许 XHR 访问 chrome/data/http/https 等协议,file 被禁:
|
|
41
|
+
*
|
|
42
|
+
* ```
|
|
43
|
+
* Access to XMLHttpRequest at 'file:///.../app.jsx' from origin 'null' has been
|
|
44
|
+
* blocked by CORS policy: Cross origin requests are only supported for protocol
|
|
45
|
+
* schemes: chrome, ..., data, http, https, ...
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* 结果是 JSX 一个都没执行、页面白屏(`Uncaught ReferenceError: App is not defined`)。
|
|
49
|
+
* 内联之后 Babel 直接读标签内容,不发请求,`file://` 下正常渲染(已实测)。
|
|
50
|
+
*
|
|
51
|
+
* 只对 buildless 的 `design-html` / `html` 生效 —— vite 系产物已经打包过,不存在这种标签。
|
|
52
|
+
*
|
|
53
|
+
* 保留原 `.jsx` 文件不删:起 HTTP server 时两条路都能走,删了反而少一条。
|
|
54
|
+
*/
|
|
55
|
+
function inlineBabelScripts(root) {
|
|
56
|
+
const changed = [];
|
|
57
|
+
const rootResolved = node_path_1.default.resolve(root);
|
|
58
|
+
for (const rel of walkHtml(root)) {
|
|
59
|
+
const htmlPath = node_path_1.default.join(root, rel);
|
|
60
|
+
const htmlDir = node_path_1.default.dirname(htmlPath);
|
|
61
|
+
const original = node_fs_1.default.readFileSync(htmlPath, 'utf-8');
|
|
62
|
+
const next = original.replace(SCRIPT_EL_RE, (el, attrs) => {
|
|
63
|
+
if (!BABEL_TYPE_RE.test(attrs))
|
|
64
|
+
return el;
|
|
65
|
+
const m = SRC_ATTR_RE.exec(attrs);
|
|
66
|
+
if (!m)
|
|
67
|
+
return el; // 已是内联块
|
|
68
|
+
const src = m[1];
|
|
69
|
+
if (REMOTE_SRC_RE.test(src))
|
|
70
|
+
return el; // http(s) / 协议相对 / data:
|
|
71
|
+
const target = node_path_1.default.resolve(htmlDir, src);
|
|
72
|
+
// 防路径穿越:只内联产物树内的文件,否则可能把树外的内容打进产物
|
|
73
|
+
if (target !== rootResolved && !target.startsWith(rootResolved + node_path_1.default.sep))
|
|
74
|
+
return el;
|
|
75
|
+
if (!node_fs_1.default.existsSync(target))
|
|
76
|
+
return el;
|
|
77
|
+
// `</script` 出现在 JS 字符串里会提前闭合标签,必须转义
|
|
78
|
+
const code = node_fs_1.default.readFileSync(target, 'utf-8').replace(/<\/script/gi, '<\\/script');
|
|
79
|
+
const keptAttrs = attrs.replace(SRC_ATTR_RE, '').replace(/\s+/g, ' ').trimEnd();
|
|
80
|
+
(0, logger_1.debug)(`pack: inlined ${src} into ${rel}`);
|
|
81
|
+
return `<script${keptAttrs} data-inlined-from="${src}">\n${code}\n</script>`;
|
|
82
|
+
});
|
|
83
|
+
// 用「文本是否变化」判定,不用外层 flag —— 回调里的赋值 TS 控制流分析看不到,
|
|
84
|
+
// 会把 `if (hit)` 判成恒假(@typescript-eslint/no-unnecessary-condition)。
|
|
85
|
+
if (next !== original) {
|
|
86
|
+
node_fs_1.default.writeFileSync(htmlPath, next, 'utf-8');
|
|
87
|
+
changed.push(rel);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return changed.sort();
|
|
91
|
+
}
|
|
@@ -19,6 +19,14 @@ exports.TEXT_EXTENSIONS = new Set([
|
|
|
19
19
|
'.js',
|
|
20
20
|
'.mjs',
|
|
21
21
|
'.cjs',
|
|
22
|
+
// buildless 栈(design-html / html)的源文件**就是产物**,JSX/TS 里的图片
|
|
23
|
+
// URL 同样要扫要改。漏了这几个扩展名会导致图片下载成功、但 .jsx 里的引用
|
|
24
|
+
// 仍指向线上绝对路径 —— 真机踩过(app.jsx 里 5 处未改写)。
|
|
25
|
+
'.jsx',
|
|
26
|
+
'.ts',
|
|
27
|
+
'.tsx',
|
|
28
|
+
'.mts',
|
|
29
|
+
'.cts',
|
|
22
30
|
'.json',
|
|
23
31
|
'.svg',
|
|
24
32
|
'.webmanifest',
|