SharerMax / 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

vite-plugin-electron.gif

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
  /**
   * 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]
}

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.
้€šๅธธ็š„๏ผŒVite ๅฏ่ƒฝไธ่ƒฝๆญฃ็กฎ็š„ๆž„ๅปบ Node.js ็š„ๅŒ…๏ผŒๅฐคๅ…ถๆ˜ฏ C/C++ ๅŽŸ็”Ÿๆจกๅ—๏ผŒไฝ†ๆ˜ฏ Vite ๅฏไปฅๅฐ†ๅฎƒไปฌไปฅๅค–้ƒจๅŒ…็š„ๅฝขๅผๅŠ ่ฝฝใ€‚ๆ‰€ไปฅ๏ผŒ่ฏทๅฐ† Node.js ๅŒ…ๆ”พๅˆฐ dependencies ไธญใ€‚้™ค้žไฝ ็Ÿฅ้“ๅฆ‚ไฝ•็”จ Vite ๆญฃ็กฎ็š„ๆž„ๅปบๅฎƒไปฌใ€‚

Electron-Renderer

You can see ๐Ÿ‘‰ dependencies vs devDependencies

About

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


Languages

Language:TypeScript 100.0%