MCPcopy
hub / github.com/rollup/rollup / decodedSourcemap

Function decodedSourcemap

src/utils/decodedSourcemap.ts:62–106  ·  view source on GitHub ↗
(map: Input)

Source from the content-addressed store, hash-verified

60export function decodedSourcemap(map: Exclude<Input, null | undefined>): ExistingDecodedSourceMap;
61export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null;
62export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null {
63 if (!map) return null;
64
65 if (typeof map === 'string') {
66 map = JSON.parse(map) as ExistingRawSourceMap;
67 }
68 if (!map.mappings) {
69 return {
70 mappings: [],
71 names: [],
72 sources: [],
73 version: 3
74 };
75 }
76
77 const originalMappings = map.mappings;
78 const isAlreadyDecoded = Array.isArray(originalMappings);
79 const cache: CachedSourcemapData = {
80 decodedMappings: isAlreadyDecoded ? originalMappings : undefined,
81 encodedMappings: isAlreadyDecoded ? undefined : originalMappings
82 };
83
84 const decodedMap = {
85 ...(map as ExistingRawSourceMap | ExistingDecodedSourceMap),
86 // By moving mappings behind an accessor, we can avoid unneeded computation for cases
87 // where the mappings field is never actually accessed. This appears to greatly reduce
88 // the overhead of sourcemap decoding in terms of both compute time and memory usage.
89 get mappings() {
90 if (cache.decodedMappings) {
91 return cache.decodedMappings;
92 }
93 // If decodedMappings doesn't exist then encodedMappings should.
94 // The only scenario where cache.encodedMappings should be undefined is if the map
95 // this was constructed from was already decoded, or if mappings was set to a new
96 // decoded string. In either case, this line shouldn't get hit.
97 cache.decodedMappings = cache.encodedMappings ? decode(cache.encodedMappings) : [];
98 cache.encodedMappings = undefined;
99 return cache.decodedMappings;
100 }
101 };
102
103 sourceMapCache.set(decodedMap, cache);
104
105 return decodedMap;
106}

Callers 5

setSourceMethod · 0.90
collapseSourcemapFunction · 0.90
transformChunkFunction · 0.90
transformFunction · 0.90
transformReducerFunction · 0.90

Calls 1

setMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…