【问题标题】:electron-packager with sqlite3 and webpack带有 sqlite3 和 webpack 的电子打包器
【发布时间】:2018-12-02 03:20:18
【问题描述】:

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

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

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

这是我的渲染器配置:

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,以防有人需要更多信息。

谁能帮帮我?

谢谢,

【问题讨论】:

    标签: webpack sqlite electron electron-packager


    【解决方案1】:

    经过electron-packagerelectron-builder 的一些问答。我找到了一种使sqlite3webpack 一起使用的解决方案。

    webpack.config.js,我补充说:

    externals: {
        sqlite3: 'commonjs sqlite3'
    }
    

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

    【讨论】:

    • 谢谢,我的项目中缺少的部分是commonjs,我只有sqlite3,没有成功,但添加commonjs sqlite3后,sqlite正在工作
    【解决方案2】:

    sqlite3 是一个本地模块,取决于系统架构。 Electron-packager 没有做任何特别的事情来帮助你编译原生模块。

    所以您可以使用纯 JS SQLite 包,例如 https://github.com/kripken/sql.js/

    或者试试 electron-builder : https://github.com/electron-userland/electron-builder 它有一个“安装后”脚本。

    另见:https://electronjs.org/docs/tutorial/using-native-node-modules

    【讨论】:

    • 我正在使用 webpack 来捆绑我的源代码。我可以用electron-builder 做什么来处理sqlite3
    猜你喜欢
    • 2021-09-26
    • 2017-10-31
    • 2017-02-04
    • 2016-06-25
    • 2016-11-30
    • 2016-12-10
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多