Skip to content

Commit 87d6994

Browse files
committed
Fix embedding source file contents in source maps and add tests
Somehow these had been added in #33 but lost in a later merge.
1 parent 3cfdda9 commit 87d6994

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

lib/source-map-support.js

+12-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ exports.emit = function(str, pos, startOnly) {
7070
}
7171
});
7272

73-
this.addFile(pos);
73+
this.addFile(sourceFile, pos);
7474
}
7575

7676
this.updatePosition(str);
@@ -88,32 +88,37 @@ exports.emit = function(str, pos, startOnly) {
8888
}
8989
});
9090

91-
this.addFile(pos);
91+
this.addFile(sourceFile, pos);
9292
}
9393

9494
return str;
9595
};
9696

9797
/**
9898
* Adds a file to the source map output if it has not already been added
99+
* @param {String} file
99100
* @param {Object} pos
100101
*/
101102

102-
exports.addFile = function(pos) {
103-
var file = pos.source || 'source.css';
104-
if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
103+
exports.addFile = function(file, pos) {
105104
if (typeof pos.content !== 'string') return;
105+
if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
106+
106107
this.files[file] = pos.content;
107108
};
108109

109110
/**
110-
* Applies any original source maps to the output.
111+
* Applies any original source maps to the output and embeds the source file
112+
* contents in the source map.
111113
*/
112114

113115
exports.applySourceMaps = function() {
114116
Object.keys(this.files).forEach(function(file) {
117+
var content = this.files[file];
118+
this.map.setSourceContent(file, content);
119+
115120
var originalMap = sourceMapResolve.resolveSync(
116-
this.files[file], file, fs.readFileSync);
121+
content, file, fs.readFileSync);
117122
if (originalMap) {
118123
originalMap = new SourceMapConsumer(originalMap.map);
119124
this.map.applySourceMap(originalMap, file);

test/css-stringify.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ describe('stringify(obj)', function(){
2828
});
2929

3030
describe('stringify(obj, {sourcemap: true})', function(){
31-
var src = read('test/source-map-case.css', 'utf8');
32-
var stylesheet = parse(src, { source: 'rules.css', position: true });
31+
var file = 'test/source-map-case.css';
32+
var src = read(file, 'utf8');
33+
var stylesheet = parse(src, { source: file, position: true });
3334
function loc(line, column) {
34-
return { line: line, column: column, source: 'rules.css', name: null }
35+
return { line: line, column: column, source: file, name: null }
3536
};
3637

3738
var locs = {
@@ -54,6 +55,7 @@ describe('stringify(obj, {sourcemap: true})', function(){
5455
map.originalPositionFor({ line: 11, column: 0 }).should.eql(locs.mediaBlock);
5556
map.originalPositionFor({ line: 12, column: 2 }).should.eql(locs.mediaOnly);
5657
map.originalPositionFor({ line: 17, column: 0 }).should.eql(locs.comment);
58+
map.sourceContentFor(file).should.eql(src);
5759
});
5860

5961
it('should generate source maps alongside when using compress compiler', function(){
@@ -66,6 +68,7 @@ describe('stringify(obj, {sourcemap: true})', function(){
6668
map.originalPositionFor({ line: 1, column: 10 }).should.eql(locs.tobiNameValue);
6769
map.originalPositionFor({ line: 1, column: 50 }).should.eql(locs.mediaBlock);
6870
map.originalPositionFor({ line: 1, column: 64 }).should.eql(locs.mediaOnly);
71+
map.sourceContentFor(file).should.eql(src);
6972
});
7073

7174
it('should apply included source maps', function(){

0 commit comments

Comments
 (0)