Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

feat: add fixWebpackSourcePath option (options.fixWebpackSourcePath) #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ The loader supports all options supported by `istanbul-lib-instrument`
|**`coverageVariable`**|`{String}`|`__coverage__`|Name of global coverage variable|
|**`preserveComments`**|`{Boolean}`|`false`|Preserve comments in `output`|
|**`produceSourceMap`**|`{Boolean}`|`false`|Set to `true` to produce a source map for the instrumented code|
|**`fixWebpackSourcePaths`**|`{Boolean}`|`false`|Set to `true` to prevent filepath error in coverage|
|**`sourceMapUrlCallback`**|`{Function}`|`null`|A callback function that is called when a source map URL is found in the original code. This function is called with the source filename and the source map URL|

**webpack.config.js**
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ export default function (source, sourceMap) {
const instrumenter = createInstrumenter(options);

instrumenter.instrument(source, this.resourcePath, (error, instrumentedSource) => {
this.callback(error, instrumentedSource, instrumenter.lastSourceMap());
let src = instrumentedSource;
if (options.fixWebpackSourcePaths) {
src = src.replace(/sources:\[([^\]])*\]/g, (match) => {
const lines = match.slice('sources:['.length, -1).split(',');
return `sources:[${
lines
.map(l => l.slice(1, -1).split('!'))
.map(arr => `"${arr[arr.length - 1]}"`)
.join(',')
}]`;
});
}
this.callback(error, src, instrumenter.lastSourceMap());
}, srcMap);
}
3 changes: 3 additions & 0 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
},
"produceSourceMap": {
"type": "boolean"
},
"fixWebpackSourcePaths": {
"type": "boolean"
}
},
"additionalProperties": true
Expand Down