60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import type { Plugin } from 'vite'
|
|
import fs from 'node:fs'
|
|
import * as electronBuilder from 'electron-builder'
|
|
import path from 'node:path'
|
|
|
|
// 生产环境的插件electron
|
|
const BuildBackground = ()=>{
|
|
// 要使用require,需要 在package.json中把type:module删除
|
|
// 编译ts脚本为js
|
|
require('esbuild').buildSync({
|
|
entryPoints: ['src/background.ts'],
|
|
bundle: true,
|
|
outfile: 'dist/background.js',
|
|
platform: 'node',
|
|
target: 'node12',
|
|
external:['electron']
|
|
})
|
|
}
|
|
|
|
// 打包 需要先等vite打完包之后就有index.html 在执行electron-builder打包
|
|
export const ElectronBuildPlugin = (): Plugin => {
|
|
return {
|
|
name: 'electron-build',
|
|
closeBundle(){
|
|
BuildBackground()
|
|
//electron-builder 需要制定package.json main
|
|
const json = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
|
|
json.main = 'background.js'
|
|
fs.writeFileSync('dist/package.json', JSON.stringify(json, null, 4))
|
|
// bug electron-builder 他会给你下载垃圾文件
|
|
fs.mkdirSync('dist/node_modules')
|
|
|
|
electronBuilder.build({
|
|
config:{ // 通常是文件 electron-builder.yml
|
|
directories: {
|
|
output: path.resolve(process.cwd(), 'release'),
|
|
app: path.resolve(process.cwd(), 'dist'),
|
|
},
|
|
appId: 'com.c1.game.launcher',
|
|
productName: 'c1-game-launcher',
|
|
win: {
|
|
icon: path.resolve(process.cwd(), 'src/assets/test-icon.ico'), // 指定图标文件路径
|
|
target: [
|
|
{
|
|
target: 'portable',
|
|
arch: ['x64', 'ia32']
|
|
}
|
|
]
|
|
}
|
|
}
|
|
})
|
|
.then(() => {
|
|
console.log('Build successful!')
|
|
})
|
|
.catch((error) => {
|
|
console.error('Error during build:', error)
|
|
})
|
|
}
|
|
}
|
|
} |