bfydir
v2.0.0
Published
http-server to browserify *.js in a dir
Downloads
7
Readme
bfydir
a http-server which watchify's
all the files in a dir simultaneously uppon request. also it supports
minifying js-files and inlining it into html-tags:
<html><body><script>/* bundled source here */</script></body></html>.
cli
bfydir [<dir>] [-p,--port <port>] [-b,--bundles <bundles>] [-d,--debug]
[--key <key>] [--cert <cert>] [--pfx <pfx>]
[-a,--auth <user>:<pwd>[,<user>:<pwd>]]
[-h,--help]
<dir> .. serve files from that directory (default: pwd)
<port> .. listen on that port (default: 8001)
<bundles> .. write bundles into that directory (default: <dir>/.bfydir-bundles)
debug .. write infos about bundling/minifying to stdout
<key> .. path to ssl-key-file
<cert> .. path to cert-key-file
<pfx> .. path to pfx-file
auth .. basic auth (comma separated list of user:pwd
help .. only show this message, otherwise just start bfydir
querystring-parameters:
bundle, b .. bundles the requested file instead of serving it
min, m .. minifies the bundle (sets b automatically)
inline, i .. inlines the bundle into html (sets b)
transform, t .. comma-separated list of transforms (sets b)
ignore .. comma-separated list of modules to ignore (sets b)
example:
$ bfydir ~ -p 8080 -a user1:pwd1,user2:pwd2
$ echo "module.exports = 'a'" > ~/a.js
$ echo "module.exports = 'b'" > ~/b.js
$ echo "console.log(require('./a'), require('./b'))" > ~/c.js
$ node -e `curl "http://user1:pwd1@localhost:8005/c.js?b"`
a busage
install bfydir and start the server:
$ npm i -g bfydir
$ bfydir ~ -p 8005write code:
$ echo "module.exports = 'a'" > ~/a.js
$ echo "module.exports = 'b'" > ~/b.js
$ echo "console.log(require('./a'), require('./b'))" > ~/c.jscreate a bundle:
$ node -e `curl http://localhost:8005/c.js?bundle`
a bwhen you change the code it will update the bundles:
$ echo "module.exports = 'foo'" > ~/a.js
$ node -e `curl http://localhost:8005/c.js?bundle`
foo bwith the inline querystring-parameter it wrap the bundle with html-tags:
$ curl http://localhost:8005/c.js?inline
<html><body><script>/* bundled source */</script></body></html>inline and minify:
$ curl http://localhost:8005/c.js?inline&min
<html><body><script>/* bundled source minified */</script></body></html>use one or multiple transforms:
$ curl http://localhost:8005/some-file.js?transform=glslify,otherTransform
< transformed bundle >without any querystring-parameters it will serve the source without bundling:
$ curl http://localhost:8005/a.js
module.exports = 'foo'api
var bfydir = require('bfydir')([opts])
opts is optional
opts.dir- serve files from that directory (default:process.cwd())opts.bundles- write bundled files into that directory (default:process.cwd()+'/.bfydir-bundles')
bfydir is an eventemitter which emits following events:
bundlingbundling:<path>bundledbundled:<path>minifyingminifying:<path>minifiedminified:<path>
<path> is the url-pathname of the served bundle
all the events emit with a info-object:
bfydir.on('bundled', function(info){
// info.urlPath
// info.entryPath
// info.bundlePath
// info.bundlePathMin (only when minifying)
// info.bundleSize
// info.bundleSizeMin
})var server = bfydir.createServer()
server is a http-server, its a shortcut for
http.createServer(this.requestHandler())
var handler = bfydir.requestHandler()
this is a shortcut for var handler = bfydir.handleRequest.bind(bfydir)
bfydir.handleRequest(req, res[, next])
this will look at the query-string object require('url').parse(req.url)
for various member attributes:
bundleorb- if set pipe a browserify-bundle-stream intores(or if the bundle exists on disk pipe it directly from disk)inlineori- if set pipe the content throughbfydir.inlineStream()minorm- if set pipe the content throughbfydir.minifyStream()transformort- (comma-separated list) if set use these transformsignore- (comma-separated list) if set tell browserify to ignore
var bStream = bfydir.bundleStream(opts)
bStreamis a through-stream in which a browserify-bundle-stream gets piped into. the result gets piped to disk.optsmust be an objectopts.urlPathis used to identify the bundle-watcheropts.entryPathopts.bundlePathopts.bundleOpts
var mStream = bfydir.minifyStream(opts)
mStreamis a through-stream which buffers all content and then minifys it. then it writes the result to disk (so actually its just uglifyjs pretending to be streaming :D).optsmust be an objectopts.urlPathopts.bundlePathMinopts.entryPath
var iStream = require('bfydir').inlineStream([sourcePath])
- if
sourcePathis set,fs.createReadStream(bundlePath)gets piped through the stream iStreamis a through-stream which envelops the content into html-tags:
<html>
<head>
<meta content="width=device-width,
initial-scale=1.0,
maximum-scale=1.0,
user-scalable=0"
name="viewport" />
<meta charset=utf-8>
</head>
<body>
<script>
/* here is the content */
</script>
</body>
</html>bfydir.close()
close all the watchify-bundle-watchers
