Skip to content

Apply original source maps from source files #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ module.exports = function(node, options){
sourcemaps(compiler);

var code = compiler.compile(node);
compiler.applySourceMaps();
return { code: code, map: compiler.map.toJSON() };
}

var code = compiler.compile(node);
return code;
};

46 changes: 46 additions & 0 deletions lib/source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
*/

var SourceMap = require('source-map').SourceMapGenerator;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var sourceMapResolve = require('source-map-resolve');
var fs = require('fs');

/**
* Expose `mixin()`.
Expand All @@ -20,8 +23,10 @@ module.exports = mixin;

function mixin(compiler) {
var file = compiler.options.filename || 'generated.css';
compiler._comment = compiler.comment;
compiler.map = new SourceMap({ file: file });
compiler.position = { line: 1, column: 1 };
compiler.files = {};
for (var k in exports) compiler[k] = exports[k];
}

Expand Down Expand Up @@ -62,6 +67,7 @@ exports.emit = function(str, pos, startOnly) {
column: pos.start.column - 1
}
});
this.addFile(pos);
}

this.updatePosition(str);
Expand All @@ -78,7 +84,47 @@ exports.emit = function(str, pos, startOnly) {
column: pos.end.column - 1
}
});
this.addFile(pos);
}

return str;
};

/**
* Adds a file to the source map output if it has not already been added
* @param {Object} pos
*/

exports.addFile = function(pos) {
var file = pos.source || 'source.css';
if (Object.prototype.hasOwnProperty.call(this.files, file)) return;
if (typeof pos.content !== 'string') return;
this.files[file] = pos.content;
};

/**
* Applies any original source maps to the output.
*/

exports.applySourceMaps = function() {
Object.keys(this.files).forEach(function(file) {
var originalMap = sourceMapResolve.resolveSync(
this.files[file], file, fs.readFileSync);
if (originalMap) {
originalMap = new SourceMapConsumer(originalMap.map);
this.map.applySourceMap(originalMap, file);
}
}, this);
};

/**
* Process comments, drops sourceMap comments.
* @param {Object} node
*/

exports.comment = function(node) {
if (/^# sourceMappingURL=/.test(node.comment))
return this.emit('', node.position);
else
return this._comment(node);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test": "make test"
},
"dependencies": {
"source-map": "~0.1.31"
"source-map": "~0.1.31",
"source-map-resolve": "^0.1.3"
}
}
Empty file.
24 changes: 24 additions & 0 deletions test/css-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,28 @@ describe('stringify(obj, {sourcemap: true})', function(){
map.originalPositionFor({ line: 1, column: 50 }).should.eql(locs.mediaBlock);
map.originalPositionFor({ line: 1, column: 64 }).should.eql(locs.mediaOnly);
});

it('should apply included source maps', function(){
var file = 'test/source-map-apply.css';
var src = read(file, 'utf8');
var stylesheet = parse(src, { source: file, position: true });
var result = stringify(stylesheet, { sourcemap: true });
result.should.have.property('code');
result.should.have.property('map');

var map = new SourceMapConsumer(result.map);
map.originalPositionFor({ line: 1, column: 0 }).should.eql({
column: 0,
line: 1,
name: null,
source: 'source-map-apply.scss'
});

map.originalPositionFor({ line: 2, column: 2 }).should.eql({
column: 7,
line: 1,
name: null,
source: 'source-map-apply.scss'
});
});
});
4 changes: 4 additions & 0 deletions test/source-map-apply.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/source-map-apply.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/source-map-apply.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tobi { name: 'tobi'; }