Skip to content

Instantly share code, notes, and snippets.

@donpayne
Last active October 19, 2015 15:46
Show Gist options
  • Save donpayne/b82ad6e73ba3f0d05887 to your computer and use it in GitHub Desktop.
Save donpayne/b82ad6e73ba3f0d05887 to your computer and use it in GitHub Desktop.
// Modules
var gulp = require('gulp');
var nodemon = require('gulp-nodemon');
var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');
var browserify = require('browserify');
var watchify = require('watchify');
var envify = require('envify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var notify = require('gulp-notify');
var gutil = require('gulp-util');
// TODO: determine when to set the environment to 'production'
// process.env.NODE_ENV = 'production';
//********************************************************************
var production = process.env.NODE_ENV === 'production';
function handleError (task)
{
return function (err)
{
gutil.log(gutil.colors.red(err));
notify.onError(task + ' failed, check the logs...')(err);
};
}
gulp.task('lint', function ()
{
gulp.src([ './app/**/*.js', './config/**/*.js', './src/**/*.js' ], { base: __dirname })
.pipe(jshint('.jshintrc'))
.pipe(jshint.reporter('jshint-stylish'));
});
gulp.task('watch', function ()
{
gulp.watch([ './app/**/*.js', './config/**/*.js', './src/**/*.js' ], [ 'lint' ]);
gutil.log('Linting...');
});
gulp.task('nodemon', [ 'scripts' ], function ()
{
nodemon(
{
script: 'app.js',
ext: 'html js',
env: { NODE_ENV: 'development' },
nodeArgs: [ '--debug' ],
stdout: false,
stderr: false,
watch: [ 'app/' ]
})
.on('restart', function (files)
{
gutil.log('[server] App restarted due to: ', gutil.colors.cyan(files));
})
.on('stdout', function (raw)
{
var msg = raw.toString('utf8');
gutil.log('[server]', gutil.colors.green(msg));
})
.on('stderr', function (err)
{
var msg = err.toString('utf8');
// For some reason debugger attachment gets logged on 'stderr', so we catch it here...
if (msg.indexOf('Debugger listening on port') === 0)
{
gutil.log('[server]', gutil.colors.green(msg));
}
else
{
handleError('Node server')(msg);
}
});
});
function buildScript (watching)
{
// Browserify Options
var bundler = browserify(
{
basedir: __dirname,
debug: !production,
entries: './src/app.js',
cache: {},
packageCache: {},
fullPaths: watching
});
// Watchify if watching requested
if (watching)
{
bundler = watchify(bundler);
gutil.log('Watchifying...');
}
// Transform NODE_ENV variables to strings with Envify
bundler.transform({ global: true }, envify);
// Uglify and Output
function rebundle ()
{
var start = Date.now();
var stream = bundler.bundle();
stream.on('error', handleError('Browserify'));
if (production)
{
return stream.pipe(source('bundle.js')) // gives streaming vinyl file object
.pipe(buffer()) // convert from streaming to buffered vinyl file object
.pipe(uglify())
.pipe(gulp.dest('./public'))
.pipe(notify(function ()
{
gutil.log('Bundled (minified) in ' + (Date.now() - start) + 'ms');
}));
}
else
{
return stream.pipe(source('bundle.js')) // gives streaming vinyl file object
.pipe(gulp.dest('./public'))
.pipe(notify(function ()
{
gutil.log('Bundled (debug) in ' + (Date.now() - start) + 'ms');
}));
}
}
// listen for an update and run rebundle
bundler.on('update', function ()
{
rebundle();
gutil.log('Rebundling...');
});
return rebundle();
}
// run once
gulp.task('scripts', function ()
{
return buildScript(false);
});
// run 'scripts' task first, then watch for future changes
gulp.task('default', [ 'watch', 'lint', 'nodemon' ], function ()
{
return buildScript(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment