electron打包dist為可執(zhí)行程序的實現(xiàn)步驟
前言
甲方爹:BS=>CS?
我方領導:OJBK。
項目是普普通通的vue項目,要求封裝成arm64的Linux可執(zhí)行程序。
提示:以下是本篇文章正文內容,下面案例可供參考
一、直接看效果
二、實現(xiàn)步驟
1.準備dist文件夾
publicPath得是./,不然打包出來的dist跑起來是空白的,雙擊index.html能在瀏覽器中看到頁面。
2.修改接口映射
接口請求映射關系修改,如果不修改映射關系,接口請求會變成通過file:///協(xié)議訪問。我看有的人說把項目里面的接口都替換寫死,wo...
修改一下.env.production
生產(chǎn)環(huán)境配置文件中VUE_APP_BASE_API的值為你的生產(chǎn)環(huán)境要訪問的接口就行,格式為:http://ip地址:端口號。這里是vue.config.js的proxy和request.js的請求配置的變量配置。
3.NVM管理node版本
項目框架比較成熟,electron-quick-start比較新,中間遇到版本不兼容,一個16一個20。所以需要用NVM管理node版本,執(zhí)行構建命令的時候切一下。
注意:通過NVM install的node不能直接切,需要把之前安裝的node卸載了并且刪除類似npmrc這樣的文件或者文件夾,網(wǎng)上一搜一大把的說明文檔。
4.準備electron容器并npm run start
下載下來后是這樣的。
把前面準備的dist文件夾復制到根目錄中來,像下面這樣。
修改main.js的load路徑。
修改完執(zhí)行npm run start就能看到打包后的效果了,需要屏蔽操作欄或者默認最大化之類的可以看看官方手冊的BrowserWindow配置內容
。
官方手冊: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);//去掉默認的操作欄 // 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.手動下載electron對應版本的zip文件,解決打包緩慢問題
下載地址:electron zip文件
新建cache文件夾,把壓縮包放進去,如下。
5.2.安裝packager
npm install electron-packager
5.3.配置打包命令執(zhí)行內容
"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方法,替換為下面代碼塊的內容。
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
總結
以上就是electron打包dist為可執(zhí)行程序的實現(xiàn)步驟的詳細內容,更多關于electron打包dist的資料請關注腳本之家其它相關文章!
相關文章
LayUi中接口傳數(shù)據(jù)成功,表格不顯示數(shù)據(jù)的解決方法
今天小編就為大家分享一篇LayUi中接口傳數(shù)據(jù)成功,表格不顯示數(shù)據(jù)的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08JavaScript獲取css行間樣式,內連樣式和外鏈樣式的簡單方法
下面小編就為大家?guī)硪黄狫avaScript獲取css行間樣式,內連樣式和外鏈樣式的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07JavaScript代碼因逗號不規(guī)范導致IE不兼容的問題
這篇文章主要介紹了JavaScript代碼因逗號不規(guī)范導致IE不兼容的問題的相關資料,需要的朋友可以參考下2016-02-02JS+CSS實現(xiàn)網(wǎng)頁加載中的動畫效果
這篇文章主要為大家詳細介紹了JS+CSS實現(xiàn)網(wǎng)頁加載中的動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10