Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active August 29, 2015 14:19
Show Gist options
  • Save radist2s/43da49b76d9e68c63892 to your computer and use it in GitHub Desktop.
Save radist2s/43da49b76d9e68c63892 to your computer and use it in GitHub Desktop.
Gulpfile for using Less compiler with sourcemap support and autoprefixer postcss
var lessSourceFiles = '../../static/less/*.less'
var cssOutPath = '../../static/css'
var gulp = require('gulp')
var watch = require('gulp-watch')
var less = require('gulp-less-sourcemap')
var path = require('path')
var gutil = require('gulp-util')
var notifier = require('node-notifier')
var postcss = require('gulp-postcss')
var sourcemaps = require('gulp-sourcemaps')
var autoprefixer = require('autoprefixer-core')
var runSequence = require('run-sequence')
var fs = require('fs')
lessSourceFiles = path.resolve(lessSourceFiles)
gulp.task('less', function () {
var cssDestination = cssOutPath
return gulp
.src(lessSourceFiles)
.pipe(
less({
ieCompat: true,
sourceMap: {
sourceMapRootpath: '../less'
}
})
)
.on('error', function (error) {
gutil.log(gutil.colors.red(error.message))
notifier.notify({title: 'Less compilation error', message: error.message})
this.emit('end');
})
.pipe(gulp.dest(path.join(cssDestination)))
});
gulp.task('autoprefixer', function () {
return gulp.src(path.join(cssOutPath, '*.css'))
.pipe(sourcemaps.init())
.pipe(
postcss([
autoprefixer({browsers: ['last 2 version', '> 1%', 'ie >= 8', 'Firefox > 15', 'iOS >= 5', 'Android >= 2.3']})
])
.on('error', function (error) {
notifier.notify({title: 'Autoprefixer compilation error', message: error.message.replace(/"+/, "'")})
gutil.log(gutil.colors.red(error.message))
this.emit('end');
})
)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(cssOutPath))
})
gulp.task('begin-watching', function () {
watch(path.join(path.dirname(lessSourceFiles), '**', '*.less'), function (file) {
runSequence(
'less',
'autoprefixer'
)
})
})
gulp.task('watch', function (callback) {
runSequence(
'less',
'autoprefixer',
'begin-watching',
callback
)
})
gulp.task('default', ['watch']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment