Skip to content

Commit

Permalink
Fix sourcemap plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Nov 6, 2020
1 parent 803e67c commit a2739d5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
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, ''),
map: null,
}),
};
Expand Down
20 changes: 20 additions & 0 deletions test/esinstall/package-es-module-shims/__snapshots__
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snowpack install package-es-module-shims: cli output 1`] = `
"[snowpack] snowpack - A faster build system for the modern web.
Snowpack is best configured via config file.
But, most configuration can also be passed via CLI flags.
📖 https://www.snowpack.dev/#configuration
Commands:
snowpack init Create a new project config file.
snowpack dev Develop your app locally.
snowpack build Build your app for production.
snowpack install (Advanced) Install web-ready dependencies.
Flags:
--config [path] Set the location of your project config file.
--help Show this help message.
--version Show the current version.
--reload Clear Snowpack's local cache (troubleshooting).
--verbose View debug info (where available)
--quiet Don’t output anything (dev server will still log minimally)"
`;
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);
});
});
});

0 comments on commit a2739d5

Please sign in to comment.