1 | import { AnyMap, encodedMappings, originalPositionFor, generatedPositionFor, allGeneratedPositionsFor, sourceContentFor, eachMapping } from '@jridgewell/trace-mapping';
|
---|
2 | import { GenMapping, fromMap, maybeAddMapping, setSourceContent, toEncodedMap, toDecodedMap } from '@jridgewell/gen-mapping';
|
---|
3 |
|
---|
4 | class SourceMapConsumer {
|
---|
5 | constructor(map, mapUrl) {
|
---|
6 | const trace = (this._map = new AnyMap(map, mapUrl));
|
---|
7 | this.file = trace.file;
|
---|
8 | this.names = trace.names;
|
---|
9 | this.sourceRoot = trace.sourceRoot;
|
---|
10 | this.sources = trace.resolvedSources;
|
---|
11 | this.sourcesContent = trace.sourcesContent;
|
---|
12 | this.version = trace.version;
|
---|
13 | }
|
---|
14 | static fromSourceMap(map, mapUrl) {
|
---|
15 | // This is more performant if we receive
|
---|
16 | // a @jridgewell/source-map SourceMapGenerator
|
---|
17 | if (map.toDecodedMap) {
|
---|
18 | return new SourceMapConsumer(map.toDecodedMap(), mapUrl);
|
---|
19 | }
|
---|
20 | // This is a fallback for `source-map` and `source-map-js`
|
---|
21 | return new SourceMapConsumer(map.toJSON(), mapUrl);
|
---|
22 | }
|
---|
23 | get mappings() {
|
---|
24 | return encodedMappings(this._map);
|
---|
25 | }
|
---|
26 | originalPositionFor(needle) {
|
---|
27 | return originalPositionFor(this._map, needle);
|
---|
28 | }
|
---|
29 | generatedPositionFor(originalPosition) {
|
---|
30 | return generatedPositionFor(this._map, originalPosition);
|
---|
31 | }
|
---|
32 | allGeneratedPositionsFor(originalPosition) {
|
---|
33 | return allGeneratedPositionsFor(this._map, originalPosition);
|
---|
34 | }
|
---|
35 | hasContentsOfAllSources() {
|
---|
36 | if (!this.sourcesContent || this.sourcesContent.length !== this.sources.length) {
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 | for (const content of this.sourcesContent) {
|
---|
40 | if (content == null) {
|
---|
41 | return false;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 | sourceContentFor(source, nullOnMissing) {
|
---|
47 | const sourceContent = sourceContentFor(this._map, source);
|
---|
48 | if (sourceContent != null) {
|
---|
49 | return sourceContent;
|
---|
50 | }
|
---|
51 | if (nullOnMissing) {
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 | throw new Error(`"${source}" is not in the SourceMap.`);
|
---|
55 | }
|
---|
56 | eachMapping(callback, context /*, order?: number*/) {
|
---|
57 | // order is ignored as @jridgewell/trace-map doesn't implement it
|
---|
58 | eachMapping(this._map, context ? callback.bind(context) : callback);
|
---|
59 | }
|
---|
60 | destroy() {
|
---|
61 | // noop.
|
---|
62 | }
|
---|
63 | }
|
---|
64 | class SourceMapGenerator {
|
---|
65 | constructor(opts) {
|
---|
66 | // TODO :: should this be duck-typed ?
|
---|
67 | this._map = opts instanceof GenMapping ? opts : new GenMapping(opts);
|
---|
68 | }
|
---|
69 | static fromSourceMap(consumer) {
|
---|
70 | return new SourceMapGenerator(fromMap(consumer));
|
---|
71 | }
|
---|
72 | addMapping(mapping) {
|
---|
73 | maybeAddMapping(this._map, mapping);
|
---|
74 | }
|
---|
75 | setSourceContent(source, content) {
|
---|
76 | setSourceContent(this._map, source, content);
|
---|
77 | }
|
---|
78 | toJSON() {
|
---|
79 | return toEncodedMap(this._map);
|
---|
80 | }
|
---|
81 | toString() {
|
---|
82 | return JSON.stringify(this.toJSON());
|
---|
83 | }
|
---|
84 | toDecodedMap() {
|
---|
85 | return toDecodedMap(this._map);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | export { SourceMapConsumer, SourceMapGenerator };
|
---|
90 | //# sourceMappingURL=source-map.mjs.map
|
---|