1、配置鏡像
因為下載慢,所以。(網速快的可以跳過這步)
右鍵我的電腦-屬性-高級系統設置-高級-環境變量-新建
變量名:
ELECTRON_MIRROR
變量值:
https://cdn.npm.taobao.org/dist/electron/
完成下面步驟后,可以刪除該變量
2、安裝nodejs
百度一下(已安裝的跳過)
檢查系統變量path里有沒有
C:\Users\你的用戶名\AppData\Roaming\npm
3、安裝electron(主程序)
這里代碼是全局安裝,個人推薦,局部安裝的查閱本文檔第一行的官方文檔里
npm install electron -g
4、安裝electron-packager(打包用)
npm install electron-packager -g
這里也是全局安裝,因為全局只需安裝一次,以后不需要重復安裝
安裝的程序目錄地址:
C:\Users\你的用戶名\AppData\Roaming\npm\node_modules
5、uniapp的manifest.json修改
根目錄下的manifest.json-h5配置
運行的基礎路徑修改為:
./
不然打包出來會出現白屏,讀取不到,因為打包出來的h5默認加載地址為/static/
去掉啟用https協議
6、h5打包
HbuilderX頂部菜單欄-發布-h5手機版發行-發行
發現的默認目錄為:unpackage\dist\build\h5
7、新建
你的項目目錄/
├── static
└── index.html
新建package.json和main.js
新建后項目目錄/
├── static
├── package.json
├── main.js
└── index.html
在 package.json 中添加如下內容
{ "name" : "app-name", "version" : "0.1.0", "main" : "main.js" }
在 main.js 中添加下面的內容,這個 main.js 文件就是上面 package.json 中的 "main"鍵 的值,所以可根據需要修改
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()
// 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.
如果你的網頁首頁的文件名不是 “index.html”,那么請在 main.js 中將其中的 'index.html' 修改為你的網頁首頁名
8、更新內容后再次使用HbuilderX生成h5前記得備份
package.json
main.js
!!!!!!
9、打包
建議使用cmd,本人使用powershell和git hash有踩坑
按shift+右鍵根目錄,或者cd 你的目錄
electron-packager . 可執行文件的文件名 --win --out 打包成的文件夾名 --arch=x64位還是32位 --electron-version版本號(不是你的h5版本號,是electron版本號) --overwrite --ignore=node_modules
示例(可直接復制下面代碼):
nodejs版本 10.12.0以上
electron-packager . MyApp --win --out MyApp --arch=x64 --electron-version 1.4.14 --overwrite --ignore=node_modules
10、打包成功后,根目錄會生成一個新的文件夾,點進去,找到 exe 文件,雙擊就可以看到網頁變成了一個桌面應用啦!
11、以上是最簡單的打包方式,至於怎么修改窗口大小、菜單欄怎么加、怎么調用系統API這些,就給你慢慢去研究Electron了。
12、最后,記得備份package.json和main.js
