Skip to content

Commit 4432067

Browse files
committed
sourcemap-tools: attempt to add snippets and comments if they do not exist
1 parent e291bb9 commit 4432067

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

tools/sourcemap-tools/src/DebugIdGenerator.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class DebugIdGenerator {
1515
const replaceAll = () => source.replace(oldDebugId, newDebugId);
1616

1717
// Try to replace more safely first
18-
const oldSourceSnippet = this.generateSourceSnippet(oldDebugId);
18+
const oldSourceSnippet = this.generateSourceSnippet(oldDebugId).replace(/^;+|;+$/g, '');
1919
if (source.indexOf(oldSourceSnippet) !== -1) {
2020
source = source.replace(oldSourceSnippet, this.generateSourceSnippet(newDebugId));
2121
} else {
@@ -32,7 +32,17 @@ export class DebugIdGenerator {
3232
return source;
3333
}
3434

35-
public getSourceDebugId(source: string): string | undefined {
35+
public hasCodeSnippet(source: string, debugId: string) {
36+
const sourceSnippet = this.generateSourceSnippet(debugId).replace(/^;+|;+$/g, '');
37+
return source.includes(sourceSnippet);
38+
}
39+
40+
public hasCommentSnippet(source: string, debugId: string) {
41+
const commentSnippet = this.generateSourceComment(debugId);
42+
return source.includes(commentSnippet);
43+
}
44+
45+
public getSourceDebugIdFromComment(source: string): string | undefined {
3646
const regex = new RegExp(`^//# ${SOURCE_DEBUG_ID_COMMENT}=(.+)$`, 'm');
3747
const match = source.match(regex);
3848
if (!match) {

tools/sourcemap-tools/src/SourceProcessor.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class SourceProcessor {
2222
constructor(private readonly _debugIdGenerator: DebugIdGenerator) {}
2323

2424
public isSourceProcessed(source: string): boolean {
25-
return !!this._debugIdGenerator.getSourceDebugId(source);
25+
return !!this._debugIdGenerator.getSourceDebugIdFromComment(source);
2626
}
2727

2828
public isSourceMapProcessed(sourceMap: RawSourceMap): boolean {
@@ -47,7 +47,7 @@ export class SourceProcessor {
4747
}
4848

4949
public getSourceDebugId(source: string): string | undefined {
50-
return this._debugIdGenerator.getSourceDebugId(source);
50+
return this._debugIdGenerator.getSourceDebugIdFromComment(source);
5151
}
5252

5353
public getSourceMapDebugId(sourceMap: RawSourceMap): string | undefined {
@@ -74,29 +74,29 @@ export class SourceProcessor {
7474
source: string,
7575
sourceMap: RawSourceMap,
7676
debugId?: string,
77+
force?: boolean,
7778
): Promise<ProcessResult> {
7879
const sourceDebugId = this.getSourceDebugId(source);
7980
if (!debugId) {
8081
debugId = sourceDebugId ?? stringToUuid(source);
8182
}
8283

83-
let newSource: string | undefined;
84+
let newSource = source;
8485
let offsetSourceMap: RawSourceMap | undefined;
8586

8687
// If source has debug ID, but it is different, we need to only replace it
8788
if (sourceDebugId && debugId !== sourceDebugId) {
8889
newSource = this._debugIdGenerator.replaceDebugId(source, sourceDebugId, debugId);
89-
} else if (!sourceDebugId) {
90+
}
91+
92+
if (force || !sourceDebugId || !this._debugIdGenerator.hasCodeSnippet(source, debugId)) {
9093
const sourceSnippet = this._debugIdGenerator.generateSourceSnippet(debugId);
9194

9295
const shebang = source.match(/^(#!.+\n)/)?.[1];
93-
const sourceWithSnippet = shebang
96+
newSource = shebang
9497
? shebang + sourceSnippet + '\n' + source.substring(shebang.length)
9598
: sourceSnippet + '\n' + source;
9699

97-
const sourceComment = this._debugIdGenerator.generateSourceComment(debugId);
98-
newSource = appendBeforeWhitespaces(sourceWithSnippet, '\n' + sourceComment);
99-
100100
// We need to offset the source map by amount of lines that we're inserting to the source code
101101
// Sourcemaps map code like this:
102102
// original code X:Y => generated code A:B
@@ -107,8 +107,13 @@ export class SourceProcessor {
107107
offsetSourceMap = await this.offsetSourceMap(sourceMap, sourceSnippetNewlineCount + 1);
108108
}
109109

110+
if (force || !sourceDebugId || !this._debugIdGenerator.hasCommentSnippet(source, debugId)) {
111+
const sourceComment = this._debugIdGenerator.generateSourceComment(debugId);
112+
newSource = appendBeforeWhitespaces(newSource, '\n' + sourceComment);
113+
}
114+
110115
const newSourceMap = this._debugIdGenerator.addSourceMapDebugId(offsetSourceMap ?? sourceMap, debugId);
111-
return { debugId, source: newSource ?? source, sourceMap: newSourceMap };
116+
return { debugId, source: newSource, sourceMap: newSourceMap };
112117
}
113118

114119
/**
@@ -123,6 +128,7 @@ export class SourceProcessor {
123128
sourcePath: string,
124129
sourceMapPath?: string,
125130
debugId?: string,
131+
force?: boolean,
126132
): ResultPromise<ProcessResultWithPaths, string> {
127133
const sourceReadResult = await readFile(sourcePath);
128134
if (sourceReadResult.isErr()) {
@@ -152,7 +158,7 @@ export class SourceProcessor {
152158
}
153159
const sourceMap = parseResult.data;
154160

155-
const processResult = await this.processSourceAndSourceMap(source, sourceMap, debugId);
161+
const processResult = await this.processSourceAndSourceMap(source, sourceMap, debugId, force);
156162
return Ok({
157163
...processResult,
158164
sourcePath,

tools/sourcemap-tools/tests/DebugIdGenerator.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('DebugIdGenerator', () => {
156156
].join('\n');
157157

158158
const debugIdGenerator = new DebugIdGenerator();
159-
const actual = debugIdGenerator.getSourceDebugId(source);
159+
const actual = debugIdGenerator.getSourceDebugIdFromComment(source);
160160

161161
expect(actual).toEqual(expected);
162162
});
@@ -167,7 +167,7 @@ describe('DebugIdGenerator', () => {
167167
);
168168

169169
const debugIdGenerator = new DebugIdGenerator();
170-
const actual = debugIdGenerator.getSourceDebugId(source);
170+
const actual = debugIdGenerator.getSourceDebugIdFromComment(source);
171171

172172
expect(actual).toBeUndefined();
173173
});

tools/sourcemap-tools/tests/SourceProcessor.spec.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ function foo(){console.log("Hello World!")}foo();`;
3636
};
3737

3838
const processedSource = (
39+
debugIdGenerator: DebugIdGenerator,
3940
debugId: string,
40-
) => `;!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},n=(new Error).stack;n&&(e._btDebugIds=e._btDebugIds||{},e._btDebugIds[n]="${debugId}")}catch(e){}}();
41+
) => `${debugIdGenerator.generateSourceSnippet(debugId)}
4142
(()=>{"use strict";console.log("Hello World!")})();
4243
//# sourceMappingURL=source.js.map
43-
//# debugId=${debugId}
44+
${debugIdGenerator.generateSourceComment(debugId)}
4445
`;
4546

4647
const processedSourceMap = (debugId: string) => ({
@@ -225,7 +226,7 @@ function foo(){console.log("Hello World!")}foo();`;
225226

226227
await sourceProcessor.processSourceAndSourceMapFiles(sourcePath, sourceMapPath, debugId);
227228

228-
expect(processFn).toBeCalledWith(sourceContent, sourceMapContent, debugId);
229+
expect(processFn).toBeCalledWith(sourceContent, sourceMapContent, debugId, undefined);
229230
});
230231

231232
it('should call process function with sourcemap detected from source', async () => {
@@ -246,22 +247,24 @@ function foo(){console.log("Hello World!")}foo();`;
246247

247248
await sourceProcessor.processSourceAndSourceMapFiles(sourcePath, undefined, debugId);
248249

249-
expect(processFn).toBeCalledWith(sourceContent, sourceMapContent, debugId);
250+
expect(processFn).toBeCalledWith(sourceContent, sourceMapContent, debugId, undefined);
250251
});
251252

252253
it('should return unmodified source when source has debug ID', async () => {
253254
const debugId = randomUUID();
254-
const source = processedSource(debugId);
255-
const sourceProcessor = new SourceProcessor(new DebugIdGenerator());
255+
const debugIdGenerator = new DebugIdGenerator();
256+
const source = processedSource(debugIdGenerator, debugId);
257+
const sourceProcessor = new SourceProcessor(debugIdGenerator);
256258
const result = await sourceProcessor.processSourceAndSourceMap(source, processedSourceMap(debugId));
257259

258260
expect(result.source).toEqual(source);
259261
});
260262

261263
it('should return unmodified source when source has same debug ID as provided', async () => {
262264
const debugId = randomUUID();
263-
const source = processedSource(debugId);
264-
const sourceProcessor = new SourceProcessor(new DebugIdGenerator());
265+
const debugIdGenerator = new DebugIdGenerator();
266+
const source = processedSource(debugIdGenerator, debugId);
267+
const sourceProcessor = new SourceProcessor(debugIdGenerator);
265268
const result = await sourceProcessor.processSourceAndSourceMap(
266269
source,
267270
processedSourceMap(debugId),
@@ -273,17 +276,21 @@ function foo(){console.log("Hello World!")}foo();`;
273276

274277
it("should return sourcemap with source's debug ID when source has debug ID", async () => {
275278
const debugId = randomUUID();
276-
const sourceProcessor = new SourceProcessor(new DebugIdGenerator());
277-
const result = await sourceProcessor.processSourceAndSourceMap(processedSource(debugId), sourceMap);
279+
const debugIdGenerator = new DebugIdGenerator();
280+
const sourceProcessor = new SourceProcessor(debugIdGenerator);
281+
const result = await sourceProcessor.processSourceAndSourceMap(
282+
processedSource(debugIdGenerator, debugId),
283+
sourceMap,
284+
);
278285

279286
expect(result.sourceMap.debugId).toEqual(debugId);
280287
});
281288

282289
it('should call replace debug ID when source has different debug ID than provided', async () => {
283290
const oldDebugId = randomUUID();
284291
const newDebugId = randomUUID();
285-
const source = processedSource(oldDebugId);
286292
const debugIdGenerator = new DebugIdGenerator();
293+
const source = processedSource(debugIdGenerator, oldDebugId);
287294

288295
const spy = jest.spyOn(debugIdGenerator, 'replaceDebugId');
289296

0 commit comments

Comments
 (0)