Skip to content

Commit c51515a

Browse files
committed
v2.0.0
1 parent 5b06724 commit c51515a

File tree

3 files changed

+55
-100
lines changed

3 files changed

+55
-100
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
## 2.0.0 (2023-06-20)
2+
3+
**Break**. This will be a completely new version.
4+
5+
Due to the bundling format of Vite (Rollup), it is not suitable to build a pure js module. It happens that a C/C++ native module can only be a cjs module. So this new version will use Webpack for pre-bundle. It behaves really similar to Vite's own [Dependency Pre-Bundling](https://vitejs.dev/guide/dep-pre-bundling.html#dependency-pre-bundling).
6+
Thanks to [Erick Zhao](https://github.com/erickzhao) for the inspiration.
7+
8+
- Supports Electron/Node
9+
- Compatible [Electron Forge](https://github.com/electron/forge) and [
10+
Electron⚡️Vite](https://github.com/electron-vite)
11+
12+
#### [How to work](https://github.com/vite-plugin/vite-plugin-native/blob/v2.0.0/README.md#how-to-work)
13+
114
## 0.2.0 (2023-06-07)
215

316
- 8127327 feat: support `node-bindings`

README.md

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
# vite-plugin-native
22

3-
Plugin for Node native extensions. The project is inspired by the [rollup-plugin-natives](https://github.com/danielgindi/rollup-plugin-natives)
3+
Supports Node/Electron C/C++ native addons. It is a bundle solution based on [Webpack](https://github.com/webpack/webpack).
4+
5+
> Thanks [Erick Zhao](https://github.com/erickzhao) for providing inspiration :)
46
57
[![NPM version](https://img.shields.io/npm/v/vite-plugin-native.svg)](https://npmjs.org/package/vite-plugin-native)
68
[![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-native.svg)](https://npmjs.org/package/vite-plugin-native)
79

8-
-[@mapbox/node-pre-gyp](https://github.com/mapbox/node-pre-gyp)
9-
-[node-gyp-build](https://github.com/prebuild/node-gyp-build)
10-
- ✅ Cross-platform cross-compile
10+
English | [简体中文](./README.zn-CN.md)
11+
1112
## Install
1213

1314
```bash
14-
npm i vite-plugin-native -D
15+
npm i -D vite-plugin-native
1516
```
1617

1718
## Usage
1819

19-
```javascript
20+
```js
2021
import native from 'vite-plugin-native'
2122

2223
export default {
2324
plugins: [
24-
native(/* options */)
25+
native({
26+
// Enable Webpack
27+
webpack: {},
28+
})
2529
]
2630
}
2731
```
@@ -30,62 +34,21 @@ export default {
3034

3135
```ts
3236
export interface NativeOptions {
33-
/**
34-
* Where we want to physically put the extracted `.node` files
35-
* @default 'dist-native'
36-
*/
37-
outDir?: string
38-
/**
39-
* - Modify the final filename for specific modules
40-
* - A function that receives a full path to the original file, and returns a desired filename
41-
* - Or a function that returns a desired file name and a specific destination to copy to
42-
* @experimental
43-
* @todo better calculation value of `id` automatically
44-
*/
45-
map?: (mapping: {
46-
/** `.node` file path */
47-
native: string
48-
/** require id of `.node` file */
49-
id: string
50-
/** `.node` file output location */
51-
output: string
52-
}) => typeof mapping
53-
/**
54-
* - Use `dlopen` instead of `require`/`import`
55-
* - This must be set to true if using a different file extension that `.node`
56-
*/
57-
dlopen?: boolean
58-
/**
59-
* If the target is `esm`, so we can't use `require` (and `.node` is not supported in `import` anyway), we will need to use `createRequire` instead
60-
* @default 'cjs'
61-
*/
62-
target?: 'cjs' | 'esm'
37+
/** @default 'node_natives' */
38+
assetsDir?: string
39+
/** By default native modules are automatically detected if this option is not explicitly configure by the user. */
40+
natives?: string[] | ((natives: string[]) => string[])
41+
/** Enable and configure webpack. */
42+
webpack?: {
43+
config?: (config: Configuration) => Configuration | undefined | Promise<Configuration | undefined>
44+
'node-loader'?: NodeLoaderOptions,
45+
'@vercel/webpack-asset-relocator-loader'?: WebpackAssetRelocatorLoader,
46+
},
6347
}
6448
```
6549

66-
## Advanced
50+
## How to work
6751

68-
Three advanced uses are described in the code comments
52+
> TODO: Translate into English.
6953
70-
```ts
71-
import native from 'vite-plugin-native'
72-
73-
export default {
74-
plugins: [
75-
native({
76-
map(mapping) {
77-
// 🚨 If you want to cross-compile across platforms, you can change the path to `mapping.native` to do so
78-
79-
// 🚨 If your `build.outDir` is not `dist`, you may need to dynamically adjust the value of `mapping.id` to fit it
80-
81-
// 🚨 Avoid named conflict
82-
if (mapping.native.includes('serialport')) {
83-
mapping.id = mapping.id.replace('node.napi', 'node_serialport.napi')
84-
mapping.output = mapping.output.replace('node.napi', 'node_serialport.napi')
85-
}
86-
return mapping
87-
},
88-
}),
89-
],
90-
}
91-
```
54+
See 👉🏻 [工作原理 (How to work)](./README.zn-CN.md#工作原理)

README.zn-CN.md

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ npm i -D vite-plugin-native
1717

1818
## Usage
1919

20-
```javascript
20+
```js
2121
import native from 'vite-plugin-native'
2222

2323
export default {
2424
plugins: [
25-
native(/* options */)
25+
native({
26+
// Enable Webpack
27+
webpack: {},
28+
})
2629
]
2730
}
2831
```
@@ -31,36 +34,16 @@ export default {
3134

3235
```ts
3336
export interface NativeOptions {
34-
/**
35-
* Where we want to physically put the extracted `.node` files
36-
* @default 'dist-native'
37-
*/
38-
outDir?: string
39-
/**
40-
* - Modify the final filename for specific modules
41-
* - A function that receives a full path to the original file, and returns a desired filename
42-
* - Or a function that returns a desired file name and a specific destination to copy to
43-
* @experimental
44-
* @todo better calculation value of `id` automatically
45-
*/
46-
map?: (mapping: {
47-
/** `.node` file path */
48-
native: string
49-
/** require id of `.node` file */
50-
id: string
51-
/** `.node` file output location */
52-
output: string
53-
}) => typeof mapping
54-
/**
55-
* - Use `dlopen` instead of `require`/`import`
56-
* - This must be set to true if using a different file extension that `.node`
57-
*/
58-
dlopen?: boolean
59-
/**
60-
* If the target is `esm`, so we can't use `require` (and `.node` is not supported in `import` anyway), we will need to use `createRequire` instead
61-
* @default 'cjs'
62-
*/
63-
target?: 'cjs' | 'esm'
37+
/** @default 'node_natives' */
38+
assetsDir?: string
39+
/** By default native modules are automatically detected if this option is not explicitly configure by the user. */
40+
natives?: string[] | ((natives: string[]) => string[])
41+
/** Enable and configure webpack. */
42+
webpack?: {
43+
config?: (config: Configuration) => Configuration | undefined | Promise<Configuration | undefined>
44+
'node-loader'?: NodeLoaderOptions,
45+
'@vercel/webpack-asset-relocator-loader'?: WebpackAssetRelocatorLoader,
46+
},
6447
}
6548
```
6649

@@ -303,11 +286,7 @@ C/C++ 构建的 .node 文件本质上是一个 cjs 模块,无论怎样它都
303286

304287
## 为什么是 Webpack
305288

306-
<!--
307-
首先我们从上面的说明中关于 C/C++ 模块的支持能总结出两个点:
308-
309-
1. 必须是 bundle 方案,并且以 cjs 格式构建
310-
2. 模块需要被一个模块中心管理,这样才能避免 作用域以及同步/异步 带来的副作用问题
311-
-->
312-
313289
你可能已经意识到 Vite 的 Pre-Bundling 的行为与 Webpack 的行为趋同,为什么我们不直接使用 Pre-Bundling 构建 C/C++ 模块,而且它也是基于 cjs 格式的 bundle 方案。主要是 Webpack 的生态优势,这能让我们少做很多事情且低风险。
290+
291+
## 基于 Vite API 方案
292+
未来是否会提供一个基于 Vite(Rollup) 自身 API 的方案?暂时没有明确计划。作者目前就职于一个与该插件毫无关系的商业公司,对于此项目的维护仅仅是出于对 Vite/Electron 社区的热爱!

0 commit comments

Comments
 (0)