【问题标题】:Webpack Karma + Typescript with ES6Webpack Karma + Typescript 与 ES6
【发布时间】:2017-01-28 15:34:38
【问题描述】:

我正在尝试对用 typescript 编写的库运行 karma 测试,但即使 babel 已导入并配置为 es2015,也会不断收到 Uncaught SyntaxError: Unexpected token import。 (很遗憾,karma 从 v2.5 开始只支持 ES6)

我需要对配置进行哪些更改才能使事情正常进行?

karma.conf.js:

var webpackConfig = require('./webpack.config.test');

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            {
                pattern: './config/karma.tests.js',
                watched: false
            }
        ],
        preprocessors: {
            './config/karma.tests.js': ['babel', 'webpack', 'sourcemap']
        },
        plugins: [
            'karma-webpack',
            'karma-jasmine',
            'karma-sourcemap-loader',
            'karma-chrome-launcher',
            'karma-phantomjs-launcher',
            'karma-babel-preprocessor'
        ],
        babelPreprocessor: {
            options: {
                presets: ['es2015']
            }
        },
        webpack: webpackConfig,
        webpackMiddleware: {
            stats: 'errors-only'
        },
        webpackServer: {
            noInfo: true
        },
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: false,
        browsers: ['PhantomJS'],
        singleRun: true
    });
};

webpack.config.test.js

var webpack                         = require('webpack');
var helpers                         = require('./helpers');

var config = {
    devtool: 'inline-source-map',
    resolve: {
        root: helpers.root('src'),
        extensions: [ '', '.js', '.ts' ]
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                include: helpers.root('src'),
                exclude: helpers.root('node_modules')
            }
        ]
    },
}

module.exports = config;

tsconfig.json:

{
    "compileOnSave": false,
    "compilerOptions": {
        "declaration": true,
        "declarationDir": "declarations",
        "module": "commonjs",
        "moduleResolution": "node",
        "noImplicitAny": true,
        "outDir": "dist",
        "preserveConstEnums": true,
        "removeComments": false,
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,        
        "target": "es6",
        "typeRoots": [
            "node_modules/@types"
        ]
    },
    "exclude": [
        "node_modules",
        "dist"
    ]
}

【问题讨论】:

  • 这个错误实际发生在哪里?哪个文件/哪一行?
  • 任何文件;总是第一个使用 ES6 语言特性的。
  • 任何文件是什么意思? src 中的任何文件? node_modules? 中的任何文件你能发帖tsconfig.json?
  • 编辑添加tsconfig.json。是的,src 中包含importlet 等的任何文件。
  • 您使用的是什么版本的nodenpm

标签: typescript webpack karma-runner


【解决方案1】:

希望我能在这里解决您的问题。 我认为webpack 配置需要加载器来转换 ES6 文件,因此您的webpack.config.test.js 文件中需要加载器。

var webpack = require('webpack');
var helpers = require('./helpers');

var config = {
    devtool: 'inline-source-map',
    resolve: {
        root: helpers.root('src'),
        extensions: [ '', '.js', '.ts' ]
    },
    module: {
        loaders: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                include: helpers.root('src'),
                exclude: helpers.root('node_modules')
            },
                test: /\.js$/,
                exclude: helpers.root('node_modules'),
                loader: 'babel-loader'
        ]
    },
}

module.exports = config;

【讨论】:

  • 谢谢,在我像这样组合两个加载器后,这有效:babel-loader?presets[]=es2015!ts-loader。我还从karma.conf.js 中删除了所有babel 部分,并在karma.tests.json 中添加了require('babel-polyfill')。 (本质上,诀窍是使用 webpack 而不是 karma 来处理 babel。)
猜你喜欢
  • 2017-02-27
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 2017-06-24
  • 2017-11-27
  • 1970-01-01
  • 2016-06-02
  • 2016-05-10
相关资源
最近更新 更多