From ac88c568648bb57995c454de3a959da17e18e162 Mon Sep 17 00:00:00 2001 From: RiaanN Date: Tue, 17 Feb 2015 07:35:13 +0200 Subject: [PATCH] Fix path issues with source maps 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 https://github.com/mozilla/source-map/issues/91#issuecomment-34845169. This fix is similar to the one made for grunt-contrib-uglify: https://github.com/gruntjs/grunt-contrib-uglify/pull/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 #110 and #95 and all unit tests should now pass. --- .gitattributes | 1 + tasks/lib/sourcemap.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitattributes b/.gitattributes index 176a458..c60256b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ * text=auto +/test/fixtures/* text eol=lf diff --git a/tasks/lib/sourcemap.js b/tasks/lib/sourcemap.js index 4671df1..5368ab3 100644 --- a/tasks/lib/sourcemap.js +++ b/tasks/lib/sourcemap.js @@ -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) || @@ -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 @@ -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(