commit 148819612e99c461ef8029e4246341dd2f377d07 Author: Lee Byron Date: Thu Aug 11 15:04:16 2016 -0700 rollup plugin async diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ade14b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +npm-debug.log +node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c6b857b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,6 @@ +language: node_js +node_js: + - "6" + - "4" + - "iojs" + - "0.12" diff --git a/README.md b/README.md new file mode 100644 index 0000000..ca10766 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +Rollup Async functions plugin +============================= + +[![Build Status](https://travis-ci.org/leebyron/rollup-plugin-async.svg?branch=master)](https://travis-ci.org/leebyron/rollup-plugin-async) + +This [Rollup](http://rollupjs.org/) plugin will replace [async functions](https://tc39.github.io/ecmascript-asyncawait/) with generator functions that can run in +modern browsers or in most versions of node.js during bundling using [`async-to-gen`](https://github.com/leebyron/async-to-gen). + +## Install + +``` +npm install --save rollup-plugin-async +``` + +```js +var rollup = require('rollup').rollup; +var async = require('rollup-plugin-async'); + +rollup({ + entry: 'main.js', + plugins: [ async() ] +}).then(...); +``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..a45a62d --- /dev/null +++ b/index.js @@ -0,0 +1,43 @@ +var asyncToGen = require('async-to-gen'); +var fs = require('fs'); +var os = require('os'); +var path = require('path'); +var createFilter = require('rollup-pluginutils').createFilter; + +module.exports = function(options) { + options = options || {}; + var filter = createFilter(options.include, options.exclude); + const sourceMap = options.sourceMap !== false; + + return { + name: 'async-to-gen', + transform: function(code, id) { + if (filter(id)) { + var result = asyncToGen(code, { + sourceMap: sourceMap, + includeHelper: false + }); + if (result.isEdited) { + result.prepend('import { __async } from "' + getAsyncHelperFile() + '"\n'); + } + return { + code: result.toString(), + map: sourceMap ? result.generateMap() : { mappings: '' } + }; + } + } + }; +} + +var _asyncHelperFile; + +function getAsyncHelperFile() { + if (!_asyncHelperFile) { + _asyncHelperFile = path.join(os.tmpdir(), 'asyncHelper.' + Date.now() + '.js'); + fs.writeFileSync(_asyncHelperFile, 'export ' + asyncToGen.asyncHelper); + process.on('exit', function () { + fs.unlinkSync(_asyncHelperFile) + }) + } + return _asyncHelperFile; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b9ebf42 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "rollup-plugin-async", + "version": "1.0.1", + "description": "Transforms Async functions to generator functions before bundling.", + "author": "Lee Byron (http://leebyron.com/)", + "license": "BSD-3-Clause", + "main": "index.js", + "homepage": "https://github.com/leebyron/rollup-plugin-async", + "bugs": { + "url": "https://github.com/leebyron/rollup-plugin-async/issues" + }, + "repository": { + "type": "git", + "url": "http://github.com/leebyron/rollup-plugin-async.git" + }, + "scripts": { + "test": "DIFF=$(rollup -c test/rollup.config.js test/source.js | diff test/expected.js -); if [ -n \"$DIFF\" ]; then echo \"$DIFF\"; exit 1; fi;", + "test-update": "rollup -c test/rollup.config.js test/source.js > test/expected.js" + }, + "keywords": [ + "rollup-plugin", + "async", + "await", + "async-to-gen" + ], + "dependencies": { + "async-to-gen": "^1.0.1", + "rollup-pluginutils": "^1.5.1" + }, + "devDependencies": { + "rollup": "^0.34.1" + } +} diff --git a/test/expected.js b/test/expected.js new file mode 100644 index 0000000..cc38054 --- /dev/null +++ b/test/expected.js @@ -0,0 +1,14 @@ +'use strict'; + +function __async(f){var g=f();return new Promise(function(s,j){function c(a,x){try{var r=g[x?"throw":"next"](a)}catch(e){return j(e)}return r.done?s(r.value):Promise.resolve(r.value).then(c,d)}function d(e){return c(e,1)}c()})} + +function mystery() {return __async(function*(){ + return yield 'oOOoooOOOooo' +})} + +// async function statement +function foo() {return __async(function*(){ + return yield mystery() +})} + +foo().then(console.log) \ No newline at end of file diff --git a/test/rollup.config.js b/test/rollup.config.js new file mode 100644 index 0000000..47cfc42 --- /dev/null +++ b/test/rollup.config.js @@ -0,0 +1,6 @@ +var async = require('../'); + +module.exports = { + plugins: [ async() ], + format: 'cjs' +}; diff --git a/test/source.js b/test/source.js new file mode 100644 index 0000000..9cb9bec --- /dev/null +++ b/test/source.js @@ -0,0 +1,8 @@ +import { mystery } from './source2' + +// async function statement +async function foo() { + return await mystery() +} + +foo().then(console.log) diff --git a/test/source2.js b/test/source2.js new file mode 100644 index 0000000..284f86f --- /dev/null +++ b/test/source2.js @@ -0,0 +1,3 @@ +export async function mystery() { + return await 'oOOoooOOOooo' +}