Files

46 lines
1.7 KiB
JavaScript
Raw Permalink 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.
// 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`)
}
}
}