Skip to content

Commit 6a7e4cf

Browse files
committed
Strip the sourcemap comment from the preload script
1 parent 213e042 commit 6a7e4cf

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"build:src": "tsc",
1414
"build:electron": "npm run server:setup && electron-builder build",
1515
"build:dir-only": "npm run server:setup && electron-builder --dir",
16+
"postbuild": "ts-node ./strip-preload-map.ts",
1617
"start": "npm run server:setup && npm run start:app",
1718
"start:dev": "ts-node ./skip-server.ts && cross-env HTK_DEV=true APP_URL='http://local.httptoolkit.tech:8080' npm run start:app",
1819
"start:app": "tsc-watch --onSuccess \"electron .\""

strip-preload-map.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as path from 'path';
2+
import { readFileSync, writeFileSync } from 'fs';
3+
4+
function removeSourceMapAnnotation(filePath: string): void {
5+
const fileContent = readFileSync(filePath, 'utf8');
6+
const lines = fileContent.split('\n');
7+
8+
if (lines[lines.length - 1].startsWith('//# sourceMappingURL=')) {
9+
lines.pop(); // Remove the last line
10+
} else {
11+
console.log(`No sourcemap annotation found in ${filePath}`);
12+
return;
13+
}
14+
15+
const newContent = lines.join('\n');
16+
writeFileSync(filePath, newContent, 'utf8');
17+
console.log(`Sourcemap annotation removed from ${filePath}`);
18+
}
19+
20+
// We have to strip the source map comment from here, otherwise when it loads
21+
// in Chrome, it tries to get preload.js.map from the real server, leaving
22+
// us with an annoying error because that's never going to work.
23+
const filePath = path.join(__dirname, 'build', 'preload.js');
24+
removeSourceMapAnnotation(filePath);

0 commit comments

Comments
 (0)