Files
xbcr/esbuild.config.mjs
T
zzy 8ece55983a feat(前端): TypeScript 重写版(esbuild IIFE 产物,暂不接管线上)
- 新增 src-ts/(22 个 .ts / 7792 行):按 core / map / card / order / nav / share /
  subscribe / ui / compat 分层,types 里有 BMapGL 与 API 响应声明
- compat/globals.ts 是唯一挂 window 的地方(292 个),覆盖模板 139 处内联事件
- esbuild 产出 static/dist/app.js(226.6 KiB,IIFE,es2019)+ README(构建/切换/回滚)
- 构建可复现:node esbuild.config.mjs、node --check、npx tsc --noEmit 均通过
- .gitignore 增加 node_modules/ 与 static/dist/*.map(sourcemap 可重生成,不入库)
- 暂不改 templates/map.html 与 MAP_ASSETS:等独立核对通过再切换,回滚=换回四个 script
2026-11-16 10:44:11 +08:00

66 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* esbuild 构建脚本:把 src-ts/ 打成**一个 IIFE**
* (templates/map.html 用 <script defer src>,不是 type="module",所以必须是 IIFE/UMD)。
*
* node esbuild.config.mjs
*
* 产物:
* static/dist/app.js 入口 bundle(含 sourceMappingURL 注释)
* static/dist/app.js.map sourcemap(外部,便于对着原 static/js/*.js 调试)
*/
import esbuild from "esbuild";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const OUT_FILE = path.join(HERE, "static", "dist", "app.js");
const result = await esbuild.build({
entryPoints: [path.join(HERE, "src-ts", "main.ts")],
outfile: OUT_FILE,
bundle: true,
format: "iife",
target: "es2019",
platform: "browser",
charset: "utf8",
sourcemap: "linked", // external map 文件 + 末尾 //# sourceMappingURL(external 模式不会写这行注释)
sourcesContent: true,
minify: false,
legalComments: "none",
logLevel: "warning",
banner: {
js: [
"/*! 小白超人地图前端 bundle —— 由 src-ts/ 经 esbuild 打包(IIFE)。",
" * 源码:src-ts/** (逐块搬自 static/js/{map,navigation,share,subscribe}.js)",
" * 构建:node esbuild.config.mjs",
" * 回滚:把 templates/map.html 里的 app.js 换回原 static/js/map.js 等脚本即可。",
" */",
].join("\n"),
},
metafile: true,
});
for (const warning of result.warnings) console.warn("[esbuild]", warning.text);
const bytes = fs.statSync(OUT_FILE).size;
const mapFile = OUT_FILE + ".map";
const mapBytes = fs.existsSync(mapFile) ? fs.statSync(mapFile).size : 0;
const kib = (n) => (n / 1024).toFixed(1) + " KiB";
console.log("构建完成");
console.log(` static/dist/app.js ${kib(bytes)} (${bytes} 字节)`);
console.log(` static/dist/app.js.map ${kib(mapBytes)} (${mapBytes} 字节)`);
/* 输入模块体积表:方便对照每个源模块的实际贡献 */
const inputs = Object.entries(result.metafile.outputs)
.find(([file]) => file.endsWith("app.js"))?.[1]?.inputs;
if (inputs) {
const rows = Object.entries(inputs)
.map(([file, info]) => [file, info.bytesInOutput])
.sort((a, b) => b[1] - a[1]);
console.log(" 按模块(压缩前进 bundle 的字节数):");
for (const [file, n] of rows) console.log(` ${file.padEnd(34)} ${String(n).padStart(7)}`);
}