Skip to content

Instantly share code, notes, and snippets.

@minibikini
Created May 28, 2015 13:33
Show Gist options
  • Save minibikini/3f456a6dae22d166d2d5 to your computer and use it in GitHub Desktop.
Save minibikini/3f456a6dae22d166d2d5 to your computer and use it in GitHub Desktop.
watchify = require 'watchify'
browserify = require 'browserify'
gulp = require 'gulp'
gutil = require 'gulp-util'
notify = require 'gulp-notify'
source = require 'vinyl-source-stream'
livereload = require 'gulp-livereload'
# pkg = require '../package.json'
production = process.env.NODE_ENV is 'production'
EXTERNALS = [
'react/addons',
'react-router',
'alt',
'iso',
'moment',
'lodash',
'es6-promise',
'superagent',
'classnames',
'react-pager',
'card'
]
BASES =
src: '../client/public'
build: 'public/js'
getBundler = (entry) ->
bundler = browserify
cache: {}
packageCache: {}
fullPaths: global.isWatching
entries: ["./client/#{entry}"]
extensions: ['.js', '.coffee', '.cjsx']
# insertGlobals: yes
debug: !production
# transform: ['coffeeify']
EXTERNALS.forEach (lib) ->
bundler.external lib.expose or lib.file or lib
bundler
getBundle = (entry) ->
bundler = getBundler entry
bundle = ->
if global.isWatching
gutil.log "Start bundling `#{entry}`..."
timeBegin = Date.now()
bundler.bundle()
.on 'error', notify.onError
title: "Browserify Error"
emitError: yes
.on 'error', (err) ->
console.log err
@emit 'end'
.pipe source "#{entry}.js"
.pipe gulp.dest 'public/js/'
.on 'end', ->
if global.isWatching
gutil.log "Done bundle `#{entry}` in #{Date.now() - timeBegin} ms"
.pipe livereload()
{bundle, bundler}
['public', 'admin'].forEach (app) ->
gulp.task "client:#{app}", ['browserify:vendor'], ->
{bundle, bundler} = getBundle app
if global.isWatching
watchify(bundler).on 'update', bundle
bundle()
gulp.task 'client', ['client:public', 'client:admin']
gulp.task 'browserify:vendor', ->
browserify(debug: false)
.require EXTERNALS
.bundle()
.on 'error', notify.onError
title: "Browserify Error"
emitError: yes
.pipe source 'vendor.js'
.pipe gulp.dest "#{BASES.build}/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment