Security News
Internet Archive Hacked, 31 Million Record Compromised
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
babel-plugin-transform-regenerator
Advanced tools
Explode async and generator functions into a state machine.
The babel-plugin-transform-regenerator npm package is used to transform ES2015+ generator functions into ES5 code. This is particularly useful for writing asynchronous code in a synchronous manner using the `yield` keyword, and for ensuring compatibility with older browsers or environments that do not support generator functions natively.
Transformation of generator functions
This feature allows developers to write generator functions using the `function*` syntax and `yield` keyword. The babel-plugin-transform-regenerator will then transform this into ES5 compatible code, enabling the use of generator functions in environments that do not support them natively.
function* gen() { yield 'Hello'; yield 'World'; }
Async/Await support
Although primarily focused on generator functions, babel-plugin-transform-regenerator also plays a crucial role in enabling the use of async/await syntax in projects that use Babel for transpilation. It transforms async functions into generator functions and then into ES5 code.
async function foo() { await bar(); }
This package transforms async functions into generator functions, similar to how babel-plugin-transform-regenerator handles async/await syntax. However, it is specifically focused on the async/await syntax rather than on generator functions in general.
While not a Babel plugin, regenerator-runtime is often used in conjunction with babel-plugin-transform-regenerator to provide a runtime environment for the transformed generator functions. It includes a polyfill for the regenerator runtime, which is necessary for the transpiled generator functions to work.
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": ["transform-regenerator"]
}
With options:
name | default value |
---|---|
asyncGenerators | true |
generators | true |
async | true |
{
"plugins": [
["transform-regenerator", {
"asyncGenerators": false,
"generators": false,
"async": false
}]
]
}
babel --plugins transform-regenerator script.js
require("babel-core").transform("code", {
plugins: ["transform-regenerator"]
});
FAQs
Explode async and generator functions into a state machine.
The npm package babel-plugin-transform-regenerator receives a total of 1,203,168 weekly downloads. As such, babel-plugin-transform-regenerator popularity was classified as popular.
We found that babel-plugin-transform-regenerator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 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
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
Security News
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.