vite-plugin-target

raw JSON →
0.1.1 verified Mon Apr 27 auth: no javascript

A Vite plugin that makes Vite support Electron (main, preload, renderer) and Node.js targets. Current stable version is 0.1.1 with no frequent releases; it provides a minimal, configuration-based approach to adapt Vite's build for non-browser environments. Its key differentiator is unified API for multiple targets, though alternative tools like electron-vite or vite-plugin-electron offer more comprehensive Electron support. The plugin is early-stage and may have limited community adoption.

error The plugin 'vite-plugin-target' requires Vite >=3.0.0
cause Outdated Vite version (e.g., v2.x).
fix
Update Vite to v3 or later: npm install vite@latest
error Uncaught ReferenceError: require is not defined (in electron renderer)
cause Using nodeIntegration disabled but trying to require('electron').
fix
Enable nodeIntegration in the target options or use preload script with contextBridge.
error Cannot find module 'vite-plugin-target' or its corresponding type declarations.
cause Missing installation or incorrect import path.
fix
Install the package: npm install -D vite-plugin-target
gotcha The plugin is in early development (v0.1.1); API may change without major version bump.
fix Pin to exact version and check changelog before upgrading.
gotcha For Electron renderer with nodeIntegration disabled, importing electron may fail unless handled differently.
fix Set nodeIntegration: true or use alternative approach (e.g., preload with contextBridge).
gotcha The plugin only works with Vite's ESM build; CJS output is not supported for target environments.
fix Ensure Vite's build target and format are set appropriately (e.g., esm).
npm install vite-plugin-target
yarn add vite-plugin-target
pnpm add vite-plugin-target

Demonstrates basic usage of vite-plugin-target for Electron renderer with nodeIntegration enabled in Vite config.

import { defineConfig } from 'vite';
import target from 'vite-plugin-target';

export default defineConfig({
  plugins: [
    target({
      'electron-renderer': {
        nodeIntegration: true,
      },
    }),
  ],
});