简体   繁体   English

没有再生器的babel-plugin-transform-async-to-module-method

[英]babel-plugin-transform-async-to-module-method without regenerator

My goal is to have async/await compiled down to Bluebird promises with minimal performance impact. 我的目标是将async / await编译成Bluebird的承诺,同时将性能影响降至最低。

babel-plugin-transform-async-to-module-method appears to be the most common way to compile async/await to Bluebird, however it slows my application down by about 10-20% which is not acceptible. babel-plugin-transform-async-to-module-method似乎是将async / await编译为Bluebird的最常用方法,但它减慢了我的应用程序约10-20%,这是不可接受的。 I suspect that muchfun of this is due to regenerator, which seems to be required for babel-plugin-transform-async-to-module-method . 我怀疑这很多都是由于再生器,这似乎是babel-plugin-transform-async-to-module-method所必需的。

For instance, I have this code in index.js: 例如,我在index.js中有这个代码:

var Promise = require('bluebird');

async function foo() {
    console.log('foo');
    await Promise.delay(500);
    console.log('bar');
}

foo();

and this package.json: 而这个package.json:

{
  "name": "async-regenerator",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "browserify index.js -t babelify --outfile bundle.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-plugin-transform-async-to-module-method": "^6.7.0",
    "babel-preset-es2015": "^6.6.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.0"
  },
  "dependencies": {
    "bluebird": "^3.3.5"
  },
  "browserify": {
    "transform": [
      "babelify"
    ]
  },
  "babel": {
    "presets": [
      "es2015"
    ],
    "plugins": [
      [
        "transform-async-to-module-method",
        {
          "module": "bluebird",
          "method": "coroutine"
        }
      ]
    ]
  }
}

Compiling that with npm run build does work, but then running bundle.js produces an error: 使用npm run build编译它确实有效,但是运行bundle.js会产生错误:

ReferenceError: regeneratorRuntime is not defined

Adding regenerator to package.json does fix the error: 将regenerator添加到package.json会修复错误:

{
  "name": "async-regenerator",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "browserify index.js -t babelify --outfile bundle.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-plugin-transform-async-to-module-method": "^6.7.0",
    "babel-plugin-transform-runtime": "^6.7.5",
    "babel-preset-es2015": "^6.6.0",
    "babelify": "^7.2.0",
    "browserify": "^13.0.0"
  },
  "dependencies": {
    "babel-runtime": "^6.6.1",
    "bluebird": "^3.3.5"
  },
  "browserify": {
    "transform": [
      "babelify"
    ]
  },
  "babel": {
    "presets": [
      "es2015"
    ],
    "plugins": [
      [
        "transform-runtime",
        {
          "polyfill": false,
          "regenerator": true
        }
      ],
      [
        "transform-async-to-module-method",
        {
          "module": "bluebird",
          "method": "coroutine"
        }
      ]
    ]
  }
}

Then bundle.js does successfully run, but it makes my build 100kb larger and possibly introduces the aforementioned performance problem. 然后bundle.js成功运行,但它使我的构建更大100kb并可能引入上述性能问题。

My question is, why is regenerator even required for this? 我的问题是,为什么再生器甚至需要这个呢? My target browsers (Chrome and Firefox) support generators, so there must be some way to just use native generators, right? 我的目标浏览器(Chrome和Firefox)支持生成器,因此必须有一些方法才能使用本机生成器,对吧? I don't know if that'd fix my performance problem, but I'd like to try. 我不知道这是否能解决我的性能问题,但我想尝试一下。

I know there are a couple other similar approaches to async/await: 我知道有一些其他类似的async / await方法:

If I'm neglecting some other approach that you think would be good to try, please let me know. 如果我忽略了你认为可以尝试的其他方法,请告诉我。

I put the example code on https://github.com/dumbmatter/babel-async-await-regenerator - PRs are welcome! 我把示例代码放在https://github.com/dumbmatter/babel-async-await-regenerator上 - 欢迎PR!

You can use the async-to-generator plugin for this. 您可以使用async-to-generator插件。 But unfortunately the es2015 preset will still transform generators, so you will have to modify the es2015 preset. 但不幸的是,es2015预设仍将改变发电机,因此您必须修改es2015预设。 You could use modify-babel-preset , or just simply unfold the preset in your babel config. 您可以使用modify-babel-preset ,或者只是简单地展开您的babel配置中的预设。

"babel": {
  "plugins": [    
    "transform-es2015-template-literals",
    "transform-es2015-literals",
    "transform-es2015-function-name",
    "transform-es2015-arrow-functions",
    "transform-es2015-block-scoped-functions",
    "transform-es2015-classes",
    "transform-es2015-object-super",
    "transform-es2015-shorthand-properties",
    "transform-es2015-duplicate-keys",
    "transform-es2015-computed-properties",
    "transform-es2015-for-of",
    "transform-es2015-sticky-regex",
    "transform-es2015-unicode-regex",
    "check-es2015-constants",
    "transform-es2015-spread",
    "transform-es2015-parameters",
    "transform-es2015-destructuring",
    "transform-es2015-block-scoping",
    "transform-es2015-typeof-symbol",
    "transform-es2015-modules-commonjs",

    "transform-async-to-generator"
  ]
}

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

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