playwright-coverage icon indicating copy to clipboard operation
playwright-coverage copied to clipboard

Improve sourceMappingURL regex

Open swissspidy opened this issue 2 years ago • 0 comments

In my case, the build generated by webpack contained two references to sourceMappingURL that matched the existing regex.

The first one was in the middle of the minified script, looked something like ... 10)+1,l=R.substring(V)+(B?"//# sourceMappingURL="+B:""),J=new Blob([l],{type:"application/javascript"});...

The second one was the expected one at the very end of the file.

getSourceMap stopped at the first match, so ended up not finding the correct one.

Matching for a preceding newline before the //# sourceMappingURL comment resolves this, as it finds the correct comment and I get proper coverage reports now.

An alternative approach / further improvement would be to use a global match, eg.

const matches = source.match(/\s\/\/# *sourceMappingURL=(.*)/g);
const match = matches.at(-1);

swissspidy avatar Dec 04 '23 10:12 swissspidy