Skip to content

Commit f25156f

Browse files
perf2711Konrad Dysput
authored andcommitted
sourcemap-tools: add processSource to SourceProcessor
1 parent 333221d commit f25156f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tools/sourcemap-tools/src/SourceProcessor.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,36 @@ export class SourceProcessor {
8282
);
8383
}
8484

85+
public processSource(source: string, debugId?: string, force?: boolean) {
86+
const sourceDebugId = this.getSourceDebugId(source);
87+
if (!debugId) {
88+
debugId = sourceDebugId ?? stringToUuid(source);
89+
}
90+
91+
let newSource = source;
92+
93+
// If source has debug ID, but it is different, we need to only replace it
94+
if (sourceDebugId && debugId !== sourceDebugId) {
95+
newSource = this._debugIdGenerator.replaceDebugId(source, sourceDebugId, debugId);
96+
}
97+
98+
if (force || !sourceDebugId || !this._debugIdGenerator.hasCodeSnippet(source, debugId)) {
99+
const sourceSnippet = this._debugIdGenerator.generateSourceSnippet(debugId);
100+
101+
const shebang = source.match(/^(#!.+\n)/)?.[1];
102+
newSource = shebang
103+
? shebang + sourceSnippet + '\n' + source.substring(shebang.length)
104+
: sourceSnippet + '\n' + source;
105+
}
106+
107+
if (force || !sourceDebugId || !this._debugIdGenerator.hasCommentSnippet(source, debugId)) {
108+
const sourceComment = this._debugIdGenerator.generateSourceComment(debugId);
109+
newSource = appendBeforeWhitespaces(newSource, '\n' + sourceComment);
110+
}
111+
112+
return { debugId, source: newSource };
113+
}
114+
85115
/**
86116
* Adds required snippets and comments to source, and modifies sourcemap to include debug ID.
87117
* @param source Source content.

0 commit comments

Comments
 (0)