UNPKG

4.7 kBTypeScriptView Raw
1/**
2 * Converts a source-map from/to different formats and allows adding/changing properties.
3 * (documentation based on project's README file)
4 */
5export interface SourceMapConverter {
6 /** The parsed sourcemap object */
7 sourcemap: any;
8
9 /** Returns a copy of the underlying source map */
10 toObject(): any;
11
12 /** Converts source map to json string. If space is given (optional), this will be passed to JSON.stringify when the JSON string is generated */
13 toJSON(space?: string | number): string;
14
15 /** Converts source map to uri encoded json string */
16 toURI(): string;
17
18 /** Converts source map to base64 encoded json string */
19 toBase64(): string;
20
21 /**
22 * Converts source map to an inline comment that can be appended to the source-file.
23 * By default, the comment is formatted like: //# sourceMappingURL=..., which you would normally see in a JS source file.
24 * When `options.encoding == 'uri'`, the data will be uri encoded, otherwise they will be base64 encoded.
25 * When `options.multiline == true`, the comment is formatted like: /*# sourceMappingURL=... *\/, which you would find in a CSS source file
26 */
27 toComment(options?: { multiline?: boolean | undefined; encoding?: "uri" | undefined }): string;
28
29 /** Adds given property to the source map. Throws an error if property already exists */
30 addProperty(key: string, value: any): SourceMapConverter;
31
32 /** Sets given property to the source map. If property doesn't exist it is added, otherwise its value is updated */
33 setProperty(key: string, value: any): SourceMapConverter;
34
35 /** Gets given property of the source map */
36 getProperty(key: string): any;
37}
38
39/** Returns source map converter from given object */
40export function fromObject(obj: any): SourceMapConverter;
41
42/** Returns source map converter from given json string */
43export function fromJSON(json: string): SourceMapConverter;
44
45/** Returns source map converter from given uri encoded json string */
46export function fromURI(uri: string): SourceMapConverter;
47
48/** Returns source map converter from given base64 encoded json string */
49export function fromBase64(base64: string): SourceMapConverter;
50
51/** Returns source map converter from given base64 or uri encoded json string prefixed with //# sourceMappingURL=... */
52export function fromComment(comment: string): SourceMapConverter;
53
54/**
55 * Returns source map converter from given filename by parsing //# sourceMappingURL=filename.
56 * filename must point to a file that is found inside the mapFileDir. Most tools store this file right next to the generated file, i.e. the one containing the source map.
57 */
58export function fromMapFileComment(comment: string, readMap: (filename: string) => string): SourceMapConverter;
59export function fromMapFileComment(
60 comment: string,
61 readMap: (filename: string) => Promise<string>,
62): Promise<SourceMapConverter>;
63
64/**
65 * Finds last sourcemap comment in file and returns source map converter or returns `null` if no source map comment was found.
66 */
67export function fromSource(content: string): SourceMapConverter | null;
68
69/**
70 * Finds last sourcemap comment in file and returns source map converter or returns `null` if no source map comment was found.
71 * The sourcemap will be read from the map file found by parsing # sourceMappingURL=file comment. For more info see fromMapFileComment.
72 */
73export function fromMapFileSource(content: string, readMap: (filename: string) => string): SourceMapConverter | null;
74export function fromMapFileSource(
75 content: string,
76 readMap: (filename: string) => Promise<string>,
77): Promise<SourceMapConverter | null>;
78
79/** Returns src with all source map comments removed */
80export function removeComments(src: string): string;
81
82/** Returns src with all source map comments pointing to map files removed */
83export function removeMapFileComments(src: string): string;
84
85/**
86 * Returns a new regex used to find source map comments
87 *
88 * Breaks down a source map comment into groups: Groups: 1: media type, 2: MIME type, 3: charset, 4: encoding, 5: data.
89 */
90export const commentRegex: RegExp;
91
92/** Returns a new regex used to find source map comments pointing to map files */
93export const mapFileCommentRegex: RegExp;
94
95/**
96 * Returns a comment that links to an external source map via file.
97 * By default, the comment is formatted like: //# sourceMappingURL=..., which you would normally see in a JS source file.
98 * When options.multiline == true, the comment is formatted like: /*# sourceMappingURL=... *\/, which you would find in a CSS source file.
99 */
100export function generateMapFileComment(file: string, options?: { multiline?: boolean | undefined }): string;