babel: [Bug]: Generator functions cannot be converted using only @babel/plugin-transform-runtime

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

function* foo() {}

Configuration file name

babel.config.json

Configuration

{
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "helpers": true,
        "regenerator": true,
        "version": "^7.23.8"
      }
    ]
  ]
}

Current and expected behavior

https://babeljs.io/docs/babel-plugin-transform-runtime#regenerator-aliasing said, it can transform generator functions

"use strict";
var _regenerator = require("@babel/runtime/regenerator");
var _regenerator2 = _interopRequireDefault(_regenerator);
function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}
var _marked = [foo].map(_regenerator2.default.mark);
function foo() {
  return _regenerator2.default.wrap(
    function foo$(_context) {
      while (1) {
        switch ((_context.prev = _context.next)) {
          case 0:
          case "end":
            return _context.stop();
        }
      }
    },
    _marked[0],
    this
  );
}

Environment

System: OS: macOS 13.2.1 Binaries: Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node Yarn: 1.22.11 - /usr/local/bin/yarn npm: 9.1.2 - /usr/local/bin/npm pnpm: 8.10.5 - /usr/local/bin/pnpm npmPackages: @babel/cli: ^7.23.4 => 7.23.4 @babel/core: ^7.23.7 => 7.23.7 @babel/plugin-transform-runtime: ^7.23.7 => 7.23.7 @babel/preset-env: ^7.23.9 => 7.23.9 @babel/runtime: ^7.23.8 => 7.23.8 @babel/runtime-corejs3: ^7.23.8 => 7.23.8

Possible solution

Does @babel/plugin-transform-runtime not translate generator functions? But it’s not in the document

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 5 months ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

This plugin transforms this input code:

"use strict";

var _marked = [foo].map(regeneratorRuntime.mark);

function foo() {
  return regeneratorRuntime.wrap(
    function foo$(_context) {
      while (1) {
        switch ((_context.prev = _context.next)) {
          case 0:
          case "end":
            return _context.stop();
        }
      }
    },
    _marked[0],
    this
  );
}

to this output code:

"use strict";

var _regenerator = require("@babel/runtime/regenerator");

var _regenerator2 = _interopRequireDefault(_regenerator);

function _interopRequireDefault(obj) {
  return obj && obj.__esModule ? obj : { default: obj };
}

var _marked = [foo].map(_regenerator2.default.mark);

function foo() {
  return _regenerator2.default.wrap(
    function foo$(_context) {
      while (1) {
        switch ((_context.prev = _context.next)) {
          case 0:
          case "end":
            return _context.stop();
        }
      }
    },
    _marked[0],
    this
  );
}

If you want to compile function* foo() {}, you have to use @babel/plugin-transform-regenerator.

@nyngwang Yes, I need to set regenerator: true when removing version. Thank you for your reply.

@nyngwang @babel/preset-env incudes @babel/plugin-transform-regenerator, I think this is @babel/plugin-transform-regenerator to transform generator functions not @babel/plugin-transform-runtime. Extract inlined usage of generatorRuntime into @babel/runtime is option {helper: true} not {regenerator: true}

My example is here https://github.com/cp3hnu/Babel-Demo

@nyngwang If I don’t use @babel/plugin-transform-regenerator, the @babel/plugin-transform-runtime can not transform generator functions.

{
  "plugins": [
    [
      "@babel/plugin-transform-runtime",
      {
        "helpers": true,
        "regenerator": true,
        "version": "^7.23.8"
      }
    ]
  ]
}

Check out my problem description.