Electron 封装网页/打包应用 for Linux x64 ubuntu and Jetson arm64

2022-05-12

先 clone Electron 的官方示例 https://github.com/electron/electron-quick-start,对目录结构进行调整,代码放入 src 文件夹中。

在 main.js 中 mainWindow 创建完成后进行窗口最大化 全屏 载入网页,browser-window-focus browser-window-blur 事件中屏蔽快捷键。

载入网页的地址使用配置文件,可以在打包后进行更改,不需要写死在代码中,或者使用 "electron-serve": "^1.1.0", 进行代理前端静态文件,dist 文件夹放入 src 中,这种方式就需要每次进行打包。

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Modules to control application life and create native browser window
const { app, BrowserWindow, globalShortcut } = require('electron')
const path = require('path')
const fs = require('fs')
const serve = require('electron-serve');
const loadURL = serve({ directory: 'src/dist' });

function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})

// 窗口最大化
mainWindow.maximize();
// 窗口全屏
mainWindow.setFullScreen(true)
// 载入网页
// mainWindow.loadURL("http://localhost")



// const envpath = '/root/.DEVICEFRONTENDHOST'
// if (fs.existsSync(envpath)) {
// fs.readFile(envpath, 'utf8', (err, data) => {
// if (err) {
// console.error(err);
// return;
// }
// mainWindow.loadURL(data.replace("\n", ''))
// });
// } else {
// mainWindow.loadFile('src/index.html')
// }

if (fs.existsSync('src/dist')) {
// 代理spa electron-serve
mainWindow.loadURL('app://-');
// loadURL(mainWindow);
} else {
mainWindow.loadFile('src/index.html')
}

// Open the DevTools.
// mainWindow.webContents.openDevTools()
}

// 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.whenReady().then(() => {
createWindow()

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 (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})

// 屏蔽网页刷新快捷键
app.on('browser-window-focus', function () {
globalShortcut.register("CommandOrControl+R", () => {
console.log("CommandOrControl+R is pressed: Shortcut Disabled");
});
globalShortcut.register("F5", () => {
console.log("F5 is pressed: Shortcut Disabled");
});
globalShortcut.register("F11", () => {
console.log("F11 is pressed: Shortcut Disabled");
});
});
app.on('browser-window-blur', function () {
globalShortcut.unregister('CommandOrControl+R');
globalShortcut.unregister('F5');
globalShortcut.unregister('F11');
});

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

使用 https://www.electronforge.io/ 进行打包,导入现有项目,package.json 中务必配置 author 字段,会自动写入打包配置,但也需要修改。

1
2
npm install --save-dev @electron-forge/cli
npm exec --package=@electron-forge/cli -c "electron-forge import"

config forge packageConfig 中会配置名称 图标,每次打包是否覆盖,makers 中会配置不同平台,这里需要删掉 rpm 平台,因为如果不删除,在 jetson arm64 上也会识别成也是linux 会同时打包 deb rpm,就会报错。

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
"config": {
"forge": {
"packagerConfig": {
"name": "electron_quick_start",
"icon": "./src/icon/icon.png",
"overwrite": true
},
"makers": [
{
"name": "@electron-forge/maker-squirrel",
"platforms": [
"windows"
],
"config": {
"name": "electron_quick_start",
"icon": "./src/icon/icon.png",
"overwrite": true
}
},
{
"name": "@electron-forge/maker-zip",
"platforms": [
"darwin"
],
"config": {
"name": "electron_quick_start",
"icon": "./src/icon/logo.png",
"overwrite": true
}
},
{
"name": "@electron-forge/maker-deb",
"config": {
"name": "electron_quick_start",
"icon": "./src/icon/icon.png",
"overwrite": true
}
}
]
}
}

最后直接运行 npm run package npm run make 将会自动识别当前机器的平台架构,即可在 out/make 中看到不同平台不同架构的打包成品。

参考 https://github.com/lsdlab/electron-quick-start