Skip to content

Instantly share code, notes, and snippets.

@kirkonrails
Last active April 12, 2018 15:18
Show Gist options
  • Save kirkonrails/e4023855cfb9c75a5198 to your computer and use it in GitHub Desktop.
Save kirkonrails/e4023855cfb9c75a5198 to your computer and use it in GitHub Desktop.
My Gulp Build Script
var gulp = require('gulp'),
jshint = require('gulp-jshint'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
uglify = require('gulp-uglify'),
gutil = require('gulp-util'),
concat = require('gulp-concat'),
babel = require('gulp-babel'),
gzip = require('gulp-gzip'),
minifyCss = require('gulp-minify-css'),
autoprefixer = require('gulp-autoprefixer');
var config = {
bootstrapDir: './bower_components/bootstrap-sass',
publicDir: './public',
};
gulp.task('default', ['watch']);
// ---------------------------------------------------------------------------------
// SASS & CSS Tasks
// ---------------------------------------------------------------------------------
gulp.task('build-css', function() {
// return gulp.src('source/scss/**/*.scss')
return gulp.src('source/scss/app.scss')
.pipe(sass({includePaths: [config.bootstrapDir + '/assets/stylesheets'],}))
.pipe(gutil.env.type === 'production' ? minifyCss() : gutil.noop())
.pipe(autoprefixer({
browsers: ['> 5%', 'last 1 version'],
cascade: false
}))
.pipe(gulp.dest('public/assets/stylesheets'));
});
gulp.task('build-css-gzip', function() {
// return gulp.src('source/scss/**/*.scss')
return gulp.src('source/scss/app.scss')
.pipe(sass({includePaths: [config.bootstrapDir + '/assets/stylesheets'],}))
.pipe(gutil.env.type === 'production' ? minifyCss() : gutil.noop())
.pipe(autoprefixer({
browsers: ['> 5%', 'last 1 version'],
cascade: false
}))
.pipe(gzip())
.pipe(gulp.dest('public/assets/stylesheets'));
});
// ---------------------------------------------------------------------------------
// Javascript Tasks
// ---------------------------------------------------------------------------------
gulp.task('build-js', function() {
return gulp.src('source/javascripts/**/*.js')
.pipe(babel())
.pipe(sourcemaps.init())
.pipe(concat('bundle.js'))
.pipe(sourcemaps.write())
.pipe(gulp.dest('public/assets/javascripts'));
});
gulp.task('build-js-gzip', function() {
return gulp.src('source/javascripts/**/*.js')
.pipe(babel())
.pipe(sourcemaps.init())
.pipe(concat('bundle.js'))
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gzip())
.pipe(gulp.dest('public/assets/javascripts'));
});
// configure the jshint task
gulp.task('jshint', function() {
return gulp.src('source/javascripts/**/*.js')
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
});
// ---------------------------------------------------------------------------------
// Misc Tasks
// ---------------------------------------------------------------------------------
gulp.task('build-fonts', function() {
return gulp.src(config.bootstrapDir + '/assets/fonts/**/*')
.pipe(gulp.dest(config.publicDir + '/fonts'));
});
gulp.task('move', function(){
gulp.src('source/**/*.html')
.pipe(gulp.dest('public/'));
});
// ---------------------------------------------------------------------------------
// Production Deployment Tasks
// ---------------------------------------------------------------------------------
gulp.task('deploy', ['build-js',
'build-js-gzip',
'build-css',
'build-css-gzip',
'move',
'build-fonts']);
// ---------------------------------------------------------------------------------
// Watch
// ---------------------------------------------------------------------------------
// configure which files to watch and what tasks to use on file changes
gulp.task('watch', function() {
gulp.watch('source/javascripts/**/*.js', ['build-js']);
gulp.watch(['source/scss/**/*.scss'], ['build-css']);
gulp.watch(['source/**/*.html'], ['move']);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment