Skip to content

Instantly share code, notes, and snippets.

@mahnunchik
Created February 21, 2013 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahnunchik/5002404 to your computer and use it in GitHub Desktop.
Save mahnunchik/5002404 to your computer and use it in GitHub Desktop.
Browserify helper for Cakefile
###*
* @param options.watch - set watches on files, see below
* @param options.cache - turn on caching for AST traversals, see below
* @param options.debug - turn on source mapping for debugging with //@ sourceURL=... in browsers that support it
* @param options.exports - an array of the core items to export to the namespace. Available items: 'require', 'process'
* @param options.require - Require a file or files for inclusion in the bundle.
* @param options.entry - Append a file to the end of the bundle and execute it without having to require() it.
* @param options.filter - Transform the source using the filter function fn(src). The return value of fn should be the new source.
* @param options.handlers - Register a handler to wrap extensions.
###
browserify = (options={}, dest, cb)->
dest = path.join process.cwd(), dest
writeFile = (dest, content, cb)->
fs.writeFile dest, content, (err) ->
if err?
console.error "Error: #{err.message}"
else
console.log "Browserified #{dest} @ #{new Date().toLocaleTimeString()}"
cb? dest
browserify = require('browserify')(options)
if options.handlers?
for own ext, handler of options.handlers
browserify.register(ext, handler)
browserify.on 'syntaxError', (err)->
console.error(err)
writeFile(dest, browserify.bundle(), cb)
if options.watch == true
browserify.on 'bundle', ()->
writeFile(dest, browserify.bundle(), cb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment