├── .gitignore ├── test ├── source2.js ├── rollup.config.js ├── source.js └── expected.js ├── .travis.yml ├── README.md ├── package.json └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | npm-debug.log 3 | node_modules 4 | -------------------------------------------------------------------------------- /test/source2.js: -------------------------------------------------------------------------------- 1 | export async function mystery() { 2 | return await 'oOOoooOOOooo' 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "6" 4 | - "4" 5 | - "iojs" 6 | - "0.12" 7 | -------------------------------------------------------------------------------- /test/rollup.config.js: -------------------------------------------------------------------------------- 1 | var async = require('../'); 2 | 3 | module.exports = { 4 | plugins: [ async() ], 5 | format: 'cjs' 6 | }; 7 | -------------------------------------------------------------------------------- /test/source.js: -------------------------------------------------------------------------------- 1 | import { mystery } from './source2' 2 | 3 | // async function statement 4 | async function foo() { 5 | for await (let x of asyncIterable) { 6 | // Empty 7 | } 8 | return await mystery() 9 | } 10 | 11 | foo().then(console.log) 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Rollup Async functions plugin 2 | ============================= 3 | 4 | [![Build Status](https://travis-ci.org/leebyron/rollup-plugin-async.svg?branch=master)](https://travis-ci.org/leebyron/rollup-plugin-async) 5 | 6 | This [Rollup](http://rollupjs.org/) plugin will replace [async functions](https://tc39.github.io/ecmascript-asyncawait/) with generator functions that can run in 7 | modern browsers or in most versions of node.js during bundling using [`async-to-gen`](https://github.com/leebyron/async-to-gen). 8 | 9 | ## Install 10 | 11 | ``` 12 | npm install --save rollup-plugin-async 13 | ``` 14 | 15 | ```js 16 | var rollup = require('rollup').rollup; 17 | var async = require('rollup-plugin-async'); 18 | 19 | rollup({ 20 | entry: 'main.js', 21 | plugins: [ async() ] 22 | }).then(...); 23 | ``` 24 | -------------------------------------------------------------------------------- /test/expected.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function __async(g){return new Promise(function(s,j){function c(a,x){try{var r=g[x?"throw":"next"](a)}catch(e){j(e);return}r.done?s(r.value):Promise.resolve(r.value).then(c,d)}function d(e){c(e,1)}c()})} 4 | function __asyncIterator(o){var i=o[Symbol&&Symbol.asyncIterator||"@@asyncIterator"]||o[Symbol&&Symbol.iterator||"@@iterator"];if(!i)throw new TypeError("Object is not AsyncIterable.");return i.call(o)} 5 | 6 | function mystery() {return __async(function*(){ 7 | return yield 'oOOoooOOOooo' 8 | }())} 9 | 10 | // async function statement 11 | function foo() {return __async(function*(){ 12 | var $i1,$s1,$e1;try{for ($s1=null,$i1=__asyncIterator( asyncIterable);$s1=yield $i1.next(),!$s1.done;) {let x=$s1.value; 13 | // Empty 14 | }}catch(e){$e1=e}finally{try{!$s1.done&&$i1.return&&(yield $i1.return())}finally{if($e1)throw $e1}} 15 | return yield mystery() 16 | }())} 17 | 18 | foo().then(console.log) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-async", 3 | "version": "1.2.0", 4 | "description": "Transforms Async functions to generator functions before bundling.", 5 | "author": "Lee Byron (http://leebyron.com/)", 6 | "license": "BSD-3-Clause", 7 | "main": "index.js", 8 | "homepage": "https://github.com/leebyron/rollup-plugin-async", 9 | "bugs": { 10 | "url": "https://github.com/leebyron/rollup-plugin-async/issues" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "http://github.com/leebyron/rollup-plugin-async.git" 15 | }, 16 | "scripts": { 17 | "test": "DIFF=$(rollup -c test/rollup.config.js test/source.js | diff test/expected.js -); if [ -n \"$DIFF\" ]; then echo \"$DIFF\"; exit 1; fi;", 18 | "test-update": "rollup -c test/rollup.config.js test/source.js > test/expected.js" 19 | }, 20 | "keywords": [ 21 | "rollup-plugin", 22 | "async", 23 | "await", 24 | "async-to-gen" 25 | ], 26 | "dependencies": { 27 | "async-to-gen": "^1.2.0", 28 | "rollup-pluginutils": "^1.5.1" 29 | }, 30 | "devDependencies": { 31 | "rollup": "^0.34.1" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var asyncToGen = require('async-to-gen'); 2 | var fs = require('fs'); 3 | var os = require('os'); 4 | var path = require('path'); 5 | var createFilter = require('rollup-pluginutils').createFilter; 6 | 7 | module.exports = function(options) { 8 | options = options || {}; 9 | var filter = createFilter(options.include, options.exclude); 10 | var sourceMap = options.sourceMap !== false; 11 | 12 | return { 13 | name: 'async-to-gen', 14 | transform: function(code, id) { 15 | if (filter(id)) { 16 | var result = asyncToGen(code, { 17 | sourceMap: sourceMap, 18 | includeHelper: false 19 | }); 20 | if (result.isEdited) { 21 | result.prepend('import { __async, __asyncGen, __asyncIterator } from "' + getAsyncHelperFile() + '"\n'); 22 | } 23 | return { 24 | code: result.toString(), 25 | map: sourceMap ? result.generateMap() : { mappings: '' } 26 | }; 27 | } 28 | } 29 | }; 30 | } 31 | 32 | var _asyncHelperFile; 33 | 34 | function getAsyncHelperFile() { 35 | if (!_asyncHelperFile) { 36 | _asyncHelperFile = path.join(os.tmpdir(), 'asyncHelper.' + Date.now() + '.js'); 37 | fs.writeFileSync( 38 | _asyncHelperFile, 39 | 'export ' + asyncToGen.asyncHelper + '\n' + 40 | 'export ' + asyncToGen.asyncGenHelper + '\n' + 41 | 'export ' + asyncToGen.asyncIteratorHelper 42 | ); 43 | process.on('exit', function () { 44 | fs.unlinkSync(_asyncHelperFile) 45 | }) 46 | } 47 | 48 | // Note that while win32 uses \ as path separator, Node require() may rely on /. 49 | return os.platform() === 'win32' ? 50 | _asyncHelperFile.replace(/\\/g, '/') : 51 | _asyncHelperFile; 52 | } 53 | --------------------------------------------------------------------------------