Webpack + Karma + Typescript + Ng1:未捕获的SyntaxError:意外的令牌导入

时间:2016-10-17 23:05:41

标签: angularjs typescript coffeescript webpack karma-webpack

我有以下karma.conf ...

var webpackConfig = require('./webpack.common.js');
webpackConfig.entry = {};
webpackConfig.plugins = [];
var globFlat = require('glob-flat');

var appFiles = globFlat.sync([
  './src/main/coffee/**/*Module.coffee',
  './src/main/coffee/**/*.coffee'

]);
var styleFiles = globFlat.sync([
]);
var dependencyFiles = [
  './src/main/typescripts/*.ts',
];
var testFiles = globFlat.sync([
  './test/main/**/*.coffee',
  './test/main/**/*.js'
]);

var files = dependencyFiles.concat(appFiles, styleFiles, testFiles);
module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'requirejs', 'chai-spies', 'chai'],
    files: files,
    preprocessors: {
      '../src/main/coffee/**/*.coffee': ['webpack'],
      '../src/main/typescripts/**/*.ts': ['webpack'],
      '../src/test/**/*.coffee': ['coffee']
    },

    webpack: webpackConfig,
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    concurrency: Infinity
  })
};

以及以下网站...

var webpack = require("webpack");
var glob = require('glob-all');
const helpers = require('./helpers');
var path = require("path");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var environment = process.env.NODE_ENV || 'development';
module.exports = function(options){
  console.log("Running in " + environment+ " mode");
  isProd = options.env === 'production';
  return {
    context: helpers.root(''),
    entry: {
      ... // Shouldn't matter since it is overridden
    },
    resolve: {
      extensions: ['', '.js', '.ts', '.coffee', '.pug', '.styl', '.less', '.css'],
      modules: [helpers.root('src'), 'node_modules']
    },
    output: {
      filename: "[name].js",
      path: path.join(helpers.root(''), "src/main/webapp")
    },
    module: {
      loaders: [
        {test: /\.pug$/, loader: "pug-html-loader"},
        {test: /\.ts$/, loader: 'ts'},
        {
          test: /\.coffee$/,
          loaders: ["coffee-loader", "coffee-import"]
        },
        {test: /\.html$/, loader: "html?interpolate=require&-minimize"},
        {
          test: /\.less$/,
          loader: "style-loader!css-loader!less-loader"
        },
        {test: /\.css$/, loader: "style-loader!css-loader"},
        // I have to add the regex .*? because some of the files are named like... fontello.woff2?952370
        {
          test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)(\?.*)?$/,
          loader: 'url?limit=900000'
        }
      ]
    },

    plugins: [
      new HtmlWebpackPlugin({
        template: helpers.root('src/main/html/index.html')
      })
    ]
  }
};

但出于某种原因,当我跑karma run时,我得到......

Chrome 53.*** (Mac OS X ***) ERROR
  Uncaught SyntaxError: Unexpected token import
  at src/main/typescripts/polyfills.ts:1

如果我做

preprocessors: {
      '../src/main/coffee/**/*.coffee': ['webpack'],
      '../src/main/typescripts/**/*.ts': ['webpack'],
      '../src/test/**/*.coffee': ['coffee']
},

像......一样...

preprocessors: {
      './src/main/coffee/**/*.coffee': ['webpack'],
      './src/main/typescripts/**/*.ts': ['webpack'],
      './src/test/**/*.coffee': ['coffee']
},

我得到了

  

路径:'/ _ karma_webpack_/src/main/coffee/utilities/view/My.coffee'}

     

错误:没有这样的文件或目录

更新

好的,所以这里是我可以告诉webpack没有在我的情况下运行的交易。这就是为什么转换不起作用的原因仍然是为什么?

我这样做几乎与https://github.com/AngularClass/angular2-webpack-starter完全相同。唯一的事情是,当我像module.exports = require('./config/karma.conf.js');那样引用业力时,它不起作用。如果我将实际的业力配置文件移动到根文件夹(它当前位于src/build/karma.conf.js

,这似乎没有问题。

更新2

我尝试将webpack.test.js移动到根文件夹并尝试再次配置使用该文件,但它仍然失败,所以我必须回到webpack.config.js

webpack.test.js

// TODO: I would like this in the source folder but karma-webpack fails

const webpack = require("webpack");
const webpackMerge = require('webpack-merge'); // used to merge webpack configs
const commonConfig = require('./src/build/webpack.common.js'); // the settings that are common to prod and dev
const glob = require('glob-all');
const tests = glob.sync([
  './src/test/webapp/**/*.coffee'
]);
const ENV = process.env.ENV = process.env.NODE_ENV = 'test';
module.exports = function (options) {
  return webpackMerge(commonConfig({env: ENV}), {
    entry: {
      tests: '../src/test/**/*.coffee'
    },
    plugins: [
      new webpack.optimize.CommonsChunkPlugin({
        names: ['tests', 'styles', 'app', 'vendor', 'polyfills'],
        minChunks: Infinity
      })
    ]
  })
};

webpack.config.js

var environment = process.env.NODE_ENV || 'development';
switch (environment) {
  case 'prod':
  case 'production':
    module.exports = require('./src/build/webpack.prod')({env: 'production'});
    break;
  case 'test':
  case 'testing':
    module.exports = require('./webpack.test.js')({env: 'test'});
    break;
  case 'dev':
  case 'development':
    module.exports = require('./src/build/webpack.dev')({env: 'development'});
    break;
  default:
    module.exports = require('./src/build/webpack.common')({env: 'common'});

1 个答案:

答案 0 :(得分:0)

  

未捕获的SyntaxError:意外的令牌导入

清除症状,即在传递给JS运行时之前没有编译TypeScript。

修复

更改

{test: /\.ts$/, loader: 'ts'},

致:

{test: /\.ts$/, loader: 'ts-loader'},

更多

请参阅快速入门:https://github.com/TypeStrong/ts-loader#configuration