Skip to content

Commit

Permalink
Add decodedMap and encodedMap helpers
Browse files Browse the repository at this point in the history
These allow you to generate a SourceMap object for using in another library, or for
stringifying/base64 and saving into a file.
  • Loading branch information
jridgewell committed Apr 20, 2022
1 parent 7ac8154 commit 0f543e6
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/trace-mapping.ts
Expand Up @@ -124,6 +124,20 @@ export let eachMapping: (map: TraceMap, cb: (mapping: EachMapping) => void) => v
*/
export let presortedDecodedMap: (map: DecodedSourceMap, mapUrl?: string) => TraceMap;

/**
* Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
* a sourcemap, or to JSON.stringify.
*/
export let decodedMap: (
map: TraceMap,
) => Omit<DecodedSourceMap, 'mappings'> & { mappings: readonly SourceMapSegment[][] };

/**
* Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
* a sourcemap, or to JSON.stringify.
*/
export let encodedMap: (map: TraceMap) => EncodedSourceMap;

export { AnyMap } from './any-map';

export class TraceMap implements SourceMap {
Expand Down Expand Up @@ -304,6 +318,30 @@ export class TraceMap implements SourceMap {
tracer._decoded = map.mappings;
return tracer;
};

decodedMap = (map) => {
return {
version: 3,
file: map.file,
names: map.names,
sourceRoot: map.sourceRoot,
sources: map.sources,
sourcesContent: map.sourcesContent,
mappings: decodedMappings(map),
};
};

encodedMap = (map) => {
return {
version: 3,
file: map.file,
names: map.names,
sourceRoot: map.sourceRoot,
sources: map.sources,
sourcesContent: map.sourcesContent,
mappings: encodedMappings(map),
};
};
}
}

Expand Down

0 comments on commit 0f543e6

Please sign in to comment.