// electron-builder afterPack 钩子: // 1) 把 package.json 版本号写入 exe 同级 shell.version(shell-updater 靠它判定"打包态 + 本机版本", // 缺失会被误判为源码开发模式、不参与壳子自更新;win-unpacked 根目录 = 安装后 exe 同级目录) // 2) 校验打进包里的 better-sqlite3 原生模块是 **Electron ABI**(1.10.9 全员启动崩事故的守门人: // 当时打进的是系统 Node ABI(137) 二进制,Electron 30 只认 123,装机后 require 即抛错)。 // 不一致直接让打包失败,绝不产出装不上的安装包。 "use strict"; const fs = require("fs"); const path = require("path"); const crypto = require("crypto"); const ROOT = path.join(__dirname, ".."); const NATIVE_BIN = path.join(ROOT, "native", "electron", "build", "Release", "better_sqlite3.node"); function sha256(file) { return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex"); } function verifyNativeAbi(context) { const packed = path.join( context.appOutDir, "resources", "app.asar.unpacked", "node_modules", "better-sqlite3", "build", "Release", "better_sqlite3.node" ); if (!fs.existsSync(packed)) { throw new Error(`[after-pack] 包内未找到 better-sqlite3 原生模块:${packed}`); } if (!fs.existsSync(NATIVE_BIN)) { throw new Error( `[after-pack] 缺少 Electron ABI 参考二进制:${NATIVE_BIN}\n` + "请用 npm run pack 打包(会自动准备并切换 ABI),不要直接跑 electron-builder" ); } const got = sha256(packed); const want = sha256(NATIVE_BIN); if (got !== want) { throw new Error( "[after-pack] 包内 better-sqlite3 不是 Electron ABI,装机会启动即崩!\n" + ` 包内 sha256=${got}\n 期望 sha256=${want}\n` + "请用 npm run pack 打包(它会先切换 ABI),不要直接跑 electron-builder" ); } console.log(`[after-pack] 原生模块 ABI 校验通过(sha256=${got.slice(0, 12)})`); } exports.default = async function afterPack(context) { const pkg = require(path.join(ROOT, "package.json")); const target = path.join(context.appOutDir, "shell.version"); fs.writeFileSync(target, `${pkg.version}\n`, "utf-8"); console.log(`[after-pack] shell.version -> ${target} (${pkg.version})`); verifyNativeAbi(context); };