build: 优化打包体积(compression maximum + afterPack 裁剪语言包)zip 139.7MB -> 128.9MB
This commit is contained in:
@@ -15,6 +15,10 @@ files:
|
||||
asar: true
|
||||
# 关闭重建原生依赖(本应用无原生模块,避免下载耗时)
|
||||
npmRebuild: false
|
||||
# 压缩等级:maximum 用 7z 极限压缩 exe/asar,显著减小 zip 体积(打包稍慢)
|
||||
compression: maximum
|
||||
# 打包后处理:裁剪 locales 语言包等(见 scripts/afterPack.js)
|
||||
afterPack: ./scripts/afterPack.js
|
||||
win:
|
||||
icon: build/icon.ico
|
||||
# zip = 免安装绿色版压缩包;如需单文件 exe 可加 portable
|
||||
|
||||
@@ -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 Compiler,WebGPU 编译用,~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)`)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user