UNPKG

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