1

我有以下 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
  })
};

和下面的 webpack...

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。唯一的问题是,当我像他们一样引用 karma conf 时,它不起作用module.exports = require('./config/karma.conf.js');。如果我将实际的 karma conf 移动到根文件夹中似乎没有问题(它当前位于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'});
4

1 回答 1

0

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

TypeScript 在传递到 JS 运行时之前没有被编译的明显症状。

使固定

改变

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

至:

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

更多的

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

于 2016-10-18T07:05:40.230 回答