Karma,Webpack,Babel奇怪的导入行为

时间:2017-08-10 15:28:50

标签: typescript testing karma-mocha karma-webpack

问题:

首次导入模块时,导出的函数未定义。 例外情况下,模块已定义。

为什么不定义!?!?

情景:

主文件

karma start

正如您所看到的,此功能中有一些控制台日志。 PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'init', 'undefined', 'undefined', 'object' PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'classes', [undefined] PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'on catch', function UserClass() { ... } PhantomJS 2.1.1 (Windows 8 0.0.0) LOG: 'undefined is not an object (evaluating 'cl.className')' PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 1 of 1 SUCCESS (0.933 secs / 0.928 secs) 上的输出是:

import { FaunaClass } from '../class';

export class UserClass extends FaunaClass {

  static className = 'users';

}

UserClass.ts:

const webpackConfig = require('./webpack.config');
const webpack = require('webpack');

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai', 'sinon'],

    files: [
      'src/**/*.spec.ts'
    ],

    preprocessors: {
      '**/*.ts': ['webpack', 'sourcemap']
    },

    webpack: {
      module: webpackConfig.module,
      resolve: webpackConfig.resolve,
      devtool: 'inline-source-map'
    },

    // Webpack please don't spam the console when running in karma!
    webpackServer: { noInfo: true },

    reporters: ['progress'],
    colors: true,
    autoWatch: true,
    logLevel: config.LOG_INFO,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: 'Infinity'
  });
};

**配置文件:**

karma.conf.js

var path = require('path');

module.exports = {
  entry: './handler.ts',
  target: 'node',
  module: {
    loaders: [
      {
        test: /\.tsx?$/,
        loaders: ['babel-loader', 'ts-loader'],
        exclude: [/node_modules/]
      },
      { test: /\.json$/, loader: 'json-loader' },
    ]
  },
  resolve: {
    extensions: ['.ts', '.js', '.tsx', '.jsx']
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: 'handler.js'
  }
};

Webpack配置:

UserClass

因为您可以看到UserClass的日志记录在开始时未定义,并且在promise中抛出异常时,类将被定义。

我感觉由于某种原因它在导出"(.*?)" 之前执行代码,这导致它在那一刻被取消定义。

或者我的配置文件完全坏了。

0 个答案:

没有答案