升級 Electron
當您第一次在 Quasar 專案中新增 Electron 模式時,您將獲得最新版本的 Electron 套件。在某些時候,您會想要升級 Electron 版本。
在升級 Electron 之前,請查閱其發行說明。是否有重大變更?
# from the root of your Quasar project
$ yarn upgrade electron@latest
從 Quasar v1 升級
Quasar v2 的 Electron 模式幾乎完全翻新了先前的版本,大幅改善了開發人員體驗。此處的一些變更是為了確保與 Electron 領域的最新發展相容(因此可以防止即將到來的上游變更)。
改善項目的高階概述
- 立即支援 Typescript。只需將 electron-main.js 和 electron-preload.js 重新命名為 electron-main.ts 和 electron-preload.ts 即可。
- 支援 Electron 11,並準備立即支援 Electron 12+ 中即將到來的變更(未來您無需變更任何內容)。其中一項變更是我們將使用
contextIsolation
而非已棄用的Node Integration
。 - 預載腳本不再有舊的限制。您可以使用相對路徑匯入其他 js 檔案,因為腳本現在已捆綁並通過 Babel 處理(因此您也可以使用
import X from Y
語法)。您也可以為其啟用 linting。 - 您也可以為主執行緒和預載腳本啟用 linting。
- 我們已移除預設的 electron-main.dev.js 支援,因為它似乎不再需要。但是,您可以通過建立它並從 electron-main 引用它來重新新增它(Quasar CLI 不再自動偵測到它 – 因為我們不需要;稍後會詳細說明)。
/src-electron 資料夾
舊結構是
新結構是
請注意,不再有 electron-main.dev.js
檔案(不再需要),並且 electron-preload/main.js
檔案需要直接移至 /src-electron
下。
electron-main.js 檔案
為了讓我們與未來版本的 Electron 向前相容,您需要進行一些小的(但很重要!)變更
mainWindow = new BrowserWindow({
// ...
webPreferences: {
nodeIntegration: process.env.QUASAR_NODE_INTEGRATION,
nodeIntegrationInWorker: process.env.QUASAR_NODE_INTEGRATION,
// preload: path.resolve(__dirname, 'electron-preload.js')
}
})
mainWindow = new BrowserWindow({
// ...
webPreferences: {
// we enable contextIsolation (Electron 12+ has it enabled by default anyway)
contextIsolation: true,
// we use a new way to reference the preload script
// (it's going to be needed, so add it and create the file if it's not there already)
preload: path.resolve(__dirname, process.env.QUASAR_ELECTRON_PRELOAD)
}
})
electron-preload.js 檔案
如果您還沒有這個檔案,您將需要它。因此,如果它遺失了,請建立它。沒有它,您將無法在渲染器執行緒中使用 Node.js 的強大功能。
更多資訊:預載腳本。
警告
您需要將所有 Node.js 物件從渲染器執行緒(/src 中的 UI 程式碼)移轉到預載腳本中。通過 contextBridge
提供相同的功能,如下所示。
這是 electron-preload.js
的預設內容
/**
* This file is used specifically for security reasons.
* Here you can access Nodejs stuff and inject functionality into
* the renderer thread (accessible there through the "window" object)
*
* WARNING!
* If you import anything from node_modules, then make sure that the package is specified
* in package.json > "dependencies" and NOT in "devDependencies"
*
* Example (injects window.myAPI.doAThing() into renderer thread):
*
* const { contextBridge } = require('electron')
*
* contextBridge.exposeInMainWorld('myAPI', {
* doAThing: () => {}
* })
*/
quasar.config 檔案變更
electron: {
// it's gone now (upcoming upstream breaking change)
// replaced by a change in electron-main.js documented earlier
nodeIntegration: true, // remove me!
// renamed to chainWebpackMain
chainWebpack (chain) { /* ... */ },
// renamed to extendWebpackMain
extendWebpack (cfg) { /* ... */ }
}
electron: {
// was renamed from chainWebpack()
chainWebpackMain (chain) {
// example for its content (adds linting)
chain.plugin('eslint-webpack-plugin')
.use(ESLintPlugin, [{ extensions: ['js'] }])
},
// was renamed from extendWebpack()
extendWebpackMain (cfg) { /* ... */ },
// New!
chainWebpackPreload (chain) {
// example (adds linting)
chain.plugin('eslint-webpack-plugin')
.use(ESLintPlugin, [{ extensions: ['js'] }])
}
// New!
extendWebpackPreload (cfg) { /* ... */ }
}
渲染器執行緒 (/src)
$q 物件 不再包含 electron
屬性。您需要使用預載腳本來存取它並將其提供給渲染器執行緒。
此外,openURL 工具程式不再能利用 Electron 開啟新視窗。您需要從預載腳本提供您自己的工具程式。
警告
您需要將所有 Node.js 物件從渲染器執行緒(/src 中的 UI 程式碼)移轉到預載腳本中。通過 contextBridge
提供相同的功能,如上面的預載腳本章節所示。
瀏覽器開發人員工具
您可能還希望在您的 electron-main.js 中加入以下程式碼,以便在開發模式(或啟用偵錯的生產模式)下自動開啟開發人員工具,並在生產版本(未啟用偵錯)中停用開發人員工具
function createWindow () {
mainWindow = new BrowserWindow({ /* ... */ })
if (process.env.DEBUGGING) {
// if on DEV or Production with debug enabled
mainWindow.webContents.openDevTools()
}
else {
// we're on production; no access to devtools pls
mainWindow.webContents.on('devtools-opened', () => {
mainWindow.webContents.closeDevTools()
})
}
}