|
| 1 | +const {SourceMapConsumer, SourceMapGenerator} = require('source-map'); |
| 2 | +const lineCounter = require('./utils/lineCounter'); |
| 3 | + |
| 4 | +class SourceMap { |
| 5 | + constructor(mappings, sources) { |
| 6 | + this.mappings = mappings || []; |
| 7 | + this.sources = sources || {}; |
| 8 | + this.lineCount = null; |
| 9 | + } |
| 10 | + |
| 11 | + async getConsumer(map) { |
| 12 | + if (map instanceof SourceMapConsumer) { |
| 13 | + return map; |
| 14 | + } |
| 15 | + |
| 16 | + return await new SourceMapConsumer(map); |
| 17 | + } |
| 18 | + |
| 19 | + async addMap(map, lineOffset = 0, columnOffset = 0) { |
| 20 | + if (!(map instanceof SourceMap) && map.version) { |
| 21 | + let consumer = await this.getConsumer(map); |
| 22 | + |
| 23 | + consumer.eachMapping(mapping => { |
| 24 | + this.addConsumerMapping(mapping, lineOffset, columnOffset); |
| 25 | + if (!this.sources[mapping.source]) { |
| 26 | + this.sources[mapping.source] = consumer.sourceContentFor( |
| 27 | + mapping.source, |
| 28 | + true |
| 29 | + ); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + if (consumer.destroy) { |
| 34 | + // Only needs to happen in source-map 0.7 |
| 35 | + consumer.destroy(); |
| 36 | + } |
| 37 | + } else { |
| 38 | + if (!map.eachMapping) { |
| 39 | + map = new SourceMap(map.mappings, map.sources); |
| 40 | + } |
| 41 | + |
| 42 | + if (lineOffset === 0 && columnOffset === 0) { |
| 43 | + this.mappings = this.mappings.concat(map.mappings); |
| 44 | + } else { |
| 45 | + map.eachMapping(mapping => { |
| 46 | + this.addMapping(mapping, lineOffset, columnOffset); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + Object.keys(map.sources).forEach(sourceName => { |
| 51 | + if (!this.sources[sourceName]) { |
| 52 | + this.sources[sourceName] = map.sources[sourceName]; |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + addMapping(mapping, lineOffset = 0, columnOffset = 0) { |
| 61 | + mapping.generated = { |
| 62 | + line: mapping.generated.line + lineOffset, |
| 63 | + column: mapping.generated.column + columnOffset |
| 64 | + }; |
| 65 | + |
| 66 | + this.mappings.push(mapping); |
| 67 | + } |
| 68 | + |
| 69 | + addConsumerMapping(mapping, lineOffset = 0, columnOffset = 0) { |
| 70 | + if ( |
| 71 | + !mapping.source || |
| 72 | + !mapping.originalLine || |
| 73 | + (!mapping.originalColumn && mapping.originalColumn !== 0) |
| 74 | + ) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + this.mappings.push({ |
| 79 | + source: mapping.source, |
| 80 | + original: { |
| 81 | + line: mapping.originalLine, |
| 82 | + column: mapping.originalColumn |
| 83 | + }, |
| 84 | + generated: { |
| 85 | + line: mapping.generatedLine + lineOffset, |
| 86 | + column: mapping.generatedColumn + columnOffset |
| 87 | + }, |
| 88 | + name: mapping.name |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + eachMapping(callback) { |
| 93 | + this.mappings.forEach(callback); |
| 94 | + } |
| 95 | + |
| 96 | + generateEmptyMap(sourceName, sourceContent) { |
| 97 | + this.sources[sourceName] = sourceContent; |
| 98 | + |
| 99 | + this.lineCount = lineCounter(sourceContent); |
| 100 | + for (let line = 1; line < this.lineCount + 1; line++) { |
| 101 | + this.addMapping({ |
| 102 | + source: sourceName, |
| 103 | + original: { |
| 104 | + line: line, |
| 105 | + column: 0 |
| 106 | + }, |
| 107 | + generated: { |
| 108 | + line: line, |
| 109 | + column: 0 |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + async extendSourceMap(original, extension) { |
| 118 | + if (!(extension instanceof SourceMap)) { |
| 119 | + throw new Error( |
| 120 | + '[SOURCEMAP] Type of extension should be a SourceMap instance!' |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + original = await this.getConsumer(original); |
| 125 | + extension.eachMapping(mapping => { |
| 126 | + let originalMapping = original.originalPositionFor({ |
| 127 | + line: mapping.original.line, |
| 128 | + column: mapping.original.column |
| 129 | + }); |
| 130 | + |
| 131 | + if (!originalMapping.line) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + this.addMapping({ |
| 136 | + source: originalMapping.source, |
| 137 | + name: originalMapping.name, |
| 138 | + original: { |
| 139 | + line: originalMapping.line, |
| 140 | + column: originalMapping.column |
| 141 | + }, |
| 142 | + generated: { |
| 143 | + line: mapping.generated.line, |
| 144 | + column: mapping.generated.column |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + if (!this.sources[originalMapping.source]) { |
| 149 | + this.sources[originalMapping.source] = original.sourceContentFor( |
| 150 | + originalMapping.source, |
| 151 | + true |
| 152 | + ); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + if (original.destroy) { |
| 157 | + // Only needs to happen in source-map 0.7 |
| 158 | + original.destroy(); |
| 159 | + } |
| 160 | + |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + offset(lineOffset = 0, columnOffset = 0) { |
| 165 | + this.mappings.map(mapping => { |
| 166 | + mapping.generated.line = mapping.generated.line + lineOffset; |
| 167 | + mapping.generated.column = mapping.generated.column + columnOffset; |
| 168 | + return mapping; |
| 169 | + }); |
| 170 | + |
| 171 | + if (this.lineCount != null) { |
| 172 | + this.lineCount += lineOffset; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + stringify(file) { |
| 177 | + let generator = new SourceMapGenerator({ |
| 178 | + file: file |
| 179 | + }); |
| 180 | + |
| 181 | + this.eachMapping(mapping => generator.addMapping(mapping)); |
| 182 | + Object.keys(this.sources).forEach(sourceName => |
| 183 | + generator.setSourceContent(sourceName, this.sources[sourceName]) |
| 184 | + ); |
| 185 | + |
| 186 | + return generator.toString(); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +module.exports = SourceMap; |
0 commit comments