Last active
February 3, 2019 21:57
-
-
Save zellwk/4b65d9cf031d8a1654c9 to your computer and use it in GitHub Desktop.
Gulpfile-development-phase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var sass = require('gulp-sass'); | |
var plumber = require('gulp-plumber'); | |
var notify = require('gulp-notify'); | |
var browserSync = require('browser-sync'); | |
var autoprefixer = require('gulp-autoprefixer'); | |
var sourcemaps = require('gulp-sourcemaps'); | |
var spritesmith = require('gulp.spritesmith'); | |
var gulpIf = require('gulp-if'); | |
var nunjucksRender = require('gulp-nunjucks-render'); | |
var data = require('gulp-data'); | |
var fs = require('fs'); | |
var del = require('del'); | |
var runSequence = require('run-sequence'); | |
// =========== | |
// INTRO PHASE | |
// =========== | |
// Hello task | |
gulp.task('hello', function() { | |
console.log('Hello Zell'); | |
}); | |
// ================= | |
// DEVELOPMENT PHASE | |
// ================= | |
// Custom Plumber function for catching errors | |
function customPlumber(errTitle) { | |
return plumber({ | |
errorHandler: notify.onError({ | |
// Customizing error title | |
title: errTitle || 'Error running Gulp', | |
message: 'Error: <%= error.message %>', | |
}) | |
}); | |
} | |
// Browser Sync | |
gulp.task('browserSync', function() { | |
browserSync({ | |
server: { | |
baseDir: 'app' | |
}, | |
}); | |
}); | |
// Compiles Sass to CSS | |
gulp.task('sass', function() { | |
return gulp.src('app/scss/**/*.scss') | |
.pipe(customPlumber('Error Running Sass')) | |
.pipe(sourcemaps.init()) | |
.pipe(sass({ | |
includePaths: [ | |
'app/bower_components', | |
'node_modules' | |
] | |
})) | |
.pipe(autoprefixer()) | |
.pipe(sourcemaps.write()) | |
.pipe(gulp.dest('app/css')) | |
.pipe(browserSync.reload({ | |
stream: true | |
})); | |
}); | |
// Sprites | |
gulp.task('sprites', function() { | |
gulp.src('app/images/sprites/**/*') | |
.pipe(spritesmith({ | |
cssName: '_sprites.scss', // CSS file | |
imgName: 'sprites.png', // Image file | |
retinaSrcFilter: 'app/images/sprites/*@2x.png', | |
retinaImgName: '[email protected]' | |
})) | |
.pipe(gulpIf('*.png', gulp.dest('app/images'))) | |
.pipe(gulpIf('*.scss', gulp.dest('app/scss'))); | |
}); | |
// Watchers files for changes | |
gulp.task('watch', function() { | |
gulp.watch('app/scss/**/*.scss', ['sass', 'lint:scss']); | |
gulp.watch('app/js/**/*.js', browserSync.reload); | |
gulp.watch([ | |
'app/pages/**/*.+(html|nunjucks)', | |
'app/templates/**/*', | |
'app/data.json' | |
], ['nunjucks']); | |
}); | |
// Templating | |
gulp.task('nunjucks', function() { | |
return gulp.src('app/pages/**/*.+(html|nunjucks)') | |
.pipe(customPlumber('Error Running Nunjucks')) | |
.pipe(data(function() { | |
return JSON.parse(fs.readFileSync('./app/data.json')) | |
})) | |
.pipe(nunjucksRender({ | |
path: ['app/templates'] | |
})) | |
.pipe(gulp.dest('app')) | |
.pipe(browserSync.reload({ | |
stream: true | |
})) | |
}); | |
// Clean | |
gulp.task('clean:dev', function(callback) { | |
return del.sync([ | |
'app/css', | |
'app/*.html' | |
]); | |
}); | |
// Consolidated dev phase task | |
gulp.task('default', function(callback) { | |
runSequence( | |
'clean:dev', | |
['sprites',], | |
['sass', 'nunjucks'], | |
['browserSync', 'watch'], | |
callback | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment