Created
October 12, 2015 17:10
-
-
Save jbmoelker/b33137e5a580b649a48f to your computer and use it in GitHub Desktop.
Watch and compile Sass with notifications on error; Serve generated files independently using BrowserSync;
This file contains 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 autoprefixer = require('gulp-autoprefixer'); | |
var browserSync = require('browser-sync').create(); | |
var gulp = require('gulp'); | |
var gulpIf = require('gulp-if'); | |
var notify = require('gulp-notify'); | |
var sass = require('gulp-sass'); | |
var plumber = require('gulp-plumber'); | |
var sourcemaps = require('gulp-sourcemaps'); | |
gulp.task('build:css', buildCss); | |
gulp.task('serve', serveTask); | |
gulp.task('watch', watchTask); | |
var isWatching = false; | |
function buildCss() { | |
return gulp.src('src/index.scss') | |
.pipe(sourcemaps.init()) | |
.pipe(gulpIf(isWatching, plumber({ | |
errorHandler: notify.onError(function formatErrorAsNotification(err) { | |
return err.message | |
.replace(/.*(\/.*\.scss)/, '.. $1') | |
.replace('\n', ': '); | |
}) | |
}))) | |
.pipe(sass({outputStyle: 'compressed'})) | |
.pipe(autoprefixer({browsers: ['> 1%', 'last 2 versions']})) | |
.pipe(sourcemaps.write('./')) | |
.pipe(gulp.dest('dist/')); | |
} | |
function serveTask() { | |
return browserSync.init({ | |
files: ['dist/**/*'], | |
online: false, | |
server: { | |
baseDir: 'dist' | |
}, | |
ui: false | |
}); | |
} | |
function watchTask() { | |
isWatching = true; | |
gulp.watch('src/**/*.scss', ['build:css']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment