Unverified Commit d509c2d5 authored by Dylan700's avatar Dylan700 Committed by GitHub

examples: Update Electron Typescript Example with Best Practices (#58947)

Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent 92f7d0c5
# Electron with Typescript application example # Electron with Typescript application example
This example show how you can use Next.js inside an Electron application to avoid a lot of configuration, use Next.js router as view and use server-render to speed up the initial render of the application. Both Next.js and Electron layers are written in TypeScript and compiled to JavaScript during the build process. This example shows how to use Next.js inside an Electron application. To avoid a lot of configuration, we use Next.js as a router for pages, and use server rendering to speed up the initial render of the application. Both Next.js and Electron layers are written in TypeScript and compiled to JavaScript during the build process.
| Part | Source code (Typescript) | Builds (JavaScript) | | Part | Source code (Typescript) | Builds (JavaScript) |
| ---------- | ------------------------ | ------------------- | | ---------- | ------------------------ | ------------------- |
...@@ -8,7 +8,7 @@ This example show how you can use Next.js inside an Electron application to avoi ...@@ -8,7 +8,7 @@ This example show how you can use Next.js inside an Electron application to avoi
| Electron | `/electron-src` | `/main` | | Electron | `/electron-src` | `/main` |
| Production | | `/dist` | | Production | | `/dist` |
For development it's going to run a HTTP server and let Next.js handle routing. In production it use `output: 'export'` to pre-generate HTML static files and use them in your app instead of running an HTTP server. For development it's going to run a HTTP server and let Next.js handle routing. In production it will use `output: 'export'` to pre-generate HTML static files and use them in your app (instead of running a HTTP server).
## How to use ## How to use
......
...@@ -16,7 +16,7 @@ app.on('ready', async () => { ...@@ -16,7 +16,7 @@ app.on('ready', async () => {
height: 600, height: 600,
webPreferences: { webPreferences: {
nodeIntegration: false, nodeIntegration: false,
contextIsolation: false, contextIsolation: true,
preload: join(__dirname, 'preload.js'), preload: join(__dirname, 'preload.js'),
}, },
}) })
......
/* eslint-disable @typescript-eslint/no-namespace */ /* eslint-disable @typescript-eslint/no-namespace */
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import { ipcRenderer, IpcRenderer } from 'electron' import { contextBridge, ipcRenderer } from 'electron'
import { IpcRendererEvent } from 'electron/main'
declare global { // We are using the context bridge to securely expose NodeAPIs.
namespace NodeJS { // Please note that many Node APIs grant access to local system resources.
interface Global { // Be very cautious about which globals and APIs you expose to untrusted remote content.
ipcRenderer: IpcRenderer contextBridge.exposeInMainWorld('electron', {
} sayHello: () => ipcRenderer.send('message', 'hi from next'),
} receiveHello: (handler: (event: IpcRendererEvent, ...args: any[]) => void) =>
} ipcRenderer.on('message', handler),
stopReceivingHello: (
// Since we disabled nodeIntegration we can reintroduce handler: (event: IpcRendererEvent, ...args: any[]) => void
// needed node functionality here ) => ipcRenderer.removeListener('message', handler),
process.once('loaded', () => {
global.ipcRenderer = ipcRenderer
}) })
...@@ -13,20 +13,20 @@ ...@@ -13,20 +13,20 @@
"type-check": "tsc -p ./renderer/tsconfig.json && tsc -p ./electron-src/tsconfig.json" "type-check": "tsc -p ./renderer/tsconfig.json && tsc -p ./electron-src/tsconfig.json"
}, },
"dependencies": { "dependencies": {
"electron-is-dev": "^1.1.0", "electron-is-dev": "^1.2.0",
"electron-next": "^3.1.5", "electron-next": "^3.1.5",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.6", "@types/node": "^14.18.63",
"@types/react": "^16.9.9", "@types/react": "^16.14.52",
"@types/react-dom": "^16.9.9", "@types/react-dom": "^16.9.24",
"electron": "^13", "electron": "^27.1.2",
"electron-builder": "^23.0.3", "electron-builder": "^24.9.1",
"next": "latest", "next": "latest",
"rimraf": "^3.0.0", "rimraf": "^3.0.2",
"typescript": "^4.0.5" "typescript": "^4.9.5"
}, },
"build": { "build": {
"asar": true, "asar": true,
......
...@@ -4,13 +4,14 @@ ...@@ -4,13 +4,14 @@
// //
// import User from 'path/to/interfaces'; // import User from 'path/to/interfaces';
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import { IpcRenderer } from 'electron'
declare global { declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace // eslint-disable-next-line @typescript-eslint/no-namespace
namespace NodeJS { interface Window {
interface Global { electron: {
ipcRenderer: IpcRenderer sayHello: () => void
receiveHello: (handler: (event, args) => void) => void
stopReceivingHello: (handler: (event, args) => void) => void
} }
} }
} }
......
...@@ -6,16 +6,16 @@ const IndexPage = () => { ...@@ -6,16 +6,16 @@ const IndexPage = () => {
useEffect(() => { useEffect(() => {
const handleMessage = (_event, args) => alert(args) const handleMessage = (_event, args) => alert(args)
// add a listener to 'message' channel // listen to the 'message' channel
global.ipcRenderer.addListener('message', handleMessage) window.electron.receiveHello(handleMessage)
return () => { return () => {
global.ipcRenderer.removeListener('message', handleMessage) window.electron.stopReceivingHello(handleMessage)
} }
}, []) }, [])
const onSayHiClick = () => { const onSayHiClick = () => {
global.ipcRenderer.send('message', 'hi from next') window.electron.sayHello()
} }
return ( return (
......
...@@ -12,8 +12,9 @@ ...@@ -12,8 +12,9 @@
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"jsx": "preserve" "jsx": "preserve",
"incremental": true
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"], "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "../next.config.js"],
"exclude": ["node_modules"] "exclude": ["node_modules"]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment