“dependencies”: {
“electron”: “^12.0.0”
}
}
“`
其中,name表示應用程序的名稱,version表示應用程序的版本號,main表示應用程序的入口文件,dependencies表示應用程序依賴的包。
4. 編寫代碼
在文件夾中創建一個main.js文件,用于啟動應用程序。具體代碼如下:
“`
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函數用于創建窗口,win.loadFile(‘index.html’)表示加載index.html文件。
在文件夾中創建一個index.html文件,用于編寫HTML代碼。
5. 打包應用程序
在文件夾中打開終端,輸入以下命令:
“`
electron-packager . my-app –platform=win32 –arch=x64 –electron-version=12.0.0 –overwrite
“`
其中,.表示當前文件夾,my-app表示打包后的應用程序名稱,–platform表示打包的平臺,–arch表示打包的架構,–electron-version表示使用的Electron版本,–overwrite表示覆蓋已有的應用程序。
打包成功后,會在文件夾中生成一個my-app-win32-x64文件夾,里面包含了可執行文件和相關資源文件。
四、總結
通過上述步驟,可以將HTML文件打包成exe軟件,使用戶可以直接雙擊運行。需要注意的是,使用不同的工具會有一些細微的差別,具體操作時需要參考相關文檔。