Disable promise transpilation using babel-preset-env?

徘徊边缘 提交于 2020-08-05 19:21:03

问题


I'm using babel-preset-env (Babel 6) and I can't find out how to disable the "promise" transpilation.

I tried to use:

{
  "presets": [
    [
      "env",
      {
        "exclude": ["transform-async-to-generator", "es6.promise"]
      }
    ]
  ],
  "plugins": [
    "transform-object-rest-spread",
    "transform-class-properties",
    [
      "fast-async",
      {
        "spec": true,
        "compiler": { "promises": true, "generators": false }
      }
    ]
  ]
}

and, while it doesn't throws any errors (unlike it happens when an invalid option is passed), it still transpiles promises into runtimeGenerator functions.

How can I make so that babel-preset-env will transpile everything but preserve promises?


回答1:


Okay, now with the actual .babelrc and mention of fast-async and so on... A .babelrc like this seems to be enough to use fast-async instead of regenerator-runtime etc.

{
  "presets": [
    [
      "env",
      {
        "exclude": ["babel-plugin-transform-async-to-generator", "babel-plugin-transform-regenerator"]
      }
    ]
  ],
  "plugins": [
    [
      "fast-async",
      {
        "spec": true,
        "compiler": { "promises": true, "generators": false }
      }
    ]
  ]
}



回答2:


Promises shouldn't be transpiled to anything using babel-preset-env. async/await is, though. You can see here in the Babel playground that

const x = () => new Promise((res) => res('hi'));

const y = async () => {
  const z = await x();
};

gets transpiled to

'use strict';

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

var x = function x() {
  return new Promise(function (res) {
    return res('hi');
  });
};

var y = function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var z;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return x();

          case 2:
            z = _context.sent;

          case 3:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, undefined);
  }));

  return function y() {
    return _ref.apply(this, arguments);
  };
}();

using the default env settings. If you change the env string to, say, chrome>60 (or, in fact, interestingly >4%!), the code is passed through as-is, as Chrome > 60 supports async/await and arrow functions natively.



来源:https://stackoverflow.com/questions/52704820/disable-promise-transpilation-using-babel-preset-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!