gulp-sass/index.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-01-20 19:56:56 +01:00
var map = require('map-stream')
2013-09-01 01:53:11 +02:00
, sass = require('node-sass')
, path = require('path')
, gutil = require('gulp-util')
, ext = gutil.replaceExtension
2013-09-01 01:53:11 +02:00
;
module.exports = function (options) {
2014-01-20 19:56:56 +01:00
var opts = options ? options : {};
2013-09-01 01:53:11 +02:00
function nodeSass (file, cb) {
var fileDir = path.dirname(file.path);
var addedLocalDirPath = false;
2014-01-05 16:15:20 +01:00
if (file.isNull()) {
2014-01-19 09:39:41 +01:00
return cb(null, file);
2014-01-05 16:15:20 +01:00
}
2014-01-20 22:41:36 +01:00
if (path.basename(file.path).indexOf('_') === 0) {
return cb();
}
2014-01-05 16:15:20 +01:00
opts.data = file.contents.toString();
2013-09-01 06:54:25 +02:00
2014-01-22 21:52:04 +01:00
if (opts.includePaths && Array.isArray(opts.includePaths)) {
if (opts.includePaths.indexOf(fileDir) === -1) {
opts.includePaths.push(fileDir);
addedLocalDirPath = true;
2014-01-22 21:52:04 +01:00
}
} else {
opts.includePaths = [fileDir];
}
2013-09-01 04:00:14 +02:00
opts.success = function (css) {
file.path = ext(file.path, '.css');
file.contents = new Buffer(css);
cb(null, file);
2014-01-19 09:39:41 +01:00
};
2013-09-01 06:54:25 +02:00
opts.error = function (err) {
2014-01-13 07:27:45 +01:00
if (opts.errLogToConsole) {
2014-01-19 09:39:41 +01:00
gutil.log('[gulp-sass] ' + err);
2014-01-13 07:27:45 +01:00
return cb();
}
2014-01-10 22:34:30 +01:00
return cb(new gutil.PluginError('gulp-sass', err));
2014-01-19 09:39:41 +01:00
};
2013-09-01 06:54:25 +02:00
2013-09-01 04:00:14 +02:00
sass.render(opts);
if (addedLocalDirPath) opts.includePaths.pop();
2013-09-01 01:53:11 +02:00
}
2014-01-20 19:56:56 +01:00
return map(nodeSass);
2014-01-19 09:39:41 +01:00
};