|
| 1 | +import fs from 'fs'; |
| 2 | +import { BasicSourceMapConsumer, Position, RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map'; |
| 3 | +import { DebugIdGenerator } from './DebugIdGenerator'; |
| 4 | +import { stringToUuid } from './helpers/stringToUuid'; |
| 5 | + |
| 6 | +export class SourceProcessor { |
| 7 | + constructor(private readonly _debugIdGenerator: DebugIdGenerator) {} |
| 8 | + |
| 9 | + /** |
| 10 | + * Adds required snippets and comments to source, and modifies sourcemap to include debug ID. |
| 11 | + * @param source Source content. |
| 12 | + * @param sourceMap Sourcemap object or JSON. |
| 13 | + * @param debugId Debug ID. If not provided, one will be generated from `source`. |
| 14 | + * @returns Used debug ID, new source and new sourcemap. |
| 15 | + */ |
| 16 | + public async processSourceAndSourceMap(source: string, sourceMap: string | RawSourceMap, debugId?: string) { |
| 17 | + if (!debugId) { |
| 18 | + debugId = stringToUuid(source); |
| 19 | + } |
| 20 | + |
| 21 | + const sourceSnippet = this._debugIdGenerator.generateSourceSnippet(debugId); |
| 22 | + const sourceComment = this._debugIdGenerator.generateSourceComment(debugId); |
| 23 | + |
| 24 | + const newSource = sourceSnippet + '\n' + source + '\n' + sourceComment; |
| 25 | + |
| 26 | + // We need to offset the source map by amount of lines that we're inserting to the source code |
| 27 | + // Sourcemaps map code like this: |
| 28 | + // original code X:Y => generated code A:B |
| 29 | + // So if we add any code to generated code, mappings after that code will become invalid |
| 30 | + // We need to offset the mapping lines by sourceSnippetNewlineCount: |
| 31 | + // original code X:Y => generated code (A + sourceSnippetNewlineCount):B |
| 32 | + const sourceSnippetNewlineCount = sourceSnippet.match(/\n/g)?.length ?? 0; |
| 33 | + const offsetSourceMap = await this.offsetSourceMap(sourceMap, 0, sourceSnippetNewlineCount + 1); |
| 34 | + const newSourceMap = this._debugIdGenerator.addSourceMapKey(offsetSourceMap, debugId); |
| 35 | + |
| 36 | + return { debugId, source: newSource, sourceMap: newSourceMap }; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Adds required snippets and comments to source, and modifies sourcemap to include debug ID. |
| 41 | + * Will write modified content to the files. |
| 42 | + * @param sourcePath Path to the source. |
| 43 | + * @param sourceMapPath Path to the sourcemap. |
| 44 | + * @param debugId Debug ID. If not provided, one will be generated from `source`. |
| 45 | + * @returns Used debug ID. |
| 46 | + */ |
| 47 | + public async processSourceAndSourceMapFiles(sourcePath: string, sourceMapPath: string, debugId?: string) { |
| 48 | + const source = await fs.promises.readFile(sourcePath, 'utf8'); |
| 49 | + const sourceMap = await fs.promises.readFile(sourceMapPath, 'utf8'); |
| 50 | + |
| 51 | + const result = await this.processSourceAndSourceMap(source, sourceMap, debugId); |
| 52 | + |
| 53 | + await fs.promises.writeFile(sourcePath, result.source, 'utf8'); |
| 54 | + await fs.promises.writeFile(sourceMapPath, JSON.stringify(result.sourceMap), 'utf8'); |
| 55 | + |
| 56 | + return result.debugId; |
| 57 | + } |
| 58 | + |
| 59 | + private async offsetSourceMap( |
| 60 | + sourceMap: string | RawSourceMap, |
| 61 | + fromLine: number, |
| 62 | + count: number, |
| 63 | + ): Promise<RawSourceMap> { |
| 64 | + const consumer = (await new SourceMapConsumer(sourceMap)) as BasicSourceMapConsumer; |
| 65 | + const newSourceMap = new SourceMapGenerator({ |
| 66 | + file: consumer.file, |
| 67 | + sourceRoot: consumer.sourceRoot, |
| 68 | + }); |
| 69 | + |
| 70 | + consumer.eachMapping((m) => { |
| 71 | + if (m.generatedLine < fromLine) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + // Despite how the mappings are written, addMapping expects here a null value if the column/line is not set |
| 76 | + newSourceMap.addMapping({ |
| 77 | + source: m.source, |
| 78 | + name: m.name, |
| 79 | + generated: |
| 80 | + m?.generatedColumn != null && m?.generatedLine != null |
| 81 | + ? { column: m.generatedColumn, line: m.generatedLine + count } |
| 82 | + : (null as unknown as Position), |
| 83 | + original: |
| 84 | + m?.originalColumn != null && m?.originalLine != null |
| 85 | + ? { column: m.originalColumn, line: m.originalLine } |
| 86 | + : (null as unknown as Position), |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + return newSourceMap.toJSON(); |
| 91 | + } |
| 92 | +} |
0 commit comments