Skip to content

Commit b67d500

Browse files
committed
Merge branch 'migration/electron-preload' into develop
2 parents 20db57e + c7a86c4 commit b67d500

File tree

9 files changed

+66
-11
lines changed

9 files changed

+66
-11
lines changed

src/main/main.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import * as path from 'path';
55
import * as url from 'url';
66
import * as nodeEnv from '_utils/node-env';
77
// eslint-disable-next-line import/no-extraneous-dependencies
8-
import { BrowserWindow, app } from 'electron';
8+
import { BrowserWindow, app, ipcMain } from 'electron';
99

10-
let mainWindow: Electron.BrowserWindow | null = null;
10+
let mainWindow: Electron.BrowserWindow | undefined;
1111

1212
function createWindow(): void {
1313
// Create the browser window.
1414
mainWindow = new BrowserWindow({
1515
height: 600,
1616
width: 800,
1717
webPreferences: {
18-
webSecurity: false,
1918
devTools: nodeEnv.dev,
19+
preload: path.join(__dirname, './preload.bundle.js'),
20+
webSecurity: false,
2021
},
2122
});
2223

@@ -34,7 +35,7 @@ function createWindow(): void {
3435
// Dereference the window object, usually you would store windows
3536
// in an array if your app supports multi windows, this is the time
3637
// when you should delete the corresponding element.
37-
mainWindow = null;
38+
mainWindow = undefined;
3839
});
3940
}
4041

@@ -55,11 +56,16 @@ app.on('window-all-closed', () => {
5556
app.on('activate', () => {
5657
// On OS X it"s common to re-create a window in the app when the
5758
// dock icon is clicked and there are no other windows open.
58-
if (mainWindow === null) {
59+
if (mainWindow === undefined) {
5960
createWindow();
6061
}
6162
});
6263

64+
ipcMain.on('renderer-ready', () => {
65+
// eslint-disable-next-line no-console
66+
console.log('Renderer is ready.');
67+
});
68+
6369
// In this file you can include the rest of your app"s specific main process
6470
// code. You can also put them in separate files and require them here.
6571

src/preload/ipc-api.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
2+
import { ipcRenderer } from 'electron';
3+
4+
/** Notify main the renderer is ready. */
5+
function rendererReady() {
6+
ipcRenderer.send('renderer-ready');
7+
}
8+
9+
export default { rendererReady };

src/preload/preload.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ipcAPI from '_preload/ipc-api';
2+
// eslint-disable-next-line import/no-extraneous-dependencies
3+
import { contextBridge } from 'electron';
4+
5+
contextBridge.exposeInMainWorld('ipcAPI', ipcAPI);

src/renderer/App.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as React from 'react';
2+
3+
function App(): JSX.Element {
4+
React.useEffect(() => {
5+
window.ipcAPI?.rendererReady();
6+
}, []);
7+
8+
return (
9+
<div className="app">
10+
<h4>Welcome to React, Electron and TypeScript</h4>
11+
<p>Hello</p>
12+
</div>
13+
);
14+
}
15+
16+
export default App;

src/renderer/renderer.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
// Import the styles here to process them with webpack
55
import '_public/style.css';
66

7+
import App from '_renderer/App';
78
import * as React from 'react';
89
import * as ReactDOM from 'react-dom';
910

1011
ReactDOM.render(
11-
<div className="app">
12-
<h4>Welcome to React, Electron and Typescript</h4>
13-
<p>Hello</p>
14-
</div>,
12+
<App />,
1513
document.getElementById('app'),
1614
);

src/types/global.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare global {
2+
interface Window {
3+
/** APIs for Electron IPC */
4+
ipcAPI?: typeof import('_preload/ipc-api').default
5+
}
6+
}
7+
8+
// Makes TS sees this as an external modules so we can extend the global scope.
9+
export { };

tests/main/main.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { exportedForTests } from '_main/main';
22

33
jest.mock('electron', () => ({
44
app: { on: jest.fn() },
5+
ipcMain: { on: jest.fn() },
56
}));
67

78
test('Private props exported for unit tests', () => {

tsconfig.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
"_public/*": ["public/*"],
99
"_main/*": ["src/main/*"],
1010
"_models/*": ["src/models/*"],
11+
"_preload/*": ["src/preload/*"],
1112
"_renderer/*": ["src/renderer/*"],
13+
"_types/*": ["src/types/*"],
1214
"_utils/*": ["src/utils/*"],
1315
"_tests/*": ["tests/*"]
1416
},
17+
"typeRoots": [
18+
"./node_modules/@types",
19+
"./src/types"
20+
],
1521
"module": "commonjs",
1622
"moduleResolution": "node",
1723
"noImplicitAny": true,
@@ -20,5 +26,5 @@
2026
"sourceMap": true,
2127
"target": "esnext"
2228
},
23-
"include": ["src/**/*"]
29+
"include": ["src/**/*", "tests/**/*"]
2430
}

webpack.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ mainConfig.plugins = [
7575
}),
7676
];
7777

78+
const preloadConfig = lodash.cloneDeep(commonConfig);
79+
preloadConfig.entry = './src/preload/preload.ts';
80+
preloadConfig.target = 'electron-preload';
81+
preloadConfig.output.filename = 'preload.bundle.js';
82+
7883
const rendererConfig = lodash.cloneDeep(commonConfig);
7984
rendererConfig.entry = './src/renderer/renderer.tsx';
8085
rendererConfig.target = 'electron-renderer';
@@ -85,4 +90,4 @@ rendererConfig.plugins = [
8590
}),
8691
];
8792

88-
module.exports = [mainConfig, rendererConfig];
93+
module.exports = [mainConfig, preloadConfig, rendererConfig];

0 commit comments

Comments
 (0)