
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
vite-plugin-electron-renderer
Advanced tools
Support use Node.js API in Electron-Renderer
npm i vite-plugin-electron-renderer -D
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... */})
renderer(options: Options)
export interface Options {
/**
* Explicitly include/exclude some CJS modules
* `modules` includes `dependencies` of package.json, Node.js's `builtinModules` and `electron`
*/
resolve?: (modules: string[]) => typeof modules | undefined
}
dependencies
vs devDependencies
The easiest way
dependencies
devDependencies
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 正确的构建它们。
e.g.
Electron-Main
import { ipcMain } from 'electron'
↓
const { ipcMain } = require('electron')
Electron-Renderer
import { ipcRenderer } from 'electron'
↓
// Generate a virtual module by load-hook
const electron = require('electron')
export const ipcRenderer = electron.ipcRenderer
↓
// The following code snippet will work in Electron-Renderer,
// and it doesn't seem to have changed :)
import { ipcRenderer } from 'electron'
See more about Vite loading Node.js package 👉
Using Electron API in Electron-Renderer
import { ipcRenderer } from 'electron'
↓
// Actually will redirect by `resolve.alias`
import { ipcRenderer } from 'vite-plugin-electron-renderer/electron-renderer.js'
Using Node.js API and package in Electron-Renderer 👉
base = './'
build.assetsDir = ''
-> TODO: Automatic splicing build.assetsDir
build.emptyOutDir = false
build.cssCodeSplit = false
build.rollupOptions.output.format = 'cjs'
resolve.conditions = ['node']
electron
module into optimizeDeps.exclude
The plugin transform Electron and Node.js built-in modules to ESModule format in vite serve
phase.
Add Electron and Node.js built-in modules to Rollup output.external
option in the vite build
phase.
[2022-06-28] v0.5.0
require('fs')
config.build.emptyOutDir=false
vite-plugin-electron-renderer:use-node.js
vite-plugin-electron-renderer:polyfill-exports
vite-plugin-electron-renderer:build-config
FAQs
Support use Node.js API in Electron-Renderer
The npm package vite-plugin-electron-renderer receives a total of 9,572 weekly downloads. As such, vite-plugin-electron-renderer popularity was classified as popular.
We found that vite-plugin-electron-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.