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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export function rollupPluginStripSourceMapping(): Plugin {
return {
name: 'snowpack:rollup-plugin-strip-source-mapping',
transform: (code) => ({
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

map: null,
}),
};
Expand Down
49 changes: 49 additions & 0 deletions test/rollup-plugins.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const {
rollupPluginStripSourceMapping,
} = require('../esinstall/lib/rollup-plugins/rollup-plugin-strip-source-mapping.js');

describe('snowpack:rollup-plugin-strip-source-mapping', () => {
const tests = [
{
name: 'inline',
given: `console.log('foo');//# sourceMappingURL=js.map.js`,
expected: `console.log('foo');`,
},
{
name: 'end of file',
given: `console.log('foo');
//# sourceMappingURL=js.map.js`,
expected: `console.log('foo');
`,
},
{
name: 'middle of file',
given: `console.log('foo');
//# sourceMappingURL=js.map.js
console.log('bar');
//# sourceMappingURL=js.map.js`,
expected: `console.log('foo');

console.log('bar');
`,
},
{
name: 'inside string', // leave alone
given: `const myString ='//# sourceMappingURL=js.map.js';`,
expected: `const myString ='//# sourceMappingURL=js.map.js';`,
},
{
name: 'es-module-shim', // leave alone
given: ` sourceMappingResolved = \`\n//# sourceMappingURL=\` + resolveUrl(sourceMapping.slice(21), load.r);`,
expected: ` sourceMappingResolved = \`\n//# sourceMappingURL=\` + resolveUrl(sourceMapping.slice(21), load.r);`,
},
];

const {transform} = rollupPluginStripSourceMapping();

tests.forEach(({name, given, expected}) => {
it(name, () => {
expect(transform(given).code).toBe(expected);
});
});
});