Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bankai: require instance before run #50

Merged
merged 1 commit into from
Aug 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 26 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,47 @@ $ npm install bankai

## Usage
```js
const serverRouter = require('server-router')
const browserify = require('browserify')
const bankai = require('bankai')
const http = require('http')
const path = require('path')

const router = createRouter()
http.createServer(function (req, res) {
router(req, res).pipe(res)
}).listen(1337)
const client = path.join(__dirname, 'client.js')

function createRouter () {
const router = serverRouter()
const assets = bankai()
const js = assets.js(browserify, client)
const html = assets.html()
const css = assets.css()

const html = bankai.html()
router.on('/', html)

const css = bankai.css({ use: [ 'sheetify-cssnext' ] })
router.on('/bundle.css', css)

const js = bankai.js(browserify, '/src/index.js', { transform: 'babelify' })
router.on('/bundle.js', js)

return router
}
http.createServer((req, res) => {
switch req.url {
case '/': return html(req, res).pipe(res)
case '/bundle.js': return js(req, res).pipe(res)
case '/bundle.css': return css(req, res).pipe(res)
default: return res.statusCode = 404 && res.end('404 not found')
}
}).listen(8080)
```

## API
### bankai.html(opts)
Return an `html` stream. Cached by default. Includes livereload if
`NODE_ENV=development`. Takes the following options:
### assets = bankai(opts?)
- __optimize:__ default `false`. Disable livereload scripts, cache output and
optimize all bundles.

### assets.html(opts?)
Return an `html` stream. Takes the following options:
- __opts.entry:__ `js` entry point. Defaults to `/bundle.js`
- __opts.css:__ `css` entry point. Defaults to `/bundle.css`

### bankai.css(opts)
### assets.css(opts?)
Return a `css` stream using [sheetify](https://github.com/stackcss/sheetify).
Cached if `NODE_ENV=production`. Takes the following options:
- __use:__ array of sheetify transforms. Empty by default.
. Takes the following options:
- __use:__ array of transforms. Empty by default.
- __basedir:__ project base directory. Defaults to `process.cwd()`

### bankai.js(browserify, src, opts)
Return a `js` stream. Uses `watchify` for incremental builds if
`NODE_ENV=development`. `src` is the bundle entry file. Cached by default.
### assets.js(browserify, src, opts?)
Return a `js` stream. `src` is the bundle entry file. `opts` are passed
directly to `browserify`

## See Also
- [budo](https://www.npmjs.com/package/budo)
Expand Down
8 changes: 3 additions & 5 deletions handler-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const hyperstream = require('hyperstream')
const xtend = require('xtend')
const bl = require('bl')

const env = process.env.NODE_ENV

module.exports = html

// create html stream
Expand All @@ -20,9 +18,9 @@ function html (state) {
}
const htmlOpts = xtend(defaultOpts, opts)
const html = htmlIndex(htmlOpts).pipe(createMetaTag())
const htmlBuf = (env === 'development')
? html.pipe(lrScript()).pipe(bl())
: html.pipe(bl())
const htmlBuf = (state.optimize)
? html.pipe(bl())
: html.pipe(lrScript()).pipe(bl())

return function (req, res) {
res.setHeader('Content-Type', 'text/html')
Expand Down
13 changes: 5 additions & 8 deletions handler-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ function js (state) {
return function (browserify, src, opts) {
opts = opts || {}

assert.equal(typeof opts, 'object', 'opts should be an object')
assert.equal(typeof browserify, 'function', 'browserify should be a fn')
assert.equal(typeof src, 'string', 'src should be a location')

// signal to CSS that browserify is registered
state.jsRegistered = true
assert.equal(typeof opts, 'object', 'bankai/js: opts should be an object')
assert.equal(typeof browserify, 'function', 'bankai/js: browserify should be a fn')
assert.equal(typeof src, 'string', 'bankai/js: src should be a location')

const baseBrowserifyOpts = {
cache: {},
Expand All @@ -33,7 +30,7 @@ function js (state) {

// enable css if registered
if (state.cssOpts) {
if (!state.cssBuf || process.env.NODE_ENV === 'development') {
if (!state.cssBuf || !state.optimize) {
state.cssBuf = bl()
state.cssReady = false
}
Expand All @@ -47,7 +44,7 @@ function js (state) {
})
}

if (process.env.NODE_ENV === 'development') {
if (!state.optimize) {
b.plugin(errorify)
b = watchify(b)
}
Expand Down
33 changes: 24 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
const stream = require('readable-stream')
const mutate = require('xtend/mutable')
const Emitter = require('events')

const state = new Emitter()
const html = require('./handler-html')
const css = require('./handler-css')
const js = require('./handler-js')

state.cssStream = new stream.PassThrough()
state.jsRegistered = false
state.cssReady = false
state.cssOpts = null
state.cssBuf = null
module.exports = bankai

exports.html = require('./handler-html')(state)
exports.css = require('./handler-css')(state)
exports.js = require('./handler-js')(state)
// create a new bankai instance
// (obj?) -> obj
function bankai (opts) {
opts = opts || {}

const state = new Emitter()
state.cssStream = new stream.PassThrough()
state.cssBuf = null
state.jsRegistered = false
state.cssReady = false
state.cssOpts = null
mutate(state, opts)

return {
html: html(state),
css: css(state),
js: js(state)
}
}
33 changes: 24 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const bankai = require('../')
test('html', function (t) {
t.test('returns data', function (t) {
t.plan(2)
const html = bankai.html()
const assets = bankai()
const html = assets.html()
const server = http.createServer(function (req, res) {
html(req, res).pipe(res)
})
Expand All @@ -29,13 +30,15 @@ test('html', function (t) {
test('css', function (t) {
t.test('asserts input types', function (t) {
t.plan(1)
t.throws(bankai.css.bind(null, 'foo'), /object/)
const assets = bankai()
t.throws(assets.css.bind(null, 'foo'), /object/)
})

t.test('returns data', function (t) {
t.plan(2)
const css = bankai.css({ basedir: __dirname })
bankai.js(browserify, path.join(__dirname, './fixture.js'))
const assets = bankai()
const css = assets.css({ basedir: __dirname })
assets.js(browserify, path.join(__dirname, './fixture.js'))
const server = http.createServer(function (req, res) {
css(req, res).pipe(res)
})
Expand All @@ -46,7 +49,6 @@ test('css', function (t) {
res.pipe(concat(function (buf) {
const str = String(buf)
t.equal(res.headers['content-type'], 'text/css')
console.log(str)
t.ok(/\.foo {}/.test(str), 'css is equal')
server.close()
}))
Expand All @@ -58,23 +60,36 @@ test('css', function (t) {
test('js', function (t) {
t.test('js asserts input types', function (t) {
t.plan(2)
t.throws(bankai.js, /browserify/)
t.throws(bankai.js.bind(null, browserify), /src/)
const assets = bankai()
t.throws(assets.js, /browserify/)
t.throws(assets.js.bind(null, browserify), /src/)
})

t.test('js returns data', function (t) {
t.plan(1)
const js = bankai.js(browserify, './test/fixture.js')
const assets = bankai()
const js = assets.js(browserify, './test/fixture.js')
const server = http.createServer(function (req, res) {
js(req, res).pipe(res)
})
server.listen()

http.get('http://localhost:' + getPort(server), function (res) {
res.pipe(concat(function (buf) {
t.equal(res.headers['content-type'], 'application/javascript')
const actual = res.headers['content-type']
const expected = 'application/javascript'
t.equal(actual, expected, 'content type is equal')
server.close()
}))
})
})
})

test('__END__', function (t) {
t.on('end', function () {
setTimeout(function () {
process.exit(0)
}, 100)
})
t.end()
})