Skip to content

Commit 7eb406c

Browse files
committed
Move electron import to a preload script
1 parent e1fc902 commit 7eb406c

File tree

9 files changed

+19
-26
lines changed

9 files changed

+19
-26
lines changed

package-lock.json

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
"name": "nwb-guide",
33
"productName": "NWB GUIDE",
44
"version": "0.0.10",
5-
"private": true,
65
"description": "",
76
"main": "./build/main/main.js",
87
"engine": {
98
"node": ">=18"
109
},
11-
"workspaces": [
12-
"src/renderer"
13-
],
1410
"scripts": {
1511
"start": "electron-vite dev --outDir build",
1612
"postinstall": "electron-builder install-app-deps",

src/main/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import './application-menu.js';
1919

2020
import icon from '../renderer/assets/img/logo-guide-draft.png?asset'
2121
import splashHTML from './splash-screen.html?asset'
22+
import preloadUrl from './../preload/preload.js?asset'
2223

2324
// autoUpdater.channel = "latest";
2425

@@ -242,7 +243,7 @@ function initialize() {
242243
enableRemoteModule: true,
243244
contextIsolation: false,
244245
sandbox: false,
245-
// preload: path.join(__dirname, "preload.js"),
246+
preload: path.join(preloadUrl),
246247
},
247248
};
248249

src/preload/preload.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
const electron = require('electron')
3+
4+
// Expose ipcRenderer
5+
if (process.contextIsolated) {
6+
try {
7+
electron.contextBridge.exposeInMainWorld('electron', electron)
8+
} catch (error) {
9+
console.error(error)
10+
}
11+
} else {
12+
globalThis.electron = electron
13+
}

src/preload/preload.ts

Whitespace-only changes.

src/renderer/package.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/renderer/src/electron/check.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
var userAgent = navigator.userAgent.toLowerCase();
22
export const isElectron = userAgent.indexOf(" electron/") > -1;
3-
43
export default isElectron;

src/renderer/src/electron/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import isElectron from "./check.js";
44
export { isElectron };
55

66
export let port = 4242;
7-
export let electron = {};
7+
export const electron = globalThis.electron ?? {}; // ipcRenderer, remote, shell, etc.
88
export let fs = null;
99
export let os = null;
1010
export let remote = {};
@@ -15,13 +15,9 @@ export let crypto = null;
1515

1616
if (isElectron) {
1717
try {
18-
// Import Electron API
19-
electron = require("electron"); // ipcRenderer, remote, shell, etc.
2018
fs = require("fs-extra"); // File System
2119
os = require("os");
22-
2320
crypto = require("crypto");
24-
2521
remote = require("@electron/remote");
2622
app = remote.app;
2723

src/renderer/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const appVersion = app?.getVersion();
2525

2626
const statusBar = new StatusBar({
2727
items: [
28-
{ label: unsafeSVG(webAssetSVG), value: appVersion ?? 'Web' },
28+
{ label: unsafeSVG(webAssetSVG), value: isElectron ? appVersion ?? 'ERROR' : 'Web' },
2929
{ label: unsafeSVG(wifiSVG) },
3030
{ label: unsafeSVG(serverSVG) }
3131
]

0 commit comments

Comments
 (0)