1.安装electron(主程序)和electron-packager(打包工具)
设置阿里镜像
npm config set registry=https://registry.npmmirror.com
npm config set disturl=https://registry.npmmirror.com/-/binary/node
设置electron仓库
npm config set electron_mirror=https://registry.npmmirror.com/-/binary/electron/
安装electron
npm install electron -g
安装electron-packager
npm install electron-packager -g
2.在manifest.json中修改配置:
3.发行成网站-PC Web或手机H5
4.进入到打包生成的web文件夹中, 创建main.js和package.json两个文件
main.js代码
const {
app,
BrowserWindow,
ipcMain,
globalShortcut
} = 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)
//解决点击窗口最大化样式整体过大问题
const WIDTH = 1860
const HEIGHT = 900
const aspectRatio = WIDTH / HEIGHT // 窗口宽高比
win = new BrowserWindow({
fullscreen: true, //(设置默认打开满屏)
// width: 800,
// height: 600,
width: WIDTH,
height: HEIGHT,
// frame: false, // 无边框窗口
webPreferences: {
nodeIntegration: true, // 使渲染进程拥有node环境
},
icon: path.join(__dirname, 'icon.ico') // 设置ico
})
// 注册全局快捷键ESC来退出全屏(如果窗口当前处于全屏状态)
globalShortcut.register('Escape', () => {
if (win.isFullScreen()) {
win.setFullScreen(false);
}
});
// 隐藏默认菜单(在某些操作系统上可能不起作用)
win.setMenuBarVisibility(false);
win.once('ready-to-show', () => {
// 限制窗口最小尺寸(int整形), 无边框模式下,不考虑标题栏高度
win.setMinimumSize(WIDTH / 2, HEIGHT / 2)
win.show()
})
// 控制等比缩放
win.on('will-resize', resizeWindow)
function resizeWindow(event, newBounds) {
const wins = event.sender
event.preventDefault() // 拦截,使窗口先不变
const currentSize = wins.getSize()
const widthChanged = currentSize[0] !== newBounds.width // 判断是宽变了还是高变了,两者都变优先按宽适配
// ! 虽然搞不懂为何有1px偏差,但是可以解决问题(Windows 10)
if (widthChanged) {
wins.setContentSize(newBounds.width - 1, parseInt(newBounds.width / aspectRatio + 0.5) - 1)
} else {
wins.setContentSize(parseInt(aspectRatio * newBounds.height + 0.5) - 1, newBounds.height - 1)
}
}
// 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.
package.json代码
{
"name": "myApp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
5.在web文件夹路径打开命令行
6.执行打包命令
33.2.1是我的electron的版本, 实际情况通过electron -v 查看替换
electron-packager . MyApp --win --out MyApp --arch=x64 --electron-version 33.2.1 --overwrite --ignore=node_modules