Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@babel/plugin-transform-regenerator
Advanced tools
Explode async and generator functions into a state machine.
The @babel/plugin-transform-regenerator package is a Babel plugin that enables the transformation of generator functions and async functions into ES5-compatible JavaScript code using the regenerator runtime. It allows developers to use these modern features in environments that do not natively support them.
Transformation of generator functions
This plugin transforms generator functions into ES5 code by converting the generator syntax into a state machine that is compatible with older JavaScript engines.
function* gen() { yield 1; yield 2; yield 3; }
Transformation of async functions
It also transforms async functions into generator functions and then applies the generator function transformation to ensure compatibility with environments that do not support async/await syntax.
async function foo() { await someAsyncFunction(); }
The regenerator-runtime package provides the runtime needed to support the generator functions in environments that do not natively support them. It is often used in conjunction with @babel/plugin-transform-regenerator, which handles the syntax transformation.
This package is similar to @babel/plugin-transform-regenerator in that it transforms async functions into generator functions. However, it focuses solely on async functions and does not handle generator functions.
fast-async is a Babel plugin that transforms async/await functions into ES5-compatible code using Promises instead of generators. It provides an alternative approach to handling async functions and can be faster than regenerator-based transformations.
Transform async/generator functions with regenerator
In
function* a() {
yield 1;
}
Out
var _marked = [a].map(regeneratorRuntime.mark);
function a() {
return regeneratorRuntime.wrap(function a$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return 1;
case 2:
case "end":
return _context.stop();
}
}
}, _marked[0], this);
}
npm install --save-dev @babel/plugin-transform-regenerator
.babelrc
(Recommended)Without options:
{
"plugins": ["@babel/plugin-transform-regenerator"]
}
With options:
name | default value |
---|---|
asyncGenerators | true |
generators | true |
async | true |
{
"plugins": [
["@babel/plugin-transform-regenerator", {
"asyncGenerators": false,
"generators": false,
"async": false
}]
]
}
babel --plugins @babel/plugin-transform-regenerator script.js
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-regenerator"]
});
FAQs
Explode async and generator functions into a state machine.
The npm package @babel/plugin-transform-regenerator receives a total of 17,812,225 weekly downloads. As such, @babel/plugin-transform-regenerator popularity was classified as popular.
We found that @babel/plugin-transform-regenerator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.