带sqlite3和Webpack的电子包装机

时间:2018-06-22 15:42:44

标签: webpack sqlite electron electron-packager

我正在制作一个小型应用程序,该应用程序使用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

有人可以帮我吗?

谢谢

2 个答案:

答案 0 :(得分:0)

sqlite3是一个本地模块,具体取决于系统体系结构。 Electron-packager并没有做任何特殊的事情来帮助您编译本机模块。

因此您可以使用https://github.com/kripken/sql.js/之类的纯JS SQLite软件包

或者尝试使用带有“安装后”脚本的电子构建器:https://github.com/electron-userland/electron-builder

另请参阅:https://electronjs.org/docs/tutorial/using-native-node-modules

答案 1 :(得分:0)

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

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

externals: {
    sqlite3: 'commonjs sqlite3'
}

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