From 7eb406cc1bb8ec0829a95d41856ca5844b7f5ff4 Mon Sep 17 00:00:00 2001 From: Garrett Date: Thu, 5 Oct 2023 10:40:10 +0200 Subject: [PATCH] Move electron import to a preload script --- package-lock.json | 9 ++------- package.json | 4 ---- src/main/main.ts | 3 ++- src/preload/preload.js | 13 +++++++++++++ src/preload/preload.ts | 0 src/renderer/package.json | 7 ------- src/renderer/src/electron/check.js | 1 - src/renderer/src/electron/index.js | 6 +----- src/renderer/src/index.ts | 2 +- 9 files changed, 19 insertions(+), 26 deletions(-) create mode 100644 src/preload/preload.js delete mode 100644 src/preload/preload.ts delete mode 100644 src/renderer/package.json diff --git a/package-lock.json b/package-lock.json index d90459be0..5d07fc752 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,6 @@ "version": "0.0.10", "hasInstallScript": true, "license": "MIT", - "workspaces": [ - "src/renderer" - ], "dependencies": { "@electron/remote": "^2.0.11", "@fortawesome/fontawesome-free": "^5.15.3", @@ -15832,10 +15829,6 @@ "node": "*" } }, - "node_modules/nwb-guide-frontend": { - "resolved": "src/renderer", - "link": true - }, "node_modules/nwsapi": { "version": "2.2.7", "resolved": "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.7.tgz", @@ -20933,11 +20926,13 @@ } }, "src": { + "extraneous": true, "dependencies": { "electron": "^26.2.2" } }, "src/renderer": { + "extraneous": true, "dependencies": { "electron": "^26.2.2" } diff --git a/package.json b/package.json index 7f0b8aaf4..7144bbe1e 100644 --- a/package.json +++ b/package.json @@ -2,15 +2,11 @@ "name": "nwb-guide", "productName": "NWB GUIDE", "version": "0.0.10", - "private": true, "description": "", "main": "./build/main/main.js", "engine": { "node": ">=18" }, - "workspaces": [ - "src/renderer" - ], "scripts": { "start": "electron-vite dev --outDir build", "postinstall": "electron-builder install-app-deps", diff --git a/src/main/main.ts b/src/main/main.ts index 7feb5b6da..5873b6308 100755 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -19,6 +19,7 @@ import './application-menu.js'; import icon from '../renderer/assets/img/logo-guide-draft.png?asset' import splashHTML from './splash-screen.html?asset' +import preloadUrl from './../preload/preload.js?asset' // autoUpdater.channel = "latest"; @@ -242,7 +243,7 @@ function initialize() { enableRemoteModule: true, contextIsolation: false, sandbox: false, - // preload: path.join(__dirname, "preload.js"), + preload: path.join(preloadUrl), }, }; diff --git a/src/preload/preload.js b/src/preload/preload.js new file mode 100644 index 000000000..0448d3170 --- /dev/null +++ b/src/preload/preload.js @@ -0,0 +1,13 @@ + +const electron = require('electron') + +// Expose ipcRenderer +if (process.contextIsolated) { + try { + electron.contextBridge.exposeInMainWorld('electron', electron) + } catch (error) { + console.error(error) + } + } else { + globalThis.electron = electron + } \ No newline at end of file diff --git a/src/preload/preload.ts b/src/preload/preload.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/renderer/package.json b/src/renderer/package.json deleted file mode 100644 index eafeb8053..000000000 --- a/src/renderer/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "nwb-guide-frontend", - "type": "module", - "dependencies": { - "electron": "^26.2.2" - } -} \ No newline at end of file diff --git a/src/renderer/src/electron/check.js b/src/renderer/src/electron/check.js index a8e9ab7dd..ba7d2b583 100644 --- a/src/renderer/src/electron/check.js +++ b/src/renderer/src/electron/check.js @@ -1,4 +1,3 @@ var userAgent = navigator.userAgent.toLowerCase(); export const isElectron = userAgent.indexOf(" electron/") > -1; - export default isElectron; diff --git a/src/renderer/src/electron/index.js b/src/renderer/src/electron/index.js index 4a9a7326a..90acb3eda 100644 --- a/src/renderer/src/electron/index.js +++ b/src/renderer/src/electron/index.js @@ -4,7 +4,7 @@ import isElectron from "./check.js"; export { isElectron }; export let port = 4242; -export let electron = {}; +export const electron = globalThis.electron ?? {}; // ipcRenderer, remote, shell, etc. export let fs = null; export let os = null; export let remote = {}; @@ -15,13 +15,9 @@ export let crypto = null; if (isElectron) { try { - // Import Electron API - electron = require("electron"); // ipcRenderer, remote, shell, etc. fs = require("fs-extra"); // File System os = require("os"); - crypto = require("crypto"); - remote = require("@electron/remote"); app = remote.app; diff --git a/src/renderer/src/index.ts b/src/renderer/src/index.ts index 7fa37ac61..a0ef0da74 100644 --- a/src/renderer/src/index.ts +++ b/src/renderer/src/index.ts @@ -25,7 +25,7 @@ const appVersion = app?.getVersion(); const statusBar = new StatusBar({ items: [ - { label: unsafeSVG(webAssetSVG), value: appVersion ?? 'Web' }, + { label: unsafeSVG(webAssetSVG), value: isElectron ? appVersion ?? 'ERROR' : 'Web' }, { label: unsafeSVG(wifiSVG) }, { label: unsafeSVG(serverSVG) } ]