uniapp 打包生成EXE文件
*uniapp及vue 打包electron生成EXE文件,并讲解给.exe新增头部菜单
·
一、安装nodejs
二、安装electron(主程序)
npm install electron -g
三、安装electron-packager(打包用)
npm install electron-packager -g
四、uniapp的manifest.json修改
五、h5打包
注意:h5打包不需要输入网站域名,直接打包即可
六、h5文件夹下新建package.json和main.js
package.json
{
"name" : "app-name",
"version" : "0.1.0",
"main" : "main.js"
}
main.js
const {app, BrowserWindow,ipcMain } = require('electron')
const fs=require('fs')
const electron = require('electron')
const path = require('path')
const url = require('url')
// ipcMain.on('asynchronous-message', function(event, arg) {
// // arg是从渲染进程返回来的数据
// console.log(arg,1);
// // 这里是传给渲染进程的数据C:/Users/Administrator/Desktop/test.txt
// fs.readFile(path.join(__dirname,"./test.txt"),"utf8",(err,data)=>{
// if(err){
// event.sender.send('asynchronous-reply', "读取失败");
// }else{
// event.sender.send('asynchronous-reply', data);
// }
// })
// });
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win
const Menu = electron.Menu
function createWindow () {
// Create the browser window.
Menu.setApplicationMenu(null)
win = new BrowserWindow({
fullscreen: true,
webPreferences: {
nodeIntegration: true, // 使渲染进程拥有node环境
}
})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
//cpu
// Open the DevTools.
win.webContents.openDevTools()
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
// ipcMain.on('asynchronous-message', function(event, arg) {
// // arg是从渲染进程返回来的数据
// console.log(arg,2);
// // 这里是传给渲染进程的数据C:/Users/Administrator/Desktop/test.txt
// fs.readFile(path.join(__dirname,"./test.txt"),"utf8",(err,data)=>{
// if(err){
// event.sender.send('asynchronous-reply', "读取失败");
// }else{
// event.sender.send('asynchronous-reply', data);
// }
// })
// });
}
// 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', createWindow)
// 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 (win === null) {
createWindow()
}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
七、electron-packager打包
electron-packager . MyApp --win --out MyApp --arch=x64 --electron-version 12.0.7 --overwrite --ignore=node_modules
八、给软件新增菜单
- main.js文件加贴入代码
const {
app,
BrowserWindow
} = require('electron'); //引入electron
let win;
let windowConfig = {
width: 800,
height: 600
}; //窗口配置程序运行窗口的大小
function createWindow() {
win = new BrowserWindow(windowConfig); //创建一个窗口
win.loadURL(`file://${__dirname}/index.html`); //在窗口内要展示的内容index.html 就是打包生成的index.html
win.webContents.openDevTools(); //开启调试工具
win.on('close', () => {
//回收BrowserWindow对象
win = null;
});
win.on('resize', () => {
win.reload();
})
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (win == null) {
createWindow();
}
});
- package.json文件贴入代码
{
"name": "cang",
"productName": "cang",
"author": "cang",
"version": "1.1.1",
"main": "main.js",
"description": "cang",
"scripts": {
"pack": "electron-builder --dir",
"dist": "electron-builder",
"postinstall": "electron-builder install-app-deps"
},
"build": {
"electronVersion": "1.8.4",
"win": {
"requestedExecutionLevel": "highestAvailable",
"target": [
{
"target": "nsis",
"arch": [
"x64"
]
}
]
},
"appId": "cang",
"artifactName": "cang-version−{arch}.${ext}",
"nsis": {
"artifactName": "cang-version−{arch}.${ext}"
},
"extraResources": [
{
"from": "",
"to": "app-server",
"filter": [
"**/*"
]
}
],
"publish": [
{
"provider": "generic",
"url": "cang"
}
]
},
"dependencies": {
"core-js": "^2.4.1",
"electron-updater": "^4.0.0",
"fs-extra": "^4.0.1",
"install.js": "^1.0.1",
"moment": "^2.18.1",
"moment-es6": "^1.0.0",
"electron-packager": "^12.1.0"
},
"devDependencies": {},
"license": "ISC"
}
这样打包之后就出现头部菜单了
细节补充:
- 在打包命令最后加 --icon=图标名 可以更换.exe图标
- 打包命令MyApp为.exe的名称,可以改为自己的软件名
总结
更多推荐
已为社区贡献4条内容
所有评论(0)