Skip to content

[WIP] Fix #39: Generate correct source paths #40

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
29 changes: 24 additions & 5 deletions lib/source-map-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
var SourceMap = require('source-map').SourceMapGenerator;
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var sourceMapResolve = require('source-map-resolve');
var urix = require('urix');
var fs = require('fs');
var path = require('path');
var url = require('url');

/**
* Expose `mixin()`.
Expand All @@ -22,14 +25,29 @@ module.exports = mixin;
*/

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

/**
* Convert `filepath` to a URL, relative to `this.sourcemapPath`.
*
* @param {String} filepath
* @return {String}
* @api private
*/

exports.relativeUrl = function(filepath) {
return url.resolve(urix(this.sourcemapPath), urix(filepath))
};

/**
* Update position.
*
Expand All @@ -55,7 +73,7 @@ exports.updatePosition = function(str) {
*/

exports.emit = function(str, pos, startOnly) {
var sourceFile = pos && pos.source || 'source.css';
var sourceFile = this.relativeUrl(pos && pos.source || 'source.css');

if (pos && pos.start) {
this.map.addMapping({
Expand Down Expand Up @@ -115,8 +133,9 @@ exports.applySourceMaps = function() {
var originalMap = sourceMapResolve.resolveSync(
this.files[file], file, fs.readFileSync);
if (originalMap) {
originalMap = new SourceMapConsumer(originalMap.map);
this.map.applySourceMap(originalMap, file);
var map = new SourceMapConsumer(originalMap.map);
var relativeTo = this.relativeUrl(path.dirname(originalMap.sourcesRelativeTo));
this.map.applySourceMap(map, file, relativeTo);
}
}, this);
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"source-map": "~0.1.31",
"source-map-resolve": "^0.1.3"
"source-map-resolve": "^0.1.3",
"urix": "~0.1.0"
}
}
4 changes: 2 additions & 2 deletions test/css-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ describe('stringify(obj, {sourcemap: true})', function(){
column: 0,
line: 1,
name: null,
source: 'source-map-apply.scss'
source: 'test/source-map-apply.scss'
});

map.originalPositionFor({ line: 2, column: 2 }).should.eql({
column: 7,
line: 1,
name: null,
source: 'source-map-apply.scss'
source: 'test/source-map-apply.scss'
});
});
});