需求:在electron正式打包为一个exe前修改安装目录内的某些文件
前言:在 Electron Builder 中,win-unpacked 是在 Windows 平台上使用 Electron Builder 构建应用程序后的输出文件夹名称。在该文件夹中,你将找到已解压缩的应用程序所需的所有文件。
大白话:electron-builder将win-unpacked内的文件打包成一个exe之后在最终安装目录中解压出来,也就是说我们修改这里的文件会影响最终用户使用的程序
解决方案
(1)添加afterSign配置,指定在打包和签名之后(但在打包为可分发格式之前)运行的函数
vue.config.js
pluginOptions: {
electronBuilder: {
builderOptions: {
afterSign: './update-shell.js',
}
}
}
update-shell.js
const fs = require('fs')
// package-file放置一些要打包进安装包的文件(可以是套壳程序)
exports.default = async function(context) {
// win-unpacked地址
const packageFileDir = context.outDir + '\\package-file'
const files = fs.readdirSync(packageFileDir);
for(let i = 0; i < files.length; i++){
const file = files[i]
const inputAbsoPath = packageFileDir + '\\' + file
const outputAbsoPath = context.appOutDir + '\\' + file
// 同步方式
try {
// 打包文件移动成功
fs.copyFileSync(inputAbsoPath, outputAbsoPath);
} catch (error) {
console.error('移动文件失败:', error);
}
}
}