Skip to content

Instantly share code, notes, and snippets.

@getjump
Created October 26, 2015 09:02
Show Gist options
  • Save getjump/8ce29c05b8375e0a4735 to your computer and use it in GitHub Desktop.
Save getjump/8ce29c05b8375e0a4735 to your computer and use it in GitHub Desktop.
var gulp, sass, sourcemaps, browserify, coffeeify, source, util, watchify, _, livereload, notify, browserSync;
gulp = require('gulp');
util = require('gulp-util');
watchify = require('watchify');
sass = require('gulp-sass');
sourcemaps = require('gulp-sourcemaps');
browserify = require('browserify');
coffeeify = require('coffeeify');
source = require('vinyl-source-stream');
livereload = require('gulp-livereload');
_ = require('lodash');
notify = require("gulp-notify");
browserSync = require('browser-sync');
function browserifyInstance(fileName, userOpts) {
if(!userOpts) {
userOpts = {};
}
var defaultOpts = {
extensions: ['.coffee', '.js']
};
return browserify(fileName, _.assign(defaultOpts, userOpts))
}
gulp.task('browserSync', function() {
browserSync({
proxy: 'localhost:3000',
files: ['./app/views/**']
});
});
gulp.task('watch', ['watch-sass', 'watch-scss', 'watch-coffee', 'browserSync'], function() {
livereload.listen();
});
gulp.task('watch-sass', function() {
gulp.watch('app/assets/stylesheets/**/*.sass', ['compile-sass']);
});
gulp.task('watch-scss', function() {
gulp.watch('app/assets/stylesheets/**/*.scss', ['compile-scss']);
});
gulp.task('watch-coffee', function() {
var watchBrowserify = watchify(browserifyInstance('./app/assets/javascripts/application.coffee', _.assign(watchify.args, { debug: true })));
var updateOnChange = function() {
return watchBrowserify
.bundle()
.on('error', util.log.bind(util, 'Browserify Error'))
.pipe(source('bundle.js'))
.pipe(gulp.dest('public/javascripts'))
.pipe(browserSync.reload({stream:true}));
};
watchBrowserify
.transform('coffeeify')
.on('log', util.log)
.on('update', updateOnChange)
updateOnChange();
});
gulp.task('default', ['compile-sass', 'compile-scss', 'compile-coffee']);
gulp.task('compile-sass', function() {
gulp.src('app/assets/stylesheets/**/*.sass')
.pipe(sourcemaps.init())
.pipe(sass({ indentedSyntax: true, errLogToConsole: true }))
.pipe(sourcemaps.write())
.pipe(gulp.dest('public/stylesheets'))
.pipe(livereload())
.pipe(browserSync.reload({stream:true}));
});
gulp.task('compile-scss', function() {
gulp.src('app/assets/stylesheets/**/*.scss')
.pipe(sourcemaps.init())
.pipe(sass({ indentedSyntax: false, errLogToConsole: true }))
.pipe(sourcemaps.write())
.pipe(gulp.dest('public/stylesheets'))
.pipe(livereload())
.pipe(browserSync.reload({stream:true}));
});
gulp.task('compile-coffee', function() {
var stream = browserifyInstance('./app/assets/javascripts/application.coffee',
{ debug: true /* enables source maps */ }
)
.transform('coffeeify')
.bundle();
stream.pipe(source('bundle.js'))
.pipe(gulp.dest('public/javascripts'))
.pipe(livereload());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment