Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vite-plugin-electron-renderer

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-plugin-electron-renderer

Support use Node.js API in Electron-Renderer

  • 0.10.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9.4K
decreased by-27.79%
Maintainers
1
Weekly downloads
 
Created
Source

vite-plugin-electron-renderer

Support use Node.js API in Electron-Renderer

NPM version NPM Downloads

English | 简体中文

Install

npm i vite-plugin-electron-renderer -D

Examples

Usage

vite.config.ts

import renderer from 'vite-plugin-electron-renderer'

export default {
  plugins: [
    renderer(/* options */),
  ],
}

renderer.js

import { readFile } from 'fs'
import { ipcRenderer } from 'electron'

readFile(/* something code... */)
ipcRenderer.on('event-name', () => {/* something code... */})

API (Define)

renderer(options: RendererOptions)

export interface RendererOptions {
  /**
   * Whether node integration is enabled. Default is `false`.
   */
  nodeIntegration?: boolean
  /**
   * If the npm-package you are using is a Node.js package, then you need to Pre Bundling it.
   * @see https://vitejs.dev/guide/dep-pre-bundling.html
   */
  optimizeDeps?: {
    include?: (string | {
      name: string
      /**
       * Explicitly specify the module type
       */
      type?: "commonjs" | "module"
    })[]
    buildOptions?: import('esbuild').BuildOptions
  }
}

Worker

vite.config.ts

import { worker } from 'vite-plugin-electron-renderer'

export default {
  worker: {
    plugins: [
      worker(/* options */),
    ],
  },
}

API (Define)

renderer(options: WorkerOptions)

export interface WorkerOptions {
  /**
   * Whether node integration is enabled in web workers. Default is `false`. More
   * about this can be found in Multithreading.
   */
  nodeIntegrationInWorker?: boolean
}

How to work

Load Electron and Node.js cjs-packages/builtin-modules (Schematic)

Electron-Renderer(vite serve)
┏————————————————————————————————————————┓                    ┏—————————————————┓
│ import { ipcRenderer } from 'electron' │                    │ Vite dev server │
┗————————————————————————————————————————┛                    ┗—————————————————┛
                   │                                                   │
                   │ 1. HTTP(Request): electron module                 │
                   │ ————————————————————————————————————————————————> │
                   │                                                   │
                   │                                                   │
                   │ 2. Intercept in load-hook(Plugin)                 │
                   │ 3. Generate a virtual ESM module(electron)        │
                   │    ↓                                              │
                   │    const { ipcRenderer } = require('electron')    │
                   │    export { ipcRenderer }                         │
                   │                                                   │
                   │                                                   │
                   │ 4. HTTP(Response): electron module                │
                   │ <———————————————————————————————————————————————— │
                   │                                                   │
┏————————————————————————————————————————┓                    ┏—————————————————┓
│ import { ipcRenderer } from 'electron' │                    │ Vite dev server │
┗————————————————————————————————————————┛                    ┗—————————————————┛
Electron-Renderer(vite build)
import { ipcRenderer } from 'electron'const { ipcRenderer } = require('electron')

Dependency Pre-Bundling

When you run vite for the first time, you may notice this message:

$ vite
Pre-bundling: serialport
Pre-bundling: electron-store
Pre-bundling: execa
Pre-bundling: node-fetch
Pre-bundling: got
The Why

In general, Vite may not correctly build Node.js packages, especially Node.js C/C++ native modules, but Vite can load them as external packages.
Unless you know how to properly build them with Vite.
See example

By the way. If an npm package is a pure ESM format package, and the packages it depends on are also in ESM format, then put it in optimizeDeps.exclude and it will work normally.
See an explanation of it.

dependencies vs devDependencies

Classifye.g.dependenciesdevDependencies
Node.js C/C++ native modulesserialport, sqlite3
Node.js CJS packageselectron-store
Node.js ESM packagesexeca, got, node-fetch✅ (Recommend)
Web packagesVue, React✅ (Recommend)

Doing so will reduce the size of the packaged APP by electron-builder.

Config presets (Opinionated)

If you do not configure the following options, the plugin will modify their default values

  • base = './'
  • build.emptyOutDir = false
  • build.cssCodeSplit = false (TODO)
  • build.rollupOptions.output.format = 'cjs' (nodeIntegration: true)
  • resolve.conditions = ['node']
  • optimizeDeps.exclude = ['electron'] - always

Keywords

FAQs

Package last updated on 24 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc