Skip to content

Commit

Permalink
fix(es6): move to src/ folder, es6 modules, rollup
Browse files Browse the repository at this point in the history
TAG: latest
  • Loading branch information
tunnckoCore committed Aug 15, 2017
1 parent 75134c7 commit 0a0887b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"check-coverage": true,
"statements": 100,
"functions": 100,
"branches": 98.25,
"branches": 100,
"lines": 100
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
"homepage": "https://github.com/tunnckoCore/koa-better-serve",
"author": "Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)",
"nspId": "1efca2b5-9131-4e4e-9081-b911c996598e",
"main": "./index.js",
"module": "./dest/index.mjs",
"main": "./dest/index.js",
"license": "MIT",
"scripts": {
"fresh": "hela fresh",
"contrib": "hela contrib",
"renovate": "hela renovate",
"lint": "hela lint -c .eslintrc.json",
"pretest": "hela build && hela pretest",
"test": "hela test node test.js",
"posttest": "hela posttest",
"precommit": "hela precommit",
"commit": "hela commit",
"postcommit": "git push",
Expand All @@ -25,7 +28,7 @@
"npm": ">=5"
},
"files": [
"index.js"
"dest/"
],
"dependencies": {
"kind-of": "5.0.2",
Expand Down
11 changes: 11 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const pkg = require('./package.json')

module.exports = {
entry: 'src/index.js',
interop: false,
external: Object.keys(pkg.dependencies),
targets: [
{ dest: 'dest/index.js', format: 'cjs' },
{ dest: 'dest/index.mjs', format: 'es' },
],
}
6 changes: 3 additions & 3 deletions index.js → src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Released under the MIT license.
*/

const send = require('koa-send')
const kindOf = require('kind-of')
import send from 'koa-send'
import kindOf from 'kind-of'

/**
* Serving `dir` of files and folders, when request
Expand All @@ -28,7 +28,7 @@ const kindOf = require('kind-of')
* @return {Function} a [koa][] plugin which returns `Promise` when called
* @public
*/
module.exports = function koaBetterServe (root, pathname, options) {
export default function koaBetterServe (root, pathname, options) {
if (kindOf(root) !== 'string') {
throw new TypeError('koa-better-serve: expect `root` to be string')
}
Expand Down
29 changes: 16 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* Released under the MIT license.
*/

const Koa = require('koa')
const path = require('path')
const test = require('mukla') // eslint-disable-line
const Koa = require('koa')
const request = require('supertest')
const serve = require('./index')
const serve = require('./src/index')

const relative = (fp) => path.join(__dirname, fp)

test('throw error if `root` not a string', () => {
test('1. throw error if `root` not a string', () => {
function fixture () {
serve(123)
}
Expand All @@ -19,7 +22,7 @@ test('throw error if `root` not a string', () => {
test.throws(fixture, /expect `root` to be string/)
})

test('throw error if `pathname` not a string or `options` object', () => {
test('2. throw error if `pathname` not a string or `options` object', () => {
function fixture () {
serve('./foo', 123)
}
Expand All @@ -28,10 +31,10 @@ test('throw error if `pathname` not a string or `options` object', () => {
test.throws(fixture, /expect `pathname` to be string/)
})

test('accept `options` as second parameter', () => {
test('3. accept `options` as second parameter', () => {
let app = new Koa()
app.use(
serve('./', {
serve(__dirname, {
hidden: true,
})
)
Expand All @@ -42,36 +45,36 @@ test('accept `options` as second parameter', () => {
.expect(200)
})

test('response 404 when `root` not exists', () => {
test('4. response 404 when `root` not exists', () => {
let app = new Koa()
app.use(serve('./not-exists', '/'))

return request(app.callback()).get('/LICENSE').expect(404, /Not Found/)
})

test('serve file from root when pathname `/pkg/`', () => {
test('5. serve file from root when pathname `/pkg/`', () => {
let server = new Koa()

// both `/pkg/` and `pkg/` works
return request(server.use(serve('./', 'pkg/')).callback())
return request(server.use(serve(__dirname, 'pkg/')).callback())
.get('/pkg/package.json')
.expect(/name": "koa-better-serve"/)
.expect(200)
})

test('serve package.json from nested root', () => {
test('6. serve package.json from nested root', () => {
let app = new Koa()
app.use(serve('./node_modules/koa-send'))
app.use(serve(relative('node_modules/koa-send')))

return request(app.callback())
.get('/package.json')
.expect(/name": "koa-send"/)
.expect(200)
})

test('serve file when request has prefix', () => {
test('7. serve file when request has prefix', () => {
let app = new Koa()
app.use(serve('./node_modules/supertest', '/some/foo/bar'))
app.use(serve(relative('node_modules/supertest'), '/some/foo/bar'))

return request(app.callback())
.get('/some/foo/bar/package.json')
Expand Down

0 comments on commit 0a0887b

Please sign in to comment.