From 09ada29881cf6251cfddc503b0f53f01f07de90f Mon Sep 17 00:00:00 2001 From: jongleberry Date: Wed, 4 Nov 2015 11:15:18 -0800 Subject: [PATCH 1/6] test: add a babel example --- Makefile | 3 ++- package.json | 3 ++- test/babel/.babelrc | 3 +++ test/babel/_test.js | 42 ++++++++++++++++++++++++++++++++++++++++++ test/babel/index.js | 6 ++++++ 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/babel/.babelrc create mode 100644 test/babel/_test.js create mode 100644 test/babel/index.js diff --git a/Makefile b/Makefile index a0c97bc..44a89df 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ REQUIRED = --require should --require should-http TESTS = test/application/* \ test/context/* \ test/request/* \ - test/response/* + test/response/* \ + test/babel/index.js lint: @./node_modules/.bin/eslint lib test diff --git a/package.json b/package.json index 7213ac5..84c6150 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,9 @@ "vary": "^1.0.0" }, "devDependencies": { - "babel": "^5.0.0", + "babel-core": "^6.0.20", "babel-eslint": "^4.1.3", + "babel-preset-stage-3": "^6.0.15", "eslint": "^1.6.0", "eslint-config-standard": "^4.4.0", "eslint-plugin-standard": "^1.3.1", diff --git a/test/babel/.babelrc b/test/babel/.babelrc new file mode 100644 index 0000000..1d63d54 --- /dev/null +++ b/test/babel/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["stage-3"] +} diff --git a/test/babel/_test.js b/test/babel/_test.js new file mode 100644 index 0000000..6459c49 --- /dev/null +++ b/test/babel/_test.js @@ -0,0 +1,42 @@ +'use strict'; + +const request = require('supertest'); +const Koa = require('../..'); + +describe('require("babel-core/register")', () => { + describe('app.use(fn)', () => { + it('should compose middleware w/ async functions', done => { + const app = new Koa(); + const calls = []; + + app.use(async function (ctx, next){ + calls.push(1); + await next(); + calls.push(6); + }); + + app.use(async function (ctx, next){ + calls.push(2); + await next(); + calls.push(5); + }); + + app.use(async function (ctx, next){ + calls.push(3); + await next(); + calls.push(4); + }); + + const server = app.listen(); + + request(server) + .get('/') + .expect(404) + .end(err => { + if (err) return done(err); + calls.should.eql([1, 2, 3, 4, 5, 6]); + done(); + }); + }); + }); +}); diff --git a/test/babel/index.js b/test/babel/index.js new file mode 100644 index 0000000..939dd71 --- /dev/null +++ b/test/babel/index.js @@ -0,0 +1,6 @@ +'ues strict'; + +// http://babeljs.io/docs/setup/#babel_register + +require('babel-core/register'); +require('./_test.js'); From a6547bcbceed91af78a95ab8b11dd31f21566b4e Mon Sep 17 00:00:00 2001 From: jongleberry Date: Thu, 5 Nov 2015 08:49:20 -0800 Subject: [PATCH 2/6] :arrow_up: babel and use async arrow functions --- package.json | 2 ++ test/babel/_test.js | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 84c6150..1310b2f 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,8 @@ "license": "MIT", "dependencies": { "accepts": "^1.2.2", + "babel-core": "^6.1.2", + "babel-preset-stage-3": "^6.1.2", "content-disposition": "~0.5.0", "content-type": "^1.0.0", "cookies": "~0.5.0", diff --git a/test/babel/_test.js b/test/babel/_test.js index 6459c49..d4b3460 100644 --- a/test/babel/_test.js +++ b/test/babel/_test.js @@ -9,19 +9,19 @@ describe('require("babel-core/register")', () => { const app = new Koa(); const calls = []; - app.use(async function (ctx, next){ + app.use(async (ctx, next) => { calls.push(1); await next(); calls.push(6); }); - app.use(async function (ctx, next){ + app.use(async (ctx, next) => { calls.push(2); await next(); calls.push(5); }); - app.use(async function (ctx, next){ + app.use(async (ctx, next) => { calls.push(3); await next(); calls.push(4); From 51b51331ba08b3cb58bbc88c37b82fcf17de2766 Mon Sep 17 00:00:00 2001 From: jongleberry Date: Fri, 6 Nov 2015 09:37:35 -0800 Subject: [PATCH 3/6] us eslint-plugin-babel for better listing --- .eslintrc | 8 +++++++- package.json | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.eslintrc b/.eslintrc index efce1dc..7f50cc1 100644 --- a/.eslintrc +++ b/.eslintrc @@ -3,13 +3,19 @@ parser: babel-eslint extends: standard +plugins: [ + "babel" +] + rules: + arrow-parens: 0 + babel/arrow-parens: [2, "as-needed"] + eqeqeq: 0 no-var: 2 semi: [2, always] space-before-function-paren: [2, never] yoda: 0 - arrow-parens: [2, "as-needed"] arrow-spacing: 2 dot-location: [2, "property"] prefer-arrow-callback: 2 diff --git a/package.json b/package.json index 1310b2f..fb9d55a 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,6 @@ "license": "MIT", "dependencies": { "accepts": "^1.2.2", - "babel-core": "^6.1.2", - "babel-preset-stage-3": "^6.1.2", "content-disposition": "~0.5.0", "content-type": "^1.0.0", "cookies": "~0.5.0", @@ -44,11 +42,12 @@ "vary": "^1.0.0" }, "devDependencies": { - "babel-core": "^6.0.20", + "babel-core": "^6.1.2", "babel-eslint": "^4.1.3", - "babel-preset-stage-3": "^6.0.15", + "babel-preset-stage-3": "^6.1.2", "eslint": "^1.6.0", "eslint-config-standard": "^4.4.0", + "eslint-plugin-babel": "^2.1.1", "eslint-plugin-standard": "^1.3.1", "istanbul": "^0.4.0", "mocha": "^2.0.1", From eb0bd4c2c3e8944b759049552e7828ba296b26a1 Mon Sep 17 00:00:00 2001 From: jongleberry Date: Fri, 6 Nov 2015 09:38:00 -0800 Subject: [PATCH 4/6] test: fix use-strict typo --- test/babel/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/babel/index.js b/test/babel/index.js index 939dd71..e3ba34f 100644 --- a/test/babel/index.js +++ b/test/babel/index.js @@ -1,4 +1,4 @@ -'ues strict'; +'use strict'; // http://babeljs.io/docs/setup/#babel_register From 65f645d341fc274abbd755d2cb9196adc6c21437 Mon Sep 17 00:00:00 2001 From: jongleberry Date: Fri, 6 Nov 2015 09:40:44 -0800 Subject: [PATCH 5/6] use babel-plugin-transform-async-to-generator --- package.json | 2 +- test/babel/.babelrc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index fb9d55a..4d6ec52 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "devDependencies": { "babel-core": "^6.1.2", "babel-eslint": "^4.1.3", - "babel-preset-stage-3": "^6.1.2", + "babel-plugin-transform-async-to-generator": "^6.0.14", "eslint": "^1.6.0", "eslint-config-standard": "^4.4.0", "eslint-plugin-babel": "^2.1.1", diff --git a/test/babel/.babelrc b/test/babel/.babelrc index 1d63d54..0d67235 100644 --- a/test/babel/.babelrc +++ b/test/babel/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["stage-3"] + "plugins": ["transform-async-to-generator"] } From a09000357d1115919aafc78a3c3038c46fe6dfa5 Mon Sep 17 00:00:00 2001 From: jongleberry Date: Fri, 6 Nov 2015 09:45:53 -0800 Subject: [PATCH 6/6] add docs for babel --- docs/api/index.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/api/index.md b/docs/api/index.md index 886b987..9135337 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -85,6 +85,29 @@ app.use(function *(){ app.listen(3000); ``` +## Async Functions with Babel + +To use `async` functions in Koa, we recommend using [babel's require hook](http://babeljs.io/docs/usage/require/). + +```js +require('babel-core/register'); +// require the rest of the app that needs to be transpiled after the hook +const app = require('./app'); +``` + +To parse and transpile async functions, +you should at a minimum have the [transform-async-to-generator](http://babeljs.io/docs/plugins/transform-async-to-generator/) +or [transform-async-to-module-method](http://babeljs.io/docs/plugins/transform-async-to-module-method/) plugins. +For example, in your `.babelrc` file, you should have: + +```json +{ + "plugins": ["transform-async-to-generator"] +} +``` + +Currently, you could also use the [stage-3 preset](http://babeljs.io/docs/plugins/preset-stage-3/). + ## Settings Application settings are properties on the `app` instance, currently