vite-plugin-electron-renderer
Advanced tools
Comparing version 0.5.1 to 0.5.2
## [2022-07-07] v0.5.2 | ||
- 9dd8d4c feat: export `resolveModules()` | ||
- 609e582 feat: interface `ResolveModules` | ||
- dc6d6f6 docs: update | ||
- 201eb71 docs: 🚨 ESM packages | ||
- c8fe50b docs: `import { ipcRenderer } from 'electron'` | ||
## [2022-07-01] v0.5.1 | ||
@@ -3,0 +11,0 @@ |
{ | ||
"name": "vite-plugin-electron-renderer", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "Support use Node.js API in Electron-Renderer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,5 +0,9 @@ | ||
import { Plugin } from 'vite'; | ||
import { Plugin, ResolvedConfig } from 'vite'; | ||
declare const useNodeJs: UseNodeJs; | ||
export default useNodeJs; | ||
declare const resolveModules: ResolveModules; | ||
export { | ||
useNodeJs as default, | ||
resolveModules, | ||
} | ||
@@ -17,1 +21,12 @@ export interface Options { | ||
} | ||
export interface ResolveModules { | ||
(config: ResolvedConfig, options: Options): { | ||
/** Node.js builtin modules */ | ||
builtins: string[]; | ||
/** dependencies of package.json */ | ||
dependencies: string[]; | ||
/** dependencies(ESM) of package.json */ | ||
ESM_deps: string[]; | ||
} | ||
} |
@@ -8,3 +8,3 @@ const fs = require('fs'); | ||
*/ | ||
module.exports = function useNodeJs(options = {}) { | ||
function useNodeJs(options = {}) { | ||
const builtins = []; | ||
@@ -167,3 +167,3 @@ const dependencies = []; | ||
/** | ||
* @type {(config: import('vite').ResolvedConfig, options: import('.').Options) => { builtins: string[]; dependencies: string[]; ESM_deps: string[]; }} | ||
* @type {import('.').ResolveModules} | ||
*/ | ||
@@ -173,3 +173,3 @@ function resolveModules(config, options) { | ||
const cwd = process.cwd(); | ||
const builtins = builtinModules.filter(e => !e.startsWith('_')); builtins.push(...builtins.map(m => [m, `node:${m}`])); | ||
const builtins = builtinModules.filter(e => !e.startsWith('_')); builtins.push(...builtins.map(m => `node:${m}`)); | ||
// dependencies of package.json | ||
@@ -197,3 +197,3 @@ let dependencies = []; | ||
// TODO: Nested package name | ||
// TODO: Nested package name, but you can explicity include it by `options.resolve` | ||
dependencies.push(package); | ||
@@ -223,1 +223,5 @@ } | ||
} | ||
useNodeJs.resolveModules = resolveModules; | ||
useNodeJs.default = useNodeJs; | ||
module.exports = useNodeJs; |
@@ -52,3 +52,3 @@ # vite-plugin-electron-renderer | ||
## `dependencies` vs `devDependencies` | ||
## 🚨 `dependencies` vs `devDependencies` | ||
@@ -62,7 +62,7 @@ **The easiest way** | ||
By default, `vite-plugin-electron-renderer` treats packages in `dependencies` as `external` modules. During development, a virtual module in ESM format is generated by `load-hook` to ensure that it can work properly. It is inserted into `rollupOptions.external` during build time. If you don't want this, you can control the behavior with `options.resolve()`. | ||
By default, `vite-plugin-electron-renderer` treats packages in `dependencies`(CJS modules) as `external` modules. During development, a virtual module in ESM format is generated by `load-hook` to ensure that it can work properly. It is inserted into `rollupOptions.external` during build time. If you don't want this, you can control the behavior with `options.resolve()`. | ||
*通常的,Vite 可能不能正确的构建 Node.js 的包,尤其是 C/C++ 原生模块,但是 Vite 可以将它们以外部包的形式加载。所以,请将 Node.js 包放到 `dependencies` 中。除非你知道如何用 Vite 正确的构建它们。* | ||
*默认情况下,`vite-plugin-electron-renderer` 会将 `dependencies` 中的包视为 `external` 模块。在开发期间会通过 `load-hook` 生成一个 ESM 格式的虚拟模块,以保障其能够正常工作。在构建期间会将其插入到 `rollupOptions.external` 中。如果你不希望这样,你可以通过 `options.resolve()` 来控制该行为。* | ||
*默认情况下,`vite-plugin-electron-renderer` 会将 `dependencies`(CJS 模块) 中的包视为 `external` 模块。在开发期间会通过 `load-hook` 生成一个 ESM 格式的虚拟模块,以保障其能够正常工作。在构建期间会将其插入到 `rollupOptions.external` 中。如果你不希望这样,你可以通过 `options.resolve()` 来控制该行为。* | ||
@@ -75,4 +75,6 @@ **e.g.** | ||
import { readFile } from 'fs' | ||
import { ipcRenderer } from 'electron' | ||
↓ | ||
const { readFile } = require('fs') | ||
const { ipcRenderer } = require('electron') | ||
``` | ||
@@ -84,4 +86,6 @@ | ||
import { readFile } from 'fs' | ||
import { ipcRenderer } from 'electron' | ||
↓ | ||
const { readFile } = require('fs') | ||
const { ipcRenderer } = require('electron') | ||
``` | ||
@@ -102,3 +106,3 @@ | ||
│ 3. Generate a virtual module(fs) │ | ||
│ │ | ||
│ ↓ │ | ||
│ const _M_ = require('fs') │ | ||
@@ -116,26 +120,35 @@ │ export const readFile = _M_.readFile │ | ||
**There are three cases that will be intercepted by Vite dev server** | ||
[👉 See Vite loading Node.js package source code.](https://github.com/electron-vite/vite-plugin-electron-renderer/blob/2bb38a1dbd50b462d33cbc314bb5db71119b52cf/plugins/use-node.js/index.js#L91) | ||
*You can control the behavior with `options.resolve()`* | ||
```js | ||
import { ipcRenderer } from 'electron' | ||
↓ | ||
// Actually redirect via `resolve.alias` | ||
import { ipcRenderer } from 'vite-plugin-electron-renderer/plugins/use-node.js/electron-renderer.js' | ||
``` | ||
1. `electron` module | ||
2. Nod.js builtin modules | ||
3. Packages in `dependencies` | ||
## 🚨 ESM packages | ||
**e.g.** `node-fetch` `execa` `got` ...others | ||
> [👉 See Vite loading Node.js package source code.](https://github.com/electron-vite/vite-plugin-electron-renderer/blob/2bb38a1dbd50b462d33cbc314bb5db71119b52cf/plugins/use-node.js/index.js#L91) | ||
1. `npm i vite-plugin-esmodule -D` | ||
2. Configure in vite.config.ts | ||
```ts | ||
import esmodule from 'vite-plugin-esmodule' | ||
export default { | ||
plugins: [ | ||
esmodule(['got', 'execa', 'node-fetch']), | ||
], | ||
} | ||
``` | ||
[👉 See electron-renderer.js](https://github.com/electron-vite/vite-plugin-electron-renderer/blob/main/plugins/use-node.js/electron-renderer.js) | ||
## How to work | ||
Using Electron API in Electron-Renderer | ||
The plugin is just the encapsulation of the built-in plugins of [electron-vite-boilerplate/packages/renderer/plugins](https://github.com/electron-vite/electron-vite-boilerplate/tree/main/packages/renderer/plugins) | ||
```js | ||
import { ipcRenderer } from 'electron' | ||
↓ | ||
// Actually will redirect by `resolve.alias` | ||
import { ipcRenderer } from 'vite-plugin-electron-renderer/plugins/use-node.js/electron-renderer.js' | ||
``` | ||
###### Config presets | ||
#### Config presets | ||
1. Fist, the plugin will configuration something. | ||
@@ -142,0 +155,0 @@ *If you do not configure the following options, the plugin will modify their default values* |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21859
343
162