Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve sourceMappingURL regex #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

swissspidy
Copy link

@swissspidy swissspidy commented Dec 4, 2023

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);

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant