From 425365e41f09f601d39976f0973c60084fd42df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=89=E9=9E=8B=E6=B2=A1=E5=8F=B7?= <308487730@qq.com> Date: Sat, 9 Sep 2023 11:51:51 +0800 Subject: [PATCH] feat: add C/C++ addons --- .vitepress/config.ts | 3 +++ faq/build-performance.md | 1 + faq/cpp-addons.md | 53 ++++++++++++++++++++++++++++++++++++++++ faq/preload-not-split.md | 1 + 4 files changed, 58 insertions(+) create mode 100644 faq/build-performance.md create mode 100644 faq/cpp-addons.md create mode 100644 faq/preload-not-split.md diff --git a/.vitepress/config.ts b/.vitepress/config.ts index 554df83..112d84f 100644 --- a/.vitepress/config.ts +++ b/.vitepress/config.ts @@ -64,10 +64,13 @@ export default defineConfig({ text: 'FAQ', collapsed: false, items: [ + { text: 'C/C++ Addons', link: '/faq/cpp-addons' }, { text: 'Pre-Bundling', link: '/faq/pre-bundling' }, { text: 'dependencies', link: '/faq/dependencies' }, { text: 'Env Variables', link: '/faq/env-variables' }, { text: 'Static Resource', link: '/faq/static-resource' }, + { text: 'Preload Code Not Split', link: '/faq/preload-not-split' }, + { text: 'Improve Build Performance', link: '/faq/build-performance' }, ], }, ], diff --git a/faq/build-performance.md b/faq/build-performance.md new file mode 100644 index 0000000..9ee4c11 --- /dev/null +++ b/faq/build-performance.md @@ -0,0 +1 @@ +# Improve Build Performance diff --git a/faq/cpp-addons.md b/faq/cpp-addons.md new file mode 100644 index 0000000..1a9c3af --- /dev/null +++ b/faq/cpp-addons.md @@ -0,0 +1,53 @@ +# C/C++ Addons + +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. + +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`. + +So, many times we have to use the `external` option to exclude `C/C++` addons builds to ensure that it can work normally. + + + +::: tip + +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. + +Additionally, some samples for `C/C++` addons are provided here 👉 [electron-vite-samples](https://github.com/caoxiemeihao/electron-vite-samples). + +::: + +**e.g.** + +```ts +import electron from 'vite-plugin-electron' + +export default { + plugins: [ + electron({ + // Main process entry file of the Electron App. + entry: 'electron/main/index.ts', + vite: { + build: { + rollupOptions: { + external: [ + 'better-sqlite3', + 'sqlite3', + 'serialport', + // other `C/C++` addons + ], + }, + }, + }, + }), + ], +} +``` + + diff --git a/faq/preload-not-split.md b/faq/preload-not-split.md new file mode 100644 index 0000000..399a8fe --- /dev/null +++ b/faq/preload-not-split.md @@ -0,0 +1 @@ +# Preload Scripts Code Not Split