electron打包dist為可執(zhí)行程序的實(shí)現(xiàn)步驟
前言
甲方爹:BS=>CS?
我方領(lǐng)導(dǎo):OJBK。
項(xiàng)目是普普通通的vue項(xiàng)目,要求封裝成arm64的Linux可執(zhí)行程序。
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、直接看效果

二、實(shí)現(xiàn)步驟
1.準(zhǔn)備dist文件夾

publicPath得是./,不然打包出來的dist跑起來是空白的,雙擊index.html能在瀏覽器中看到頁面。
2.修改接口映射
接口請求映射關(guān)系修改,如果不修改映射關(guān)系,接口請求會(huì)變成通過file:///協(xié)議訪問。我看有的人說把項(xiàng)目里面的接口都替換寫死,wo...

修改一下.env.production 生產(chǎn)環(huán)境配置文件中VUE_APP_BASE_API的值為你的生產(chǎn)環(huán)境要訪問的接口就行,格式為:http://ip地址:端口號(hào)。這里是vue.config.js的proxy和request.js的請求配置的變量配置。

3.NVM管理node版本
項(xiàng)目框架比較成熟,electron-quick-start比較新,中間遇到版本不兼容,一個(gè)16一個(gè)20。所以需要用NVM管理node版本,執(zhí)行構(gòu)建命令的時(shí)候切一下。
注意:通過NVM install的node不能直接切,需要把之前安裝的node卸載了并且刪除類似npmrc這樣的文件或者文件夾,網(wǎng)上一搜一大把的說明文檔。
4.準(zhǔn)備electron容器并npm run start


下載下來后是這樣的。

把前面準(zhǔn)備的dist文件夾復(fù)制到根目錄中來,像下面這樣。

修改main.js的load路徑。

修改完執(zhí)行npm run start就能看到打包后的效果了,需要屏蔽操作欄或者默認(rèn)最大化之類的可以看看官方手冊的BrowserWindow配置內(nèi)容。
官方手冊:BrowserWindow
下面貼一下我自己的。
// Modules to control application life and create native browser window
const { app, BrowserWindow,Menu } = require('electron')
const path = require('node:path')
function createWindow () {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
},
minimizable: false,//窗口是否可最小化
fullscreen: false,//是否全屏展示:沒有窗口
})
mainWindow.maximize();//窗口最大化展示
// and load the index.html of the app.
mainWindow.loadFile('./dist/index.html')
Menu.setApplicationMenu(null);//去掉默認(rèn)的操作欄
// Open the DevTools.開發(fā)者工具是否打開
// 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()
})
// 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.5.封裝成可執(zhí)行程序
5.1.手動(dòng)下載electron對應(yīng)版本的zip文件,解決打包緩慢問題
下載地址:electron zip文件
新建cache文件夾,把壓縮包放進(jìn)去,如下。


5.2.安裝packager
npm install electron-packager

5.3.配置打包命令執(zhí)行內(nèi)容
"scripts": {
"packager:win": "electron-packager ./ winApp --platform=win32 --arch=x64 --overwrite --no-prune --ignore=/node_modules",
"packager:linux-x64": "electron-packager ./ linuxApp --platform=linux --arch=x64 --overwrite --no-prune --ignore=/node_modules",
"packager:linux-arm64": "electron-packager ./ linuxApp --platform=linux --arch=arm64 --overwrite --no-prune --ignore=/node_modules"
},
5.4.修改electron-packager源碼
找到electron-packager的src文件夾下面的index.js搜一下packageForPlatformAndArchWithOpts方法,替換為下面代碼塊的內(nèi)容。

async packageForPlatformAndArchWithOpts (comboOpts, downloadOpts) {
// const zipPath = await this.getElectronZipPath(downloadOpts) ---
const arch = downloadOpts.arch // +++
const zipPath = arch === 'arm64' ? './cache/electron-v22.0.0-linux-arm64.zip' : './cache/electron-v22.0.0-linux-x64.zip' // +++
if (!this.useTempDir) {
return this.createApp(comboOpts, zipPath)
}
if (common.isPlatformMac(comboOpts.platform)) {
/* istanbul ignore else */
if (this.canCreateSymlinks === undefined) {
return this.testSymlink(comboOpts, zipPath)
} else if (!this.canCreateSymlinks) {
return this.skipHostPlatformSansSymlinkSupport(comboOpts)
}
}
return this.checkOverwrite(comboOpts, zipPath)
}替換:

5.5.執(zhí)行打包命令
npm run packager:linux-arm64
總結(jié)
以上就是electron打包dist為可執(zhí)行程序的實(shí)現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于electron打包dist的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript?canvas?實(shí)現(xiàn)用代碼畫畫
這篇文章主要為大家介紹了JavaScript?canvas?實(shí)現(xiàn)用代碼畫畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
JavaScript 實(shí)現(xiàn)文件跳轉(zhuǎn)方法示例詳解
JavaScript頁面跳轉(zhuǎn)方法包括window.location、超鏈接、meta標(biāo)簽、history API和表單提交,需區(qū)分相對路徑與絕對路徑,單頁應(yīng)用推薦使用路由庫(如React Router),選擇方式應(yīng)結(jié)合需求與項(xiàng)目架構(gòu),本文給大家介紹JavaScript 文件跳轉(zhuǎn)方法,感興趣的朋友一起看看吧2025-08-08
JavaScript實(shí)現(xiàn)表格表單的隨機(jī)選擇和簡單的隨機(jī)點(diǎn)名
本文主要介紹了JavaScript實(shí)現(xiàn)表格表單的隨機(jī)選擇和簡單的隨機(jī)點(diǎn)名,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
javascript:json數(shù)據(jù)的頁面綁定示例代碼
本篇文章主要是對javascript:json數(shù)據(jù)的頁面綁定示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
JS題解leetcode去掉最低工資和最高工資后的工資平均值
這篇文章主要為大家介紹了JS題解leetcode去掉最低工資和最高工資后的工資平均值,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
js計(jì)時(shí)事件實(shí)現(xiàn)圓形時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了js計(jì)時(shí)事件實(shí)現(xiàn)圓形時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10

