Lee Byron
8 years ago
commit
148819612e
9 changed files with 139 additions and 0 deletions
@ -0,0 +1,3 @@ |
|||||
|
.DS_Store |
||||
|
npm-debug.log |
||||
|
node_modules |
@ -0,0 +1,6 @@ |
|||||
|
language: node_js |
||||
|
node_js: |
||||
|
- "6" |
||||
|
- "4" |
||||
|
- "iojs" |
||||
|
- "0.12" |
@ -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(...); |
||||
|
``` |
@ -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; |
||||
|
} |
@ -0,0 +1,33 @@ |
|||||
|
{ |
||||
|
"name": "rollup-plugin-async", |
||||
|
"version": "1.0.1", |
||||
|
"description": "Transforms Async functions to generator functions before bundling.", |
||||
|
"author": "Lee Byron <lee@leebyron.com> (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" |
||||
|
} |
||||
|
} |
@ -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) |
@ -0,0 +1,6 @@ |
|||||
|
var async = require('../'); |
||||
|
|
||||
|
module.exports = { |
||||
|
plugins: [ async() ], |
||||
|
format: 'cjs' |
||||
|
}; |
@ -0,0 +1,8 @@ |
|||||
|
import { mystery } from './source2' |
||||
|
|
||||
|
// async function statement
|
||||
|
async function foo() { |
||||
|
return await mystery() |
||||
|
} |
||||
|
|
||||
|
foo().then(console.log) |
@ -0,0 +1,3 @@ |
|||||
|
export async function mystery() { |
||||
|
return await 'oOOoooOOOooo' |
||||
|
} |
Loading…
Reference in new issue