[d565449] | 1 | (function (global, factory) {
|
---|
| 2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@jridgewell/set-array'), require('@jridgewell/sourcemap-codec'), require('@jridgewell/trace-mapping')) :
|
---|
| 3 | typeof define === 'function' && define.amd ? define(['exports', '@jridgewell/set-array', '@jridgewell/sourcemap-codec', '@jridgewell/trace-mapping'], factory) :
|
---|
| 4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.genMapping = {}, global.setArray, global.sourcemapCodec, global.traceMapping));
|
---|
| 5 | })(this, (function (exports, setArray, sourcemapCodec, traceMapping) { 'use strict';
|
---|
| 6 |
|
---|
| 7 | const COLUMN = 0;
|
---|
| 8 | const SOURCES_INDEX = 1;
|
---|
| 9 | const SOURCE_LINE = 2;
|
---|
| 10 | const SOURCE_COLUMN = 3;
|
---|
| 11 | const NAMES_INDEX = 4;
|
---|
| 12 |
|
---|
| 13 | const NO_NAME = -1;
|
---|
| 14 | /**
|
---|
| 15 | * Provides the state to generate a sourcemap.
|
---|
| 16 | */
|
---|
| 17 | class GenMapping {
|
---|
| 18 | constructor({ file, sourceRoot } = {}) {
|
---|
| 19 | this._names = new setArray.SetArray();
|
---|
| 20 | this._sources = new setArray.SetArray();
|
---|
| 21 | this._sourcesContent = [];
|
---|
| 22 | this._mappings = [];
|
---|
| 23 | this.file = file;
|
---|
| 24 | this.sourceRoot = sourceRoot;
|
---|
| 25 | this._ignoreList = new setArray.SetArray();
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | /**
|
---|
| 29 | * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
|
---|
| 30 | * with public access modifiers.
|
---|
| 31 | */
|
---|
| 32 | function cast(map) {
|
---|
| 33 | return map;
|
---|
| 34 | }
|
---|
| 35 | function addSegment(map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
|
---|
| 36 | return addSegmentInternal(false, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
|
---|
| 37 | }
|
---|
| 38 | function addMapping(map, mapping) {
|
---|
| 39 | return addMappingInternal(false, map, mapping);
|
---|
| 40 | }
|
---|
| 41 | /**
|
---|
| 42 | * Same as `addSegment`, but will only add the segment if it generates useful information in the
|
---|
| 43 | * resulting map. This only works correctly if segments are added **in order**, meaning you should
|
---|
| 44 | * not add a segment with a lower generated line/column than one that came before.
|
---|
| 45 | */
|
---|
| 46 | const maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
|
---|
| 47 | return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content);
|
---|
| 48 | };
|
---|
| 49 | /**
|
---|
| 50 | * Same as `addMapping`, but will only add the mapping if it generates useful information in the
|
---|
| 51 | * resulting map. This only works correctly if mappings are added **in order**, meaning you should
|
---|
| 52 | * not add a mapping with a lower generated line/column than one that came before.
|
---|
| 53 | */
|
---|
| 54 | const maybeAddMapping = (map, mapping) => {
|
---|
| 55 | return addMappingInternal(true, map, mapping);
|
---|
| 56 | };
|
---|
| 57 | /**
|
---|
| 58 | * Adds/removes the content of the source file to the source map.
|
---|
| 59 | */
|
---|
| 60 | function setSourceContent(map, source, content) {
|
---|
| 61 | const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
|
---|
| 62 | const index = setArray.put(sources, source);
|
---|
| 63 | sourcesContent[index] = content;
|
---|
| 64 | }
|
---|
| 65 | function setIgnore(map, source, ignore = true) {
|
---|
| 66 | const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
|
---|
| 67 | const index = setArray.put(sources, source);
|
---|
| 68 | if (index === sourcesContent.length)
|
---|
| 69 | sourcesContent[index] = null;
|
---|
| 70 | if (ignore)
|
---|
| 71 | setArray.put(ignoreList, index);
|
---|
| 72 | else
|
---|
| 73 | setArray.remove(ignoreList, index);
|
---|
| 74 | }
|
---|
| 75 | /**
|
---|
| 76 | * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
|
---|
| 77 | * a sourcemap, or to JSON.stringify.
|
---|
| 78 | */
|
---|
| 79 | function toDecodedMap(map) {
|
---|
| 80 | const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList, } = cast(map);
|
---|
| 81 | removeEmptyFinalLines(mappings);
|
---|
| 82 | return {
|
---|
| 83 | version: 3,
|
---|
| 84 | file: map.file || undefined,
|
---|
| 85 | names: names.array,
|
---|
| 86 | sourceRoot: map.sourceRoot || undefined,
|
---|
| 87 | sources: sources.array,
|
---|
| 88 | sourcesContent,
|
---|
| 89 | mappings,
|
---|
| 90 | ignoreList: ignoreList.array,
|
---|
| 91 | };
|
---|
| 92 | }
|
---|
| 93 | /**
|
---|
| 94 | * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
|
---|
| 95 | * a sourcemap, or to JSON.stringify.
|
---|
| 96 | */
|
---|
| 97 | function toEncodedMap(map) {
|
---|
| 98 | const decoded = toDecodedMap(map);
|
---|
| 99 | return Object.assign(Object.assign({}, decoded), { mappings: sourcemapCodec.encode(decoded.mappings) });
|
---|
| 100 | }
|
---|
| 101 | /**
|
---|
| 102 | * Constructs a new GenMapping, using the already present mappings of the input.
|
---|
| 103 | */
|
---|
| 104 | function fromMap(input) {
|
---|
| 105 | const map = new traceMapping.TraceMap(input);
|
---|
| 106 | const gen = new GenMapping({ file: map.file, sourceRoot: map.sourceRoot });
|
---|
| 107 | putAll(cast(gen)._names, map.names);
|
---|
| 108 | putAll(cast(gen)._sources, map.sources);
|
---|
| 109 | cast(gen)._sourcesContent = map.sourcesContent || map.sources.map(() => null);
|
---|
| 110 | cast(gen)._mappings = traceMapping.decodedMappings(map);
|
---|
| 111 | if (map.ignoreList)
|
---|
| 112 | putAll(cast(gen)._ignoreList, map.ignoreList);
|
---|
| 113 | return gen;
|
---|
| 114 | }
|
---|
| 115 | /**
|
---|
| 116 | * Returns an array of high-level mapping objects for every recorded segment, which could then be
|
---|
| 117 | * passed to the `source-map` library.
|
---|
| 118 | */
|
---|
| 119 | function allMappings(map) {
|
---|
| 120 | const out = [];
|
---|
| 121 | const { _mappings: mappings, _sources: sources, _names: names } = cast(map);
|
---|
| 122 | for (let i = 0; i < mappings.length; i++) {
|
---|
| 123 | const line = mappings[i];
|
---|
| 124 | for (let j = 0; j < line.length; j++) {
|
---|
| 125 | const seg = line[j];
|
---|
| 126 | const generated = { line: i + 1, column: seg[COLUMN] };
|
---|
| 127 | let source = undefined;
|
---|
| 128 | let original = undefined;
|
---|
| 129 | let name = undefined;
|
---|
| 130 | if (seg.length !== 1) {
|
---|
| 131 | source = sources.array[seg[SOURCES_INDEX]];
|
---|
| 132 | original = { line: seg[SOURCE_LINE] + 1, column: seg[SOURCE_COLUMN] };
|
---|
| 133 | if (seg.length === 5)
|
---|
| 134 | name = names.array[seg[NAMES_INDEX]];
|
---|
| 135 | }
|
---|
| 136 | out.push({ generated, source, original, name });
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | return out;
|
---|
| 140 | }
|
---|
| 141 | // This split declaration is only so that terser can elminiate the static initialization block.
|
---|
| 142 | function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
|
---|
| 143 | const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = cast(map);
|
---|
| 144 | const line = getLine(mappings, genLine);
|
---|
| 145 | const index = getColumnIndex(line, genColumn);
|
---|
| 146 | if (!source) {
|
---|
| 147 | if (skipable && skipSourceless(line, index))
|
---|
| 148 | return;
|
---|
| 149 | return insert(line, index, [genColumn]);
|
---|
| 150 | }
|
---|
| 151 | const sourcesIndex = setArray.put(sources, source);
|
---|
| 152 | const namesIndex = name ? setArray.put(names, name) : NO_NAME;
|
---|
| 153 | if (sourcesIndex === sourcesContent.length)
|
---|
| 154 | sourcesContent[sourcesIndex] = content !== null && content !== void 0 ? content : null;
|
---|
| 155 | if (skipable && skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
|
---|
| 156 | return;
|
---|
| 157 | }
|
---|
| 158 | return insert(line, index, name
|
---|
| 159 | ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
|
---|
| 160 | : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
|
---|
| 161 | }
|
---|
| 162 | function getLine(mappings, index) {
|
---|
| 163 | for (let i = mappings.length; i <= index; i++) {
|
---|
| 164 | mappings[i] = [];
|
---|
| 165 | }
|
---|
| 166 | return mappings[index];
|
---|
| 167 | }
|
---|
| 168 | function getColumnIndex(line, genColumn) {
|
---|
| 169 | let index = line.length;
|
---|
| 170 | for (let i = index - 1; i >= 0; index = i--) {
|
---|
| 171 | const current = line[i];
|
---|
| 172 | if (genColumn >= current[COLUMN])
|
---|
| 173 | break;
|
---|
| 174 | }
|
---|
| 175 | return index;
|
---|
| 176 | }
|
---|
| 177 | function insert(array, index, value) {
|
---|
| 178 | for (let i = array.length; i > index; i--) {
|
---|
| 179 | array[i] = array[i - 1];
|
---|
| 180 | }
|
---|
| 181 | array[index] = value;
|
---|
| 182 | }
|
---|
| 183 | function removeEmptyFinalLines(mappings) {
|
---|
| 184 | const { length } = mappings;
|
---|
| 185 | let len = length;
|
---|
| 186 | for (let i = len - 1; i >= 0; len = i, i--) {
|
---|
| 187 | if (mappings[i].length > 0)
|
---|
| 188 | break;
|
---|
| 189 | }
|
---|
| 190 | if (len < length)
|
---|
| 191 | mappings.length = len;
|
---|
| 192 | }
|
---|
| 193 | function putAll(setarr, array) {
|
---|
| 194 | for (let i = 0; i < array.length; i++)
|
---|
| 195 | setArray.put(setarr, array[i]);
|
---|
| 196 | }
|
---|
| 197 | function skipSourceless(line, index) {
|
---|
| 198 | // The start of a line is already sourceless, so adding a sourceless segment to the beginning
|
---|
| 199 | // doesn't generate any useful information.
|
---|
| 200 | if (index === 0)
|
---|
| 201 | return true;
|
---|
| 202 | const prev = line[index - 1];
|
---|
| 203 | // If the previous segment is also sourceless, then adding another sourceless segment doesn't
|
---|
| 204 | // genrate any new information. Else, this segment will end the source/named segment and point to
|
---|
| 205 | // a sourceless position, which is useful.
|
---|
| 206 | return prev.length === 1;
|
---|
| 207 | }
|
---|
| 208 | function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
|
---|
| 209 | // A source/named segment at the start of a line gives position at that genColumn
|
---|
| 210 | if (index === 0)
|
---|
| 211 | return false;
|
---|
| 212 | const prev = line[index - 1];
|
---|
| 213 | // If the previous segment is sourceless, then we're transitioning to a source.
|
---|
| 214 | if (prev.length === 1)
|
---|
| 215 | return false;
|
---|
| 216 | // If the previous segment maps to the exact same source position, then this segment doesn't
|
---|
| 217 | // provide any new position information.
|
---|
| 218 | return (sourcesIndex === prev[SOURCES_INDEX] &&
|
---|
| 219 | sourceLine === prev[SOURCE_LINE] &&
|
---|
| 220 | sourceColumn === prev[SOURCE_COLUMN] &&
|
---|
| 221 | namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
|
---|
| 222 | }
|
---|
| 223 | function addMappingInternal(skipable, map, mapping) {
|
---|
| 224 | const { generated, source, original, name, content } = mapping;
|
---|
| 225 | if (!source) {
|
---|
| 226 | return addSegmentInternal(skipable, map, generated.line - 1, generated.column, null, null, null, null, null);
|
---|
| 227 | }
|
---|
| 228 | return addSegmentInternal(skipable, map, generated.line - 1, generated.column, source, original.line - 1, original.column, name, content);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | exports.GenMapping = GenMapping;
|
---|
| 232 | exports.addMapping = addMapping;
|
---|
| 233 | exports.addSegment = addSegment;
|
---|
| 234 | exports.allMappings = allMappings;
|
---|
| 235 | exports.fromMap = fromMap;
|
---|
| 236 | exports.maybeAddMapping = maybeAddMapping;
|
---|
| 237 | exports.maybeAddSegment = maybeAddSegment;
|
---|
| 238 | exports.setIgnore = setIgnore;
|
---|
| 239 | exports.setSourceContent = setSourceContent;
|
---|
| 240 | exports.toDecodedMap = toDecodedMap;
|
---|
| 241 | exports.toEncodedMap = toEncodedMap;
|
---|
| 242 |
|
---|
| 243 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
| 244 |
|
---|
| 245 | }));
|
---|
| 246 | //# sourceMappingURL=gen-mapping.umd.js.map
|
---|