node
包必须使用 npm
安装不可使用 cnpm
, 使用 cnpm
安装的 node
包会导致打包时间无限可能npm
和 cnpm
安装的包结构安装 electron 包
npm install electron --save-dev
package.js 添加入口文件
"main": "main.js",
创建 main.js 入口文件 内容如下
const {app, BrowserWindow, dialog} = require('electron'); const path = require('path'); const url = require('url'); // 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 mainWindow; function createWindow () { // Create the browser window. mainWindow = new BrowserWindow({ width: 1300, height: 800, frame: true, autoHideMenuBar: true, fullscreenable: true, transparent: false, webPreferences: { javascript: true, plugins: true, nodeIntegration: true, // Nodejs webSecurity: false, preload: path.join(__dirname, './preload.js') } }); // mainWindow.webContents.openDevTools();//打开调试工具 //测试时使用mainWindow.loadURL(http://localhost:80000); //打包时加载本地文件 mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'dist/index.html'), protocol: 'file:', slashes: true })); // Emitted when the window is closed. mainWindow.on('closed', function () { // 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. mainWindow = null }) } // 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', function () { // 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', function () { // 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 (mainWindow === null) createWindow() });
preload.js 文件内添加, 将 electron 做全局导入 未做此操作无法在其他地方使用 electron 模块
global.electron = require('electron')
在 package.json 文件中加入启动命令
"scripts": { "electron-start": "electron .", },
试启动 electron 窗口内容加载成功则成功
npm run electron-start
渲染进程如需和主进程通信查看官方文档
https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes
config/config.js 文件添加
history: 'hash', //更改路由方式 publicPath: './', //使打包后的文件使用相对路径
src/utils/request.js 此目录并非标准 不同版本下文件可能有所区别 重点在于给请求配置前缀
当项目打包成应用后使用的是 file:协议
ant pro
的请求无法发出 需要使用完整的请求地址 目前方法为配置前缀
/** * 配置request请求时的默认参数 */ const request = extend({ errorHandler, // 默认错误处理 prefix: 'http://hotel-system.yc384.com/api', // 请求前缀 credentials: 'include', // 默认请求是否带上cookie });
"homepage": ".",
app
目录是为了不将 node
包打包进去,减少应用大小node
包或者内容较少可直接在当前操作, 省略 app
目录相关操作npm install electron-builder
"electron-build": "electron-builder --win --x64"
"build": { "appId": "com.xxx.app", "directories": { "output": "build" }, // 打包后存放目录 "mac": {// mac安装包dmg "target": ["dmg","zip"] }, "win": {// win安装包nsis "target": ["nsis","zip"] }
创建app目录(builder默认打包app下内容,否则会打包当前所有内容)
将ant pro打包后的dist文件和main.js放入app目录
在app下创建package.json文件(外层package做打包使用,app下的package是打包后的应用依赖)
"name": "hotel", "version": "2.3.1", "main": "main.js",
打包后文件会在 build 目录下
npm run electron-build
安装electron-package
npm install electron-packager --save-dev
package.json下script添加命令(具体含义百度)
"electron-package": "electron-packager . hotelSystem --win32 --out app --arch=x64 --overwrite --ignore=node_modulesls --electron-version=6.0.5",
npm run electron-package
dist
和 main.js
文件放入一个新目录package.json
文件打包参数 其他删除即可"name": "hotel", "version": "2.3.1", "main": "main.js", "homepage": ".", "scripts": { "electron-start": "electron .", }