4

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

我从这里遵循了示例业力配置文件,所以现在有这个:

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 文件?

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

谢谢

4

1 回答 1

2

Solved it by creating a new file called app/vendor.bundle.ts.

Instead of importing all external vendors through the webpack config I now have them in the bundle file.

The vendor.bundle.ts file itself looks something like this:
(I also think the approach to seperate the vendors from the webpack config keeps the webpack config more clean here)

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

So in my webpack config I only have one entry point now, like this:

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

And in my karma.conf.js I can now import the vendor as a single entry point like this (Note that my app is also an single entry point here):

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']
    },
    ...
  })
}
于 2018-11-07T15:53:21.210 回答