vite-plugin-electron
Integrate Vite and Electron
Install
npm i vite-plugin-electron -D
Usage
Example 👉 vite-plugin-electron-quick-start
vite.config.ts
import electron from 'vite-plugin-electron'
export default {
plugins: [
electron({
main: {
entry: 'electron/main.ts',
},
}),
],
}
API
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
resolve?: (modules: string[]) => typeof modules | undefined
}
export interface Configuration {
main: CommonConfiguration & {
entry: LibraryOptions['entry']
}
preload?: CommonConfiguration & {
input: InputOption
}
renderer?: Parameters<VitePluginElectronRenderer>[0]
}
How to work
The plugin is just the encapsulation of the built-in scripts of electron-vite-boilerplate/scripts
Recommend structure
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
Put Node.js packages in dependencies
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.
By default, vite-plugin-electron
treats packages in dependencies
as external
modules. If you don't want this, you can control this behavior with options.resolve()
.
通常的,Vite 可能不能正确的构建 Node.js 的包,尤其是 C/C++ 原生模块,但是 Vite 可以将它们以外部包的形式加载。所以,请将 Node.js 包放到 dependencies
中。除非你知道如何用 Vite 正确的构建它们。
默认情况下,vite-plugin-electron
会将 dependencies
中的包视为 external
模块。如果你不希望这样,你可以通过 options.resolve()
来控制改行为。
Electron-Renderer
You can see 👉 dependencies vs devDependencies