From 1a4cc5624f4d63d3d76333e193182b9924a3667b Mon Sep 17 00:00:00 2001 From: Chris Sewart Date: Sat, 18 Jun 2016 18:44:21 +0100 Subject: [PATCH] Relative path manipulation handles Windows paths and different extensions --- index.js | 7 ++++- test/combine-source-map.js | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 78d9f69..881af8e 100644 --- a/index.js +++ b/index.js @@ -34,14 +34,19 @@ var rebaseRelativePath = memoize(function(sourceFile, relativeRoot, relativePath // join relative path to root (e.g. 'src/' + 'file.js') var relativeRootedPath = relativeRoot ? path.join(relativeRoot, relativePath) : relativePath; + // Ultimately these will be used in URLs so normalise to URL path separators before comparison + relativeRootedPath = relativeRootedPath.replace(/\\/g, '/'); + sourceFile = sourceFile.replace(/\\/g, '/'); + if (sourceFile === relativeRootedPath || // same path, + path.dirname(sourceFile) === path.dirname(relativeRootedPath) || // same dir, different file (e.g. foo.ts > foo.js) pathIsAbsolute(relativeRootedPath) || // absolute path, nor protocolRx.test(relativeRootedPath)) { // absolute protocol need rebasing return relativeRootedPath; } // make relative to source file - return path.join(path.dirname(sourceFile), relativeRootedPath); + return path.join(path.dirname(sourceFile), relativeRootedPath).replace(/\\/g, '/'); }, function(a, b, c) { return a + '::' + b + '::' + c; }); diff --git a/test/combine-source-map.js b/test/combine-source-map.js index 3a51e5f..4b80eca 100644 --- a/test/combine-source-map.js +++ b/test/combine-source-map.js @@ -345,3 +345,64 @@ test('remove comments', function (t) { }) t.end() }) + +test('relative path mainipulation copes with changing file extension', function (t) { + + var foo = { + version : 3, + file : 'bar/foo.js', + sourceRoot : '', + sources : [ 'bar/foo.coffee' ], + names : [], + mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ', + sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] }; + + var mapComment = convert.fromObject(foo).toComment(); + + var file = { + id: 'xyz' + , source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment + , sourceFile: 'bar/foo.js' + }; + + var lineOffset = 3 + var base64 = combine.create() + .addFile(file, { line: lineOffset }) + .base64() + + var sm = convert.fromBase64(base64).toObject(); + + t.deepEqual(sm.sources, ['bar/foo.coffee'], 'includes original relative file path') + t.end() +}) + +test('relative path mainipulation generates paths with URL path separators', function (t) { + + var foo = { + version : 3, + file : 'bar/foo.js', + sourceRoot : '', + sources : [ 'bar\\foo.coffee' ], + names : [], + mappings : ';AAAA;CAAA;CAAA,CAAA,CAAA,IAAO,GAAK;CAAZ', + sourcesContent : [ 'console.log(require \'./bar.js\')\n' ] }; + + var mapComment = convert.fromObject(foo).toComment(); + + var file = { + id: 'xyz' + , source: '(function() {\n\n console.log(require(\'./bar.js\'));\n\n}).call(this);\n' + '\n' + mapComment + , sourceFile: 'bar/foo.js' + }; + + var lineOffset = 3 + var base64 = combine.create() + .addFile(file, { line: lineOffset }) + .base64() + + var sm = convert.fromBase64(base64).toObject(); + var res = checkMappings(foo, sm, lineOffset); + + t.deepEqual(sm.sources, ['bar/foo.coffee'], 'includes relative file ulr path') + t.end() +})