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

Fix sourcemap plugin #1505

Merged
merged 1 commit into from
Nov 6, 2020
Merged

Fix sourcemap plugin #1505

merged 1 commit into from
Nov 6, 2020

Conversation

drwpow
Copy link
Collaborator

@drwpow drwpow commented Nov 4, 2020

Changes

Was investigating CDN issues and came across skypackjs/skypack-cdn#22. Guy Bedford’s es-module-shims package won’t load because our builtin Rollup plugin catches the following line:

      sourceMappingResolved = `\n//# sourceMappingURL=` + resolveUrl(sourceMapping.slice(21), load.r);

As a result this package couldn’t be installed.

This changes the RegEx to handle end-of-file (single line) differently than middle-of-file

  • end of file: if this comment occurs at the end of the file and nothing follows it, remove
  • middle of file: this MUST occur on its own line (otherwise it‘s not a comment)

Testing

New snapshot was added for this

Docs

@vercel
Copy link

vercel bot commented Nov 4, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/8yaaahtjl
✅ Preview: https://snowpack-git-drwpow-fix-sourcemap-plugin.pikapkg.vercel.app

@@ -9,7 +9,7 @@ export function rollupPluginStripSourceMapping(): Plugin {
return {
name: 'snowpack:rollup-plugin-strip-source-mapping',
transform: (code) => ({
code: code.replace(/[^'"`]\/\/+#\s*sourceMappingURL=.+$/gm, ''),
code: code.replace(/\/\/#\s*sourceMappingURL=.+$/, ''),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FredKSchott this now breaks the “mid-file“ rule; maybe we should limit to newline-only instead? I think that’d be muuuch safer

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, unfortunately we see this mid-file after bundling multiple files together. That would be a regression (I think lit-html or lit-element was the most popular known example of this).

I know you hate this, but these are such one-off weird cases I'd rather handle this as a one-off solution. How about:

- code.replace(/[^'"`]\/\/+#\s*sourceMappingURL=.+$/gm, '')
+ code.replace(/[^'"`]\/\/+#\s*sourceMappingURL=[^'"`].+$/gm, '')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What breaks it is the \n too so it‘d have to be:

/[^'"`](\\n)?\/\/+#\s*sourceMappingURL=[^'"`]+$/gm

If it works it works, but I‘m gonna add some tests for this plugin specifically to make sure

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sounds good

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, after adding tests, there are more problems with the current RegEx:

Screen Shot 2020-11-06 at 11 03 57 AM

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to strip any character that comes before it. Gonna try and get these tests passing, then you can tell me how you feel about the result

@FredKSchott
Copy link
Owner

FredKSchott commented Nov 6, 2020

Ah yea, in that case you'll need to do:

.replace(/[^'"`]((\\n)?\/\/+#\s*sourceMappingURL=[^'"`]+$)/gm, '$1')
// I think $1 would be correct, but may be $2 due to nested `(\n)`.
// you could turn that nested capture group into a non-capturing group as well

@drwpow
Copy link
Collaborator Author

drwpow commented Nov 6, 2020

Updated the RegEx. LMK what you think! And if you have any other cases we need to preserve, happy to add it to the test cases.

code: code.replace(/[^'"`]\/\/+#\s*sourceMappingURL=.+$/gm, ''),
code: code
// [a-zA-Z0-9-_\*?\.\/\&=+%]: valid URL characters (for sourcemaps)
.replace(/\/\/#\s*sourceMappingURL=[a-zA-Z0-9-_\*\?\.\/\&=+%\s]+$/gm, ''),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah gotcha, so now youre testing that nothing non-URL comes afterwards in the line? Smart!

only random thing that I can think of: can this line end with a semicolon?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don‘t think so? Because it‘s a line comment. And a sourcemap URL probably won‘t have a semicolon in it right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I don't think so either

@drwpow drwpow merged commit 77e6101 into master Nov 6, 2020
@drwpow drwpow deleted the drwpow/fix-sourcemap-plugin branch November 6, 2020 20:57
@drwpow
Copy link
Collaborator Author

drwpow commented Nov 6, 2020

FTR if we need to rewrite this RegEx I really don‘t care about the final structure now that we have tests. Please modify this as-needed (and add tests) if you come across any other cases we need to handle.

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.

2 participants