CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-babel-plugin-transform-regenerator

Babel plugin that transforms async functions and generators into state machines using regenerator runtime.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

babel-plugin-transform-regenerator

babel-plugin-transform-regenerator transforms async functions and generator functions into state machines using Facebook's regenerator library. It enables support for ES6 generators and ES2017 async/await syntax by converting them to ES5-compatible code that works with the regenerator runtime.

Package Information

  • Package Name: babel-plugin-transform-regenerator
  • Package Type: npm
  • Language: JavaScript
  • Main Entry Point: lib/index.js
  • Installation: npm install --save-dev babel-plugin-transform-regenerator

Core Imports

For Babel configuration files:

{
  "plugins": ["transform-regenerator"]
}

For Node.js API usage:

const babel = require("babel-core");

Basic Usage

Via .babelrc

{
  "plugins": ["transform-regenerator"]
}

With Options

{
  "plugins": [
    ["transform-regenerator", {
      "asyncGenerators": true,
      "generators": true,
      "async": true
    }]
  ]
}

Via CLI

babel --plugins transform-regenerator script.js

Via Node API

const babel = require("babel-core");

const result = babel.transform(code, {
  plugins: ["transform-regenerator"]
});

Architecture

babel-plugin-transform-regenerator is designed as a lightweight wrapper around Facebook's regenerator-transform library. The plugin follows Babel's standard plugin architecture:

  • Plugin Wrapper: The main entry point (src/index.js) re-exports the regenerator-transform package, providing seamless integration with Babel's plugin system
  • Transform Engine: The underlying regenerator-transform library handles the actual AST transformation, converting async/await and generator syntax into state machine implementations
  • Configuration Interface: The plugin accepts options (asyncGenerators, generators, async) that control which JavaScript features are transformed
  • Runtime Integration: Transformed code depends on the regeneratorRuntime, which must be provided by babel-runtime, babel-polyfill, or the standalone regenerator-runtime package

This architecture separates the Babel integration layer from the transformation logic, allowing the plugin to leverage the mature regenerator-transform library while maintaining compatibility with Babel's ecosystem.

Capabilities

Plugin Function

The main plugin export that provides Babel transformation functionality for async functions and generators.

/**
 * Babel plugin function that transforms async and generator functions
 * Exported from regenerator-transform package
 * @returns {BabelPlugin} Babel plugin object with visitor methods
 */
module.exports = require("regenerator-transform");

Configuration Options

The plugin accepts configuration options to control which types of functions are transformed.

/**
 * Plugin configuration options
 * @typedef {Object} RegeneratorOptions
 * @property {boolean} [asyncGenerators=true] - Enable transformation of async generator functions
 * @property {boolean} [generators=true] - Enable transformation of generator functions  
 * @property {boolean} [async=true] - Enable transformation of async functions
 */

Usage with Options:

// Via .babelrc
{
  "plugins": [
    ["transform-regenerator", {
      "asyncGenerators": false,  // Disable async generator transformation
      "generators": true,        // Keep generator transformation enabled
      "async": true             // Keep async function transformation enabled
    }]
  ]
}

// Via Node API
babel.transform(code, {
  plugins: [
    ["transform-regenerator", {
      asyncGenerators: true,
      generators: true,
      async: true
    }]
  ]
});

Transformation Examples

Generator Functions

Input:

function* generator() {
  yield 1;
  yield 2;
  return 3;
}

Output:

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

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

Async Functions

Input:

async function fetchData() {
  const response = await fetch('/api/data');
  return response.json();
}

Output:

function fetchData() {
  return regeneratorRuntime.async(function fetchData$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return regeneratorRuntime.awrap(fetch('/api/data'));
        case 2:
          response = _context.sent;
          return _context.abrupt("return", response.json());
        case 4:
        case "end":
          return _context.stop();
      }
    }
  }, null, this);
}

Async Generator Functions

Input:

async function* asyncGenerator() {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
}

Output:

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

function asyncGenerator() {
  return regeneratorRuntime.wrap(function asyncGenerator$(_context) {
    while (1) {
      switch (_context.prev = _context.next) {
        case 0:
          _context.next = 2;
          return regeneratorRuntime.awrap(Promise.resolve(1));
        case 2:
          _context.next = 4;
          return _context.sent;
        case 4:
          _context.next = 6;
          return regeneratorRuntime.awrap(Promise.resolve(2));
        case 6:
          _context.next = 8;
          return _context.sent;
        case 8:
        case "end":
          return _context.stop();
      }
    }
  }, _marked[0], this);
}

Runtime Requirements

The transformed code requires the regeneratorRuntime to be available in the execution environment. This can be provided by:

  • babel-runtime: For individual function imports
  • babel-polyfill: For global runtime availability
  • regenerator-runtime: Standalone runtime package
// If using babel-runtime
import "babel-runtime/regenerator";

// If using babel-polyfill
import "babel-polyfill";

// If using standalone regenerator-runtime
import "regenerator-runtime/runtime";

Integration Patterns

Webpack Configuration

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            plugins: ['transform-regenerator']
          }
        }
      }
    ]
  }
};

Rollup Configuration

import babel from 'rollup-plugin-babel';

export default {
  plugins: [
    babel({
      plugins: ['transform-regenerator']
    })
  ]
};

Selective Transformation

When you only need specific transformation types:

{
  "plugins": [
    ["transform-regenerator", {
      "asyncGenerators": false,
      "generators": true,
      "async": false
    }]
  ]
}

This configuration will only transform generator functions, leaving async functions and async generators untransformed.

docs

index.md

tile.json