Skip to content

Instantly share code, notes, and snippets.

@Nitive
Last active August 29, 2015 14:20
Show Gist options
  • Save Nitive/30a991866a400c04cf57 to your computer and use it in GitHub Desktop.
Save Nitive/30a991866a400c04cf57 to your computer and use it in GitHub Desktop.
gulp + browserify + watchify + error notify + coffeescript
# npm install gulp-uglify browser-sync gulp-notify browserify vinyl-source-stream gulp-streamify watchify gulp-if colors gulp --save-dev
uglify = require 'gulp-uglify'
sync = require 'browser-sync'
notify = require 'gulp-notify'
browserify = require 'browserify'
source = require 'vinyl-source-stream'
streamify = require 'gulp-streamify'
watchify = require 'watchify'
gulpif = require 'gulp-if'
colors = require 'colors'
gulp = require 'gulp'
production = false
# or you can get it with yargs or the another simular thing
# args = require('yargs').argv
# production = args.p or args.production
paths =
browserify: './app.coffee'
output: 'app.js'
dest: './public/'
buildScript = (files, watch) ->
rebundle = (callback) ->
stream = bundler.bundle()
stream
.on "error", notify.onError # optional (for gulp-notify)
title: "Compile Error" #
message: "<%= error.message %>" #
.pipe source paths.output
.pipe gulpif production, streamify do uglify # optional (for gulp-uglify)
.pipe gulp.dest paths.dest
.pipe sync.reload stream: true # optional (for browser-sync)
stream.on 'end', ->
do callback if typeof callback == "function"
props = watchify.args
props.entries = files
props.debug = not production
bundler = if watch then watchify(browserify props) else browserify props
bundler.transform "coffee-reactify" # "coffeeify" or whatever or comment it
bundler.on "update", ->
now = new Date().toTimeString()[..7]
console.log "[#{now.gray}] Starting #{"'browserify'".cyan}..."
startTime = new Date().getTime()
rebundle ->
time = (new Date().getTime() - startTime) / 1000
now = new Date().toTimeString()[..7]
console.log "[#{now.gray}] Finished #{"'browserify'".cyan} after #{(time + 's').magenta}"
rebundle()
gulp.task "browserify", -> # compile (slow)
buildScript paths.browserify, false
gulp.task "watchjs", -> # watch and compile (first time slow, after fast)
buildScript paths.browserify, true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment