From 138e8f8d56f6bba61e03c39194b5276c6b42ba96 Mon Sep 17 00:00:00 2001 From: Matt Lewis Date: Tue, 26 Dec 2017 17:17:11 +0000 Subject: [PATCH] fix: don't prepend the webpack context to absolute source roots Fixes #33 --- src/util.js | 8 +++++++- test/util.spec.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index ef5761e..dae0660 100644 --- a/src/util.js +++ b/src/util.js @@ -26,7 +26,13 @@ function fixWebpackSourcePaths(sourceMap, webpackConfig) { let sourceRoot = sourceMap.sourceRoot; // Fix for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/32 // The sourceRoot is relative to the project directory and not an absolute path, so add the webpack context to it if set - if (webpackConfig && webpackConfig.context && sourceMap.sourceRoot && !sourceMap.sourceRoot.startsWith(webpackConfig.context)) { + if ( + webpackConfig && + webpackConfig.context && + sourceMap.sourceRoot && + !sourceMap.sourceRoot.startsWith(webpackConfig.context) && + !path.isAbsolute(sourceMap.sourceRoot) + ) { sourceRoot = path.join(webpackConfig.context, sourceRoot); } diff --git a/test/util.spec.js b/test/util.spec.js index 336b312..1baa804 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -142,5 +142,22 @@ describe('util', () => { context: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript' })).to.deep.equal(output); }); + + it('should not the webpack context to the source root if the source root is absolute', () => { + const input = { + file: 'example.ts', + sourceRoot: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript', + sources: ['example.ts'] + }; + + const output = { + file: 'example.ts', + sourceRoot: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript', + sources: ['example.ts'] + }; + expect(fixWebpackSourcePaths(input, { + context: '/Users/mattlewis/Code/open-source/karma-coverage-istanbul-reporter/test/fixtures/typescript/src' + })).to.deep.equal(output); + }); }); });