feat: bumps electron@28, vite@5, vite-plugin-electron@0.28.0-beta.2
This commit is contained in:
1 parent
5303923340
commit
208ab386b1
12 files changed
+164
-54
No files matched your search
+10
-5
@@ -1,15 +1,19 @@
|
|||||||
import { app, BrowserWindow, shell, ipcMain } from 'electron'
|
import { app, BrowserWindow, shell, ipcMain } from 'electron'
|
||||||
import { release } from 'node:os'
|
import { release } from 'node:os'
|
||||||
import { join } from 'node:path'
|
import { dirname, join } from 'node:path'
|
||||||
|
import { fileURLToPath } from 'node:url'
|
||||||
import { update } from './update'
|
import { update } from './update'
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url)
|
||||||
|
const __dirname = dirname(__filename)
|
||||||
|
|
||||||
// The built directory structure
|
// The built directory structure
|
||||||
//
|
//
|
||||||
// ├─┬ dist-electron
|
// ├─┬ dist-electron
|
||||||
// │ ├─┬ main
|
// │ ├─┬ main
|
||||||
// │ │ └── index.js > Electron-Main
|
// │ │ └── index.js > Electron-Main
|
||||||
// │ └─┬ preload
|
// │ └─┬ preload
|
||||||
// │ └── index.js > Preload-Scripts
|
// │ └── index.mjs > Preload-Scripts
|
||||||
// ├─┬ dist
|
// ├─┬ dist
|
||||||
// │ └── index.html > Electron-Renderer
|
// │ └── index.html > Electron-Renderer
|
||||||
//
|
//
|
||||||
@@ -37,7 +41,7 @@ if (!app.requestSingleInstanceLock()) {
|
|||||||
|
|
||||||
let win: BrowserWindow | null = null
|
let win: BrowserWindow | null = null
|
||||||
// Here, you can also use other preload
|
// Here, you can also use other preload
|
||||||
const preload = join(__dirname, '../preload/index.js')
|
const preload = join(__dirname, '../preload/index.mjs')
|
||||||
const url = process.env.VITE_DEV_SERVER_URL
|
const url = process.env.VITE_DEV_SERVER_URL
|
||||||
const indexHtml = join(process.env.DIST, 'index.html')
|
const indexHtml = join(process.env.DIST, 'index.html')
|
||||||
|
|
||||||
@@ -48,10 +52,11 @@ async function createWindow() {
|
|||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload,
|
preload,
|
||||||
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
|
// Warning: Enable nodeIntegration and disable contextIsolation is not secure in production
|
||||||
|
// nodeIntegration: true,
|
||||||
|
|
||||||
// Consider using contextBridge.exposeInMainWorld
|
// Consider using contextBridge.exposeInMainWorld
|
||||||
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
|
// Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation
|
||||||
nodeIntegration: true,
|
// contextIsolation: false,
|
||||||
contextIsolation: false,
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { app, ipcMain } from 'electron'
|
import { app, ipcMain } from 'electron'
|
||||||
import {
|
import { createRequire } from 'node:module'
|
||||||
type ProgressInfo,
|
import type {
|
||||||
type UpdateDownloadedEvent,
|
ProgressInfo,
|
||||||
autoUpdater
|
UpdateDownloadedEvent,
|
||||||
} from 'electron-updater'
|
} from 'electron-updater'
|
||||||
|
|
||||||
|
const { autoUpdater } = createRequire(import.meta.url)('electron-updater');
|
||||||
|
|
||||||
export function update(win: Electron.BrowserWindow) {
|
export function update(win: Electron.BrowserWindow) {
|
||||||
|
|
||||||
// When set to false, the update download will be triggered through the API
|
// When set to false, the update download will be triggered through the API
|
||||||
|
|||||||
@@ -1,3 +1,28 @@
|
|||||||
|
import { ipcRenderer, contextBridge } from 'electron'
|
||||||
|
|
||||||
|
// --------- Expose some API to the Renderer process ---------
|
||||||
|
contextBridge.exposeInMainWorld('ipcRenderer', withPrototype(ipcRenderer))
|
||||||
|
|
||||||
|
// `exposeInMainWorld` can't detect attributes and methods of `prototype`, manually patching it.
|
||||||
|
function withPrototype(obj: Record<string, any>) {
|
||||||
|
const protos = Object.getPrototypeOf(obj)
|
||||||
|
|
||||||
|
for (const [key, value] of Object.entries(protos)) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(obj, key)) continue
|
||||||
|
|
||||||
|
if (typeof value === 'function') {
|
||||||
|
// Some native APIs, like `NodeJS.EventEmitter['on']`, don't work in the Renderer process. Wrapping them into a function.
|
||||||
|
obj[key] = function (...args: any) {
|
||||||
|
return value.call(obj, ...args)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
obj[key] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------- Preload scripts loading ---------
|
||||||
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
function domReady(condition: DocumentReadyState[] = ['complete', 'interactive']) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
if (condition.includes(document.readyState)) {
|
if (condition.includes(document.readyState)) {
|
||||||
|
|||||||
+4
-6
@@ -11,6 +11,7 @@
|
|||||||
"VITE_DEV_SERVER_URL": "http://127.0.0.1:7777/"
|
"VITE_DEV_SERVER_URL": "http://127.0.0.1:7777/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "tsc && vite build && electron-builder",
|
"build": "tsc && vite build && electron-builder",
|
||||||
@@ -27,18 +28,15 @@
|
|||||||
"@types/react-dom": "^18.2.7",
|
"@types/react-dom": "^18.2.7",
|
||||||
"@vitejs/plugin-react": "^4.0.4",
|
"@vitejs/plugin-react": "^4.0.4",
|
||||||
"autoprefixer": "^10.4.16",
|
"autoprefixer": "^10.4.16",
|
||||||
"electron": "^26.0.0",
|
"electron": "^28.1.0",
|
||||||
"electron-builder": "^24.6.3",
|
"electron-builder": "^24.6.3",
|
||||||
"postcss": "^8.4.31",
|
"postcss": "^8.4.31",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
"tailwindcss": "^3.3.3",
|
"tailwindcss": "^3.3.3",
|
||||||
"typescript": "^5.1.6",
|
"typescript": "^5.1.6",
|
||||||
"vite": "^4.4.9",
|
"vite": "^5.0.10",
|
||||||
"vite-plugin-electron": "^0.15.5",
|
"vite-plugin-electron": "^0.28.0-beta.2",
|
||||||
"vite-plugin-electron-renderer": "^0.14.5"
|
"vite-plugin-electron-renderer": "^0.14.5"
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": "^14.18.0 || >=16.0.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,8 +4,6 @@ import logoVite from './assets/logo-vite.svg'
|
|||||||
import logoElectron from './assets/logo-electron.svg'
|
import logoElectron from './assets/logo-electron.svg'
|
||||||
import './App.css'
|
import './App.css'
|
||||||
|
|
||||||
console.log('[App.tsx]', `Hello world from Electron ${process.versions.electron}!`)
|
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [count, setCount] = useState(0)
|
const [count, setCount] = useState(0)
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import { ipcRenderer } from 'electron'
|
|
||||||
import type { ProgressInfo } from 'electron-updater'
|
import type { ProgressInfo } from 'electron-updater'
|
||||||
import { useCallback, useEffect, useState } from 'react'
|
import { useCallback, useEffect, useState } from 'react'
|
||||||
import Modal from '@/components/update/Modal'
|
import Modal from '@/components/update/Modal'
|
||||||
@@ -19,7 +18,7 @@ const Update = () => {
|
|||||||
onOk?: () => void
|
onOk?: () => void
|
||||||
}>({
|
}>({
|
||||||
onCancel: () => setModalOpen(false),
|
onCancel: () => setModalOpen(false),
|
||||||
onOk: () => ipcRenderer.invoke('start-download'),
|
onOk: () => window.ipcRenderer.invoke('start-download'),
|
||||||
})
|
})
|
||||||
|
|
||||||
const checkUpdate = async () => {
|
const checkUpdate = async () => {
|
||||||
@@ -27,7 +26,7 @@ const Update = () => {
|
|||||||
/**
|
/**
|
||||||
* @type {import('electron-updater').UpdateCheckResult | null | { message: string, error: Error }}
|
* @type {import('electron-updater').UpdateCheckResult | null | { message: string, error: Error }}
|
||||||
*/
|
*/
|
||||||
const result = await ipcRenderer.invoke('check-update')
|
const result = await window.ipcRenderer.invoke('check-update')
|
||||||
setProgressInfo({ percent: 0 })
|
setProgressInfo({ percent: 0 })
|
||||||
setChecking(false)
|
setChecking(false)
|
||||||
setModalOpen(true)
|
setModalOpen(true)
|
||||||
@@ -46,7 +45,7 @@ const Update = () => {
|
|||||||
...state,
|
...state,
|
||||||
cancelText: 'Cancel',
|
cancelText: 'Cancel',
|
||||||
okText: 'Update',
|
okText: 'Update',
|
||||||
onOk: () => ipcRenderer.invoke('start-download'),
|
onOk: () => window.ipcRenderer.invoke('start-download'),
|
||||||
}))
|
}))
|
||||||
setUpdateAvailable(true)
|
setUpdateAvailable(true)
|
||||||
} else {
|
} else {
|
||||||
@@ -69,22 +68,22 @@ const Update = () => {
|
|||||||
...state,
|
...state,
|
||||||
cancelText: 'Later',
|
cancelText: 'Later',
|
||||||
okText: 'Install now',
|
okText: 'Install now',
|
||||||
onOk: () => ipcRenderer.invoke('quit-and-install'),
|
onOk: () => window.ipcRenderer.invoke('quit-and-install'),
|
||||||
}))
|
}))
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Get version information and whether to update
|
// Get version information and whether to update
|
||||||
ipcRenderer.on('update-can-available', onUpdateCanAvailable)
|
window.ipcRenderer.on('update-can-available', onUpdateCanAvailable)
|
||||||
ipcRenderer.on('update-error', onUpdateError)
|
window.ipcRenderer.on('update-error', onUpdateError)
|
||||||
ipcRenderer.on('download-progress', onDownloadProgress)
|
window.ipcRenderer.on('download-progress', onDownloadProgress)
|
||||||
ipcRenderer.on('update-downloaded', onUpdateDownloaded)
|
window.ipcRenderer.on('update-downloaded', onUpdateDownloaded)
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
ipcRenderer.off('update-can-available', onUpdateCanAvailable)
|
window.ipcRenderer.off('update-can-available', onUpdateCanAvailable)
|
||||||
ipcRenderer.off('update-error', onUpdateError)
|
window.ipcRenderer.off('update-error', onUpdateError)
|
||||||
ipcRenderer.off('download-progress', onDownloadProgress)
|
window.ipcRenderer.off('download-progress', onDownloadProgress)
|
||||||
ipcRenderer.off('update-downloaded', onUpdateDownloaded)
|
window.ipcRenderer.off('update-downloaded', onUpdateDownloaded)
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import { ipcRenderer } from 'electron'
|
||||||
|
|
||||||
|
ipcRenderer.on('main-process-message', (_event, ...args) => {
|
||||||
|
console.log('[Receive Main-process message]:', ...args)
|
||||||
|
})
|
||||||
@@ -1,10 +1,5 @@
|
|||||||
import { lstat } from 'node:fs/promises'
|
import { lstat } from 'node:fs/promises'
|
||||||
import { cwd } from 'node:process'
|
import { cwd } from 'node:process'
|
||||||
import { ipcRenderer } from 'electron'
|
|
||||||
|
|
||||||
ipcRenderer.on('main-process-message', (_event, ...args) => {
|
|
||||||
console.log('[Receive Main-process message]:', ...args)
|
|
||||||
})
|
|
||||||
|
|
||||||
lstat(cwd()).then(stats => {
|
lstat(cwd()).then(stats => {
|
||||||
console.log('[fs.lstat]', stats)
|
console.log('[fs.lstat]', stats)
|
||||||
+5
-1
@@ -1,9 +1,13 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import ReactDOM from 'react-dom/client'
|
import ReactDOM from 'react-dom/client'
|
||||||
import App from './App'
|
import App from './App'
|
||||||
import './samples/node-api'
|
|
||||||
import './index.css'
|
import './index.css'
|
||||||
|
|
||||||
|
// `nodeIntegration` needs to be enabled in the Main process.
|
||||||
|
// import './demos/node'
|
||||||
|
// import './demos/ipc'
|
||||||
|
|
||||||
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
|
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
|
||||||
<React.StrictMode>
|
<React.StrictMode>
|
||||||
<App />
|
<App />
|
||||||
|
|||||||
Vendored
+5
@@ -1 +1,6 @@
|
|||||||
/// <reference types="vite/client" />
|
/// <reference types="vite/client" />
|
||||||
|
|
||||||
|
interface Window {
|
||||||
|
// expose in the `electron/preload/index.ts`
|
||||||
|
ipcRenderer: import('electron').IpcRenderer
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
import { rmSync } from 'node:fs'
|
||||||
|
import path from 'node:path'
|
||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import react from '@vitejs/plugin-react'
|
||||||
|
import electron from 'vite-plugin-electron'
|
||||||
|
import renderer from 'vite-plugin-electron-renderer'
|
||||||
|
import pkg from './package.json'
|
||||||
|
|
||||||
|
// https://vitejs.dev/config/
|
||||||
|
export default defineConfig(({ command }) => {
|
||||||
|
rmSync('dist-electron', { recursive: true, force: true })
|
||||||
|
|
||||||
|
const isServe = command === 'serve'
|
||||||
|
const isBuild = command === 'build'
|
||||||
|
const sourcemap = isServe || !!process.env.VSCODE_DEBUG
|
||||||
|
|
||||||
|
return {
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'@': path.join(__dirname, 'src')
|
||||||
|
},
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
react(),
|
||||||
|
electron([
|
||||||
|
{
|
||||||
|
// Main-Process entry file of the Electron App.
|
||||||
|
entry: 'electron/main/index.ts',
|
||||||
|
onstart(options) {
|
||||||
|
if (process.env.VSCODE_DEBUG) {
|
||||||
|
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
|
||||||
|
} else {
|
||||||
|
options.startup()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
vite: {
|
||||||
|
build: {
|
||||||
|
sourcemap,
|
||||||
|
minify: isBuild,
|
||||||
|
outDir: 'dist-electron/main',
|
||||||
|
rollupOptions: {
|
||||||
|
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
entry: 'electron/preload/index.ts',
|
||||||
|
onstart(options) {
|
||||||
|
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
|
||||||
|
// instead of restarting the entire Electron App.
|
||||||
|
options.reload()
|
||||||
|
},
|
||||||
|
vite: {
|
||||||
|
build: {
|
||||||
|
sourcemap: sourcemap ? 'inline' : undefined, // #332
|
||||||
|
minify: isBuild,
|
||||||
|
outDir: 'dist-electron/preload',
|
||||||
|
rollupOptions: {
|
||||||
|
external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
]),
|
||||||
|
// Use Node.js API in the Renderer-process
|
||||||
|
renderer(),
|
||||||
|
],
|
||||||
|
server: process.env.VSCODE_DEBUG && (() => {
|
||||||
|
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
|
||||||
|
return {
|
||||||
|
host: url.hostname,
|
||||||
|
port: +url.port,
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
clearScreen: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
+14
-18
@@ -2,8 +2,7 @@ import { rmSync } from 'node:fs'
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import react from '@vitejs/plugin-react'
|
import react from '@vitejs/plugin-react'
|
||||||
import electron from 'vite-plugin-electron'
|
import electron from 'vite-plugin-electron/simple'
|
||||||
import renderer from 'vite-plugin-electron-renderer'
|
|
||||||
import pkg from './package.json'
|
import pkg from './package.json'
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
// https://vitejs.dev/config/
|
||||||
@@ -22,15 +21,15 @@ export default defineConfig(({ command }) => {
|
|||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
react(),
|
react(),
|
||||||
electron([
|
electron({
|
||||||
{
|
main: {
|
||||||
// Main-Process entry file of the Electron App.
|
// Shortcut of `build.lib.entry`
|
||||||
entry: 'electron/main/index.ts',
|
entry: 'electron/main/index.ts',
|
||||||
onstart(options) {
|
onstart(args) {
|
||||||
if (process.env.VSCODE_DEBUG) {
|
if (process.env.VSCODE_DEBUG) {
|
||||||
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
|
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
|
||||||
} else {
|
} else {
|
||||||
options.startup()
|
args.startup()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
vite: {
|
vite: {
|
||||||
@@ -44,13 +43,10 @@ export default defineConfig(({ command }) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
preload: {
|
||||||
entry: 'electron/preload/index.ts',
|
// Shortcut of `build.rollupOptions.input`.
|
||||||
onstart(options) {
|
// Preload scripts may contain Web assets, so use the `build.rollupOptions.input` instead `build.lib.entry`.
|
||||||
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
|
input: 'electron/preload/index.ts',
|
||||||
// instead of restarting the entire Electron App.
|
|
||||||
options.reload()
|
|
||||||
},
|
|
||||||
vite: {
|
vite: {
|
||||||
build: {
|
build: {
|
||||||
sourcemap: sourcemap ? 'inline' : undefined, // #332
|
sourcemap: sourcemap ? 'inline' : undefined, // #332
|
||||||
@@ -61,10 +57,10 @@ export default defineConfig(({ command }) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
]),
|
// Use Node.js API in the Renderer process
|
||||||
// Use Node.js API in the Renderer-process
|
renderer: {},
|
||||||
renderer(),
|
}),
|
||||||
],
|
],
|
||||||
server: process.env.VSCODE_DEBUG && (() => {
|
server: process.env.VSCODE_DEBUG && (() => {
|
||||||
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
|
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
|
||||||
|
|||||||
Reference in new issue
Block a user