ES7异步等待功能与巴贝尔装载机不工作 [英] ES7 async await functions with babel-loader not working

查看:232
本文介绍了ES7异步等待功能与巴贝尔装载机不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行的异步使用的WebPack的通天装载机在JavaScript中等待功能。我使用了以下配置:

  {
  名称:'客户',
  背景:path.join(__目录名称,'src'中,'静','脚本'),
  项:{
    索引:./index.js
  },
  输出:{
    路径:path.join(__目录名称,'src'中,'静','捆绑'),
    文件名:[名] .js文件'
  },
  模块:{
    装载机:
      {
        测试:/\\.js$/,
        装载机:巴别塔,
        查询:{
          $ P $的pset:ES2015,阶段0]
        }
      }
    ]
  },
  解析:{
    根:path.join(__目录名)
    回退:path.join(__目录名称,node_modules'),
    modulesDirectories:['node_modules'],
  }
}

但它一直力推的错误以下消息:


  

模块构建失败:错误:./src/static/scripts/index.js:预期类型标识使用选项{}


我index.js有这样的内容:

 的console.log('喜从应用程序');异步函数你好(){
  返回Promise.resolve('你好')
}异步函数谈话(){
  常量HI =等待你好()
  的console.log(HI);
}


解决方案

它看起来像你打一个错误。这似乎是解决,但尚未公布。

如果您不能等待直到它被释放,你可以申请相应的提交补丁,解决了问题。

应用补丁的方法之一:

保存下列差异到一个文件中( visit.patch 为例)在你的包根文件夹。然后用应用补丁的git申请visit.patch


 从940b86dadbd0151c33c02e89f0b5ff61077c9214周一09月17日00:00:00 2001年
来源:亨利朱< hi@henryzoo.com>
日期:星期四,2015年11月5日20时10分十五秒-0500
主题:[PATCH]变换再生:node.id设置标识符,如果空 -
 修正#2835---
 包/巴贝尔-插件 - 转换 - 再生/ lib目录/ visit.js | 8 +++++ ---
 1文件改变了,5插入(+),3缺失( - )差异--git A / node_modules / babel- preSET-ES2015 / node_modules /巴贝尔-插件 - 转换 - 再生/ lib目录/ visit.js B / node_modules / babel- preSET-ES2015 / node_modules /巴贝尔,插件-transform - 再生/ lib目录/ visit.js
指数0f68ffc..c4a0d2e 100644
--- A / node_modules / babel- preSET-ES2015 / node_modules /巴贝尔-插件 - 转换 - 再生/ lib目录/ visit.js
+++ B / node_modules / babel- preSET-ES2015 / node_modules /巴贝尔-插件 - 转换 - 再生/ lib目录/ visit.js
@@ -146,6 +146,10 @@功能getOuterFnExpr(funPath){
   VAR节点= funPath.node;
   t.assertFunction(节点);如果+(!node.id){
+ node.id = funPath.scope.parent.generateUidIdentifier(被调用者);
+}
+
   如果(node.generator&放大器;&放大器; //非发生器功能不需要进行标记。
       t.isFunctionDeclaration(节点)){
     变种PP = funPath.findParent(功能(路径){
@@ -171,9 +175,7 @@功能getOuterFnExpr(funPath){
     );
   } - 回报node.id || (
- node.id = funPath.scope.parent.generateUidIdentifier(被叫方)
- );
+回报node.id;
 } 功能getRuntimeMarkDecl(blockPath){


更新:

看来,<一个href=\"https://github.com/babel/babel/commit/940b86dadbd0151c33c02e89f0b5ff61077c9214#commitcomment-14259015\"相对=nofollow>错误是没有正确固定和承诺已恢复。看起来这是由于已在此拉申请另一个问题。

I'm trying to run async await functions in JavaScript using the babel-loader of webpack. I'm using the following configuration:

{
  name: 'client',
  context: path.join(__dirname, 'src', 'static', 'scripts'),
  entry: {
    index: './index.js'
  },
  output: {
    path: path.join(__dirname, 'src', 'static', 'bundles'),
    filename: '[name].js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'stage-0']
        }
      }
    ]
  },
  resolve: {
    root: path.join(__dirname),
    fallback: path.join(__dirname, 'node_modules'),
    modulesDirectories: ['node_modules'],
  }
}

but it keeps pushing an error with the following message:

Module build failed: Error: ./src/static/scripts/index.js: Expected type "Identifier" with option {}

My index.js has this content:

console.log('hi from app');

async function hello() {
  return Promise.resolve('hi')
}

async function conversation () {
  const hi = await hello()
  console.log(hi);
}

解决方案

It looks like you hit a bug. It seems to be solved but not released yet.

If you cannot wait until it is released, you can apply the patch corresponding to the commit that solves the issue.

One way to apply the patch:

Save the following diff into a file (visit.patch for example) in your package root folder. Then apply the patch with git apply visit.patch.


From 940b86dadbd0151c33c02e89f0b5ff61077c9214 Mon Sep 17 00:00:00 2001
From: Henry Zhu <hi@henryzoo.com>
Date: Thu, 5 Nov 2015 20:10:15 -0500
Subject: [PATCH] transform-regenerator: set node.id to an identifier if null -
 fixes #2835

---
 packages/babel-plugin-transform-regenerator/lib/visit.js | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/node_modules/babel-preset-es2015/node_modules/babel-plugin-transform-regenerator/lib/visit.js b/node_modules/babel-preset-es2015/node_modules/babel-plugin-transform-regenerator/lib/visit.js
index 0f68ffc..c4a0d2e 100644
--- a/node_modules/babel-preset-es2015/node_modules/babel-plugin-transform-regenerator/lib/visit.js
+++ b/node_modules/babel-preset-es2015/node_modules/babel-plugin-transform-regenerator/lib/visit.js
@@ -146,6 +146,10 @@ function getOuterFnExpr(funPath) {
   var node = funPath.node;
   t.assertFunction(node);

+  if (!node.id) {
+    node.id = funPath.scope.parent.generateUidIdentifier("callee");
+  }
+
   if (node.generator && // Non-generator functions don't need to be marked.
       t.isFunctionDeclaration(node)) {
     var pp = funPath.findParent(function (path) {
@@ -171,9 +175,7 @@ function getOuterFnExpr(funPath) {
     );
   }

-  return node.id || (
-    node.id = funPath.scope.parent.generateUidIdentifier("callee")
-  );
+  return node.id;
 }

 function getRuntimeMarkDecl(blockPath) {


Update:

It seems that the bug wasn't properly fixed and the commit has been reverted. Looks like it was due to another problem that has been addressed in this pull request.

这篇关于ES7异步等待功能与巴贝尔装载机不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆