Skip to content

Commit dd7a7d2

Browse files
committed
Update regex to overwrite existing debugId
1 parent 31c1ae7 commit dd7a7d2

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export function stringToUUID(str: string): string {
2828
}
2929

3030
export function addDebugIdToSource(input: string, debugId: string): string {
31-
return input.replace(/(\n*)?(\/\/# .+\n*)?$/, `\n//# debugId=${debugId}\n$2`);
31+
return input.replace(
32+
/\s*(?:\/\/# debugId=.+)?\s*(\/\/# .+)?\s*$/,
33+
`\n//# debugId=${debugId}\n$1`
34+
);
3235
}
3336

3437
export function addDebugIdToSourcemap(input: string, debugId: string): string {

test/common/common.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ describe("common", () => {
1212
`);
1313
});
1414

15+
test("adds debugId to source with existing debugId", () => {
16+
expect(
17+
addDebugIdToSource(
18+
`console.log('nothing')
19+
//# debugId=302a139c-0c62-4af7-bfd3-03d083925e5e`,
20+
"000-000-000-000"
21+
)
22+
).toMatchInlineSnapshot(`
23+
"console.log('nothing')
24+
//# debugId=000-000-000-000
25+
"
26+
`);
27+
});
28+
1529
test("adds debugId to source with sourceMapURL", () => {
1630
expect(
1731
addDebugIdToSource(
@@ -26,4 +40,19 @@ describe("common", () => {
2640
`);
2741
});
2842
});
43+
44+
test("adds debugId to source with sourceMapURL and existing debugId", () => {
45+
expect(
46+
addDebugIdToSource(
47+
`console.log('nothing');
48+
//# debugId=302a139c-0c62-4af7-bfd3-03d083925e5e
49+
//# sourceMappingURL=main.js.map`,
50+
"000-000-000-000"
51+
)
52+
).toMatchInlineSnapshot(`
53+
"console.log('nothing');
54+
//# debugId=000-000-000-000
55+
//# sourceMappingURL=main.js.map"
56+
`);
57+
});
2958
});

0 commit comments

Comments
 (0)