Skip to content

Instantly share code, notes, and snippets.

@leymannx
Last active July 13, 2023 15:29
Show Gist options
  • Save leymannx/f7867942184d01aa2311 to your computer and use it in GitHub Desktop.
Save leymannx/f7867942184d01aa2311 to your computer and use it in GitHub Desktop.
Gulp Sass with autoprefixer and minify.
var gulp = require('gulp'),
sass = require('gulp-sass'),
rename = require('gulp-rename'),
cssmin = require('gulp-cssnano'),
prefix = require('gulp-autoprefixer'),
plumber = require('gulp-plumber'),
notify = require('gulp-notify'),
sassLint = require('gulp-sass-lint'),
sourcemaps = require('gulp-sourcemaps');
// Temporary solution until gulp 4
// https://github.com/gulpjs/gulp/issues/355
runSequence = require('run-sequence');
var displayError = function(error) {
// Initial building up of the error
var errorString = '[' + error.plugin.error.bold + ']';
errorString += ' ' + error.message.replace("\n",''); // Removes new line at the end
// If the error contains the filename or line number add it to the string
if(error.fileName)
errorString += ' in ' + error.fileName;
if(error.lineNumber)
errorString += ' on line ' + error.lineNumber.bold;
// This will output an error like the following:
// [gulp-sass] error message in file_name on line 1
console.error(errorString);
};
var onError = function(err) {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Basso"
})(err);
this.emit('end');
};
var sassOptions = {
outputStyle: 'expanded'
};
var prefixerOptions = {
browsers: ['last 2 versions']
};
// BUILD SUBTASKS
// ---------------
gulp.task('styles', function() {
return gulp.src('scss/main.scss')
.pipe(plumber({errorHandler: onError}))
.pipe(sourcemaps.init())
.pipe(sass(sassOptions))
.pipe(prefix(prefixerOptions))
.pipe(rename('main.css'))
.pipe(gulp.dest('css'))
.pipe(cssmin())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('css'))
});
gulp.task('sass-lint', function() {
gulp.src('scss/**/*.scss')
.pipe(sassLint())
.pipe(sassLint.format())
.pipe(sassLint.failOnError());
});
gulp.task('watch', function() {
gulp.watch('scss/**/*.scss', ['styles']);
});
// BUILD TASKS
// ------------
gulp.task('default', function(done) {
runSequence('styles', 'watch', done);
});
gulp.task('build', function(done) {
runSequence('styles', done);
});
@michaellee
Copy link

Thanks for sharing this Norman, this was helpful :)

Copy link

ghost commented Aug 22, 2018

Thank you! Very helpful! =)

@isaacdanielanderson
Copy link

Nice!

@spartanatreyu
Copy link

Great gulpfile, but I noticed two things.

  1. It doesn't look like the displayError function is being used anywhere in this example.
  2. Gulp 4 is out in alpha which might make this look a little cleaner. I've had no trouble with it, that said it is still in alpha so you can go either way.

@Juandresyn
Copy link

This helped me a lot.

@zatamine
Copy link

zatamine commented Jun 22, 2019

You should add sourcemaps.write() because it's not generated

@honkinglin
Copy link

Thank you! Very helpful~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment