简体   繁体   English

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

[英]Issues parsing async functions with webpack

I'm trying to get webpack to parse a javascript file that is using the new async/await syntax, but it keeps giving me a parsing error. 我正在尝试让webpack解析使用新的async / await语法的javascript文件,但它一直给我一个解析错误。

Here is my webpack.config.js file: 这是我的webpack.config.js文件:

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

My package.json file: 我的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"
  }
}

My babel.rc file: 我的babel.rc文件:

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

And the foo.js file: 和foo.js文件:

async function asyncFunc() {
  return 123
}

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

If I run the npm script 'buildWithBabel', it runs fine with no errors and creates the babel-compiled.js with the proper output. 如果我运行npm脚本'buildWithBabel',它可以正常运行且没有错误,并创建具有正确输出的babel-compiled.js。

However if I run the npm script 'buildWithWebpack', I get the following error message: 但是,如果我运行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) 

I don't need to transform the async functions, just parse it. 我不需要转换异步函数,只需对其进行解析即可。 I'm not sure why it's not working for webpack as it should using the "syntax-async-functions" plugin in the .babelrc right? 我不确定为什么它不适用于webpack,因为它应该使用.babelrc中的“ syntax-async-functions”插件,对吗?

You will need to use the transform-regenerator plugin as well, syntax-async-functions only allows Babel to parse the input (and then leave it alone). 您还需要使用transform-regenerator插件syntax-async-functions仅允许Babel解析输入(然后将其保留)。 Webpack doesn't understand ES8 syntax yet, that's why it fails without having them transpiled. Webpack尚不了解ES8语法,这就是为什么如果不对它们进行编译就会失败。

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

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