Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库。 Electron通过将Chromium和Node.js合并到同一个运行时环境中,并将其打包为Mac,Windows和Linux系统下的应用来实现这一目的。
本文利用vue-cli快速搭建一个项目说明怎么将一个web应用打包构建成桌面应用的过程
开始
使用npm init初始化项目,配置依赖electron,electron-builder(打包组件),这里如果安装electron或者启动时报错,可以删除node_modules中electron的使用npm i electron@+指定版本即可
{
"name": "electron-quick-start",
"version": "1.0.0",
"description": "A minimal Electron application",
"main": "main.js",
"scripts": {
"start": "electron .",
"build_win": "electron-builder --win --x64",
"build_mac": "electron-builder --mac --x64"
},
"build": {
"productName": "vuecli"
},
"repository": "https://github.com/electron/electron-quick-start",
"keywords": [
"Electron",
"quick",
"start",
"tutorial",
"demo"
],
"author": "GitHub",
"license": "CC0-1.0",
"devDependencies": {
"electron": "^3.1.0",
"electron-builder": "^21.2.0"
}
}
package.json需要注意的参数:
:项目入口文件,文件用于配置启动所需参数
scripts:启动命令
- start:开发启动命令
- build_win:构建win桌面应用
- build_mac:构建macos桌面应用
build:构建应用参数,所有参数参考electron-build - productName:应用名称
- output:产出目录
main.js
官方例子
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
// 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: 800,
height: 600,
webPreferences: {
webSecurity: false
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// 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()
})
BrowserWindow浏览器代理,设置窗口信息等,mainWindow.loadFile('index.html')引入主页文件,webSecurity设置false可忽略浏览器跨域等,该设置可禁止浏览器同源策略,只能在测试环境使用,如需上线正式环境需另外配置网络代理。
静态文件准备
http请求
在测试环境下,如在main.js中设置了webSecurity为false,接口请求地址可设置为绝对路径(此设置忽略跨域),生产环境需另配网络转发。
路由
需修改的相关静态文件地址为相对路径,vue.config.js配置如下:
module.exports = {
assetsDir: 'static',
publicPath: './'
};
启动npm run build开始打包,打包后的文件在dist中,dist文件夹下的文件全部拷贝到electron的初始化项目的根目录下,运行npm start即可正常访问,npm run build_mac开发打包,完成后在根目录的dist下生成vuecli-1.0.0.dmg的桌面应用安装包
原文地址