How to use regenerator - 10 common examples

To help you get started, we’ve selected a few regenerator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vigour-io / builder-boy / lib / ast / es5.js View on Github external
const deps = []

      if (es6.async) {
        console.log('  ', chalk.blue('has async -- convert to generator'), file.key)
        result = asyncToGen(result).toString()
      }

      if (es6.fetch) {
        console.log('  ', chalk.blue('has fetch -- add shim'), file.key)
        deps.push(addDependency({ browser: 'whatwg-fetch', dependencies, result }))
        // deps.push(addDependency({ node: 'node-fetch', dependencies, result }))
      }

      if (es6.generator) {
        console.log('  ', chalk.blue('has generator -- add shim'), file.key)
        es5 = regenerator.compile(result).code
        deps.push(addDependency({ browser: 'regenerator-runtime/runtime', dependencies, result }))
      }

      if (es6.promise) {
        console.log('  ', chalk.blue('has promise -- add shim'), file.key)
        deps.push(addDependency({ browser: 'promise-polyfill', dependencies, result }))
      }

      Promise.all(deps).then(() => {
        es5 = buble.transform(es5 || result).code
        resolve({ result, dependencies, es5 })
      }).catch(err => reject(err))
    } else {
      es5 = buble.transform(result).code
      resolve({ result, dependencies, es5 })
    }
github vigour-io / builder-boy / lib / plugins / js / ast / es5.js View on Github external
const id = file.id.val.join('/')

      if (es6.async) {
        if (boy.__verbose__) console.log('  ', chalk.blue('has async - convert to generator'), id)
        result = asyncToGen(result).toString()
      }

      if (es6.fetch) {
        if (boy.__verbose__) console.log('  ', chalk.blue('has fetch - add shim'), id)
        deps.push(addDependency({ file, browser: 'whatwg-fetch', dependencies, result }))
        // deps.push(addDependency({ node: 'node-fetch', dependencies, result }))
      }

      if (es6.generator) {
        if (boy.__verbose__) console.log('  ', chalk.blue('has generator - add shim'), id)
        es5 = regenerator.compile(result).code
        deps.push(addDependency({ file, browser: 'regenerator-runtime/runtime', dependencies, result }))
      }

      if (es6.promise && file.id.val.join('/') !== 'promise-polyfill/promise.js') {
        if (boy.__verbose__) console.log('  ', chalk.blue('has promise -- add shim'), id)
        deps.push(addDependency({ file, browser: 'promise-polyfill', dependencies, result }))
      }

      Promise.all(deps).then((results) => {
        results.forEach((val) => {
          dependencies.push(val)
        })

        es5 = buble.transform(es5 || result, bubleOptions).code
        resolve({ result, dependencies, es5 })
      }).catch(err => reject(err))
github babel / babel / src / babel / transformation / transformers / other / regenerator.js View on Github external
exit(node) {
      if (node.async || node.generator) {
        // Although this code transforms only the subtree rooted at the given
        // Function node, that node might contain other generator functions
        // that will also be transformed. It might help performance to ignore
        // nested functions, and rely on the traversal to visit them later,
        // but that's a small optimization. Starting here instead of at the
        // root of the AST is the key optimization, since huge async/generator
        // functions are relatively rare.
        regenerator.transform(convertNodePath(this));
      }
    }
  }
github babel / babel / packages / babel-core / src / transformation / transformers / other / regenerator.js View on Github external
exit(node) {
      if (node.async || node.generator) {
        // Although this code transforms only the subtree rooted at the given
        // Function node, that node might contain other generator functions
        // that will also be transformed. It might help performance to ignore
        // nested functions, and rely on the traversal to visit them later,
        // but that's a small optimization. Starting here instead of at the
        // root of the AST is the key optimization, since huge async/generator
        // functions are relatively rare.
        regenerator.transform(convertNodePath(this));
      }
    }
  }
github metascript / metascript / lib / build-regenerator-runtime.js View on Github external
// Dumps an escodegen AST for the regenerator minified runtime.
// It allows to merge it directly into the generated program
// to avoid breaking source maps.

var fs = require('fs');

var regenerator = require('regenerator');
// HACK: Use recast from regenerator's deps
var recast = require('regenerator/node_modules/recast');

var runtime = fs.readFileSync(regenerator.runtime.min, "utf-8");
var body = recast.parse(runtime, {
  sourceFileName: regenerator.runtime.min,
}).program.body;

// Reduce source maps size by removing location information
var StripLoc = recast.Visitor.extend({
  visit: function (node) {
    this.genericVisit(node);
    node.loc = null;
    return node;
  }
});
(new StripLoc).visit(body);

fs.writeFileSync('./lib/regenerator-runtime.json', JSON.stringify(body[0]))
github metascript / metascript / gulpfile.js View on Github external
gulp.task('regenerator', function (done) {
  // Dumps an escodegen AST for the regenerator minified runtime.
  // It allows to merge it directly into the generated program
  // to avoid breaking source maps.
  var fs = require('fs');
  var regenerator = require('regenerator');
  // HACK: Use recast from regenerator's deps
  var recast = require('regenerator/node_modules/recast');

  var runtime = fs.readFileSync('node_modules/regenerator/runtime.js', "utf-8");
  var body = recast.parse(runtime, {
    sourceFileName: regenerator.runtime.min,
  }).program.body;

  // Reduce source maps size by removing location information
  recast.visit(body, {
    visitNode: function (path) {
      path.node.loc = null;
      this.traverse(path);
    }
  });

  fs.writeFileSync('./lib/regenerator-runtime.json', JSON.stringify(body[0]))

  done();
});
github metascript / metascript / lib / build-regenerator-runtime.js View on Github external
// Dumps an escodegen AST for the regenerator minified runtime.
// It allows to merge it directly into the generated program
// to avoid breaking source maps.

var fs = require('fs');

var regenerator = require('regenerator');
// HACK: Use recast from regenerator's deps
var recast = require('regenerator/node_modules/recast');

var runtime = fs.readFileSync(regenerator.runtime.min, "utf-8");
var body = recast.parse(runtime, {
  sourceFileName: regenerator.runtime.min,
}).program.body;

// Reduce source maps size by removing location information
var StripLoc = recast.Visitor.extend({
  visit: function (node) {
    this.genericVisit(node);
    node.loc = null;
    return node;
  }
});
(new StripLoc).visit(body);

fs.writeFileSync('./lib/regenerator-runtime.json', JSON.stringify(body[0]))
github babel / babel / packages / babel-core / src / transformation / transformers / other / regenerator.js View on Github external
import regenerator from "regenerator";
import * as t from "babel-types";

// It's important to use the exact same NodePath constructor that
// Regenerator uses, rather than require("ast-types").NodePath, because
// the version of ast-types that Babel knows about might be different from
// the version that Regenerator depends on. See for example #1958.
const NodePath = regenerator.types.NodePath;

export let metadata = {
  group: "builtin-advanced"
};

export let visitor = {
  Function: {
    exit(node) {
      if (node.async || node.generator) {
        // Although this code transforms only the subtree rooted at the given
        // Function node, that node might contain other generator functions
        // that will also be transformed. It might help performance to ignore
        // nested functions, and rely on the traversal to visit them later,
        // but that's a small optimization. Starting here instead of at the
        // root of the AST is the key optimization, since huge async/generator
        // functions are relatively rare.
github metascript / metascript / lib / build-regenerator-runtime.js View on Github external
// Dumps an escodegen AST for the regenerator minified runtime.
// It allows to merge it directly into the generated program
// to avoid breaking source maps.

var fs = require('fs');

var regenerator = require('regenerator');
// HACK: Use recast from regenerator's deps
var recast = require('regenerator/node_modules/recast');

var runtime = fs.readFileSync(regenerator.runtime.min, "utf-8");
var body = recast.parse(runtime, {
  sourceFileName: regenerator.runtime.min,
}).program.body;

// Reduce source maps size by removing location information
var StripLoc = recast.Visitor.extend({
  visit: function (node) {
    this.genericVisit(node);
    node.loc = null;
    return node;
  }
});
(new StripLoc).visit(body);

fs.writeFileSync('./lib/regenerator-runtime.json', JSON.stringify(body[0]))
github metascript / metascript / gulpfile.js View on Github external
gulp.task('regenerator', function (done) {
  // Dumps an escodegen AST for the regenerator minified runtime.
  // It allows to merge it directly into the generated program
  // to avoid breaking source maps.
  var fs = require('fs');
  var regenerator = require('regenerator');
  // HACK: Use recast from regenerator's deps
  var recast = require('regenerator/node_modules/recast');

  var runtime = fs.readFileSync('node_modules/regenerator/runtime.js', "utf-8");
  var body = recast.parse(runtime, {
    sourceFileName: regenerator.runtime.min,
  }).program.body;

  // Reduce source maps size by removing location information
  recast.visit(body, {
    visitNode: function (path) {
      path.node.loc = null;
      this.traverse(path);
    }
  });

  fs.writeFileSync('./lib/regenerator-runtime.json', JSON.stringify(body[0]))

  done();
});

regenerator

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

MIT
Latest version published 4 months ago

Package Health Score

75 / 100
Full package analysis