Electron Forge
  • Getting Started
  • Importing an Existing Project
  • CLI
  • Core Concepts
    • Why Electron Forge?
    • Build Lifecycle
  • Configuration
    • Configuration Overview
    • TypeScript Setup
    • Plugins
      • Webpack Plugin
      • Vite Plugin
      • Electronegativity Plugin
      • Auto Unpack Native Modules Plugin
      • Local Electron Plugin
      • Fuses Plugin
    • Makers
      • AppX
      • deb
      • DMG
      • Flatpak
      • pkg
      • RPM
      • Snapcraft
      • Squirrel.Windows
      • WiX MSI
      • ZIP
    • Publishers
      • Bitbucket
      • Electron Release Server
      • GitHub
      • Google Cloud Storage
      • Nucleus
      • S3
      • Snapcraft
    • Hooks
  • Built-in Templates
    • Webpack
    • Webpack + Typescript
    • Vite
    • Vite + TypeScript
  • Guides
    • Code Signing
      • Signing a Windows app
      • Signing a macOS app
    • Custom App Icons
    • Framework Integration
      • React
      • React with TypeScript
      • Vue 3
    • Developing with WSL
  • Advanced
    • Auto Update
    • Debugging
    • Extending Electron Forge
      • Writing Plugins
      • Writing Templates
      • Writing Makers
      • Writing Publishers
    • API Docs
Powered by GitBook
On this page
  • Installation
  • Usage
  • Plugin configuration
  • Project files
  • Advanced configuration
  • Native Node modules
  • Hot Module Replacement (HMR)

Was this helpful?

Edit on GitHub
  1. Configuration
  2. Plugins

Vite Plugin

Transform and bundle code for your Electron Forge app with Vite.

PreviousWebpack PluginNextElectronegativity Plugin

Last updated 8 months ago

Was this helpful?

As of Electron Forge v7.5.0, Vite support for Electron Forge has been marked as experimental in order to reflect its stage in development and to provide maintainers with the ability to release fixes and improvements rapidly. Future minor releases may contain breaking changes, but migration steps will be listed in release notes. For more context, see the Electron Forge .

This plugin makes it easy to set up standard Vite tooling to compile both your main process code and your renderer process code.

Installation

npm install --save-dev @electron-forge/plugin-vite

Usage

Plugin configuration

You must provide two Vite configuration files: one for the main process in vite.main.config.js, and one for the renderer process in vite.renderer.config.js.

For example, this is the taken from Forge's :

module.exports = {
  plugins: [
    {
      name: '@electron-forge/plugin-vite',
      config: {
        // `build` can specify multiple entry builds, which can be
        // Main process, Preload scripts, Worker process, etc.
        build: [
          {
            // `entry` is an alias for `build.lib.entry`
            // in the corresponding file of `config`.
            entry: 'src/main.js',
            config: 'vite.main.config.mjs'
          },
          {
            entry: 'src/preload.js',
            config: 'vite.preload.config.mjs'
          }
        ],
        renderer: [
          {
            name: 'main_window',
            config: 'vite.renderer.config.mjs'
          }
        ]
      }
    }
  ]
};
{
  // ...
  "config": {
    "forge": {
      "plugins": [
        {
          "name": "@electron-forge/plugin-vite",
          "config": {
            "build": [
              {
                "entry": "src/main.js",
                "config": "vite.main.config.mjs"
              },
              {
                "entry": "src/preload.js",
                "config": "vite.preload.config.mjs"
              }
            ],
            "renderer": [
              {
                "name": "main_window",
                "config": "vite.renderer.config.mjs"
              }
            ]
          }
        }
      ]
    }
  }
  // ...
}

Project files

Vite's build config generates a separate entry for the main process and preload script, as well as each renderer process.

Your main entry in your package.json file needs to point at ".vite/build/main", like so:

package.json
{
  "name": "my-vite-app",
  "main": ".vite/build/main.js",
  // ...
}

If using the Vite template, this should be automatically set up for you.

Advanced configuration

Native Node modules

vite.main.config.js
import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    rollupOptions: {
      external: [
        'serialport',
        'sqlite3'
      ]
    }
  }
});

Hot Module Replacement (HMR)

  • The dev server will be suffixed with _DEV_SERVER_URL

  • The static file path will be suffixed with _VITE_NAME

In the case of the main_window, the global variables will be named MAIN_WINDOW_VITE_DEV_SERVER_URL and MAIN_WINDOW_VITE_NAME. An example of how to use them is given below:

main.js
const mainWindow = new BrowserWindow({ /* ... */ });

if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
  mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
} else {
  mainWindow.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`));
};

If using TypeScript, the variables can be defined as such:

main.js (Main Process)
declare const MAIN_WINDOW_VITE_DEV_SERVER_URL: string;
declare const MAIN_WINDOW_VITE_NAME: string;

Config options will largely follow the same standards as non-Electron Vite projects. You can reference for more examples of how to configure each of your entry point's config files.

If you used the template to create your application, native modules will mostly work out of the box. However, to avoid possible build issues, we recommend instructing Vite to load them as external packages:

In order to use Vite's , all loadURL paths need to reference the global variables that the Vite plugin will define for you:

v7.5.0 release notes
configuration
Vite template
Vite's documentation here
Vite
Hot Module Replacement (HMR)