繁体   English   中英

使用webpack解析异步功能的问题

[英]Issues parsing async functions with webpack

我正在尝试让webpack解析使用新的async / await语法的javascript文件,但它一直给我一个解析错误。

这是我的webpack.config.js文件:

module.exports = {
  entry: {
    foo: './foo.js'
  },
  output: {
    filename: 'webpack-compiled.js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
}

我的package.json文件:

{
  "name": "async-func-test",
  "version": "1.0.0",
  "description": "",
  "main": "foo.js",
  "scripts": {
    "buildWithBabel": "babel foo.js --out-file babel-compiled.js",
    "buildWithWebpack": "webpack --progress --colors"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.18.0",
    "babel-core": "^6.18.2",
    "babel-loader": "^6.2.8",
    "babel-plugin-syntax-async-functions": "^6.13.0",
    "webpack": "^1.13.3"
  }
}

我的babel.rc文件:

{
  "plugins": [
    "syntax-async-functions"
  ]
}

和foo.js文件:

async function asyncFunc() {
  return 123
}

asyncFunc().then(x => console.log(x))

如果我运行npm脚本'buildWithBabel',它可以正常运行且没有错误,并创建具有正确输出的babel-compiled.js。

但是,如果我运行npm脚本“ buildWithWebpack”,则会收到以下错误消息:

 ERROR in ./foo.js Module parse failed: C:\\Users\\Redark\\Desktop\\asyncFuncTest\\node_modules\\babel-loader\\lib\\index.js!C:\\Users\\Redark\\Desktop\\asyncFuncTest\\foo.js Unexpected token (1:6) You may need an appropriate loader to handle this file type. SyntaxError: Unexpected token (1:6) 

我不需要转换异步函数,只需对其进行解析即可。 我不确定为什么它不适用于webpack,因为它应该使用.babelrc中的“ syntax-async-functions”插件,对吗?

您还需要使用transform-regenerator插件syntax-async-functions仅允许Babel解析输入(然后将其保留)。 Webpack尚不了解ES8语法,这就是为什么如果不对它们进行编译就会失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM