Pouchdb Angular2 webpack问题

时间:2017-02-28 17:00:54

标签: javascript angular typescript webpack karma-jasmine

我使用Jasmine 2.5,Webpack 2.2.1和karma 1.3.0对我的Angular 2提供程序进行单元测试......

我已经在离子项目中使用我的提供程序并使用离子应用程序脚本构建它一切正常。

然而,当我尝试使用angular webpack进程构建它时出现问题

我使用Pouchdb并且其构造函数被严重翻译并且发生了错误。

  

未处理的承诺拒绝:pouchdb_browser_1.default不是构造函数;区域:ProxyZone;任务:Promise.then;值:TypeError:pouchdb_browser_1.default不是构造函数

如果我构建它,那么使用postbuild进程代码将pouchdb_browser_1.default更改为pouchdb_browser可以正常工作。

不幸的是,我无法更改将在测试期间执行的代码。

请参阅下面的 webpack.test.js

const helpers = require('./helpers'),
webpack = require('webpack'),
LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); `module.exports = {

devtool: 'inline-source-map',



resolve: {
    extensions: ['.ts', '.js'],
    modules: [helpers.root('src'), 'node_modules']
},

module: {
    rules: [{
        enforce: 'pre',
        test: /\.ts$/,
        loader: 'tslint-loader',
        exclude: [helpers.root('node_modules')]
    }, {
        enforce: 'pre',
        test: /\.js$/,
        loader: 'source-map-loader',
        exclude: [
            // these packages have problems with their sourcemaps
            helpers.root('node_modules/rxjs'),
            helpers.root('node_modules/@angular')
        ]
    }, {
        test: /\.ts$/,
        loader: 'awesome-typescript-loader',
        query: {
            // use inline sourcemaps for "karma-remap-coverage" reporter
            sourceMap: false,
            inlineSourceMap: true,
            module: "commonjs",
            removeComments: true
        },
        exclude: [/\.e2e\.ts$/]
    }, {
        enforce: 'post',
        test: /\.(js|ts)$/,
        loader: 'istanbul-instrumenter-loader',
        include: helpers.root('src'),
        exclude: [/\.spec\.ts$/, /\.e2e\.ts$/, /node_modules/]
    }],
},

plugins: [
    // fix the warning in ./~/@angular/core/src/linker/system_js_ng_module_factory_loader.js
    new webpack.ContextReplacementPlugin(
        /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
        helpers.root('./src')
    ),

    new LoaderOptionsPlugin({
        debug: true,
        options: {

            /**
             * Static analysis linter for TypeScript advanced options configuration
             * Description: An extensible linter for the TypeScript language.
             *
             * See: https://github.com/wbuchwalter/tslint-loader
             */
            tslint: {
                emitErrors: false,
                failOnHint: false,
                resourcePath: 'src'
            },

        }
    })
]};

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

听起来像是将CommonJS转换为ES6模块的问题。您是否以推荐的TypeScript方式导入PouchDB,即这样?

import * as PouchDB from 'pouchdb';