Sourcemap Support ported from 1.x

This commit is contained in:
Sam Richard 2015-03-24 04:40:48 -05:00
parent c4d97cc7fd
commit abb28ba65e

View file

@ -3,6 +3,7 @@ var through = require("through2");
var assign = require("object-assign");
var path = require('path');
var sass = require("node-sass");
var applySourceMap = require('vinyl-sourcemaps-apply');
var PLUGIN_NAME = 'gulp-sass';
@ -21,6 +22,11 @@ module.exports = function(options) {
var opts = assign({}, options);
opts.file = file.path;
// Generate Source Maps if plugin source-map present
if (file.sourceMap) {
opts.sourceMap = file.path;
}
var callback = function(error, obj) {
if (error) {
return cb(new gutil.PluginError(
@ -28,8 +34,30 @@ module.exports = function(options) {
));
}
// Build Source Maps!
if (obj.map) {
// hack to remove the already added sourceMappingURL from libsass
obj.css = obj.css.toString().replace(/\/\*#\s*sourceMappingURL\=.*\*\//, '');
// 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 = new Buffer(obj.css);
file.path = gutil.replaceExtension(file.path, '.css');
cb(null, file);
};