Integrate Vite and Electron
npm i vite-plugin-electron -D
Example ๐ vite-plugin-electron-quick-start
vite.config.ts
import electron from 'vite-plugin-electron'
export default {
plugins: [
electron({
main: {
entry: 'electron/main.ts',
},
}),
],
}
electron(config: Configuration)
import type { LibraryOptions, UserConfig } from 'vite'
import type { InputOption } from 'rollup'
import type { VitePluginElectronRenderer } from 'vite-plugin-electron-renderer'
export interface CommonConfiguration {
vite?: UserConfig
/**
* Explicitly include/exclude some CJS modules
* `modules` includes `dependencies` of package.json, Node.js's `builtinModules` and `electron`
*/
resolve?: (modules: string[]) => typeof modules | undefined
}
export interface Configuration {
main: CommonConfiguration & {
/**
* Shortcut of `build.lib.entry`
*/
entry: LibraryOptions['entry']
}
preload?: CommonConfiguration & {
/**
* Shortcut of `build.rollupOptions.input`
*/
input: InputOption
}
/**
* Support use Node.js API in Electron-Renderer
* @see https://github.com/electron-vite/vite-plugin-electron-renderer
*/
renderer?: Parameters<VitePluginElectronRenderer>[0]
}
The plugin is just the encapsulation of the built-in scripts of electron-vite-boilerplate/scripts
Let's use the vanilla-ts template created based on create vite
as an example
+ โโโฌ electron
+ โ โโโ main.ts
โโโฌ src
โ โโโ main.ts
โ โโโ style.css
โ โโโ vite-env.d.ts
โโโ .gitignore
โโโ favicon.svg
โโโ index.html
โโโ package.json
โโโ tsconfig.json
+ โโโ vite.config.ts
๐จ By default, the files in electron
folder will be built into the dist/electron
Electron-Main
In general, Vite may not correctly build Node.js packages, especially C/C++ native modules, but Vite can load them as external packages. So, put your Node.js package in dependencies
. Unless you know how to properly build them with Vite.
้ๅธธ็๏ผVite ๅฏ่ฝไธ่ฝๆญฃ็กฎ็ๆๅปบ Node.js ็ๅ
๏ผๅฐคๅ
ถๆฏ C/C++ ๅ็ๆจกๅ๏ผไฝๆฏ Vite ๅฏไปฅๅฐๅฎไปฌไปฅๅค้จๅ
็ๅฝขๅผๅ ่ฝฝใๆไปฅ๏ผ่ฏทๅฐ Node.js ๅ
ๆพๅฐ dependencies
ไธญใ้ค้ไฝ ็ฅ้ๅฆไฝ็จ Vite ๆญฃ็กฎ็ๆๅปบๅฎไปฌใ
Electron-Renderer
You can see ๐ dependencies vs devDependencies