Webpack - Typescript和Babel"模块构建失败:SyntaxError"

时间:2017-05-09 00:09:25

标签: angularjs angular typescript webpack babel

运行node_modules\.bin\webpack -p --display-error-details时,我收到了捆绑的许多不同文件的错误消息Module build failed: SyntaxError...Unexpected token, expected ,

我的webpack.config.js:

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        polyfills: './app/polyfills.ts',
        app: './app/main.ts'
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js',
        sourceMapFilename: '[file].map'
    },
    module: {
        rules: [
            //Order of loaders is important!
            {
                test: /\.ts(x?)$/,
                exclude: ['node_modules'],
                use: [{ loader: 'babel-loader' }, { loader: 'angular-router-loader' }, { loader: 'angular2-template-loader' }],
            },
            {
                test: /\.sass$/,
                exclude: ['node_modules'],
                use: [{ loader: 'css-to-string-loader' }, { loader: 'raw-loader' }, { loader: 'sass-loader', options: { sourceMap: true, sourceMapContents: true } }]
            },
            {
                test: /\.css$/,
                exclude: ['node_modules'],
                use: [{ loader: 'css-to-string-loader' }, { loader: 'raw-loader' }, { loader: 'css-loader' }]
            },
            {
                test: /\.html$/,
                exclude: ['node_modules', 'index.html'],
                use: [{ loader: 'raw-loader' }]
            },
            {
                test: /\.(otf|eot|svg|png|ttf|woff|woff2).*$/,
                exclude: ['node_modules'],
                use: [{ loader: 'url-loader' }]
            }
        ]
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js', '.css'],
        modules: ["node_modules", "."]
    },
    plugins: [

      new webpack.optimize.CommonsChunkPlugin({
          names: ['vendor'],
          minChunks: function (module, count) {
              // creates a common vendor js file for libraries in node_modules
              return module.context && module.context.indexOf('node_modules') !== -1;
          }
      }),

      new webpack.optimize.CommonsChunkPlugin({
          names: ['meta'],
          chunks: ['vendor', 'app']
      }),

      new HtmlWebpackPlugin({
          template: 'index.cshtml',
          filename: 'index.cshtml',
          inject: 'head'
          , chunks: ['app', 'vendor', 'polyfills', 'meta']
          , chunksSortMode: 'dependency'
          , hash: false
      }),

      new webpack.ContextReplacementPlugin(
        /angular(\\|\/)core(\\|\/)@angular/, //Update for Angular 4
        root('./app'),
        {}
      ),
    ]
};
function root(__path) {
    return path.join(__dirname, __path);
}

我的.babelrc

{
  "presets": ["es2015", "stage-0"],
  "plugins": ["transform-decorators-legacy"]
}

我之前没有使用webpack的经验,所以非常感谢任何帮助。我使用的是Angular 4.1.0和babel 6.24.1。

1 个答案:

答案 0 :(得分:3)

我认为你应该使用ts-loader而不是babel(或两者的结合): 试试

npm i -D ts-loader

    {
        test: /\.ts(x?)$/,
        exclude: ['node_modules'],
        use: [{ loader: 'babel-loader' }, { loader: 'ts-loader' }, { loader: 'angular-router-loader' }, { loader: 'angular2-template-loader' }],
    },