@lark-apaas/miaoda-cli 0.1.33-alpha.0e32e59 → 0.1.33-alpha.155f17e
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.
|
@@ -7,7 +7,6 @@ exports.DEFAULT_STANDALONE_OUT = void 0;
|
|
|
7
7
|
exports.handleAppExportStandalone = handleAppExportStandalone;
|
|
8
8
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
9
9
|
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
-
const exclude_patterns_1 = require("../../../utils/exclude-patterns");
|
|
11
10
|
const spark_meta_1 = require("../../../utils/spark-meta");
|
|
12
11
|
const error_1 = require("../../../utils/error");
|
|
13
12
|
const output_1 = require("../../../utils/output");
|
|
@@ -70,50 +69,17 @@ function measure(dir) {
|
|
|
70
69
|
return { fileCount, bytes };
|
|
71
70
|
}
|
|
72
71
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
* 换完 file:// 下路由完全可用。这里保留检测是为了**覆盖不到时给提示**:插件只替换
|
|
77
|
-
* 用户源码里字面的 `BrowserRouter` / `createBrowserRouter`,碰不到的情况有——
|
|
78
|
-
* 用户二次封装(`export const Router = BrowserRouter`)、非 react-router 的路由库、
|
|
79
|
-
* 代码里硬编码 `window.location.href = '/about'`。
|
|
80
|
-
*
|
|
81
|
-
* 命中时只是提示"若路由异常请用本地 server",不代表一定坏。
|
|
82
|
-
*
|
|
83
|
-
* ⚠️ **必须扫源码,不能扫产物**:编译型 stack 的产物经过 minify,`BrowserRouter`
|
|
84
|
-
* 这个标识符会被改名,扫产物永远命中不了 —— 而那恰恰是最需要这条警告的场景。
|
|
85
|
-
* 真机踩过:vite-react 应用源码里明明有 BrowserRouter、产物首屏渲染成 404,
|
|
86
|
-
* 但因为扫的是产物,warnings 是空的。
|
|
87
|
-
*
|
|
88
|
-
* 扫描面套 EXCLUDES(跳过 node_modules / dist 等),四个 stack 统一按项目根扫。
|
|
72
|
+
* 扫产物里是否残留 BrowserRouter —— standalone 不做 HashRouter 改造(明确决策),
|
|
73
|
+
* 多路由 SPA 在 file:// 下 `location.pathname` 是完整文件系统路径,匹配不上任何 Route,
|
|
74
|
+
* 且 pushState 在 opaque origin 下抛 SecurityError。检测到就告知用户改用本地 server。
|
|
89
75
|
*/
|
|
90
|
-
function detectBrowserRouter(
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
try {
|
|
98
|
-
isDir = node_fs_1.default.statSync(abs).isDirectory();
|
|
99
|
-
}
|
|
100
|
-
catch {
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
if (isDir) {
|
|
104
|
-
if (walk(abs))
|
|
105
|
-
return true;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
if (!index_1.TEXT_EXTENSIONS.has(node_path_1.default.extname(entry.name).toLowerCase()))
|
|
109
|
-
continue;
|
|
110
|
-
if (/\b(BrowserRouter|createBrowserRouter)\b/.test(node_fs_1.default.readFileSync(abs, 'utf-8'))) {
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
return false;
|
|
115
|
-
};
|
|
116
|
-
return walk(projectDir);
|
|
76
|
+
function detectBrowserRouter(outDir) {
|
|
77
|
+
for (const rel of walkText(outDir)) {
|
|
78
|
+
const text = node_fs_1.default.readFileSync(node_path_1.default.join(outDir, rel), 'utf-8');
|
|
79
|
+
if (/\b(BrowserRouter|createBrowserRouter)\b/.test(text))
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
117
83
|
}
|
|
118
84
|
/**
|
|
119
85
|
* miaoda app export-standalone [--out <dir>]
|
|
@@ -187,12 +153,10 @@ async function handleAppExportStandalone(opts) {
|
|
|
187
153
|
message: `${String(failed.length)} 张图片下载失败,产物中保留原始绝对路径(离线打不开)`,
|
|
188
154
|
});
|
|
189
155
|
}
|
|
190
|
-
if (detectBrowserRouter(
|
|
156
|
+
if (detectBrowserRouter(outDir)) {
|
|
191
157
|
warnings.push({
|
|
192
158
|
code: 'SPA_ROUTER_FILE_PROTOCOL',
|
|
193
|
-
message: '检测到 BrowserRouter
|
|
194
|
-
'若你对 router 做过二次封装或用了非 react-router 的方案,替换可能覆盖不到 —— ' +
|
|
195
|
-
'此时产物在 file:// 下路由会失效(首屏落 404 页),请改用本地 HTTP server 并从 / 访问。',
|
|
159
|
+
message: '检测到 BrowserRouter:产物在 file:// 下不可用(路由匹配不上、pushState 抛 SecurityError),请用本地 HTTP server 打开',
|
|
196
160
|
});
|
|
197
161
|
}
|
|
198
162
|
const { fileCount, bytes } = measure(outDir);
|
|
@@ -36,15 +36,9 @@ function buildStandaloneEnv(base) {
|
|
|
36
36
|
delete env.MIAODA_APP_ID;
|
|
37
37
|
delete env.CLIENT_BASE_PATH;
|
|
38
38
|
env.MIAODA_BUILD_TARGET = 'standalone';
|
|
39
|
-
env.NODE_ENV = 'production';
|
|
40
|
-
// vite-react 走 build.sh,由它把 MIAODA_RESOURCE_CDN_PREFIX 映射成 ASSETS_CDN_PATH
|
|
41
39
|
env.MIAODA_RESOURCE_CDN_PREFIX = './';
|
|
42
|
-
// nestjs 走 `npm run build:client` 直接调 vite,**没有 build.sh 那层映射** ——
|
|
43
|
-
// 必须直接给 preset 认的名字,否则 publicPath 为空 → base '/' → 产物里是
|
|
44
|
-
// `/assets/x.js` 绝对路径,file:// 下指向文件系统根,页面白屏(真机踩过)。
|
|
45
|
-
// vite-react 那边 build.sh 会用同样的值覆盖它,两条路结果一致。
|
|
46
|
-
env.ASSETS_CDN_PATH = './';
|
|
47
40
|
env.STATIC_ASSETS_BASE_URL = './static';
|
|
41
|
+
env.NODE_ENV = 'production';
|
|
48
42
|
return env;
|
|
49
43
|
}
|
|
50
44
|
/** 把 `from` 目录内容并入 `into`(into 已存在,不清空)。from 不存在则静默跳过。 */
|
|
@@ -83,31 +77,6 @@ function liftNestedEntry(outDir) {
|
|
|
83
77
|
if (node_fs_1.default.readdirSync(nestedDir).length === 0)
|
|
84
78
|
node_fs_1.default.rmdirSync(nestedDir);
|
|
85
79
|
}
|
|
86
|
-
/**
|
|
87
|
-
* 从产物树里删掉 sourcemap(`*.map`)。
|
|
88
|
-
*
|
|
89
|
-
* preset 的 prod 配置是 `sourcemap: 'hidden'` —— map 文件会产出,但产物里**没有**
|
|
90
|
-
* `sourceMappingURL` 注释,即没人引用它。留在 standalone 包里有两个实打实的问题:
|
|
91
|
-
*
|
|
92
|
-
* 1. **体积**:实测真实应用 4.7MB map / 5.9MB 总体积,占 80%
|
|
93
|
-
* 2. **泄源码**:map 的 `sourcesContent` 内嵌原始源码(实测含 12 个业务文件,
|
|
94
|
-
* 如 `src/lib/utils.ts`)。standalone 包是拿来分发给人的,等于附赠全部源码
|
|
95
|
-
*
|
|
96
|
-
* 只对有编译步骤的 stack 调用;buildless 栈(design-html / html)的产物就是源码,
|
|
97
|
-
* 那里的 `.map`(若有)是用户自己的文件,不该动。
|
|
98
|
-
*/
|
|
99
|
-
function dropSourceMaps(outDir) {
|
|
100
|
-
const walk = (dir) => {
|
|
101
|
-
for (const entry of node_fs_1.default.readdirSync(dir, { withFileTypes: true })) {
|
|
102
|
-
const abs = node_path_1.default.join(dir, entry.name);
|
|
103
|
-
if (entry.isDirectory())
|
|
104
|
-
walk(abs);
|
|
105
|
-
else if (entry.name.toLowerCase().endsWith('.map'))
|
|
106
|
-
node_fs_1.default.rmSync(abs, { force: true });
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
walk(outDir);
|
|
110
|
-
}
|
|
111
80
|
/** 从产物树里删掉 routes.json —— 平台/server 路由用,standalone 无人读取。 */
|
|
112
81
|
function dropRoutesJson(outDir) {
|
|
113
82
|
node_fs_1.default.rmSync(node_path_1.default.join(outDir, 'routes.json'), { force: true });
|
|
@@ -156,7 +125,6 @@ const viteReactStrategy = ({ projectDir, outDir }) => {
|
|
|
156
125
|
mergeInto(node_path_1.default.join(dist, 'output'), outDir);
|
|
157
126
|
mergeInto(node_path_1.default.join(dist, 'output_resource', 'assets'), node_path_1.default.join(outDir, 'assets'));
|
|
158
127
|
mergeInto(node_path_1.default.join(dist, 'output_static'), node_path_1.default.join(outDir, 'static'));
|
|
159
|
-
dropSourceMaps(outDir);
|
|
160
128
|
dropRoutesJson(outDir);
|
|
161
129
|
};
|
|
162
130
|
/**
|
|
@@ -170,33 +138,15 @@ const viteReactStrategy = ({ projectDir, outDir }) => {
|
|
|
170
138
|
*/
|
|
171
139
|
const nestjsStrategy = ({ projectDir, outDir }) => {
|
|
172
140
|
(0, logger_1.log)('export-standalone', 'Building client (nestjs-react-fullstack)...');
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
// 而我们绕过 build.sh 直接跑 `build:client`,没人清目录 —— 复用 `dist/client`
|
|
178
|
-
// 会把上次构建的 chunk 一起带进产物(真机见过新旧两个 index-<hash>.js 并存)。
|
|
179
|
-
// 2. `dist/client` 是平台链路的产物目录(dev 期 html-output 会往里写、NestJS 从
|
|
180
|
-
// 那儿取 HTML),导出这种旁路动作**不该清它**,否则破坏既有目录结构。
|
|
181
|
-
//
|
|
182
|
-
// vite CLI 的 `--outDir` 优先级高于 config,`--emptyOutDir` 覆盖 `emptyOutDir: false`。
|
|
183
|
-
// 放在 dist/ 下是为了留在 vite root 内(root 外 vite 会告警)。
|
|
184
|
-
const tmpDist = node_path_1.default.join(projectDir, 'dist', '.standalone-client');
|
|
185
|
-
try {
|
|
186
|
-
runProjectBuild(projectDir, `npm run build:client -- --outDir ${JSON.stringify(node_path_1.default.relative(projectDir, tmpDist))} --emptyOutDir`);
|
|
187
|
-
if (!node_fs_1.default.existsSync(tmpDist)) {
|
|
188
|
-
throw new error_1.AppError('EXPORT_STANDALONE_EMPTY_OUTPUT', `client 构建产物 ${node_path_1.default.relative(projectDir, tmpDist)} 不存在`);
|
|
189
|
-
}
|
|
190
|
-
node_fs_1.default.rmSync(outDir, { recursive: true, force: true });
|
|
191
|
-
node_fs_1.default.mkdirSync(outDir, { recursive: true });
|
|
192
|
-
mergeInto(tmpDist, outDir);
|
|
193
|
-
}
|
|
194
|
-
finally {
|
|
195
|
-
// 临时构建目录不留在用户项目里
|
|
196
|
-
node_fs_1.default.rmSync(tmpDist, { recursive: true, force: true });
|
|
141
|
+
runProjectBuild(projectDir, 'npm run build:client');
|
|
142
|
+
const clientDist = node_path_1.default.join(projectDir, 'dist', 'client');
|
|
143
|
+
if (!node_fs_1.default.existsSync(clientDist)) {
|
|
144
|
+
throw new error_1.AppError('EXPORT_STANDALONE_EMPTY_OUTPUT', 'client 构建产物 dist/client 不存在');
|
|
197
145
|
}
|
|
146
|
+
node_fs_1.default.rmSync(outDir, { recursive: true, force: true });
|
|
147
|
+
node_fs_1.default.mkdirSync(outDir, { recursive: true });
|
|
148
|
+
mergeInto(clientDist, outDir);
|
|
198
149
|
liftNestedEntry(outDir);
|
|
199
|
-
dropSourceMaps(outDir);
|
|
200
150
|
dropRoutesJson(outDir);
|
|
201
151
|
};
|
|
202
152
|
const STRATEGY_BY_STACK = new Map([
|