var gulp = require('gulp')
,   rename = require('gulp-rename')
,   less = require('gulp-less')
,   bg = require('gulp-bg')
,   gutil = require('gulp-util')
,   browserify = require('gulp-browserify')
,   minimist = require('minimist')
,   sync_exec = require('sync-exec')
;

var knownOptions = {
    default: {
        config: "base",
    }
}

var options = minimist(process.argv.slice(2), knownOptions);

var API_PORT = 4005;
var EXPRESS_PORT = 8000;
var EXPRESS_ROOT = __dirname + "/frontend";
var LIVERELOAD_PORT = 3572;

gulp.task('startExpress', ['build'], function() {
    var express = require('express');
    var app = express();
    app.use('/media', express.static(__dirname + "/public/media"));
    app.use(require('connect-livereload')());
    app.use(express.static(EXPRESS_ROOT));
    app.listen(EXPRESS_PORT);

    bg("python", "manage.py", "runserver", API_PORT)();
});

gulp.task('startLiveReload', ['startExpress'], function() {
    lr = require('tiny-lr')();
    lr.listen(LIVERELOAD_PORT);
});

gulp.task('injectEnvConfig', function(cb) {
    gulp.src("frontend/config." + options.config + ".json")
        .pipe(rename("config.json"))
        .pipe(gulp.dest("frontend"))
        .on('end', function(){cb();})
    ;
}
);

gulp.task('compile_less', function (cb) {
    gulp.src('frontend/styles/site-*.less')
        .pipe(less())
        .pipe(gulp.dest('frontend/styles'))
        .on('end', function(){cb();})
    ;
});

gulp.task('browserify', function(cb) {
    gulp.src('frontend/index.js')
        .pipe(browserify({
            insertGlobals : true,
            debug : options.config !== 'production',
            transform: ['hbsfy']
        }))
        .pipe(rename('bundle.js'))
        .pipe(gulp.dest('frontend'))
        .on('end', function(){cb();})
});

gulp.task('closure', ['browserify'], function(cb) {
    var res = sync_exec(
        'ccjs frontend/bundle.js --language_in=ECMASCRIPT5'
    );
    if (res.stderr) {
        console.error(res.stderr);
    }
    if (res.stdout) {
        function string_src(filename, string) {
          var src = require('stream').Readable({ objectMode: true })
          src._read = function () {
            this.push(new gutil.File({ cwd: "", base: "", path: filename, contents: new Buffer(string) }))
            this.push(null)
          }
          return src
        }
        string_src("bundle_min.js", res.stdout)
            .pipe(gulp.dest('frontend'))
        ;
    } else {
        throw new gutil.PluginError({
          plugin: "Closure",
          message: 'Failed to compile JS.'
        });
    }
    cb();
});

gulp.task('build', ['closure', 'compile_less', 'injectEnvConfig'], function(){});

// Notifies livereload of changes detected
// by `gulp.watch()`
// FIXME: This is still a function because I can't figure out how
// to get the event object if I change this to a task.
function notifyLivereload(event) {

    // `gulp.watch()` events provide an absolute path
    // so we need to make it relative to the server root
    var fileName = require('path').relative(EXPRESS_ROOT, event.path);

    gulp.run('build');

    setTimeout(function(){
        lr.changed({
            body: {
                files: [fileName]
            }
        });
    }, 500); // For some reason, triggering live reload too early serves the old version
}

gulp.task('default', ['startLiveReload'], function() {
    gulp.watch(['frontend/index.html', 'frontend/index.js', 'frontend/router.js',
                'frontend/styles/site.less', 'frontend/templates/*.hbs', 'frontend/views/*.js'],
                notifyLivereload);
});


@@ -324,24 +324,45 @@
 ig: %22base%22,%0A
+        fast: false,%0A
     %7D%0A%7D%0A%0Avar
@@ -1853,24 +1853,132 @@
 ction(cb) %7B%0A
+    if (options.fast) %7B%0A        sync_exec('cp frontend/bundle.js frontend/bundle_min.js');%0A    %7D else %7B%0A    
     var res 
@@ -1990,16 +1990,20 @@
 c_exec(%0A
+    
         
@@ -2058,15 +2058,23 @@
 T5'%0A
+    
     );%0A
+    
     
@@ -2087,24 +2087,28 @@
 s.stderr) %7B%0A
+    
         cons
@@ -2130,16 +2130,20 @@
 tderr);%0A
+    
     %7D%0A  
@@ -2140,24 +2140,28 @@
       %7D%0A    
+ 
+   
 if (res.stdo
@@ -2162,24 +2162,28 @@
 s.stdout) %7B%0A
+    
         func
@@ -2224,24 +2224,28 @@
 %7B%0A          
+    
 var src = re
@@ -2301,16 +2301,20 @@
         
+  
+  
 src._rea
@@ -2327,24 +2327,28 @@
 nction () %7B%0A
+    
             
@@ -2446,32 +2446,36 @@
 %7D))%0A            
+    
 this.push(null)%0A
@@ -2476,34 +2476,42 @@
 null)%0A          
+   
+ 
 %7D%0A
+    
           return
@@ -2515,34 +2515,42 @@
 urn src%0A        
-%7D%0A
+    %7D%0A    
         string_s
@@ -2585,32 +2585,36 @@
 ut)%0A            
+    
 .pipe(gulp.dest(
@@ -2634,18 +2634,26 @@
         
-;%0A
+    ;%0A    
     %7D el
@@ -2665,16 +2665,20 @@
         
+ 
+   
 throw ne
@@ -2699,16 +2699,20 @@
 Error(%7B%0A
+    
         
@@ -2742,16 +2742,20 @@
         
+    
 message:
@@ -2783,28 +2783,42 @@
 S.'%0A        
+  
+  
 %7D);%0A
+        %7D%0A
     %7D%0A    cb

Add --fast option for gulp