vite-plugin-electron-renderer
Support use Node.js API in Electron-Renderer
Install
npm i vite-plugin-electron-renderer -D
Usage
vite.config.ts
import renderer from 'vite-plugin-electron-renderer'
export default {
plugins: [
renderer(),
],
}
renderer.js
import { readFile } from 'fs'
import { ipcRenderer } from 'electron'
readFile()
ipcRenderer.on('event-name', () => {})
API
renderer(options: Options)
export interface Options {
resolve?: (modules: string[]) => typeof modules | undefined
}
How to work
Using Electron API in Electron-Renderer
import { ipcRenderer } from 'electron'
↓
import { ipcRenderer } from 'vite-plugin-electron-renderer/electron-renderer.js'
Using Node.js API and package in Electron-Renderer 👉
Config presets
- Fist, the plugin will configuration something.
If you do not configure the following options, the plugin will modify their default values
base = './'
build.assetsDir = ''
-> TODO: Automatic splicing build.assetsDir
build.emptyOutDir = false
build.cssCodeSplit = false
build.rollupOptions.output.format = 'cjs'
resolve.conditions = ['node']
- Always insert the
electron
module into optimizeDeps.exclude
-
The plugin transform Electron and Node.js built-in modules to ESModule format in vite serve
phase.
-
Add Electron and Node.js built-in modules to Rollup output.external
option in the vite build
phase.