Skip to content

Instantly share code, notes, and snippets.

@nicohvi
Created June 17, 2016 08:29
Show Gist options
  • Save nicohvi/aeb5742b776d0155f6c204c82facc9a2 to your computer and use it in GitHub Desktop.
Save nicohvi/aeb5742b776d0155f6c204c82facc9a2 to your computer and use it in GitHub Desktop.
const gulp = require('gulp');
const sass = require('gulp-sass');
const sassGlob = require('gulp-sass-glob');
const sourcemaps = require('gulp-sourcemaps');
const gutil = require('gulp-util');
const notify = require('gulp-notify');
const browserify = require('browserify');
const watchify = require('watchify');
const babelify = require('babelify');
const source = require('vinyl-source-stream');
const buffer = require('vinyl-buffer');
const production = process.env.ENV === 'prod';
function handleError () {
const args = Array.prototype.slice.call(arguments);
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
this.emit('end');
}
function css () {
function rebuild () {
console.log('-> Building css');
gulp.src('./app/assets/stylesheets/**/app.scss')
.pipe(sourcemaps.init())
.pipe(sassGlob())
.pipe(sass({ errLogToConsole: true }))
.on('error', function (err) {
console.log(err.message)
this.emit('end');
})
.pipe(sourcemaps.write())
.pipe(gulp.dest('public/css/'))
.pipe(notify('Built css'));
}
gulp.watch('./app/assets/stylesheets/**/*.scss', rebuild);
rebuild();
}
function js (watch) {
const src = './app/assets/scripts';
const opts = {
entries: [`${src}/app.js`],
debug: false
}
const bundler = watch ? watchify(browserify(opts)) : browserify(opts);
bundler.transform(babelify);
function rebuild () {
const start = Date.now();
gutil.log('Compiling javascript');
bundler
.bundle()
.on('error', handleError)
.pipe(source('bundle.js'))
.pipe(buffer())
.pipe(gulp.dest('./public/javascripts/'))
.pipe(notify('Compiled javascript'));
gutil.log(`Done, time spent: ${Date.now() - start} ms.`);
}
bundler.on('update', rebuild);
rebuild();
}
gulp.task('css', () => css());
gulp.task('js', () => js());
gulp.task('watch', () => {
css(true);
js(true);
});
gulp.task('build', ['css', 'js'])
gulp.task('default', ['watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment