Created
May 7, 2014 19:19
-
-
Save radist2s/fa563393ddbc6749904d to your computer and use it in GitHub Desktop.
Gulpfile for LESS compilation with external sourcemap file support
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 gulp = require('gulp'); | |
var less = require('gulp-less'); | |
var path = require('path');// | |
var fs = require('fs'); | |
var ensureDirectory = function (filepath) { | |
var dir = path.dirname(filepath), | |
cmd, | |
existsSync = fs.existsSync || path.existsSync; | |
if (!existsSync(dir)) { | |
if (mkdirp === undefined) { | |
try {mkdirp = require('mkdirp');} | |
catch(e) { mkdirp = null; } | |
} | |
cmd = mkdirp && mkdirp.sync || fs.mkdirSync; | |
cmd(dir); | |
} | |
}; | |
var lessing = function () { | |
gulp | |
.src('./style.less') | |
.pipe( | |
less(function (file) { | |
var filePath = file.path, | |
surceMapFileName = path.basename(file.path, '.less') + '.map', | |
sourceMapFilePath = path.join(path.dirname(filePath), surceMapFileName) | |
return { | |
writeSourceMap: function (output) { | |
ensureDirectory(sourceMapFilePath); | |
fs.writeFileSync(sourceMapFilePath, output, 'utf8'); | |
}, | |
sourceMapURL: surceMapFileName, | |
sourceMap: sourceMapFilePath, | |
sourceMapRootpath: '', | |
sourceMapBasepath: path.dirname(filePath), | |
paths: [ path.join(__dirname) ] | |
} | |
}) | |
) | |
.pipe(gulp.dest('./')); | |
} | |
gulp.task('default', function() { | |
lessing() | |
}); | |
/*gulp.watch('./style.less', function () { | |
lessing() | |
})*/ |
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 less = require('less'); | |
var through2 = require('through2'); | |
var gutil = require('gulp-util'); | |
var PluginError = gutil.PluginError; | |
var path = require('path'); | |
var defaults = require('lodash.defaults'); | |
module.exports = function (options) { | |
function transform (file, enc, next) { | |
var self = this; | |
if (file.isNull()) { | |
this.push(file); // pass along | |
return next(); | |
} | |
if (file.isStream()) { | |
this.emit('error', new PluginError('gulp-less', 'Streaming not supported')); | |
return next(); | |
} | |
var str = file.contents.toString('utf8'); | |
var opts = {}; | |
if (typeof options === 'function') { | |
opts = options.apply(this, arguments); | |
} | |
// Mixes in default options. | |
opts = defaults(opts || {}, { | |
compress: false, | |
paths: [] | |
}); | |
// Injects the path of the current file. | |
opts.filename = file.path; | |
less.render(str, opts, function (err, css) { | |
if (err) { | |
// convert the keys so PluginError can read them | |
err.lineNumber = err.line; | |
err.fileName = err.filename; | |
// add a better error message | |
err.message = err.message + ' in file ' + err.fileName + ' line no. ' + err.lineNumber; | |
self.emit('error', new PluginError('gulp-less', err)); | |
} else { | |
file.contents = new Buffer(css); | |
file.path = gutil.replaceExtension(file.path, '.css'); | |
self.push(file); | |
} | |
next(); | |
}); | |
} | |
return through2.obj(transform); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment