Skip to content

Commit

Permalink
Fix path issues with source maps
Browse files Browse the repository at this point in the history
On windows, node's path api returns backslashes for path separators.
Because the source-map library expects forward slashes in all cases,
their relative path logic (specifically their "normalize" function)
gives incorrect results when passing in backslash.  The
mozilla/source-map library won't change this, because they are actually
expecting URLs as input - see
mozilla/source-map#91 (comment).

This fix is similar to the one made for grunt-contrib-uglify:
gruntjs/grunt-contrib-uglify#175.

Standardized line endings for test fixtures by enforcing LF end of files for fixtures directory
(through repository .gitattributes).  This change was needed
because the expected output of source maps are based on input files with LF, rather
than CRLF. Before this change the tests were breaking due to git autocrlf on Windows.
The difference between the source maps generated from input
files using CRLF and the source maps generated from input files using
only LF can't be normalized in the test, due to the nature of the
changes:  the offsets in the source map are changed, as well as the
embedded source.  These kinds of changes aren't as simple to normalize
as just line feeds in output files - and such normalization would be too
invasive, to the point of making the test less effective.

This should fix issue gruntjs#110 and gruntjs#95 and all unit tests should now pass.
  • Loading branch information
riaann committed Feb 18, 2015
1 parent 53d374c commit ac88c56
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
/test/fixtures/* text eol=lf
10 changes: 8 additions & 2 deletions tasks/lib/sourcemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ exports.init = function(grunt) {
// prior sourcemap and return src with sourceMappingURL removed.
SourceMapConcatHelper.prototype.addlines = function(src, filename) {
var relativeFilename = path.relative(path.dirname(this.dest), filename);

// sourceMap path references are URLs, so ensure forward slashes are used for paths passed to sourcemap library
relativeFilename = relativeFilename.replace(/\\/g, '/');
var node;
if (
/\/\/[@#]\s+sourceMappingURL=(.+)/.test(src) ||
Expand All @@ -121,6 +122,8 @@ exports.init = function(grunt) {
// Consider the relative path from source files to new sourcemap.
var sourcePathToSourceMapPath =
path.relative(path.dirname(this.dest), path.dirname(sourceMapPath));
// sourceMap path references are URLs, so ensure forward slashes are used for paths passed to sourcemap library
sourcePathToSourceMapPath = sourcePathToSourceMapPath.replace(/\\/g, '/');
// Store the sourceMap so that it may later be consumed.
this.maps.push([
sourceMapConsumer, relativeFilename, sourcePathToSourceMapPath
Expand Down Expand Up @@ -170,8 +173,11 @@ exports.init = function(grunt) {

// Return a string for inline use or write the source map to disk.
SourceMapConcatHelper.prototype._write = function() {
// ensure we're using forward slashes, because these are URLs
var file = path.relative(path.dirname(this.dest), this.files.dest);
file = file.replace(/\\/g, '/');
var code_map = this.node.toStringWithSourceMap({
file: path.relative(path.dirname(this.dest), this.files.dest)
file: file
});
// Consume the new sourcemap.
var generator = SourceMapGenerator.fromSourceMap(
Expand Down

0 comments on commit ac88c56

Please sign in to comment.