Skip to content

Commit faf9757

Browse files
committed
merge
2 parents f78263b + 25b326f commit faf9757

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,33 @@ Pass in your own callback to be called upon a sass error from node-sass. The cal
4444

4545
## Source Maps
4646

47-
gulp-sass now generates *inline* source maps if you pass `sourceComments: 'map'` as an option. Note that gulp-sass won't actually do anything when passing `sourceMap: filepath`. Enjoy your source maps!
47+
gulp-sass can be used in tandem with [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) to generate source maps for the SASS to CSS compilation. You will need to initialize [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) prior to running the gulp-sass compiler and write the source maps after.
4848

49-
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.
49+
```javascript
50+
var sourcemaps = require('gulp-sourcemaps');
51+
52+
gulp.src('./scss/*.scss')
53+
.pipe(sourcemaps.init());
54+
.pipe(sass())
55+
.pipe(sourcemaps.write())
56+
.pipe('./css');
57+
58+
// will write the source maps inline in the compiled CSS files
59+
```
60+
61+
By default, [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps) writes the source maps inline in the compiled CSS files. To write them to a separate file, specify a relative file path in the `sourcemaps.write()` function.
62+
63+
```javascript
64+
var sourcemaps = require('gulp-sourcemaps');
65+
66+
gulp.src('./scss/*.scss')
67+
.pipe(sourcemaps.init());
68+
.pipe(sass())
69+
.pipe(sourcemaps.write('./maps'))
70+
.pipe('./css');
71+
72+
// will write the source maps to ./dest/css/maps
73+
```
5074

5175
#Imports and Partials
5276

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var fs = require('fs')
44
, path = require('path')
55
, gutil = require('gulp-util')
66
, ext = gutil.replaceExtension
7+
, applySourceMap = require('vinyl-sourcemaps-apply')
78
;
89

910
module.exports = function (options) {
@@ -20,6 +21,11 @@ module.exports = function (options) {
2021
return cb();
2122
}
2223

24+
if (file.sourceMap) {
25+
opts.sourceComments = 'map';
26+
opts.sourceMap = false;
27+
}
28+
2329
if (opts.sourceComments === 'map' || opts.sourceComments === 'normal') {
2430
opts.sourceMap = opts.sourceMap || '';
2531
opts.file = file.path;
@@ -41,12 +47,10 @@ module.exports = function (options) {
4147
if (typeof opts.onSuccess === 'function') opts.onSuccess(css, map);
4248

4349
if (map) {
44-
map = JSON.parse(map);
45-
map.sourcesContent = getSourcesContent(map.sources);
46-
sourceMap = new Buffer(JSON.stringify(map)).toString('base64');
47-
css = css.replace(/\/\*# sourceMappingURL=.*\*\//,
48-
"/*# sourceMappingURL=data:application/json;base64," +
49-
sourceMap + "*/");
50+
// hack to remove the already added sourceMappingURL from libsass
51+
css = css.replace(/\n\/\*#\s*sourceMappingURL\=.*\*\//, '');
52+
53+
applySourceMap(file, map);
5054
}
5155

5256
file.path = ext(file.path, '.css');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"dependencies": {
2424
"node-sass": "^0.9",
2525
"gulp-util": "^3.0",
26-
"map-stream": "~0.1"
26+
"map-stream": "~0.1",
27+
"vinyl-sourcemaps-apply": "~0.1.1"
2728
},
2829
"devDependencies": {
2930
"tape": "~2.3",

0 commit comments

Comments
 (0)