vite-plugin-electron
Integrate Vite and Electron
English | 简体中文
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',
},
}),
],
}
you can use process.env.VITE_DEV_SERVER_URL
when the vite command is called 'serve'
const win = new BrowserWindow({
title: 'Main window',
})
if (process.env.VITE_DEV_SERVER_URL) {
win.loadURL(process.env.VITE_DEV_SERVER_URL)
} else {
win.loadFile('yourOutputFile.html');
}
API
electron(config: Configuration)
import type { InlineConfig, LibraryOptions } from 'vite'
import type { InputOption } from 'rollup'
import { Options } from 'vite-plugin-electron-renderer'
export interface CommonConfiguration {
vite?: InlineConfig
resolve?: (modules: string[]) => typeof modules | undefined
}
export interface Configuration {
main: CommonConfiguration & {
entry: LibraryOptions['entry']
}
preload?: CommonConfiguration & {
input: InputOption
}
renderer?: Options
}
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 official 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()
.
Electron-Renderer
You can see 👉 dependencies vs devDependencies