gpt4 book ai didi

webpack - 带有 sqlite3 和 webpack 的 Electron 打包器

转载 作者:行者123 更新时间:2023-12-03 17:46:41 87 4
gpt4 key购买 nike

我正在制作一个使用 sqlite3 获取/存储数据的小型应用程序.

在开发阶段一切都很好。但是当我使用 electron-packager打包我的应用程序,sqlite3 不再工作了。

控制台显示异常:找不到模块 'sqlite3'sqlite 3 exception

这是我的渲染器配置:

return {
target: 'electron-renderer',
mode: argv.production ? 'production' : 'development',
context: paths.root,
devtool: !argv.production ? 'source-map' : false,
entry: {
'app': path.resolve(paths.app, 'app.js')
},
optimization: {
runtimeChunk: false,
splitChunks: {
chunks: 'all',
cacheGroups: {
default: {
enforce: true,
priority: 1
},
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: 2,
name: 'vendors',
enforce: true,
chunks: 'async'
}
}
}
},
module: {
rules: require('./rule')(paths, argv).get()
},
plugins: require('./plugin')(paths, argv).get(),
output: {
path: paths.dist,
filename: 'app.bundled.js'
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"],
alias: {
// summernote: codemirror
'CodeMirror': 'codemirror',
}
},
watch: !argv.production,
watchOptions: {
poll: false
},
externals: {
sqlite3: 'commonjs sqlite3'
}
};

这是我在其中包含 sqlite3 的渲染器进程的文件:

const typeorm = require('typeorm');
const EntitySchema = typeorm.EntitySchema;
const path = require('path');
const electron = require('electron');
require('sqlite3');

module.exports = (ngModule) => {
ngModule.service('$db', (toastr) => {

//#region Properties

// Instance of database connection
let dbConnection = null;

//#endregion

let out = {

//#region Methods
if (dbConnection == null || forceReinitialize) {
// Build a absolute path to database.
const appPath = electron.remote.app.getAppPath();
const dbPath = path.join(appPath, 'assets/db/PersonalCv.db');
return typeorm
.createConnection({
type: "sqlite",
database: dbPath,
synchronize: false,
entities: [
new EntitySchema(require('../models/entities/user')),
new EntitySchema(require('../models/entities/user-decription')),
new EntitySchema(require('../models/entities/skill-category')),
new EntitySchema(require('../models/entities/skill')),
new EntitySchema(require('../models/entities/personal-skill')),
new EntitySchema(require('../models/entities/project')),
new EntitySchema(require('../models/entities/project-skill')),
new EntitySchema(require('../models/entities/project-responsibility')),
new EntitySchema(require('../models/entities/responsibility'))
]
})
.then((connection) => {
dbConnection = connection;
return dbConnection;
})
.catch((error) => {
toastr.error(error);
throw error;
});
}

return new Promise(resolve => {
resolve(dbConnection);
});
},

/*
* Get repository by using name (table name)
* */
getRepository: (name) => {
return out
.getConnection()
.then((connection) => {
return connection.getRepository(name);
});
}

//#endregion
};

return out;
});
};

这是我的 repo以防万一有人需要更多信息。

有人能帮帮我吗?

谢谢,

最佳答案

在完成了一些关于electron-packagerelectron-builder 的问答之后。我找到了一种使 sqlite3webpack 一起工作的解决方案。

webpack.config.js 中,我添加了:

externals: {
sqlite3: 'commonjs sqlite3'
}

我使用 electron-builder 而不是 electron-packager 来构建我的应用程序。在 build 配置中,我将 sqlite3 模块复制到 dist 文件夹。

关于webpack - 带有 sqlite3 和 webpack 的 Electron 打包器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50991453/

87 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com