electron打包exe程序
在根目录创建electron文件,创建main.js和preload.js
main.js
const { app, protocol, BrowserWindow, globalShortcut,ipcMain,screen } = require('electron')
// 需在当前文件内开头引入 Node.js 的 'path' 模块
const path = require('path')
app.commandLine.appendSwitch("--ignore-certificate-errors", "true");
// Scheme must be registered before the app is ready
protocol.registerSchemesAsPrivileged([
{ scheme: "app", privileges: { secure: true, standard: true } }
]);
let win;
const createWindow = () => {
const { width, height } = screen.getPrimaryDisplay().bounds;
win = new BrowserWindow({
fullscreen: true,
// minWidth: 960,
// minHeight: 540,
width: width,
height: height,
//窗口是否在屏幕居中. 默认值为 false
center: true,
//设置为 false 时可以创建一个无边框窗口 默认值为 true。
frame: true,
//窗口是否在创建时显示。 默认值为 true。
show: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: true,
preload: path.join(__dirname, 'preload.js'),
webSecurity: false,
}
})
win.setMenu(null)
if (app.isPackaged) {
win.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`)
} else {
win.loadURL('http://localhost:5173/')
//win.loadURL('http://localhost:5173/')
win.webContents.openDevTools()
}
globalShortcut.register("CommandOrControl+Shift+i", function () {
win.webContents.openDevTools();
});
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
ipcMain.handle('enter-full-screen', () => {
win.setFullScreen(true); // 进入全屏模式
})
preload.js
// 所有的 Node.js API接口 都可以在 preload 进程中被调用.
// 它拥有与Chrome扩展一样的沙盒。
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency])
}
})
在packge.json文件里面添加,根据自己的
"author": {
"name": "lsl",
"email": "lsl@lsl.com.cn"
},
"description": "lsl",
"main": "electron/main.js",
"asar": false,
"files": [
"./dist",
"./electron",
"!**/node_modules/**"
],
"nsis": {
"oneClick": false,
"allowElevation": true,
"allowToChangeInstallationDirectory": true,
"createDesktopShortcut": true,
"createStartMenuShortcut": true
},
"scripts": {
"start": "vite | electron .",
"electron:build": "vite build && electron-builder"
},
"build": {
"appId": "lsl.first.app",
"productName": "自己设置exe名称",
"copyright": "Copyright © 2023 lsl",
"directories": {
"output": "dist_electron"
}
},
把node_modules和packge.json-lock文件删除重新下载依赖
查看一下electron的版本
%LOCALAPPDATA%/electron/Cache
如果报这个错就是权限问题,用管理员打开cmd,运行npm run electron:build
• downloading url=https://mirrors.huaweicloud.com/electron-builder-binaries/winCodeSign-2.6.0/winCodeSign-2.6.0.7z size=5.6 MB parts=1
• downloaded url=https://mirrors.huaweicloud.com/electron-builder-binaries/winCodeSign-2.6.0/winCodeSign-2.6.0.7z duration=7.871s
⨯ cannot execute cause=exit status 2
out=
7-Zip (a) 21.07 (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-12-26
Scanning the drive for archives:
1 file, 5635384 bytes (5504 KiB)
Extracting archive: C:\Users\gys\AppData\Local\electron-builder\Cache\winCodeSign\807703091.7z
--
Path = C:\Users\gys\AppData\Local\electron-builder\Cache\winCodeSign\807703091.7z
Type = 7z
Physical Size = 5635384
Headers Size = 1492
Method = LZMA2:24m LZMA:20 BCJ2
Solid = +
Blocks = 2
Sub items Errors: 2
Archives with Errors: 1
Sub items Errors: 2
就这么多了,如果遇到什么问题再讨论吧。