Skip to content

Commit e46aee6

Browse files
committed
Electron preload scaffolding
1 parent bcdaec9 commit e46aee6

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/packager/electron-preload.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const {contextBridge} = require('electron')
2+
3+
contextBridge.exposeInMainWorld('ExampleAPI', {
4+
doSomething: () => {
5+
return 'Hello!';
6+
}
7+
});

src/packager/packager.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {APP_NAME, WEBSITE, COPYRIGHT_NOTICE, ACCENT_COLOR} from './brand';
1111
import {OutdatedPackagerError} from '../common/errors';
1212
import {darken} from './colors';
1313
import {Adapter} from './adapter';
14+
import electronPreloadJS from '!raw-loader!./electron-preload.js';
1415

1516
const PROGRESS_LOADED_SCRIPTS = 0.1;
1617

@@ -435,7 +436,7 @@ cd "$(dirname "$0")"
435436
// +-- resources
436437
// +-- default_app.asar (we will delete this)
437438
// +-- app (we will create this)
438-
// +-- index.html and the other project files (we will create this)
439+
// +-- index.html, preload.js, and the other project files (we will create these)
439440
// +-- LICENSES.chromium.html and everything else
440441

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

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

517519
const icon = await Adapter.getAppIcon(this.options.app.icon);
@@ -558,6 +560,7 @@ const createWindow = (windowOptions) => {
558560
sandbox: true,
559561
contextIsolation: true,
560562
nodeIntegration: false,
563+
preload: path.resolve(__dirname, ${JSON.stringify(electronPreloadName)}),
561564
},
562565
show: true,
563566
width: 480,
@@ -697,6 +700,7 @@ app.whenReady().then(() => {
697700
});
698701
`;
699702
zip.file(`${resourcesPrefix}${electronMainName}`, mainJS);
703+
zip.file(`${resourcesPrefix}${electronPreloadName}`, electronPreloadJS);
700704

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

0 commit comments

Comments
 (0)