63 lines
1.4 KiB
JavaScript
63 lines
1.4 KiB
JavaScript
var map = require('map-stream')
|
|
, sass = require('node-sass')
|
|
, path = require('path')
|
|
, gutil = require('gulp-util')
|
|
, ext = gutil.replaceExtension
|
|
;
|
|
|
|
module.exports = function (options) {
|
|
var opts = options ? options : {};
|
|
|
|
function nodeSass (file, cb) {
|
|
var fileDir = path.dirname(file.path);
|
|
var addedLocalDirPath = false;
|
|
|
|
if (file.isNull()) {
|
|
return cb(null, file);
|
|
}
|
|
if (path.basename(file.path).indexOf('_') === 0) {
|
|
return cb();
|
|
}
|
|
|
|
opts.data = file.contents.toString();
|
|
|
|
if (opts.includePaths && Array.isArray(opts.includePaths)) {
|
|
if (opts.includePaths.indexOf(fileDir) === -1) {
|
|
opts.includePaths.push(fileDir);
|
|
addedLocalDirPath = true;
|
|
}
|
|
} else {
|
|
opts.includePaths = [fileDir];
|
|
}
|
|
|
|
opts.success = function (css) {
|
|
if (typeof opts.onSuccess === 'function') opts.onSuccess(css);
|
|
|
|
file.path = ext(file.path, '.css');
|
|
file.contents = new Buffer(css);
|
|
cb(null, file);
|
|
};
|
|
|
|
opts.error = function (err) {
|
|
if (opts.errLogToConsole) {
|
|
gutil.log('[gulp-sass] ' + err);
|
|
return cb();
|
|
}
|
|
|
|
if (typeof opts.onError === 'function') {
|
|
opts.onError(err);
|
|
return cb();
|
|
}
|
|
|
|
return cb(new gutil.PluginError('gulp-sass', err));
|
|
};
|
|
|
|
sass.render(opts);
|
|
|
|
if (addedLocalDirPath) opts.includePaths.pop();
|
|
|
|
}
|
|
|
|
return map(nodeSass);
|
|
};
|