Skip to content

Instantly share code, notes, and snippets.

@stevenpetryk
Created July 1, 2015 12:41
Show Gist options
  • Save stevenpetryk/09ae2c601c41ddc2f0d6 to your computer and use it in GitHub Desktop.
Save stevenpetryk/09ae2c601c41ddc2f0d6 to your computer and use it in GitHub Desktop.
var gulp = require('gulp');
var sequence = require('run-sequence');
var sourcemaps = require('gulp-sourcemaps');
var plumber = require('gulp-plumber')
var stylus = require('gulp-stylus');
var nib = require('nib');
var jeet = require('jeet');
var browserify = require('browserify');
var watchify = require('watchify');
var gutil = require('gulp-util');
var source = require('vinyl-source-stream');
var babelify = require('babelify');
var del = require('del');
var browserSync = require('browser-sync');
//**************************************************************************************************
// Top-level tasks
//**************************************************************************************************
gulp.task('default', ['build']);
gulp.task('serve', ['build', 'bundle:watch'], function() {
browserSync({server: 'build'});
gulp.watch('src/pages/**.html', ['pages']);
gulp.watch('src/styles/**/*.styl', ['styles']);
gulp.watch('src/images/**/*', ['assets']);
});
gulp.task('build', function() {
return sequence('clean', ['styles', 'bundle', 'pages', 'assets']);
});
//**************************************************************************************************
// Sub-tasks
//**************************************************************************************************
gulp.task('clean', function(cb) {
return del(['build'], cb);
});
gulp.task('styles', function() {
return gulp.src('src/styles/main.styl')
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(stylus({use: [nib(), jeet()]}))
.pipe(sourcemaps.write())
.pipe(gulp.dest('build/styles'))
.pipe(browserSync.stream());
});
gulp.task('assets', function() {
return gulp.src('src/images/**/*')
.pipe(gulp.dest('build/images'))
.pipe(browserSync.stream());
});
gulp.task('pages', function() {
return gulp.src('src/pages/**/*')
.pipe(plumber())
.pipe(gulp.dest('build'))
.pipe(browserSync.stream());
});
gulp.task('bundle', function() { return bundle() });
gulp.task('bundle:watch', function() { return bundle({watch: true}); });
var bundler = browserify('src/scripts/site.js', watchify.args);
bundler.transform(babelify);
function bundle(opts) {
if(opts && 'watch' in opts && opts.watch) {
bundler = watchify(bundler);
bundler.on('update', bundle);
}
return bundler.bundle()
.on('error', function(error) {
gutil.log(error.toString());
this.emit('end');
browserSync.notify('Browserify error');
})
.pipe(plumber())
.pipe(source('bundle.js'))
.pipe(gulp.dest('build/scripts'))
.pipe(browserSync.stream());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment