Skip to content

Commit 28bf177

Browse files
authored
fix: Improve source map stripping to reduce collisions (#14)
1 parent e2a7853 commit 28bf177

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/plugins/prerender-plugin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,12 @@ export function prerenderPlugin({ prerenderScript, renderTarget, additionalPrere
507507
if (output.endsWith('.js')) {
508508
const codeOrSource = bundle[output].type == 'chunk' ? 'code' : 'source';
509509
if (typeof bundle[output][codeOrSource] !== 'string') continue;
510-
bundle[output][codeOrSource] = bundle[output][codeOrSource].replace(
511-
/\n\/\/#\ssourceMappingURL=.*/,
512-
'',
513-
);
510+
511+
const linesOfCode = bundle[output][codeOrSource].trimEnd().split('\n');
512+
if (/^\/\/#\ssourceMappingURL=.*\.map$/.test(linesOfCode.at(-1))) {
513+
linesOfCode.pop();
514+
bundle[output][codeOrSource] = linesOfCode.join('\n');
515+
}
514516
}
515517
}
516518
}

tests/fixtures/source-maps/src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,15 @@ if (typeof window !== 'undefined') {
1111
export async function prerender() {
1212
return `<h1>Simple Test Result</h1>`;
1313
}
14+
15+
// Basically mirror preact-www's repl worker
16+
const PREPEND = `(function (module, exports) {\n`;
17+
const APPEND = `\n});`;
18+
export async function process() {
19+
const code = `console.log('Hello World');`;
20+
let transpiled = `${PREPEND}${code}${APPEND}`;
21+
22+
transpiled += '\n//# sourceMappingURL=' + 'some-url.js.map';
23+
24+
return transpiled;
25+
}

tests/source-maps.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ test('Should strip sourcemaps by default', async () => {
3232
outDirAssets.find((f) => /^index-.*\.js$/.test(f)),
3333
);
3434
const outputChunkCode = await fs.readFile(outputChunk, 'utf-8');
35-
assert.is(outputChunkCode.match(/\/\/#\ssourceMappingURL=(.*)/), null);
35+
// Ensure arbitrary strings don't get stripped
36+
assert.ok(outputChunkCode.match(/\/\/#\ssourceMappingURL=/));
37+
// Ensure the sourcemap comment has been removed
38+
assert.is(outputChunkCode.match(/^\/\/#\ssourceMappingURL=.*\.map$/m), null);
3639

3740
const outputAsset = path.join(
3841
outDir,
3942
outDirAssets.find((f) => /^worker-.*\.js$/.test(f)),
4043
);
4144
const outputAssetSource = await fs.readFile(outputAsset, 'utf-8');
42-
assert.is(outputAssetSource.match(/\/\/#\ssourceMappingURL=(.*)/), null);
45+
assert.is(outputAssetSource.match(/^\/\/#\ssourceMappingURL=.*\.map$/m), null);
4346
});
4447

4548
test('Should preserve sourcemaps if user has enabled them', async () => {
@@ -64,7 +67,7 @@ test('Should preserve sourcemaps if user has enabled them', async () => {
6467
const outputJs = await fs.readFile(path.join(outDir, outputJsFileName), 'utf-8');
6568
assert.match(outputJs, '//# sourceMappingURL=');
6669

67-
const outputMap = outputJs.match(/\/\/#\ssourceMappingURL=(.*)/)[1];
70+
const outputMap = outputJs.match(/^\/\/#\ssourceMappingURL=(.*)\.map$/m)[1];
6871
assert.ok(outDirAssets.includes(outputMap));
6972
});
7073

0 commit comments

Comments
 (0)