Skip to content

Commit

Permalink
Electron preload scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Jun 30, 2023
1 parent bcdaec9 commit e46aee6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/packager/electron-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const {contextBridge} = require('electron')

contextBridge.exposeInMainWorld('ExampleAPI', {
doSomething: () => {
return 'Hello!';
}
});
8 changes: 6 additions & 2 deletions src/packager/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {APP_NAME, WEBSITE, COPYRIGHT_NOTICE, ACCENT_COLOR} from './brand';
import {OutdatedPackagerError} from '../common/errors';
import {darken} from './colors';
import {Adapter} from './adapter';
import electronPreloadJS from '!raw-loader!./electron-preload.js';

const PROGRESS_LOADED_SCRIPTS = 0.1;

Expand Down Expand Up @@ -435,7 +436,7 @@ cd "$(dirname "$0")"
// +-- resources
// +-- default_app.asar (we will delete this)
// +-- app (we will create this)
// +-- index.html and the other project files (we will create this)
// +-- index.html, preload.js, and the other project files (we will create these)
// +-- LICENSES.chromium.html and everything else

// Electron macOS folder structure:
Expand Down Expand Up @@ -463,7 +464,7 @@ cd "$(dirname "$0")"
// +-- default_app.asar (we will delete this)
// +-- electron.icns (we will update this)
// +-- app (we will create this)
// +-- index.html and the other project files (we will create this)
// +-- index.html, preload.js, and the other project files (we will create these)
// +-- LICENSES.chromium.html and other license files

const zip = new (await getJSZip());
Expand Down Expand Up @@ -512,6 +513,7 @@ cd "$(dirname "$0")"
const contentsPrefix = isMac ? `${rootPrefix}${packageName}.app/Contents/` : rootPrefix;
const resourcesPrefix = isMac ? `${contentsPrefix}Resources/app/` : `${contentsPrefix}resources/app/`;
const electronMainName = 'electron-main.js';
const electronPreloadName = 'electron-preload.js';
const iconName = 'icon.png';

const icon = await Adapter.getAppIcon(this.options.app.icon);
Expand Down Expand Up @@ -558,6 +560,7 @@ const createWindow = (windowOptions) => {
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
preload: path.resolve(__dirname, ${JSON.stringify(electronPreloadName)}),
},
show: true,
width: 480,
Expand Down Expand Up @@ -697,6 +700,7 @@ app.whenReady().then(() => {
});
`;
zip.file(`${resourcesPrefix}${electronMainName}`, mainJS);
zip.file(`${resourcesPrefix}${electronPreloadName}`, electronPreloadJS);

for (const [path, data] of Object.entries(projectZip.files)) {
setFileFast(zip, `${resourcesPrefix}${path}`, data);
Expand Down

0 comments on commit e46aee6

Please sign in to comment.