“dependencies”: {
“electron”: “^12.0.0”
}
}
“`
其中,name表示應(yīng)用程序的名稱,version表示應(yīng)用程序的版本號(hào),main表示應(yīng)用程序的入口文件,dependencies表示應(yīng)用程序依賴的包。
4. 編寫代碼
在文件夾中創(chuàng)建一個(gè)main.js文件,用于啟動(dòng)應(yīng)用程序。具體代碼如下:
“`
const { app, BrowserWindow } = require(‘electron’)
const path = require(‘path’)
function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})
win.loadFile(‘index.html’)
}
app.whenReady().then(() => {
createWindow()
app.on(‘activate’, () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on(‘window-all-closed’, () => {
if (process.platform !== ‘darwin’) {
app.quit()
}
})
“`
其中,const { app, BrowserWindow } = require(‘electron’)表示引入Electron的app和BrowserWindow模塊,path表示引入path模塊,createWindow函數(shù)用于創(chuàng)建窗口,win.loadFile(‘index.html’)表示加載index.html文件。
在文件夾中創(chuàng)建一個(gè)index.html文件,用于編寫HTML代碼。
5. 打包應(yīng)用程序
在文件夾中打開終端,輸入以下命令:
“`
electron-packager . my-app –platform=win32 –arch=x64 –electron-version=12.0.0 –overwrite
“`
其中,.表示當(dāng)前文件夾,my-app表示打包后的應(yīng)用程序名稱,–platform表示打包的平臺(tái),–arch表示打包的架構(gòu),–electron-version表示使用的Electron版本,–overwrite表示覆蓋已有的應(yīng)用程序。
打包成功后,會(huì)在文件夾中生成一個(gè)my-app-win32-x64文件夾,里面包含了可執(zhí)行文件和相關(guān)資源文件。
四、總結(jié)
通過(guò)上述步驟,可以將HTML文件打包成exe軟件,使用戶可以直接雙擊運(yùn)行。需要注意的是,使用不同的工具會(huì)有一些細(xì)微的差別,具體操作時(shí)需要參考相關(guān)文檔。