build: 优化打包体积(compression maximum + afterPack 裁剪语言包)zip 139.7MB -> 128.9MB

This commit is contained in:
2026-08-23 11:23:57 +08:00
parent 2c90205766
commit 4b3ea03dc2
3 changed files with 65 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
// electron-builder afterPack 钩子:打包完成后精简发行目录
// 当前裁剪项:locales 语言包(仅保留中文 + 英文,省 ~50MB 未压缩体积)
// 如需进一步瘦身:取消下方 dxcompiler/dxil 注释可移除 WebGPU 着色器编译器(省 ~26MB,仅大屏用到 WebGPU 才受影响)
const fs = require('fs')
const path = require('path')
/** 保留的语言包(Chromium 的 .pak 文件) */
const KEEP_LOCALES = new Set(['zh-CN.pak', 'zh-TW.pak', 'zh-HK.pak', 'en-US.pak'])
/** 可选删除的文件(WebGPU 专用,一般大屏网页用不到) */
const OPTIONAL_REMOVE = [
// 'dxcompiler.dll', // DirectX Shader CompilerWebGPU 编译用,~24MB
// 'dxil.dll', // DXIL 支持库,~1.4MB
]
exports.default = async (context) => {
const appDir = context.appOutDir
// 1. 裁剪 locales:只保留中文 + 英文
const localesDir = path.join(appDir, 'locales')
if (fs.existsSync(localesDir)) {
let removed = 0
for (const f of fs.readdirSync(localesDir)) {
if (!KEEP_LOCALES.has(f)) {
try {
fs.unlinkSync(path.join(localesDir, f))
removed++
} catch (e) {
console.warn('[afterPack] 删除失败:', f, e.message)
}
}
}
console.log(`[afterPack] locales 裁剪完成,删除 ${removed} 个语言包,保留 ${KEEP_LOCALES.size}`)
}
// 2. 可选删除体积大户(默认关闭,需手动启用)
for (const name of OPTIONAL_REMOVE) {
const p = path.join(appDir, name)
if (fs.existsSync(p)) {
const size = fs.statSync(p).size
fs.unlinkSync(p)
console.log(`[afterPack] 已删除 ${name}${(size / 1024 / 1024).toFixed(1)} MB`)
}
}
}