Skip to content

Instantly share code, notes, and snippets.

@DC3
Created July 15, 2015 04:40
Show Gist options
  • Select an option

  • Save DC3/09c449a28a53a8944f10 to your computer and use it in GitHub Desktop.

Select an option

Save DC3/09c449a28a53a8944f10 to your computer and use it in GitHub Desktop.
watchify-task
fs = require 'fs'
path = require 'path'
source = require 'vinyl-source-stream'
buffer = require 'vinyl-buffer'
stream = require 'vinyl-to-stream'
watchify = require 'watchify'
browserify = require 'browserify'
defaultOptions =
extensions: ['.coffee']
debug: true
cache: {}
packageCache: {}
transform: ["coffeeify"]
defaults = (target, def) ->
for own key, val of def when not (key of target)
target[key] = val
target
module.exports = (src, dest, getInstance = false, opts = {}) ->
config = defaults(opts, defaultOptions)
config.entries = [src]
fileName = "#{path.basename(dest, '.coffee')}.js"
log = console.log.bind(console, "watchify: #{src} -> #{dest}")
b = watchify browserify config
bundle = ->
unless b
b = watchify browserify config
events()
b.bundle()
.on 'error', (err) ->
console.error('watchify ', err.toString())
this.emit('end')
b = null
.pipe source(fileName)
.pipe buffer()
.pipe stream()
.pipe fs.createWriteStream(dest)
events = ->
b.on('update', bundle)
b.on('log', log)
events()
return if getInstance then {b, bundle} else bundle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment