【问题标题】:How to include vendor bundles from webpack for use in karma test runner如何在 karma 测试运行器中包含来自 webpack 的供应商捆绑包
【发布时间】:2018-06-17 08:16:35
【问题描述】:

我有一个用 TypeScript 编写的 AngularJS 1.x 项目,并且正在使用 Webpack。我现在正在尝试使用我为我的一项服务编写的示例测试来设置 karma-typescript 模块。

我遵循了来自here 的示例业力配置文件,所以现在就拥有这个:

module.exports = function (config) {
  config.set({
    basePath: '../',
    frameworks: ['jasmine', 'karma-typescript'],
    files: [
            //Add library includes here...
      'app/**/*.ts'
    ],
    exclude: [
    ],
    preprocessors: {
      'app/**/*.ts': ['karma-typescript']
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: Infinity
  })
}

当我运行我的测试任务时,我收到一条错误消息,提示找不到 angular。我意识到为什么,因为在我的业力配置文件中,我还没有添加任何应用程序所需的库。除了单独添加这些之外,有没有办法让我将业力加载到我的 webpack 配置文件设置为构建的供应商捆绑文件中?:

const path = require('path');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const copy = require('copy-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {
  entry: {
    main: './app/app.module.ts',
    vendor: [
      'jquery',
      'angular',
      'angular-animate',
      'angular-aria',
      'angular-cookies',
      'angular-material',
      'angular-messages',
      'angular-mocks',
      'angular-resource',
      'angular-sanitize',
      'angular-ui-bootstrap',
      'angular-ui-router',
      'angular-local-storage',
      'bootstrap',
      'less',
      'lodash',
      'moment',
      'ui-select',
      'leaflet',
      'iso-currency',
      'angular-leaflet-directive'
    ],
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].bundle.js',
  },
  module: {
    rules: [
      {
        test: require.resolve('jquery'),
        use: [{
          loader: 'expose-loader',
          options: '$'
        }]
      }, {
        test: /\.less$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'less-loader']
        })
      }, {
        test: require.resolve('moment'),
        use: [{
          loader: 'expose-loader',
          options: 'moment'
        }]
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
        loader: 'url-loader'
      },
      {
        enforce: 'pre',
        test: /\.tsx?$/,
        use: 'source-map-loader'
      },
      {
        test: /\.ts?$/,
        exclude: /node_modules/,
        use: 'ts-loader'
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js']
  },
  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks: Infinity
    }),
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: 'jquery',
      'window.jQuery': 'jquery'
    }),
    new ExtractTextPlugin('./app.css'),
    new CleanWebpackPlugin(['dist']),
    new copy([
      { from: 'app' },
      { from: 'index.html' },
      { from: 'app/assets/fonts', to: 'assets/fonts' },
      { from: 'app/assets/iln18Support', to: 'assets/iln18Support' },
      { from: 'app/assets/images', to: 'assets/images' },
      { from: 'app/partials', to: 'partials' }
    ])
  ]
};

那么有没有办法告诉 karma 包含 webpack 输出的 vendor.bundle.js 文件?

我正在努力寻找好的示例\文档,所以如果有人可以向我推荐任何对我有帮助的东西,我将不胜感激!

谢谢

【问题讨论】:

    标签: angularjs typescript webpack karma-runner karma-webpack


    【解决方案1】:

    通过创建一个名为 app/vendor.bundle.ts 的新文件解决了这个问题。

    我现在将它们放在捆绑文件中,而不是通过 webpack 配置导入所有外部供应商。

    vendor.bundle.ts 文件本身看起来像这样:
    (我也认为将供应商与 webpack 配置分开的方法使 webpack 配置在这里更加干净)

    import 'jquery';
    import 'angular';
    import 'angular-animate';
    import 'angular-aria';
    ...
    

    所以在我的 webpack 配置中,我现在只有一个入口点,如下所示:

    module.exports = {
      entry: {
        vendor: './app/vendor.bundle.ts',
        main: './app/app.module.ts',
      }
    }
    ...
    

    在我的karma.conf.js 中,我现在可以像这样将供应商导入为单个入口点(请注意,我的应用在这里也是一个入口点):

    module.exports = function (config) {
      config.set({
        files: [
          // Vendors
          'app/vendor.bundle.ts',
          // App
          'app/app.module.ts',
    
          // Load tests
          'app/**/*spec.ts'
        ],
        preprocessors: {
          'app/**/*.ts': ['karma-typescript']
        },
        ...
      })
    }
    

    【讨论】:

    • 谢谢,现在几乎所有的文档都已经过时了,几乎没有人像这样使用没有 babel 的 typescript。这既简单又优雅:)
    猜你喜欢
    • 2018-07-12
    • 2016-10-30
    • 2017-08-12
    • 2014-04-15
    • 2016-08-02
    • 2023-03-23
    • 2017-10-25
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多