gulp-sass/index.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

var gutil = require("gulp-util");
var through = require("through2");
var assign = require("object-assign");
var path = require('path');
var sass = require("node-sass");
2015-03-24 10:40:48 +01:00
var applySourceMap = require('vinyl-sourcemaps-apply');
2013-09-01 01:53:11 +02:00
var PLUGIN_NAME = 'gulp-sass';
2014-01-05 16:15:20 +01:00
module.exports = function(options) {
return through.obj(function(file, enc, cb) {
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
}
if (file.isStream()) {
return cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
}
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
var opts = assign({}, options);
opts.file = file.path;
2013-09-01 06:54:25 +02:00
2015-03-24 10:40:48 +01:00
// Generate Source Maps if plugin source-map present
if (file.sourceMap) {
opts.sourceMap = file.path;
opts.omitSourceMapUrl = true;
2015-03-24 10:40:48 +01:00
}
var callback = function(error, obj) {
if (error) {
return cb(new gutil.PluginError(
PLUGIN_NAME, error.message + ' on line ' + error.line + ' in ' + error.file
));
2014-02-11 06:01:45 +01:00
}
2015-03-24 10:40:48 +01:00
// Build Source Maps!
if (obj.map) {
// libsass gives us sources' paths relative to file;
// gulp-sourcemaps needs sources' paths relative to file.base;
// so alter the sources' paths to please gulp-sourcemaps.
obj.map = JSON.parse(obj.map.toString());
if (obj.map.sources) {
obj.map.sources = obj.map.sources.map(function(source) {
var abs = path.resolve(path.dirname(file.path), source);
return path.relative(file.base, abs);
});
obj.map = JSON.stringify(obj.map);
applySourceMap(file, obj.map);
}
}
file.contents = obj.css;
file.path = gutil.replaceExtension(file.path, '.css');
2015-03-24 10:40:48 +01:00
cb(null, file);
2014-01-19 09:39:41 +01:00
};
2013-09-01 06:54:25 +02:00
sass.render(opts, callback);
});
2014-01-19 09:39:41 +01:00
};