[READ-ONLY] Mirror of https://github.com/probablykasper/vite-plugin-electron-x. A Vite plugin for bundling main.ts, preload.ts and running Electron in development
0

Configure Feed

Select the types of activity you want to include in your feed.

Main bundling

Kasper (Aug 30, 2022, 4:35 AM +0200) a425f670 8cc85c68

+82 -29
+2 -2
README.md
··· 20 20 Load the Vite dev server in Electron: 21 21 ```js 22 22 if (process.env.NODE_ENV === "development') { 23 - // vite-plugin-electorn-x automatically provides the DEV_PORT environment variable to Electron 24 - mainWindow.loadURL(`http://localhost:${process.env.DEV_PORT}`) 23 + // vite-plugin-electorn-x automatically provides the VITE_DEV_SERVER_URL environment variable to Electron 24 + mainWindow.loadURL(`http://localhost:${process.env.VITE_DEV_SERVER_URL}`) 25 25 } 26 26 ``` 27 27
-12
build.config.ts
··· 1 - import { defineBuildConfig } from 'unbuild' 2 - 3 - export default defineBuildConfig({ 4 - entries: ['src/index'], 5 - externals: ['vite', 'electron'], 6 - clean: true, 7 - declaration: true, 8 - rollup: { 9 - emitCJS: true, 10 - inlineDependencies: true, 11 - }, 12 - })
+80 -15
src/index.ts
··· 1 1 import { spawn } from 'child_process' 2 - import type { Plugin } from 'vite' 2 + import { resolve } from 'path' 3 + import { build as viteBuild, Plugin, ResolvedConfig } from 'vite' 3 4 // @ts-ignore 4 5 import { default as electronPath } from 'electron/index' 5 6 ··· 9 10 console.error('Could not get Electron path') 10 11 process.exit(1) 11 12 } 12 - const child = spawn(electronPath, args, { stdio: 'inherit', env, windowsHide: false }) 13 + const child = spawn(electronPath, args, { stdio: 'ignore', env, windowsHide: false }) 13 14 child.on('close', (code, signal) => { 14 15 if (code === null) { 15 16 console.error(electronPath, 'exited with signal', signal) ··· 20 21 21 22 function terminationHandler(signal: NodeJS.Signals) { 22 23 if (!child.killed) { 24 + console.log('- !killed') 23 25 child.kill(signal) 24 26 } 25 27 } ··· 27 29 process.on('SIGTERM', terminationHandler) 28 30 } 29 31 30 - type ViteElectronOptions = { 31 - devServerArgs?: string[] 32 - env?: NodeJS.ProcessEnv 32 + async function build(options: ViteElectronOptions, viteConfig: ResolvedConfig): Promise<string> { 33 + if (options.main === false) return options.entry 34 + if (options.main === true) options.main = {} 35 + 36 + const outDir = options.main?.outDir || resolve(viteConfig.build.outDir, 'electron') 37 + const fileName = 'main' 38 + 39 + await viteBuild({ 40 + configFile: false, // Don't load user config file 41 + publicDir: false, 42 + mode: viteConfig.mode, 43 + build: { 44 + minify: viteConfig.build.minify, 45 + emptyOutDir: viteConfig.build.emptyOutDir, 46 + outDir, 47 + lib: { 48 + entry: options.entry, 49 + formats: ['cjs'], 50 + fileName, 51 + }, 52 + }, 53 + }) 54 + return resolve(outDir, fileName + '.js') 33 55 } 34 - export function electronStarter(options: ViteElectronOptions = {}): Plugin { 35 - return { 36 - name: 'electron-start', 37 - configureServer(server) { 56 + 57 + const localhosts = ['localhost', '127.0.0.1', '::1', '0000:0000:0000:0000:0000:0000:0000:0001'] 58 + export function electron(options: ViteElectronOptions): Plugin[] { 59 + let viteConfig: ResolvedConfig 60 + 61 + const buildPlugin: Plugin = { 62 + name: 'electron-build-main', 63 + apply: 'build', 64 + enforce: 'post', 65 + configResolved(config) { 66 + viteConfig = config 67 + }, 68 + async closeBundle() { 69 + await build(options, viteConfig) 70 + }, 71 + } 72 + 73 + const servePlugin: Plugin = { 74 + name: 'electron-dev-server', 75 + apply: 'serve', 76 + enforce: 'post', 77 + configResolved(config) { 78 + viteConfig = config 79 + }, 80 + async configureServer(server) { 81 + const mainPath = await build(options, viteConfig) 82 + 38 83 server.httpServer?.once('listening', async () => { 39 - if (!server.config.server.port) { 40 - console.error('No dev server port found') 84 + const address = server.httpServer?.address() || null 85 + if (address === null || typeof address === 'string') { 86 + console.error('Unexpected dev server address', address) 41 87 process.exit(1) 42 88 } 43 - console.log('\nStarting Electron...') 44 - const args = options.devServerArgs || ['.'] 89 + const hostname = localhosts.includes(address.address) ? 'localhost' : address.address 90 + const protocol = server.config.server.https ? 'https' : 'http' 91 + const url = `${protocol}://${hostname}:${address.port}` 92 + 45 93 const env = options.env || {} 46 - if (!env.DEV_PORT) env.DEV_PORT = server.config.server.port.toString() 47 - spawnElectron(args, env) 94 + if (!env.VITE_DEV_SERVER_URL) env.VITE_DEV_SERVER_URL = url 95 + if (!env.VITE_DEV_SERVER_HOSTNAME) env.VITE_DEV_SERVER_HOSTNAME = hostname 96 + if (!env.VITE_DEV_SERVER_PORT) env.VITE_DEV_SERVER_PORT = address.port.toString() 97 + 98 + console.log('\nStarting Electron...') 99 + spawnElectron([mainPath], env) 48 100 }) 49 101 }, 50 102 } 103 + 104 + return [buildPlugin, servePlugin] 105 + } 106 + 107 + export type ViteElectronOptions = { 108 + env?: NodeJS.ProcessEnv 109 + /** Your Electron entrypoint. Used for the dev server and for building */ 110 + entry: string 111 + main?: 112 + | { 113 + outDir?: string 114 + } 115 + | boolean 51 116 }