├── .editorconfig ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── lib ├── rollup-plugin-regenerator.es.js └── rollup-plugin-regenerator.js └── package.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # See http://editorconfig.org/ 2 | # EditorConfig helps developers define and maintain consistent coding styles 3 | # between different editors and IDEs. The EditorConfig project consists of a 4 | # file format for defining coding styles and a collection of text editor plugins 5 | # that enable editors to read the file format and adhere to defined styles. 6 | # EditorConfig files are easily readable and they work nicely with version 7 | # control systems. 8 | 9 | root = true 10 | 11 | [*] 12 | 13 | # Change these settings to your own preference 14 | indent_style = space 15 | indent_size = 2 16 | 17 | # We recommend you to keep these unchanged 18 | end_of_line = lf 19 | charset = utf-8 20 | trim_trailing_whitespace = true 21 | insert_final_newline = true 22 | 23 | [*.md] 24 | trim_trailing_whitespace = false 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Include your project-specific ignores in this file 2 | # Read about how to use .gitignore: https://help.github.com/articles/ignoring-files 3 | 4 | # Dependency directories 5 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 6 | node_modules/ 7 | 8 | # Debug logs from NPM, Yarn 9 | npm-debug.log* 10 | yarn-debug.log* 11 | yarn-error.log* 12 | 13 | # NPM, Yarn lock files 14 | package-lock.json 15 | yarn.lock 16 | 17 | # Editors and IDEs 18 | .idea 19 | .vscode 20 | 21 | # OSX Files 22 | .DS_Store 23 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Use a .npmignore file to keep stuff out of your package 2 | # Read about how to use .npmignore: https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package 3 | 4 | # Ignore everything 5 | * 6 | 7 | # But not these files... 8 | !lib 9 | !lib/** 10 | 11 | # Ignore all files named .DS_Store or ending with .log 12 | **/.DS_Store 13 | **.log 14 | 15 | # The following paths and files are never ignored, 16 | # so adding them to .npmignore is pointless: 17 | 18 | # package.json 19 | # README (and its variants) 20 | # CHANGELOG (and its variants) 21 | # LICENSE / LICENCE 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 S S 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-regenerator 2 | A Rollup.js plugin to transform ECMAScript 6 generator and async functions. 3 | 4 | This [Rollup](http://rollupjs.org) plugin will replace `async` and `generator` functions with ES5 Promise based functions. 5 | Plugin using [`regenerator`](https://github.com/facebook/regenerator) module. 6 | 7 | # BETTER SOLUTION: 8 | * [`rollup-plugin-nodent`](https://github.com/oligot/rollup-plugin-nodent) - 9 | Convert ES2017 async/await with [`nodent`](https://github.com/MatAtBread/nodent). 10 | 11 | ## Install 12 | ``` 13 | npm install --save-dev rollup-plugin-regenerator 14 | ``` 15 | or with [`yarn`](https://yarnpkg.com/) 16 | ``` 17 | yarn add -D rollup-plugin-regenerator 18 | ``` 19 | 20 | ## Usage 21 | ```js 22 | var rollup = require('rollup').rollup; 23 | var regenerator = require('rollup-plugin-regenerator'); 24 | 25 | rollup({ 26 | input: 'my-entry.js', 27 | context: 'window', // or 'global' for Node.js 28 | plugins: [ 29 | regenerator() 30 | ] 31 | }).then(...); 32 | ``` 33 | 34 | ## Options 35 | * `includeRuntime`: If `false` - do not include 'regeneratorRuntime()' polyfill (optional). 36 | * `sourceMap`: If `false` - forced disable source maps generating (optional). 37 | * `include`, `exclude`: A minimatch pattern, or an array of minimatch patterns of including ID, or excluding ID (optional). 38 | 39 | ## FAQ 40 | - *I have a warning by Rollup: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`.* Just specify `context` option in Rollup input options(as shown above). 41 | - *Does it make sense to use this plugin?* I think no. Under the hood [`regenerator`](https://github.com/facebook/regenerator) uses `babel` for transformations, so it's better to use it as a `babel` [plugin](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-regenerator). 42 | 43 | ## Contributing 44 | 1. Fork it! 45 | 2. Create your feature branch: `git checkout -b my-new-feature` 46 | 3. Commit your changes: `git commit -am 'Add some feature'` 47 | 4. Push to the branch: `git push origin my-new-feature` 48 | 5. Submit a pull request 49 | 50 | ## License 51 | Licensed under [MIT license](LICENSE). 52 | -------------------------------------------------------------------------------- /lib/rollup-plugin-regenerator.es.js: -------------------------------------------------------------------------------- 1 | // [NATIVE] Provides simple wrappers around standard POSIX functions. 2 | import { readFileSync as fsReadFileSync } from 'fs'; 3 | // A set of functions commonly used by Rollup plugins. 4 | import { createFilter as rollupPluginutilsCreateFilter } from 'rollup-pluginutils'; 5 | // JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator 6 | import { parse as recastParse, print as recastPrint } from 'recast'; 7 | // Source transformer enabling ECMAScript 6 generator functions in JavaScript-of-today. 8 | import { 9 | compile as regeneratorCompile, 10 | transform as regeneratorTransform 11 | } from 'regenerator'; 12 | 13 | /** 14 | * A Rollup.js plugin to transform ECMAScript 6 generator and async functions 15 | * @param {Object} opts - Optional settings. 16 | * @param {Boolean} opts.includeRuntime - If false - do not include 'regeneratorRuntime()' polyfill. 17 | * @param {Boolean} opts.sourceMap - If false - forced disable source maps generating. 18 | * @param {Array|String} opts.include - Minimatch pattern(s) that must be met. 19 | * @param {Array|String} opts.exclude - Minimatch pattern(s) that must NOT be met. 20 | * @returns {Object} - plugin instance. 21 | */ 22 | function rollupPluginRegenerator (opts) { 23 | // defaultify options. 24 | if (!opts) opts = {}; 25 | 26 | var filter = rollupPluginutilsCreateFilter(opts.include, opts.exclude); 27 | var withSourceMap = opts.sourceMap !== false; 28 | var regeneratorRuntimeCode = 29 | opts.includeRuntime !== false 30 | ? fsReadFileSync(require.resolve('regenerator-runtime/runtime'), 'utf8') 31 | : ''; 32 | 33 | return { 34 | name: 'regenerator', 35 | banner: regeneratorRuntimeCode, 36 | transform: function (code, id) { 37 | // if `opts.include` is omitted or has zero length, filter 38 | // will return `true` by default. Otherwise, an ID must match 39 | // one or more of the minimatch patterns, and must not match 40 | // any of the `opts.exclude` patterns. 41 | if (!filter(id)) { 42 | return; 43 | } 44 | 45 | // proceed with the transformation... 46 | var res; 47 | if (withSourceMap) { 48 | var inputAst = recastParse(code, { 49 | sourceFileName: id 50 | }); 51 | var outputAst = regeneratorTransform(inputAst); 52 | 53 | res = recastPrint(outputAst, { 54 | sourceMapName: id 55 | }); 56 | } else { 57 | res = regeneratorCompile(code); 58 | } 59 | 60 | return { 61 | code: res.code, 62 | map: withSourceMap && res.map ? res.map : { mappings: '' } 63 | }; 64 | } 65 | }; 66 | } 67 | 68 | export default rollupPluginRegenerator; 69 | -------------------------------------------------------------------------------- /lib/rollup-plugin-regenerator.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // [NATIVE] Provides simple wrappers around standard POSIX functions. 4 | var fs = require('fs'); 5 | // A set of functions commonly used by Rollup plugins. 6 | var rollupPluginutils = require('rollup-pluginutils'); 7 | // JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator 8 | var recast = require('recast'); 9 | // Source transformer enabling ECMAScript 6 generator functions in JavaScript-of-today. 10 | var regenerator = require('regenerator'); 11 | 12 | /** 13 | * A Rollup.js plugin to transform ECMAScript 6 generator and async functions 14 | * @param {Object} opts - Optional settings. 15 | * @param {Boolean} opts.includeRuntime - If false - do not include 'regeneratorRuntime()' polyfill. 16 | * @param {Boolean} opts.sourceMap - If false - forced disable source maps generating. 17 | * @param {Array|String} opts.include - Minimatch pattern(s) that must be met. 18 | * @param {Array|String} opts.exclude - Minimatch pattern(s) that must NOT be met. 19 | * @returns {Object} - plugin instance. 20 | */ 21 | function rollupPluginRegenerator (opts) { 22 | // defaultify options. 23 | if (!opts) opts = {}; 24 | 25 | var filter = rollupPluginutils.createFilter(opts.include, opts.exclude); 26 | var withSourceMap = opts.sourceMap !== false; 27 | var regeneratorRuntimeCode = 28 | opts.includeRuntime !== false 29 | ? fs.readFileSync(require.resolve('regenerator-runtime/runtime'), 'utf8') 30 | : ''; 31 | 32 | return { 33 | name: 'regenerator', 34 | banner: regeneratorRuntimeCode, 35 | transform: function (code, id) { 36 | // if `opts.include` is omitted or has zero length, filter 37 | // will return `true` by default. Otherwise, an ID must match 38 | // one or more of the minimatch patterns, and must not match 39 | // any of the `opts.exclude` patterns. 40 | if (!filter(id)) { 41 | return; 42 | } 43 | 44 | // proceed with the transformation... 45 | var res; 46 | if (withSourceMap) { 47 | var inputAst = recast.parse(code, { 48 | sourceFileName: id 49 | }); 50 | var outputAst = regenerator.transform(inputAst); 51 | 52 | res = recast.print(outputAst, { 53 | sourceMapName: id 54 | }); 55 | } else { 56 | res = regenerator.compile(code); 57 | } 58 | 59 | return { 60 | code: res.code, 61 | map: withSourceMap && res.map ? res.map : { mappings: '' } 62 | }; 63 | } 64 | }; 65 | } 66 | 67 | exports = module.exports = rollupPluginRegenerator; 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-regenerator", 3 | "version": "0.6.0", 4 | "main": "lib/rollup-plugin-regenerator.js", 5 | "module": "lib/rollup-plugin-regenerator.es.js", 6 | "jsnext:main": "lib/rollup-plugin-regenerator.es.js", 7 | "engines": { 8 | "node": ">= 0.8" 9 | }, 10 | "description": "A Rollup.js plugin to transform ECMAScript 6 generator and async functions", 11 | "license": "MIT", 12 | "dependencies": { 13 | "recast": "^0.14.7", 14 | "regenerator": "^0.12.3", 15 | "rollup-pluginutils": "^2.0.1" 16 | }, 17 | "author": "notruth", 18 | "homepage": "https://github.com/notruth/rollup-plugin-regenerator", 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/notruth/rollup-plugin-regenerator.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/notruth/rollup-plugin-regenerator/issues" 25 | }, 26 | "keywords": [ 27 | "rollup", 28 | "rollup-plugin", 29 | "transform", 30 | "regenerator", 31 | "generator", 32 | "async" 33 | ] 34 | } 35 | --------------------------------------------------------------------------------