-1

我正在使用 webpack 2.3.3 使用 async/await javascript 语法构建我的 node.js 应用程序。转译是使用 babel-loader 6.4.1 完成的。我的package.json样子是这样的:

{ (...)
  "scripts": {
    "build": "rm -Rf dist; webpack -p --progress --colors --display-error-details --config webpack/prod.js",
    "postinstall": "npm run build",
    "start": "node dist/assets/server.js"
  },
  (...)
  "dependencies": {
    "babel-cli": "6.24.1",
    "babel-core": "6.24.1",
    "babel-loader": "6.4.1",
    "babel-plugin-transform-async-to-generator": "6.24.1",
    "babel-polyfill": "6.23.0",
    "babel-preset-es2015": "6.24.1",
    "babel-preset-react": "6.24.1",
    "babel-preset-stage-0": "6.24.1",
    "babel-preset-stage-1": "6.24.1",
    "eslint": "3.19.0",
    "eslint-loader": "1.7.1",
    "eslint-plugin-react": "6.10.3",
    (...)
    "webpack": "2.3.3"
  },
  (...)
}

我的 webpack 配置在配置文件中使用transform-async-to-generator带有行的插件:loaders: ['babel-loader?presets[]=es2015&presets[]=stage-0&presets[]=react&plugins[]=transform-async-to-generator', 'eslint-loader']webpack/prod.js

'use strict';

require('babel-polyfill');
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');

var nodeModules = {};

fs.readdirSync('node_modules')
    .filter(function(x) {
        return ['.bin'].indexOf(x) === -1;
    })
    .forEach(function(mod) {
        nodeModules[mod] = 'commonjs ' + mod;
    });

var serverConfig = {
    entry: ['babel-polyfill', './src/server'],
    target: 'node',
    externals: nodeModules,
    output: {
        path: path.resolve(__dirname, '../dist/assets/'),
        filename: 'server.js'
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin({
            compress: { warnings: false }
        })
    ],
    resolve: {
        extensions: ['.js', '.jsx']
    },
    module: {
        loaders: [
            {
                test: /\.jsx?$/,
                loaders: ['babel-loader?presets[]=es2015&presets[]=stage-0&presets[]=react&plugins[]=transform-async-to-generator', 'eslint-loader'],
                exclude: /node_modules/
            }
        ]
    }
};

module.exports = [ clientConfig, serverConfig ];

当我运行npm run build时,构建在包含async function foo(arg) { (...) }错误的行上失败Parsing error: Unexpected token function

当我删除异步/等待代码部分时,构建成功。

我在这里这里查看了解决方案,但无法构建 async/await 代码工作。

有人可以帮我吗?多谢

4

1 回答 1

3

解析错误发生在eslint-loader因为默认的 eslint 解析器不理解asyncawait. 您必须babel-eslint按照指定解析器中的说明使用。在您的 eslint 配置中添加:

"parser": "babel-eslint"

并安装babel-eslint

npm install --save-dev babel-eslint
于 2017-04-14T00:08:00.487 回答