| 1 | import {
|
|---|
| 2 | AnyMap,
|
|---|
| 3 | originalPositionFor,
|
|---|
| 4 | generatedPositionFor,
|
|---|
| 5 | allGeneratedPositionsFor,
|
|---|
| 6 | eachMapping,
|
|---|
| 7 | encodedMappings,
|
|---|
| 8 | sourceContentFor,
|
|---|
| 9 | } from '@jridgewell/trace-mapping';
|
|---|
| 10 | import {
|
|---|
| 11 | GenMapping,
|
|---|
| 12 | maybeAddMapping,
|
|---|
| 13 | toDecodedMap,
|
|---|
| 14 | toEncodedMap,
|
|---|
| 15 | setSourceContent,
|
|---|
| 16 | fromMap,
|
|---|
| 17 | } from '@jridgewell/gen-mapping';
|
|---|
| 18 |
|
|---|
| 19 | import type {
|
|---|
| 20 | TraceMap,
|
|---|
| 21 | SourceMapInput,
|
|---|
| 22 | SectionedSourceMapInput,
|
|---|
| 23 | DecodedSourceMap,
|
|---|
| 24 | } from '@jridgewell/trace-mapping';
|
|---|
| 25 | export type { TraceMap, SourceMapInput, SectionedSourceMapInput, DecodedSourceMap };
|
|---|
| 26 |
|
|---|
| 27 | import type { Mapping, EncodedSourceMap } from '@jridgewell/gen-mapping';
|
|---|
| 28 | export type { Mapping, EncodedSourceMap };
|
|---|
| 29 |
|
|---|
| 30 | export class SourceMapConsumer {
|
|---|
| 31 | declare private _map: TraceMap;
|
|---|
| 32 | declare file: TraceMap['file'];
|
|---|
| 33 | declare names: TraceMap['names'];
|
|---|
| 34 | declare sourceRoot: TraceMap['sourceRoot'];
|
|---|
| 35 | declare sources: TraceMap['sources'];
|
|---|
| 36 | declare sourcesContent: TraceMap['sourcesContent'];
|
|---|
| 37 | declare version: TraceMap['version'];
|
|---|
| 38 |
|
|---|
| 39 | constructor(
|
|---|
| 40 | map: ConstructorParameters<typeof AnyMap>[0],
|
|---|
| 41 | mapUrl?: ConstructorParameters<typeof AnyMap>[1],
|
|---|
| 42 | ) {
|
|---|
| 43 | const trace = (this._map = new AnyMap(map, mapUrl));
|
|---|
| 44 |
|
|---|
| 45 | this.file = trace.file;
|
|---|
| 46 | this.names = trace.names;
|
|---|
| 47 | this.sourceRoot = trace.sourceRoot;
|
|---|
| 48 | this.sources = trace.resolvedSources;
|
|---|
| 49 | this.sourcesContent = trace.sourcesContent;
|
|---|
| 50 | this.version = trace.version;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | static fromSourceMap(map: SourceMapGenerator, mapUrl?: Parameters<typeof AnyMap>[1]) {
|
|---|
| 54 | // This is more performant if we receive
|
|---|
| 55 | // a @jridgewell/source-map SourceMapGenerator
|
|---|
| 56 | if (map.toDecodedMap) {
|
|---|
| 57 | return new SourceMapConsumer(map.toDecodedMap() as SectionedSourceMapInput, mapUrl);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | // This is a fallback for `source-map` and `source-map-js`
|
|---|
| 61 | return new SourceMapConsumer(map.toJSON() as SectionedSourceMapInput, mapUrl);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | get mappings(): string {
|
|---|
| 65 | return encodedMappings(this._map);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | originalPositionFor(
|
|---|
| 69 | needle: Parameters<typeof originalPositionFor>[1],
|
|---|
| 70 | ): ReturnType<typeof originalPositionFor> {
|
|---|
| 71 | return originalPositionFor(this._map, needle);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | generatedPositionFor(
|
|---|
| 75 | originalPosition: Parameters<typeof generatedPositionFor>[1],
|
|---|
| 76 | ): ReturnType<typeof generatedPositionFor> {
|
|---|
| 77 | return generatedPositionFor(this._map, originalPosition);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | allGeneratedPositionsFor(
|
|---|
| 81 | originalPosition: Parameters<typeof generatedPositionFor>[1],
|
|---|
| 82 | ): ReturnType<typeof generatedPositionFor>[] {
|
|---|
| 83 | return allGeneratedPositionsFor(this._map, originalPosition);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | hasContentsOfAllSources(): boolean {
|
|---|
| 87 | if (!this.sourcesContent || this.sourcesContent.length !== this.sources.length) {
|
|---|
| 88 | return false;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | for (const content of this.sourcesContent) {
|
|---|
| 92 | if (content == null) {
|
|---|
| 93 | return false;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | return true;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | sourceContentFor(source: string, nullOnMissing?: boolean): string | null {
|
|---|
| 101 | const sourceContent = sourceContentFor(this._map, source);
|
|---|
| 102 | if (sourceContent != null) {
|
|---|
| 103 | return sourceContent;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | if (nullOnMissing) {
|
|---|
| 107 | return null;
|
|---|
| 108 | }
|
|---|
| 109 | throw new Error(`"${source}" is not in the SourceMap.`);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | eachMapping(
|
|---|
| 113 | callback: Parameters<typeof eachMapping>[1],
|
|---|
| 114 | context?: any /*, order?: number*/,
|
|---|
| 115 | ): void {
|
|---|
| 116 | // order is ignored as @jridgewell/trace-map doesn't implement it
|
|---|
| 117 | eachMapping(this._map, context ? callback.bind(context) : callback);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | destroy() {
|
|---|
| 121 | // noop.
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | export class SourceMapGenerator {
|
|---|
| 126 | declare private _map: GenMapping;
|
|---|
| 127 |
|
|---|
| 128 | constructor(opts: ConstructorParameters<typeof GenMapping>[0] | GenMapping) {
|
|---|
| 129 | // TODO :: should this be duck-typed ?
|
|---|
| 130 | this._map = opts instanceof GenMapping ? opts : new GenMapping(opts);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | static fromSourceMap(consumer: SourceMapConsumer) {
|
|---|
| 134 | return new SourceMapGenerator(fromMap(consumer));
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | addMapping(mapping: Parameters<typeof maybeAddMapping>[1]): ReturnType<typeof maybeAddMapping> {
|
|---|
| 138 | maybeAddMapping(this._map, mapping);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | setSourceContent(
|
|---|
| 142 | source: Parameters<typeof setSourceContent>[1],
|
|---|
| 143 | content: Parameters<typeof setSourceContent>[2],
|
|---|
| 144 | ): ReturnType<typeof setSourceContent> {
|
|---|
| 145 | setSourceContent(this._map, source, content);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | toJSON(): ReturnType<typeof toEncodedMap> {
|
|---|
| 149 | return toEncodedMap(this._map);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | toString(): string {
|
|---|
| 153 | return JSON.stringify(this.toJSON());
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | toDecodedMap(): ReturnType<typeof toDecodedMap> {
|
|---|
| 157 | return toDecodedMap(this._map);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|