electron与vue配合生成客户端

electron是什么?

官网描述:如果你可以建一个网站,你就可以建一个桌面应用程序。 Electron 是一个使用 JavaScript, HTML 和 CSS 等 Web 技术创建原生程序的框架,它负责比较难搞的部分,你只需把精力放在你的应用的核心上即可。

如何与vue搭配使用?

  1. 首先我们需要创建一个vue项目
  2. 安装electron,推荐使用全局安装,不然编译的时候比较麻烦。

    npm install -g electron

  3. 安装electron-builder,和electron采用全局安装方式

    npm install -g electron-builder

  4. 对vue项目进行打包,我使用的是yarn,用npm同理

    yarn build

  5. 进入打包好的dist文件夹,新建main.js和package.json两个文件

main.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
// const isDev = require('electron-is-dev');
// 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: {
nodeIntegration: true
}
})

// and load the index.html of the app.
// mainWindow.loadURL(url.format({
// pathname: path.join(__dirname, './build/index.html'),
// protocol: 'file:',
// slashes: true
// }))
// const devUrl = 'http://localhost:3000';
// 本地文件路径定位到打包的react文件
const localUrl = `file://${__dirname}/index.html`;
// const appUrl = isDev ? devUrl : localUrl;
mainWindow.loadURL(localUrl)

// 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()
})

// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"name": "hello",
"productName": "test-electron",
"version": "0.1.0",
"main": "main.js",
"scripts": {
"dist": "electron-builder"
},
"build": {
"directories": {
"output": "../test"
}
},
"nsis": {
"oneClick": false
}
}

  1. 最后一步,对electron进行打包,第一次打包时间可能会比较久,因为要下载一下依赖,由于众所周知的原因还可能下载失败,耐心等待一下。

    yarn dist

可能出现的问题

执行yarn dist下载依赖失败

解决办法:

  1. 手动下载electron相关依赖,然后放到(不需要解压)

    C:\Users***\AppData\Local\electron\Cache

  2. 手动下载electron-builde相关依赖,然后放到(需要解压)

    C:\Users***\AppData\Local\electron-builder\Cache

依赖下载成功后打包依然失败

将electron-builder中的winCodeSign的所有Plugin复制到nsis当中

参考链接

React + Electron 搭建一个桌面应用
正确设置 ELECTRON_MIRROR ,加速下载 electron 预编译文件
Electron 应用如何利用 create-react-app 从 0 到 1
Electron 打包问题:electron-builder 下载各种依赖出错
electron 构建打包总结
Windows 10 build Error (NSIS)