1.vite 使用 c/c++ 模块
以下是官方的原话
“ Node.js 的 C/C++
扩展有个很显著的特点,它只支持构建成为 CommonJS
格式的模块,并且使用 require()
加载它。这对强依赖 ESModule
格式的构建工具像 Vite、Rollup 十分的致命。
虽然有 @rollup/plugin-commonjs 这样的工具插件,但它不是万能的,尤其是在一些动态加载的场景,这同样也是 cjs
与 esm
最大的不同点。
所以说,很多时候我们不得不使用 external
选项排除 C/C++
模块构建,以保障它能正常工作。
当然,这不是绝对的。如果你很熟悉 Vite、Rollup 的工作原理和 C/C++
模块的 binding
方式,那么我相信你有更好的办法处理它们。”
同样也给出了解决方法
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
],
},
},
},
}),
],
}
2.使用@electron/rebuild重建node.js模块
如果直接安装使用的话,可能会报node版本的错误,重建一下对应模块即可。使用方法如下。
1.安装
yarn add @electron/rebuild -D
2.在package.json中配置脚本
"rebuild": "electron-rebuild -f -w better-sqlite3"
3.运行脚本
yarn rebuild
3.创建的数据库在修改时导致程序崩溃
如果你是采用 npm create vite 的方式进行安装,那么当你操作数据库时会出现类似这样的报错。
[14608:1122/173758.350:ERROR:crashpad_client_win.cc(844)] not connected
error Command failed with exit code 4294930435.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
在我卡了一天后,发现........似乎是版本的问题。
yarn add electron
注:配个小demo https://gitee.com/warm-with-books/electron-database