refactor: `vite-plugin-electron` instead `vite-electron-plugin`
This commit is contained in:
parent
1c707b40bb
commit
e93d649340
|
@ -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-electron-plugin'
|
import electron from 'vite-plugin-electron'
|
||||||
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
|
|
||||||
import renderer from 'vite-plugin-electron-renderer'
|
import renderer from 'vite-plugin-electron-renderer'
|
||||||
import pkg from './package.json'
|
import pkg from './package.json'
|
||||||
|
|
||||||
|
@ -11,7 +10,9 @@ import pkg from './package.json'
|
||||||
export default defineConfig(({ command }) => {
|
export default defineConfig(({ command }) => {
|
||||||
rmSync('dist-electron', { recursive: true, force: true })
|
rmSync('dist-electron', { recursive: true, force: true })
|
||||||
|
|
||||||
const sourcemap = command === 'serve' || !!process.env.VSCODE_DEBUG
|
const isServe = command === 'serve'
|
||||||
|
const isBuild = command === 'build'
|
||||||
|
const sourcemap = isServe || !!process.env.VSCODE_DEBUG
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resolve: {
|
resolve: {
|
||||||
|
@ -21,34 +22,57 @@ export default defineConfig(({ command }) => {
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
react(),
|
react(),
|
||||||
electron({
|
electron([
|
||||||
include: [
|
{
|
||||||
'electron'
|
// Main-Process entry file of the Electron App.
|
||||||
],
|
entry: 'electron/main/index.ts',
|
||||||
transformOptions: {
|
onstart(options) {
|
||||||
sourcemap,
|
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 : {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
plugins: [
|
{
|
||||||
...(!!process.env.VSCODE_DEBUG
|
entry: 'electron/preload/index.ts',
|
||||||
? [
|
onstart(options) {
|
||||||
// Will start Electron via VSCode Debug
|
// Notify the Renderer-Process to reload the page when the Preload-Scripts build is complete,
|
||||||
customStart(() => console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')),
|
// instead of restarting the entire Electron App.
|
||||||
]
|
options.reload()
|
||||||
: []),
|
},
|
||||||
// Allow use `import.meta.env.VITE_SOME_KEY` in Electron-Main
|
vite: {
|
||||||
loadViteEnv(),
|
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
|
// 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)
|
||||||
return {
|
return {
|
||||||
host: url.hostname,
|
host: url.hostname,
|
||||||
port: +url.port,
|
port: +url.port,
|
||||||
}
|
}
|
||||||
})() : undefined,
|
})(),
|
||||||
clearScreen: false,
|
clearScreen: false,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,85 +0,0 @@
|
||||||
import { rmSync } from 'node:fs'
|
|
||||||
import path from 'node:path'
|
|
||||||
import { defineConfig } from 'vite'
|
|
||||||
import react from '@vitejs/plugin-react'
|
|
||||||
import electron from 'vite-electron-plugin'
|
|
||||||
import { customStart, loadViteEnv } from 'vite-electron-plugin/plugin'
|
|
||||||
import preload from 'vite-plugin-electron'
|
|
||||||
import renderer from 'vite-plugin-electron-renderer'
|
|
||||||
import pkg from './package.json'
|
|
||||||
|
|
||||||
let preloadHasReady = false
|
|
||||||
|
|
||||||
// https://vitejs.dev/config/
|
|
||||||
export default defineConfig(({ command }) => {
|
|
||||||
rmSync('dist-electron', { recursive: true, force: true })
|
|
||||||
|
|
||||||
const sourcemap = command === 'serve' || !!process.env.VSCODE_DEBUG
|
|
||||||
|
|
||||||
return {
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
'@': path.join(__dirname, 'src')
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
react(),
|
|
||||||
electron({
|
|
||||||
include: [
|
|
||||||
'electron/main'
|
|
||||||
],
|
|
||||||
transformOptions: {
|
|
||||||
sourcemap,
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
customStart(args => {
|
|
||||||
if (process.env.VSCODE_DEBUG) {
|
|
||||||
// Start Electron via VSCode
|
|
||||||
console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
|
|
||||||
} else {
|
|
||||||
if (preloadHasReady) {
|
|
||||||
args?.startup()
|
|
||||||
} else {
|
|
||||||
console.log('[startup] waiting for preload')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
// Allow use `import.meta.env.VITE_SOME_KEY` in Main process
|
|
||||||
loadViteEnv(),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
// Preload scripts
|
|
||||||
preload({
|
|
||||||
entry: [
|
|
||||||
'electron/preload/index.ts'
|
|
||||||
],
|
|
||||||
vite: {
|
|
||||||
build: {
|
|
||||||
minify: false,
|
|
||||||
outDir: 'dist-electron/preload',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
onstart(args) {
|
|
||||||
if (preloadHasReady) {
|
|
||||||
args.reload()
|
|
||||||
} else {
|
|
||||||
preloadHasReady = true
|
|
||||||
args.startup()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
// Use Node.js API in the Renderer process
|
|
||||||
renderer({
|
|
||||||
nodeIntegration: true,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
server: !!process.env.VSCODE_DEBUG ? (() => {
|
|
||||||
const url = new URL(pkg.debug.env.VITE_DEV_SERVER_URL)
|
|
||||||
return {
|
|
||||||
host: url.hostname,
|
|
||||||
port: +url.port,
|
|
||||||
}
|
|
||||||
})() : undefined,
|
|
||||||
clearScreen: false,
|
|
||||||
}
|
|
||||||
})
|
|
Loading…
Reference in New Issue