Skip to content

Commit

Permalink
Move electron import to a preload script
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmflynn committed Oct 5, 2023
1 parent e1fc902 commit 7eb406c
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 26 deletions.
9 changes: 2 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -242,7 +243,7 @@ function initialize() {
enableRemoteModule: true,
contextIsolation: false,
sandbox: false,
// preload: path.join(__dirname, "preload.js"),
preload: path.join(preloadUrl),
},
};

Expand Down
13 changes: 13 additions & 0 deletions src/preload/preload.js
Original file line number Diff line number Diff line change
@@ -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
}
Empty file removed src/preload/preload.ts
Empty file.
7 changes: 0 additions & 7 deletions src/renderer/package.json

This file was deleted.

1 change: 0 additions & 1 deletion src/renderer/src/electron/check.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var userAgent = navigator.userAgent.toLowerCase();
export const isElectron = userAgent.indexOf(" electron/") > -1;

export default isElectron;
6 changes: 1 addition & 5 deletions src/renderer/src/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
]
Expand Down

0 comments on commit 7eb406c

Please sign in to comment.