Added: preliminary source map support

This commit is contained in:
David Manning 2014-02-10 21:01:45 -08:00
parent 6181d953f1
commit 1e7da62c18
2 changed files with 35 additions and 4 deletions

View file

@ -42,6 +42,12 @@ Pass in your own callback to be called upon successful compilaton by node-sass.
Pass in your own callback to be called upon a sass error from node-sass. The callback has the form `callback(err)`, where err is the error string generated by libsass. Note: this *does* prevent an actual `gulpPluginError` object from being created. Pass in your own callback to be called upon a sass error from node-sass. The callback has the form `callback(err)`, where err is the error string generated by libsass. Note: this *does* prevent an actual `gulpPluginError` object from being created.
## Source Maps
gulp-sass now generates *inline* source maps if you pass `sourceComments: map` as an option. Note that gulp-sass will *only* generate inline source maps, so passing `sourceMap: filepath` to node-sass won't actually do anything. Enjoy your source maps!
NB: For those wondering, inline source maps are stuck onto the end of the css file instead of being in a separate map file. In this case, the original source contents are included as well, so you don't have to make sure your scss files are servable.
#Imports and Partials #Imports and Partials
gulp-sass now automatically passes along the directory of every scss file it parses as an include path for node-sass. This means that as long as you specify your includes relative to path of your scss file, everything will just work. gulp-sass now automatically passes along the directory of every scss file it parses as an include path for node-sass. This means that as long as you specify your includes relative to path of your scss file, everything will just work.

View file

@ -1,4 +1,5 @@
var map = require('map-stream') var fs = require('fs')
, map = require('map-stream')
, sass = require('node-sass') , sass = require('node-sass')
, path = require('path') , path = require('path')
, gutil = require('gulp-util') , gutil = require('gulp-util')
@ -19,7 +20,11 @@ module.exports = function (options) {
return cb(); return cb();
} }
opts.data = file.contents.toString(); if (opts.sourceComments === 'map') {
opts.file = file.path;
} else {
opts.data = file.contents.toString();
}
if (opts.includePaths && Array.isArray(opts.includePaths)) { if (opts.includePaths && Array.isArray(opts.includePaths)) {
if (opts.includePaths.indexOf(fileDir) === -1) { if (opts.includePaths.indexOf(fileDir) === -1) {
@ -30,8 +35,18 @@ module.exports = function (options) {
opts.includePaths = [fileDir]; opts.includePaths = [fileDir];
} }
opts.success = function (css) { opts.success = function (css, map) {
if (typeof opts.onSuccess === 'function') opts.onSuccess(css); var sourceMap;
if (typeof opts.onSuccess === 'function') opts.onSuccess(css, map);
if (map) {
map = JSON.parse(map);
map.sourcesContent = getSourcesContent(map.sources);
sourceMap = new Buffer(JSON.stringify(map)).toString('base64');
css = css.replace(/\/\*# sourceMappingURL=.*\*\//,
"/*# sourceMappingURL=data:application/json;base64," +
sourceMap + "*/");
}
file.path = ext(file.path, '.css'); file.path = ext(file.path, '.css');
file.contents = new Buffer(css); file.contents = new Buffer(css);
@ -60,3 +75,13 @@ module.exports = function (options) {
return map(nodeSass); return map(nodeSass);
}; };
function getSourcesContent (sources) {
sourcesContent = [];
for (var i = 0; i < sources.length; i++) {
sourcesContent[i] = fs.readFileSync(sources[i], { encoding: 'utf8' });
}
return sourcesContent;
}