前言
最近想把自己用html+css+js做的網頁界面打包成桌面應用,網上一搜,發現Electron是一個不錯的選擇,試了試,發現效果真的不錯。這里記錄一下打包過程以作記錄,便於自己以后查看學習。
一、簡介
Electron是由Github開發,用HTML,CSS和JavaScript來構建跨平台桌面應用程序的一個開源庫。 Electron通過將Chromium和Node.js合並到同一個運行時環境中,並將其打包為Mac,Windows和Linux系統下的應用來實現這一目的。
二、打包過程
1.首先保證自己計算機全局安裝了nodejs,並且利用nodejs自帶的npm全局安裝了electron和打包神器electron-packager。兩個命令:(npm install electron -g),(npm install electron-packager -g)。注意:npm速度較慢,可以嘗試用cnpm,下載安裝速度快。
2.在自己的前端網頁項目文件夾下,新建 package.json、main.js、index.html 三個文件(注:其中的 index.html 是你的網頁首頁,就是自己已經寫好的網頁界面)。

3.在 package.json 中添加如下內容:(name屬性值可以自定義,Version屬性值也可以自定義,main屬性值是根據新建的main.js文件名來的)
{ "name" : "app-name", "version" : "0.1.0", "main" : "main.js" }
4.在main.js中插入如下代碼:
const {app, BrowserWindow} = 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 win
function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})
// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Open the DevTools.
//win.webContents.openDevTools() //注意:這行代碼取消注釋的話,就可以使打包生成的應用程序帶有開發者模式,類似於chrome的調試模式,可以調試程序的出現的bug
// 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
})
}
// 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.
注:上面這行代碼win.webContents.openDevTools()很有用,如果這行代碼取消注釋,可以使生成的應用程序自帶調試框,方便程序的調試。
4.在自己的前端網頁項目文件夾下,輸入 electron-packager . app --win --out presenterTool --arch=x64 --electron-version 3.0.10 --overwrite --ignore=node_modules 即可開始打包。

注:可根據自己需求更改命令如下:
electron-packager . 可執行文件的文件名 --win --out 打包成的文件夾名 --arch=x64位還是32位 --electron-version 版本號 --overwrite --ignore=node_modules
補充:這里的electron版本號可直接在命令行輸入electron得到(因為electron全局安裝的)。
5.打包結果

這個presenterTool文件夾下就有生成的app文件。

三、源碼加密
在上面打包完成后的文件夾下,我們會發現,在每個包下的resources文件夾里的app文件夾里都有我們寫的源程序。



這樣我們寫的代碼就是完全暴露在用戶電腦上的,這是非常不安全的,但是我們可以通過electron 自帶的加密功能解決這個問題。
1.全局安裝 asar
npm install asar -g
安裝完asar以后,就可以使用asar命令將程序文件打包了。
2.在resources目錄下使用asar指令進行加密
asar pack ./app app.asar
如下所示:

3.結果

將原來的app文件夾刪除就可以了,這樣生成的app.asar就加密了之前的源代碼,保證了安全性。

四、最終效果:(展示一部分)

參考:
https://blog.csdn.net/a727911438/article/details/70834467?utm_source=itdadao&utm_medium=referral
https://blog.csdn.net/u010683528/article/details/56279647
https://newsn.net/say/electron-asar.html
