Skip to content

Instantly share code, notes, and snippets.

@htom78
Forked from sl-digital/gulpfile.js
Last active August 29, 2015 14:11
Show Gist options
  • Save htom78/63a625c07025c63cab00 to your computer and use it in GitHub Desktop.
Save htom78/63a625c07025c63cab00 to your computer and use it in GitHub Desktop.
//==============================================================================
// H&L GulpJS + Node - Front End Asset Management
//==============================================================================
var gulp = require("gulp"),
sass = require('gulp-sass'),
autoprefixer = require('gulp-autoprefixer'),
jshint = require('gulp-jshint'),
concat = require('gulp-concat'),
uglify = require('gulp-uglifyjs'),
watch = require('gulp-watch'),
mincss = require('gulp-minify-css'),
rename = require('gulp-rename'),
inject = require('gulp-inject'),
clean = require('gulp-clean'),
sequence = require('run-sequence');
//==============================================================================
// Node Package List - Install to Project Dir
//==============================================================================
/*
npm install --save-dev gulp
npm install gulp-sass gulp-autoprefixer gulp-jshint gulp-concat gulp-uglifyjs gulp-watch gulp-minify-css gulp-rename gulp-inject gulp-clean run-sequence --save-dev
bower install zurb/bower-foundation
*/
//==============================================================================
// Asset Lists for Injection
//==============================================================================
var cssOrder = [];
var jsOrder = [];
//==============================================================================
// assets/dist Methods
//==============================================================================
//Lint all JS
gulp.task('lint', function(){
return gulp.src('public/assets/js/*.js')
.pipe(jshint())
.pipe(jshint.reporter('default'));
});
//Clean out old distribution assets
gulp.task('clean-static', function(){
return gulp.src(["public/assets/dist/js/*.js","public/assets/dist/css/*.css"],{read:false}).pipe(clean());
});
//Clean out old view templates
gulp.task('clean-views', function(){
return gulp.src(["app/views/static/*"],{read:false}).pipe(clean());
});
//Compile SASS
gulp.task('sass', function(){
return gulp.src('public/assets/scss/*.scss')
.pipe(sass({errLogToConsole:true}))
.pipe(autoprefixer('last 2 versions'))
.pipe(gulp.dest('public/assets/css/'));
});
//Compile and minify SASS
gulp.task('sass-min', function(){
return gulp.src('public/assets/scss/*.scss')
.pipe(sass({errLogToConsole:true}))
.pipe(autoprefixer('last 2 versions'))
.pipe(gulp.dest('public/assets/css/'))
.pipe(mincss())
.pipe(gulp.dest('public/assets/css/'));
});
//Concatenate and Minify JS With Timestamp
gulp.task('crush-js', function(){
gulp.src('public/assets/dist/js/*.js',{read:false}).pipe(clean());
return gulp.src(jsOrder)
.pipe(uglify('all.min.js',{mangle:false,output:{beautify:false}}))
.pipe(rename({suffix:"." + new Date().getTime(),extname:".js"}))
.pipe(gulp.dest('public/assets/dist/js/'));
});
//Concatenate and Minify CSS With Timestamp
gulp.task('crush-css', function(){
gulp.src('public/assets/dist/css/*',{read:false}).pipe(clean());
return gulp.src(cssOrder)
.pipe(concat('all.min.css'))
.pipe(rename({suffix:"." + new Date().getTime(),extname:".css"}))
.pipe(gulp.dest('public/assets/dist/css/'))
.pipe(mincss())
.pipe(gulp.dest('public/assets/dist/css/'));
});
//Inject Compressed Header Files
gulp.task('inject-header', function(){
return gulp.src('public/assets/views/header.php')
.pipe(inject(
gulp.src(["public/assets/dist/css/*.css",],
{read:false}),{addRootSlash:true})
)
.pipe(gulp.dest('app/views/static/'));
});
//Inject Compressed Header Files
gulp.task('inject-footer', function(){
return gulp.src('public/assets/views/footer.php')
.pipe(inject(
gulp.src(["public/assets/dist/js/*.js"],
{read:false}),{addRootSlash:true})
)
.pipe(gulp.dest('app/views/static/'));
});
//==============================================================================
// Default Watch - Compile SASS/SCSS
//==============================================================================
//Watch files for changes
gulp.task('watch', function(){
gulp.watch('public/assets/scss/*.scss',['build']);
gulp.watch('public/assets/js/*.js',['build']);
});
//==============================================================================
// Default Task - Compile SASS/SCSS
//==============================================================================
// Default Tasks
gulp.task('default',['sass-min']);
gulp.task('clean',['clean-all']);
//==============================================================================
// Build Project
//==============================================================================
//Full assets/dist with dependency
gulp.task('build',function(callback){
sequence('clean-static','sass-min','crush-css','crush-js','clean-views','inject-header','inject-footer',callback);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment