无法加载“ webpack”,未注册!也许您缺少一些插件?

时间:2018-07-03 11:33:51

标签: webpack karma-webpack

错误[预处理]:无法加载“ webpack”,未注册! 也许您缺少一些插件?

Webpack版本: 4.14.0

Webpack Karma版本: 2.0.4

业力版本: 3.0.0

Linux

 module.exports = ( config ) => {
      config.set( {
        basePath: path.resolve( __dirname ),
        browsers: [ 'PhantomJS' ],
        files: [ {
          pattern: '../../../spec/**/*.spec.js',
          watch: true
        } ],
        preprocessors: {
          [ '../../../spec/**/*.spec.js' ]: [ 'webpack' ]
        },
        reporters: [ 'mocha' ],
        plugins: [
          'karma-webpack',
          'karma-mocha',
          'karma-phantomjs-launcher',
          'karma-mocha-reporter',
        ],
        webpack: {
          mode: 'development',
          target: 'web',
          module: {
            rules: [ {
              test: /\.js$/,
              exclude: /node_modules/,
              use: {
                loader: 'babel-loader'
              }
            } ]
          },
          resolve: {
            extensions: [ '.js', '.jsx ' ],
            modules: [ 'node_modules', 'src' ],
          }
        },
        webpackMiddleware: {
          noInfo: true,
          stats: {
            chunks: false
          }
        },
      } )
    } 

1 个答案:

答案 0 :(得分:0)

  • 不推荐使用phantomjs,而推荐使用无头镀铬。
  • 在上面的示例中,我使用了几个不同的组件。茉莉花在摩卡咖啡上,茉莉花在JavaScript上。
  • 此外,如果您使用的是Angular JS,则加载文件的angular和angular-mocks部分非常重要:[]
  • 就我的预处理器[webpack]而言,我必须在插件中添加所有四个'karma-jasmine','karma-chrome-launcher','karma-webpack','karma-typescript-preprocessor' :[]
  • 很高兴也可以发布我的webpack配置,这很标准。此错误与webpack配置无关,全部在karma.conf.js中。

希望这会有所帮助!

这是我正在工作的karma.conf.js

'use strict';
var webpackConfig = require('./webpack.test.js');//webpack config for test

process.env.CHROME_BIN = require('puppeteer').executablePath();//Node library for headless Chrome, https://developers.google.com/web/tools/puppeteer/

module.exports = (config) => {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        plugins: [
            'karma-jasmine',
            'karma-chrome-launcher',
            'karma-webpack',
            'karma-typescript-preprocessor'
        ],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['HeadlessChrome'],
        customLaunchers: {
            HeadlessChrome: {
                base: 'ChromeHeadless',
                flags: [
                    '--no-sandbox',
                    '--headless',
                    '--disable-gpu',
                    '--disable-translate',
                    '--disable-extensions'
                ]
            }
        },
        singleRun: process.argv.indexOf("--watch") === -1,
        autoWatchBatchDelay: 300,
        files: [
            './node_modules/angular/angular.js',
            './node_modules/angular-mocks/angular-mocks.js',
            './src/test.ts'
        ],
        preprocessors: {
            './src/test.ts': ['webpack'],
        },
        client: {
            // This is important as there are a lot of tests that are reliant in the order that they were written.
            jasmine: {
                random: false
            }
        },
        webpack: webpackConfig,
        webpackMiddleware: {
            stats: {
                /* https://github.com/TypeStrong/ts-loader/commit/eb8bbf8e11779487d3af527f8b70644316075ee8
*/
                warningsFilter: /export .* was not found in/
            }
        }
    });
};