|
| 1 | +import { encode, decode } from 'sourcemap-codec'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef {{ version: number, file?: string, sourceRoot?: string, sources: string[], sourcesContent: Array<string | null>, names: string[], mappings: string}} SourceMap |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * |
| 9 | + * @param {SourceMap[]} maps |
| 10 | + * @param {import('sourcemap-codec').SourceMapMappings[]} parsedMappings |
| 11 | + * @param {Map<string, { index: number, content: string | null}>} sourcesCache |
| 12 | + * @param {number} source |
| 13 | + * @param {number} line |
| 14 | + * @param {number} column |
| 15 | + * @returns {{ line: number, column: number, source: number }} |
| 16 | + */ |
| 17 | +function getOriginalPosition(maps, parsedMappings, sourcesCache, source, line, column) { |
| 18 | + let originalLine = line; |
| 19 | + let originalColumn = column; |
| 20 | + let originalSource = source; |
| 21 | + |
| 22 | + // Traverse maps backwards, but skip last map as that's the |
| 23 | + // one we requested the position from |
| 24 | + for (let i = parsedMappings.length - 1; i >= 0; i--) { |
| 25 | + const map = parsedMappings[i]; |
| 26 | + |
| 27 | + const segments = map[originalLine]; |
| 28 | + if (!segments) break; |
| 29 | + |
| 30 | + // TODO: Binary search |
| 31 | + let lastFound; |
| 32 | + for (let j = 0; j < segments.length; j++) { |
| 33 | + const segment = segments[j]; |
| 34 | + if (segment[0] === originalColumn) { |
| 35 | + if (segment.length === 1) { |
| 36 | + break; |
| 37 | + } |
| 38 | + |
| 39 | + const sourceName = maps[i].sources[segment[1]]; |
| 40 | + const mappedName = sourcesCache.get(sourceName); |
| 41 | + if (!mappedName) { |
| 42 | + originalSource = 0; |
| 43 | + } else { |
| 44 | + originalSource = mappedName.index; |
| 45 | + } |
| 46 | + |
| 47 | + originalLine = segment[2]; |
| 48 | + originalColumn = segment[3]; |
| 49 | + break; |
| 50 | + } else if (segment[0] < originalColumn) { |
| 51 | + lastFound = segment; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (lastFound !== undefined) { |
| 56 | + if (lastFound.length === 1) { |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + const sourceName = maps[i].sources[lastFound[1]]; |
| 61 | + const mappedName = sourcesCache.get(sourceName); |
| 62 | + if (!mappedName) { |
| 63 | + originalSource = 0; |
| 64 | + } else { |
| 65 | + originalSource = mappedName.index; |
| 66 | + } |
| 67 | + |
| 68 | + originalLine = lastFound[2]; |
| 69 | + originalColumn = lastFound[3] + (originalColumn - lastFound[3]); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return { |
| 74 | + line: originalLine, |
| 75 | + column: originalColumn, |
| 76 | + source: originalSource |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Combine an array of sourcemaps into one |
| 82 | + * @param {SourceMap[]} sourceMaps |
| 83 | + * @returns {SourceMap} |
| 84 | + */ |
| 85 | +export function mergeSourceMaps(sourceMaps) { |
| 86 | + let file; |
| 87 | + let sourceRoot; |
| 88 | + |
| 89 | + /** @type {import('sourcemap-codec').SourceMapMappings[]} */ |
| 90 | + const parsedMappings = []; |
| 91 | + |
| 92 | + /** @type {Map<string, number>} */ |
| 93 | + const names = new Map(); |
| 94 | + |
| 95 | + /** @type {Map<string, { index: number, content: string | null}>} */ |
| 96 | + const sourcesCache = new Map(); |
| 97 | + |
| 98 | + for (let i = 0; i < sourceMaps.length; i++) { |
| 99 | + const map = sourceMaps[i]; |
| 100 | + |
| 101 | + if (map.file) { |
| 102 | + file = map.file; |
| 103 | + } |
| 104 | + |
| 105 | + if (map.sourceRoot) { |
| 106 | + sourceRoot = map.sourceRoot; |
| 107 | + } |
| 108 | + |
| 109 | + for (let j = 0; j < map.sources.length; j++) { |
| 110 | + const source = map.sources[j]; |
| 111 | + if (!sourcesCache.has(source)) { |
| 112 | + sourcesCache.set(source, { index: sourcesCache.size, content: map.sourcesContent[j] || null }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + for (let j = 0; j < map.names.length; j++) { |
| 117 | + const name = map.names[j]; |
| 118 | + if (!names.has(name)) { |
| 119 | + names.set(name, names.size - 1); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + // Merge mappings |
| 124 | + parsedMappings.push(decode(map.mappings)); |
| 125 | + } |
| 126 | + |
| 127 | + const sources = []; |
| 128 | + const sourcesContent = []; |
| 129 | + for (const [key, value] of sourcesCache.entries()) { |
| 130 | + sources.push(key); |
| 131 | + sourcesContent.push(value.content); |
| 132 | + } |
| 133 | + |
| 134 | + /** @type {import('sourcemap-codec').SourceMapMappings} */ |
| 135 | + const outMappings = []; |
| 136 | + const lastMap = parsedMappings[parsedMappings.length - 1]; |
| 137 | + |
| 138 | + // Loop over the mappings of the last source map and retrieve |
| 139 | + // original position for each mapping segment |
| 140 | + for (let i = 0; i < lastMap.length; i++) { |
| 141 | + const line = lastMap[i]; |
| 142 | + |
| 143 | + /** @type {import('sourcemap-codec').SourceMapSegment[]} */ |
| 144 | + const rewrittenSegments = []; |
| 145 | + for (let j = 0; j < line.length; j++) { |
| 146 | + const segment = line[j]; |
| 147 | + |
| 148 | + if (segment.length === 4) { |
| 149 | + const original = getOriginalPosition( |
| 150 | + sourceMaps, |
| 151 | + parsedMappings, |
| 152 | + sourcesCache, |
| 153 | + segment[1], |
| 154 | + segment[2], |
| 155 | + segment[3] |
| 156 | + ); |
| 157 | + rewrittenSegments.push([segment[0], original.source, original.line, original.column]); |
| 158 | + } else if (segment.length === 5) { |
| 159 | + const original = getOriginalPosition( |
| 160 | + sourceMaps, |
| 161 | + parsedMappings, |
| 162 | + sourcesCache, |
| 163 | + segment[1], |
| 164 | + segment[2], |
| 165 | + segment[3] |
| 166 | + ); |
| 167 | + rewrittenSegments.push([segment[0], original.source, original.line, original.column, segment[4]]); |
| 168 | + } else { |
| 169 | + rewrittenSegments.push(segment); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + outMappings.push(rewrittenSegments); |
| 174 | + } |
| 175 | + |
| 176 | + return { |
| 177 | + version: 3, |
| 178 | + file, |
| 179 | + names: Array.from(names.keys()), |
| 180 | + sourceRoot, |
| 181 | + sources, |
| 182 | + sourcesContent, |
| 183 | + mappings: encode(outMappings) |
| 184 | + }; |
| 185 | +} |
0 commit comments