egasimus / vite-plugin-electron

:electron: ๐Ÿ”— โšก๏ธ

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

vite-plugin-electron

Integrate Vite and Electron

NPM version NPM Downloads

English | ็ฎ€ไฝ“ไธญๆ–‡

vite-plugin-electron.gif

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'

export interface Configuration {
  main: {
    /**
     * Shortcut of `build.lib.entry`
     */
    entry: LibraryOptions['entry']
    vite?: UserConfig
  }
  preload?: {
    /**
     * Shortcut of `build.rollupOptions.input`
     */
    input: InputOption
    vite?: UserConfig
  }
}

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

๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง๐Ÿšง

vite-plugin-electron/renderer

Use Electron and Node.js API in Renderer-process

If you only need to build the Renderer-process, you can just use the vite-plugin-electron/renderer plugin

Example ๐Ÿ‘‰ electron-vite-boilerplate/packages/renderer/vite.config.ts GitHub stars

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(/* something code... */)
ipcRenderer.on('event-name', () => {/* something code... */})

How to work

Using Electron API in Renderer-process

import { ipcRenderer } from 'electron'
โ†“
// Actually will redirect by `resolve.alias`
import { ipcRenderer } from 'vite-plugin-electron/renderer/modules/electron-renderer.js'

Using Node.js API in Renderer-process

import { readFile } from 'fs'
โ†“
// All Node.js API will redirect to the directory created based on `vite-plugin-optimizer` by `resolve.alias`
import { readFile } from '.vite-plugin-electron-renderer/fs'

Config presets

  1. 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
  1. The plugin transform Electron and Node.js built-in modules to ESModule format in vite serve phase.

  2. Add Electron and Node.js built-in modules to Rollup output.external option in the vite build phase.

FAQ

You may need to use some Node.js modules from npm in the Main-process/Renderer-process.
I suggest you look at electron-vite-boilerplate.

About

:electron: ๐Ÿ”— โšก๏ธ


Languages

Language:JavaScript 50.8%Language:TypeScript 49.2%