// A source map for the page's script as web/build.mts makes it, files joined one after another // (#390): each line of the joined script maps to its line in the file it came from. Given to swc // as the input map, the built script then maps to web/src/*.ts file by file, which is what the // coverage report needs. Only a coverage build uses it. export interface SourceMap { version: 3; sources: string[]; sourcesContent: string[]; names: string[]; mappings: string; } const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; /// One number in a source map's base64 VLQ: sign in the lowest bit, five bits a digit. export function vlq(n: number): string { let v = n < 0 ? (-n << 1) | 1 : n << 1; let out = ''; do { let digit = v & 31; v >>>= 5; if (v) digit |= 32; out += B64[digit]; } while (v); return out; } /// The map of `files` joined with '\n' between them, as build.mts joins them. export function concatMap(files: { name: string; text: string }[]): SourceMap { const lines: string[] = []; // Each segment's fields are relative to the one before: source, line in it, column. let lastSource = 0, lastLine = 0; files.forEach((f, source) => { const count = f.text.split('\n').length; for (let line = 0; line < count; line++) { lines.push(vlq(0) + vlq(source - lastSource) + vlq(line - lastLine) + vlq(0)); lastSource = source; lastLine = line; } }); return { version: 3, sources: files.map((f) => f.name), sourcesContent: files.map((f) => f.text), names: [], mappings: lines.join(';'), }; }