Source transformer enabling ECMAScript 6 generator functions (yield) in JavaScript-of-today (ES5).
npx @tessl/cli install tessl/npm-regenerator@0.14.0Regenerator 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.
npm install -g regenerator// CommonJS - programmatic API
const regenerator = require("regenerator");
// Individual functions
const { compile, transform, runtime, types } = require("regenerator");# 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.jsconst 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);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);Regenerator is built around several core components:
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);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);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;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);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;// 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
}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 runtimeconst 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 supportconst 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);// In your browserify build
const browserify = require("browserify");
const regenerator = require("regenerator");
browserify("app.js")
.transform(regenerator)
.bundle()
.pipe(process.stdout);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);