Skip to content

Instantly share code, notes, and snippets.

@hmouhtar
Last active February 3, 2019 14:29
Show Gist options
  • Save hmouhtar/aae71ae170b032519176623fa8411589 to your computer and use it in GitHub Desktop.
Save hmouhtar/aae71ae170b032519176623fa8411589 to your computer and use it in GitHub Desktop.
A gulpfile to compile SASS, add necessary prefixes and minify the resulting css file. Also includes a function to compress images.
// Gulp components
const {
series,
src,
dest,
watch
} = require('gulp');
// Dependencies
const sass = require('gulp-sass');
const plumber = require('gulp-plumber');
const notify = require('gulp-notify');
const browserSync = require('browser-sync').create();
const autoprefixer = require('gulp-autoprefixer');
const cssmin = require('gulp-cssnano');
const imagemin = require('gulp-imagemin');
const imageminJpegRecompress = require('imagemin-jpeg-recompress');
const imageminPngquant = require('imagemin-pngquant');
const newer = require('gulp-newer');
// Function to display a pop-up with errors
function customPlumber(errTitle) {
return plumber({
errorHandler: notify.onError({
// Customizing error title
title: errTitle || "Error running Gulp",
message: "Error: <%= error.message %>",
sound: "Glass"
})
});
}
function compileSASS(cb) {
return src('./scss/**/*.scss').pipe(customPlumber('Error running SASS')).pipe(sass()).pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
})).pipe(cssmin()).pipe(dest('./')).pipe(browserSync.stream());
}
exports.default = function(cb) {
browserSync.init();
watch('./scss/*.scss', compileSASS);
watch('./**/*.php', function(cb) {
browserSync.reload();
cb();
});
cb();
};
exports.compressImages = function(cb) {
return src(['img/raw/*.jpg','img/raw/*.png','img/raw/*.jpeg'])
.pipe(newer('img/'))
.pipe(imagemin([
imageminJpegRecompress({
quality: 'low'
}),
imageminPngquant()
])).pipe(dest('img/'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment