Skip to content

Commit 5eaf234

Browse files
authored
Merge pull request #52 from electron-vite/dev
feat: add C/C++ addons
2 parents 7322385 + 425365e commit 5eaf234

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

.vitepress/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ export default defineConfig({
6464
text: 'FAQ',
6565
collapsed: false,
6666
items: [
67+
{ text: 'C/C++ Addons', link: '/faq/cpp-addons' },
6768
{ text: 'Pre-Bundling', link: '/faq/pre-bundling' },
6869
{ text: 'dependencies', link: '/faq/dependencies' },
6970
{ text: 'Env Variables', link: '/faq/env-variables' },
7071
{ text: 'Static Resource', link: '/faq/static-resource' },
72+
{ text: 'Preload Code Not Split', link: '/faq/preload-not-split' },
73+
{ text: 'Improve Build Performance', link: '/faq/build-performance' },
7174
],
7275
},
7376
],

faq/build-performance.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Improve Build Performance

faq/cpp-addons.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# C/C++ Addons
2+
3+
The `C/C++` addons of Node.js has a very notable feature, it only supports building in the `CommonJS` format, and using `require()` to load it. This is fatal to bundler like [Vite](https://vitejs.dev/), [Rollup](https://rollupjs.org/) that strongly rely on the `ESModule` format.
4+
5+
Although there are tool plugins like [@rollup/plugin-commonjs](https://www.npmjs.com/package/@rollup/plugin-commonjs), it is not a panacea, especially in some dynamic-require cases. This is also the biggest difference between `cjs` and `esm`.
6+
7+
So, many times we have to use the `external` option to exclude `C/C++` addons builds to ensure that it can work normally.
8+
9+
<!--
10+
Node.js 的 `C/C++` 扩展有个很显著的特点,它只支持构建成为 `CommonJS` 格式的模块,并且使用 `require` 加载它。这对强依赖 `ESModule` 格式的构建工具像 Vite、Rollup 十分的致命。
11+
虽然有 `@rollup/plugin-commonjs` 这样的工具插件,但它不是万能的,尤其是在一些动态加载的场景,这同样也是 `cjs` 与 `esm` 最大的不同点。
12+
所以说,很多时候我们不得不使用 `external` 选项排除 `C/C++` 模块构建,以保障它能正常工作。
13+
-->
14+
15+
::: tip
16+
17+
Of course, this is not absolute. If you are familiar with Vite, Rollup how works, and how `C/C++` addons are binding, then I believe you have better ways to deal with them.
18+
19+
Additionally, some samples for `C/C++` addons are provided here 👉 [electron-vite-samples](https://github.com/caoxiemeihao/electron-vite-samples).
20+
21+
:::
22+
23+
**e.g.**
24+
25+
```ts
26+
import electron from 'vite-plugin-electron'
27+
28+
export default {
29+
plugins: [
30+
electron({
31+
// Main process entry file of the Electron App.
32+
entry: 'electron/main/index.ts',
33+
vite: {
34+
build: {
35+
rollupOptions: {
36+
external: [
37+
'better-sqlite3',
38+
'sqlite3',
39+
'serialport',
40+
// other `C/C++` addons
41+
],
42+
},
43+
},
44+
},
45+
}),
46+
],
47+
}
48+
```
49+
50+
<!--
51+
当然,这不是绝对的。如果你很熟悉 Vite、Rollup 的工作原理和 C/C++ 模块的 binding 方式,那么我相信你有更好的办法处理它们。
52+
此外,这里提供了一些 C/C++ 模块的模板。
53+
-->

faq/preload-not-split.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Preload Scripts Code Not Split

0 commit comments

Comments
 (0)