(map: Input)
| 60 | export function decodedSourcemap(map: Exclude<Input, null | undefined>): ExistingDecodedSourceMap; |
| 61 | export function decodedSourcemap(map: Input): ExistingDecodedSourceMap | null; |
| 62 | export 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 | } |
no test coverage detected
searching dependent graphs…