安装依赖
package.json
"author": {"name":"milo"},
"main": "background.js", -- 增加
"scripts": {
"serve": "npm run dev",
"dev": "vite --open --mode dev",
"start": "electron --trace-warnings .", -- 增加
"commit": "cz",
"dev:mock": "vite --open --mode mock",
"dev:linux": "vite --mode dev",
"build": "vite build --mode prod",
"exe:build": "electron-builder --config electron.config.json", -- 增加
"devDependencies": {
"@commitlint/cli": "^17.0.3",
"@commitlint/config-conventional": "^16.2.1",
"@types/vue-color": "^2.4.4",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"commitizen": "^4.2.3",
"electron": "^23.1.3", --增加
"electron-builder": "^23.6.0", --增加
"electron-devtools-installer": "^3.2.0", --增加
增加background.js 到根目录
const { app, protocol, BrowserWindow } = require('electron');
const isDevelopment = process.env.NODE_ENV === 'development'
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: 'app', privileges: { secure: true, standard: true } }
])
async function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 1000,
height: 600,
minWidth: 1000,
minHeight: 800,
useContentSize: true,
transparent: false,
fullscreenable:true,
// frame: true, // PS:隐藏窗口菜单
// resizable: false,
movable: true,
maximizable: true, // 支持最大化
webPreferences: {
// Use pluginOptions.nodeIntegration, leave this alone
// See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
enableRemoteModule: true // 打开remote模块
}
})
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL)
if (!process.env.IS_TEST) win.webContents.openDevTools()
} else {
// Load the index.html when not in development
win.loadFile("./dist/index.html");
}
}
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
// if (isDevelopment && !process.env.IS_TEST) {
// // Install Vue Devtools
// try {
// await installExtension(VUEJS3_DEVTOOLS)
// } catch (e) {
// console.error('Vue Devtools failed to install:', e.toString())
// }
// }
createWindow()
})
// Exit cleanly on request from parent process in development mode.
if (isDevelopment) {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
app.quit()
}
})
} else {
process.on('SIGTERM', () => {
app.quit()
})
}
}
增加electron.config.json 到根目录
/**
* @see https://www.electron.build/configuration/configuration
*/
{
"productName": "xiongq", //项目名称 打包生成exe的前缀名
"appId": "xiongq", // 包名
"asar": true,//asar打包
"icon": "public/favicon.ico",
"compression": "maximum", //store|normal|maximum 打包压缩情况(store速度较快)
"directories": {
"output": "release/${version}" //输出文件夹(默认dist_electron)
},
// 拷贝静态资源目录到指定位置
// "extraResources": [
// {
// "from": "./public",
// "to": "static"
// },
// ],
"files": [
"dist-electron",
"dist",
"background.js"
],
"mac": {
"artifactName": "${productName}_${version}.${ext}",
"target": [
"dmg"
]
},
"win": {
"icon": "./public/favicon.ico", //图标路径
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
}
],
"artifactName": "${productName}_${version}.${ext}"
},
"nsis": {
"oneClick": false, //一键安装
"allowToChangeInstallationDirectory": true, //允许修改安装目录
"perMachine": true, //是否开启安装时权限设置(此电脑或当前用户)
"artifactName": "${productName}-${version}-${platform}-${arch}-setup.${ext}", //打包后安装包名称
"deleteAppDataOnUninstall": true, //卸载时删除数据
"createDesktopShortcut": true, //创建桌面图标
"createStartMenuShortcut": true, //创建开始菜单图标
"shortcutName": "xiongq" //桌面快捷键图标名称
}
}
配置electron
npm config set ELECTRON_MIRROR https://npm.taobao.org/mirrors/electron/
运行与打包
#运行
npm run start
#打包
npm run exe:build