CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-regenerator

Source transformer enabling ECMAScript 6 generator functions (yield) in JavaScript-of-today (ES5).

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/

Regenerator

Regenerator is a source transformation tool that enables the use of ECMAScript 2015 generator functions (yield) and async/await syntax in JavaScript environments that don't natively support them. It transforms modern JavaScript code to ES5-compatible code, providing both a programmatic API and command-line interface for transforming generator and async function syntax.

Package Information

  • Package Name: regenerator
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install -g regenerator

Core Imports

// CommonJS - programmatic API
const regenerator = require("regenerator");

// Individual functions
const { compile, transform, runtime, types } = require("regenerator");

Basic Usage

Command Line Usage

# Transform a file
regenerator es6.js > es5.js

# Include runtime in output
regenerator --include-runtime es6.js > es5.js

# Transform directory
regenerator src lib

# Disable async transformation
regenerator --disable-async input.js > output.js

Programmatic Usage

const regenerator = require("regenerator");

// Compile source code string
const result = regenerator.compile(es6Source);
console.log(result.code); // Transformed ES5 code

// Include runtime in output
const resultWithRuntime = regenerator.compile(es6Source, {
  includeRuntime: true
});
console.log(resultWithRuntime.code);

AST Transformation

const recast = require("recast");
const regenerator = require("regenerator");

// Parse and transform AST
const ast = recast.parse(es6Source);
const transformedAst = regenerator.transform(ast);
const es5Source = recast.print(transformedAst);

Architecture

Regenerator is built around several core components:

  • Babel Integration: Uses Babel core for parsing and code generation
  • AST Transformation: Leverages Recast for AST manipulation and source mapping
  • Runtime Injection: Optionally includes regenerator-runtime for execution
  • CLI Interface: Command-line tool built on Commoner for file processing
  • Stream Interface: Browserify transform support for build pipelines

Capabilities

Source Code Compilation

Transforms ES6+ generator and async function source code to ES5-compatible JavaScript.

/**
 * Compiles ES6+ source code with generator/async functions to ES5
 * @param {string} source - Source code to transform
 * @param {Object} options - Compilation options
 * @param {boolean} options.includeRuntime - Include regenerator-runtime in output
 * @param {boolean} options.disableAsync - Disable async function transformation
 * @returns {Object} Result object with transformed code and optional source map
 */
function compile(source, options);

AST Transformation

Direct AST manipulation for integration with other build tools and transformers.

/**
 * Transforms an AST containing generator/async functions
 * @param {Object} node - AST node to transform (File, Program, or other node)
 * @param {Object} options - Transformation options
 * @param {boolean} options.includeRuntime - Inject runtime directly into AST
 * @returns {Object} Transformed AST node
 */
function transform(node, options);

Runtime Management

Functions for managing the regenerator runtime dependency.

/**
 * Registers regenerator-runtime globally in the current Node.js process
 * Sets global regeneratorRuntime variable
 * @returns {void}
 */
function runtime();

/**
 * Absolute file system path to the runtime.js file
 * @type {string}
 */
runtime.path;

Stream Interface

Browserify transform stream for build pipeline integration.

/**
 * Creates a transform stream for use with browserify or other stream-based tools
 * @param {string} file - File path being transformed
 * @param {Object} options - Stream transformation options
 * @returns {Stream} Transform stream that processes JavaScript code
 */
function module.exports(file, options);

AST Types

Access to Recast AST types for advanced AST manipulation.

/**
 * Recast AST types for building and manipulating syntax trees
 * @type {Object} Recast types object with builders and checkers
 */
const types;

Types

// Compilation result interface
interface CompilationResult {
  code: string;        // Transformed JavaScript code
  map?: Object;        // Source map information (if available)
}

// Compilation options interface
interface CompileOptions {
  includeRuntime?: boolean;    // Include regenerator-runtime in output
  disableAsync?: boolean;      // Disable async function transformation
}

// Transform options interface  
interface TransformOptions {
  includeRuntime?: boolean;    // Inject runtime into AST
}

Usage Examples

Basic Generator Transformation

const regenerator = require("regenerator");

const es6Code = `
function* fibonacci() {
  let [a, b] = [0, 1];
  while (true) {
    yield a;
    [a, b] = [b, a + b];
  }
}
`;

const result = regenerator.compile(es6Code, { includeRuntime: true });
console.log(result.code);
// Outputs ES5-compatible code with regenerator runtime

Async Function Transformation

const regenerator = require("regenerator");

const asyncCode = `
async function fetchData(url) {
  const response = await fetch(url);
  return await response.json();
}
`;

const result = regenerator.compile(asyncCode);
console.log(result.code);
// Outputs transformed code using regenerator's async support

AST Transformation Example

const recast = require("recast");
const regenerator = require("regenerator");

const sourceCode = `
function* range(start, end) {
  for (let i = start; i < end; i++) {
    yield i;
  }
}
`;

// Parse to AST
const ast = recast.parse(sourceCode);

// Transform generator functions
const transformedAst = regenerator.transform(ast, {
  includeRuntime: false
});

// Generate transformed code
const transformedCode = recast.print(transformedAst).code;
console.log(transformedCode);

Browserify Integration

// In your browserify build
const browserify = require("browserify");
const regenerator = require("regenerator");

browserify("app.js")
  .transform(regenerator)
  .bundle()
  .pipe(process.stdout);

Runtime Path Access

const regenerator = require("regenerator");
const fs = require("fs");

// Access runtime file path
console.log("Runtime path:", regenerator.runtime.path);

// Read runtime source
const runtimeSource = fs.readFileSync(regenerator.runtime.path, "utf8");
console.log("Runtime size:", runtimeSource.length);

docs

index.md

tile.json