Skip to content

Commit 3c48bf8

Browse files
committed
fix: correctly strip source file prefixes when no webpack.context is defined
This now uses the same default logic as webpack for identifying the source root. Based on https://webpack.js.org/configuration/entry-context/#context, if the context is not specified, the current directory that the process is running from should be assumed instead. Align util to mimic this behaviour to ensure paths are correctly de-absoluted during processing.
1 parent 50f441f commit 3c48bf8

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/util.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ function fixPathSeparators(filePath) {
2424

2525
function fixWebpackSourcePaths(sourceMap, webpackConfig) {
2626
let { sourceRoot } = sourceMap;
27+
// As per https://webpack.js.org/configuration/entry-context/#context, if no context is specified, the current
28+
// directory that the process is running from should be assumed instead
29+
const context = (webpackConfig && webpackConfig.context) || process.cwd();
2730
// Fix for https://github.com/mattlewis92/karma-coverage-istanbul-reporter/issues/32
2831
// The sourceRoot is relative to the project directory and not an absolute path, so add the webpack context to it if set
2932
if (
30-
webpackConfig &&
31-
webpackConfig.context &&
32-
sourceMap.sourceRoot &&
33-
!sourceMap.sourceRoot.startsWith(webpackConfig.context) &&
33+
context &&
34+
sourceRoot &&
35+
!sourceRoot.startsWith(context) &&
3436
!path.isAbsolute(sourceRoot)
3537
) {
36-
sourceRoot = path.join(webpackConfig.context, sourceRoot);
38+
sourceRoot = path.join(context, sourceRoot);
3739
}
3840

3941
sourceRoot = fixPathSeparators(sourceRoot);

test/util.spec.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('util', () => {
5959
Object.defineProperty(process, 'platform', {
6060
value: 'win32'
6161
});
62+
process.cwd = () => 'C:/development/git/coverage-istanbul-reporter-path';
6263
const input = {
6364
file:
6465
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
@@ -118,6 +119,7 @@ describe('util', () => {
118119
});
119120

120121
it('should remove the sourceRoot from the source path if present', () => {
122+
process.cwd = () => 'C:/development/git/coverage-istanbul-reporter-path';
121123
const input = {
122124
file:
123125
'C:/development/git/coverage-istanbul-reporter-path/client/modules/app/app.component.ts',
@@ -175,6 +177,29 @@ describe('util', () => {
175177
).to.deep.equal(output);
176178
});
177179

180+
it('should use the current directory if webpack context is not set', () => {
181+
Object.defineProperty(process, 'platform', {
182+
value: 'win32'
183+
});
184+
process.cwd = () =>
185+
'C:\\Users\\mattlewis\\Code\\open-source\\karma-coverage-istanbul-reporter\\test\\fixtures\\typescript';
186+
const input = {
187+
file: 'example.ts',
188+
sourceRoot: 'src/',
189+
sources: [
190+
'C:\\Users\\mattlewis\\Code\\open-source\\karma-coverage-istanbul-reporter\\test\\fixtures\\typescript\\src\\example.ts'
191+
]
192+
};
193+
194+
const output = {
195+
file: 'example.ts',
196+
sourceRoot:
197+
'C:\\Users\\mattlewis\\Code\\open-source\\karma-coverage-istanbul-reporter\\test\\fixtures\\typescript\\src\\',
198+
sources: ['example.ts']
199+
};
200+
expect(fixWebpackSourcePaths(input, undefined)).to.deep.equal(output);
201+
});
202+
178203
it('should only add the webpack context to the source root if not already set', () => {
179204
const input = {
180205
file: 'example.ts',
@@ -197,7 +222,7 @@ describe('util', () => {
197222
).to.deep.equal(output);
198223
});
199224

200-
it('should not the webpack context to the source root if the source root is absolute', () => {
225+
it('should not add the webpack context to the source root if the source root is absolute', () => {
201226
const input = {
202227
file: 'example.ts',
203228
sourceRoot:

0 commit comments

Comments
 (0)