带有babel-loader属性类型的Webpack

时间:2016-07-10 12:07:41

标签: node.js ecmascript-6 webpack babeljs

我正在尝试使用Webpack将我的节点打字稿应用程序(使用exps js服务器)迁移到ES2015。 首先,我想使用属性类型。我创建了,而不是我需要使用babel-loader和eslint。所以,我安装了这个软件包并将其实现到我的快递应用程序。:

这是我的 webpack.config.js

const webpack = require('webpack');
const WebpackErrorNotificationPlugin = require("webpack-error-notification");
module.exports = 
{
    entry: './src/test/test.js',
    target: 'node',
    debug: true,
    output: {
        path: './bin',
        filename: 'test.bundle.js',
    },
    module: {
        preLoaders: [{
            test: /\.js?$/,
            loaders: ['eslint'],
            exclude: /node_modules/
        }],
        loaders: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loader: 'babel',
        }]
    },
    eslint: {
        failOnWarning: false,
        failOnError: true
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin({
            compress: {
                warnings: false,
            },
            output: {
                comments: false,
            },
        }),
        new WebpackErrorNotificationPlugin()
    ]
}

.eslintrc

{
  "parser": "babel-eslint",
  "parserOptions": {
    "sourceType": "module",
    "allowImportExportEverywhere": false
  }
}

其中一个错误代码:

export class CmsUserModel {
    /**
     * (description)
     * 
     * @type {String}
     */
    template: string;

    /**
     * (description)
     * 
     * @type {string}
     */
    pages: string[];

    constructor() {
        this.template = "";
        this.pages = new Array<string>();
    }

       public static ToModel(model: ICmsUserModel) {
         return <CmsUserModel>{
             template : model.template,
             pages : model.pages
         };       
     }
}

有两个问题。首先是,然后没有太多指定babal-eslint错误处理。例如:

ERROR in ./src/models/cmsUserModel.js

/Users/petrtomasek/Projects/cms/src/models/cmsUserModel.js
  44:40  error  Parsing error: Unexpected token

✖ 1 problem (1 error, 0 warnings)


ERROR in ./src/models/cmsUserModel.js
Module build failed: Error: Module failed because of a eslint error.

/Users/petrtomasek/Projects/cms/src/models/cmsUserModel.js
  44:40  error  Parsing error: Unexpected token

✖ 1 problem (1 error, 0 warnings)

没关系,但我希望看到标准的错误处理,如默认的webpack和带有打印当前错误行的babel加载器,如果可能的话......?

第二个错误是,然后我仍然不能使用属性类型,例如:template: string

请问有什么问题? 谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试以下步骤,

1. npm install --save-dev babel-preset-es2015
2. Add query: { presets: ['es2015'] } to your webpack config with the babel loader.

loaders: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loader: 'babel',
            query: { presets: ['es2015'] }
        }]