我正在嘗試打開一個用sqlcipher加密的sqlite3資料庫。我有密碼,我可以使用 sqlitebrowser 成功打開它。
我使用這個模板開始了我的專案。它基于electron- forge webpack 插件。
當我輸入yarn start時,它會創建一個.webpack檔案夾,所有編譯后的代碼都在其中。當我停止終端命令時,此檔案夾消失。
然后我想使用包@journeyapps/sqlcipher打開我的資料庫,但是這行導致主行程出錯:const sqlite3 = require("@journeyapps/sqlcipher")
錯誤是:
Error: Cannot find module '<<my_path>>/.webpack/main/native_modules/lib/binding/napi-v6-linux-x64/node_sqlite3.node'
軟體包檔案說明了將其與電子鍛造一起使用的兩件事:
確保檔案夾node_modules/@journeyapps/sqlcipher/lib/binding/napi-v6-linux-x64存在 -> 是的,它存在
使用 package.json 中的 electron-rebuild 的 onlyModules 選項禁用此庫的重建
"config": { "forge": { "electronRebuildConfig": { "onlyModules": [] // Specify other native modules here if required } }-> 我做到了,我添加了代碼行
我仍然有錯誤,但我覺得它可以“輕松”解決(對 webpack 的了解比我多得多)。事實上,一個解決方案是在我每次啟動應用程式時將檔案夾binding/napi-v6-linux-x64移動到.webpack中,對嗎?
我試圖這樣做electron-rebuild -f -w sqlite3,重建成功但沒有任何反應,我仍然有同樣的錯誤。
我被困在這里,無法為我的應用程式走得更遠,因為它依賴于讀取這個資料庫。我應該開始一個新專案并避免使用 webpack 嗎?您是否有成功匯入和使用此包的專案示例?
預先感謝您的幫助!
uj5u.com熱心網友回復:
好吧,我終于想通了。實際上,我嘗試了很多小改動,但我設法使應用程式(幾乎)按預期作業。
我是如何找到解決方案的
首先:這與庫 sqlcipher 本身沒有任何關系。其實是處理原生庫的時候webpack配置問題。
我使用 Webpack 和 Typescript 模板 () 開始了全新的電子鍛造最小安裝,npx create-electron-app test-electron-forge-github --template=typescript-webpack并添加了sqlite3和@journeyapps/sqlcipher。它起作用了,所以我對我的 Webpack 配置進行了很多更改,以使其更接近電子鍛造的配置。
警告
我所做的更改破壞了Redux。我選擇犧牲Redux來讓sqlcipher作業,因為今天我沒有找到讓他們都作業的解決方案。
舊的 Webpack 組態檔
main.webpack.js
module.exports = {
resolve: {
extensions: ['.ts', '.js']
},
entry: './electron/main.ts',
module: {
rules: require('./rules.webpack'),
}
}
渲染器.webpack.js
module.exports = {
resolve: {
extensions: ['.ts', '.tsx', '.js']
},
module: {
rules: require('./rules.webpack'),
},
}
規則.webpack.js
module.exports = [
{
test: /\.node$/,
use: 'node-loader',
},
{
test: /\.(m?js|node)$/,
parser: { amd: false },
use: {
loader: '@marshallofsound/webpack-asset-relocator-loader',
options: {
outputAssetBase: 'native_modules',
},
},
resolve: {
fullySpecified: false,
}
},
{
test: /\.(js|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
}
},
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
{
test: /\.(sass|less|css)$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'sass-loader',
]
},
]
新配置
main.webpack.js
module.exports = {
resolve: {
extensions: ['.ts', '.js']
},
entry: './electron/main.ts',
module: {
rules: [
...require('./rules.webpack'),
{
test: /\.(m?js|node)$/,
parser: { amd: true },
use: {
loader: '@vercel/webpack-asset-relocator-loader',
options: {
outputAssetBase: 'native_modules',
emitDirnameAll: true,
},
}
},
],
}
}
The native modules configuration has been moved from rules.webpack.js to main.webpack.js. This is due to a bug happening in the renderer process if the rule stayed in the rules.wepback.ts file.
The window would open but would stay blank. In the console, there would be an error: __dirname is not defined. See this Github issue from where I took the solution.
Also note that I changed the loader as the previous one was not doing properly its job.
renderer.webpack.js Unchanged
rules.webpack.js
module.exports = [
{
// We are specifying native_modules in the test because the asset relocator loader generates a
// "fake" .node file which is really a cjs file.
test: /native_modules\/. \.node$/,
use: 'node-loader',
},
{
test: /\.(js|ts|tsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
}
},
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
},
},
{
test: /\.(sass|less|css)$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'sass-loader',
]
},
]
Redux
I had to remove this property from rules.webpack.js:
resolve: {
fullySpecified: false,
}
This line was making Redux work.
Conclusion
我真的不知道這是如何作業的,但它確實有效。如果您像我一樣掙扎,我希望它會對您有所幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422621.html
標籤:
上一篇:Flaskwtforms資料和pic檔案更新不與sqliteDB同步
下一篇:如何回傳在表中多次出現的所有名稱
