51 lines
1.8 KiB
TypeScript
51 lines
1.8 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:{
|
||
|
directories: {
|
||
|
output: path.resolve(process.cwd(), 'release'),
|
||
|
app: path.resolve(process.cwd(), 'dist'),
|
||
|
},
|
||
|
asar: true,
|
||
|
appId: 'com.example.app',
|
||
|
productName: 'vite-electron',
|
||
|
nsis:{
|
||
|
oneClick: false, // 取消一键安装
|
||
|
allowToChangeInstallationDirectory: true, // 允许用户选择安装目录
|
||
|
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|