UNPKG

8.98 kBTypeScriptView Raw
1export interface BundleOptions {
2 intro?: string;
3 separator?: string;
4}
5
6export interface SourceMapOptions {
7 /**
8 * Whether the mapping should be high-resolution.
9 * Hi-res mappings map every single character, meaning (for example) your devtools will always
10 * be able to pinpoint the exact location of function calls and so on.
11 * With lo-res mappings, devtools may only be able to identify the correct
12 * line - but they're quicker to generate and less bulky.
13 * If sourcemap locations have been specified with s.addSourceMapLocation(), they will be used here.
14 */
15 hires?: boolean;
16 /**
17 * The filename where you plan to write the sourcemap.
18 */
19 file?: string;
20 /**
21 * The filename of the file containing the original source.
22 */
23 source?: string;
24 /**
25 * Whether to include the original content in the map's sourcesContent array.
26 */
27 includeContent?: boolean;
28}
29
30export type SourceMapSegment =
31 | [number]
32 | [number, number, number, number]
33 | [number, number, number, number, number];
34
35export interface DecodedSourceMap {
36 file: string;
37 sources: string[];
38 sourcesContent: string[];
39 names: string[];
40 mappings: SourceMapSegment[][];
41}
42
43export class SourceMap {
44 constructor(properties: DecodedSourceMap);
45
46 version: number;
47 file: string;
48 sources: string[];
49 sourcesContent: string[];
50 names: string[];
51 mappings: string;
52
53 /**
54 * Returns the equivalent of `JSON.stringify(map)`
55 */
56 toString(): string;
57 /**
58 * Returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
59 * `generateMap(options?: SourceMapOptions): SourceMap;`
60 */
61 toUrl(): string;
62}
63
64export class Bundle {
65 constructor(options?: BundleOptions);
66 addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle;
67 append(str: string, options?: BundleOptions): Bundle;
68 clone(): Bundle;
69 generateMap(options?: SourceMapOptions): SourceMap;
70 generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
71 getIndentString(): string;
72 indent(indentStr?: string): Bundle;
73 indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
74 prepend(str: string): Bundle;
75 toString(): string;
76 trimLines(): Bundle;
77 trim(charType?: string): Bundle;
78 trimStart(charType?: string): Bundle;
79 trimEnd(charType?: string): Bundle;
80 isEmpty(): boolean;
81 length(): number;
82}
83
84export type ExclusionRange = [ number, number ];
85
86export interface MagicStringOptions {
87 filename?: string,
88 indentExclusionRanges?: ExclusionRange | Array<ExclusionRange>;
89}
90
91export interface IndentOptions {
92 exclude?: ExclusionRange | Array<ExclusionRange>;
93 indentStart?: boolean;
94}
95
96export interface OverwriteOptions {
97 storeName?: boolean;
98 contentOnly?: boolean;
99}
100
101export interface UpdateOptions {
102 storeName?: boolean;
103 overwrite?: boolean;
104}
105
106export default class MagicString {
107 constructor(str: string, options?: MagicStringOptions);
108 /**
109 * Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
110 */
111 addSourcemapLocation(char: number): void;
112 /**
113 * Appends the specified content to the end of the string.
114 */
115 append(content: string): MagicString;
116 /**
117 * Appends the specified content at the index in the original string.
118 * If a range *ending* with index is subsequently moved, the insert will be moved with it.
119 * See also `s.prependLeft(...)`.
120 */
121 appendLeft(index: number, content: string): MagicString;
122 /**
123 * Appends the specified content at the index in the original string.
124 * If a range *starting* with index is subsequently moved, the insert will be moved with it.
125 * See also `s.prependRight(...)`.
126 */
127 appendRight(index: number, content: string): MagicString;
128 /**
129 * Does what you'd expect.
130 */
131 clone(): MagicString;
132 /**
133 * Generates a version 3 sourcemap.
134 */
135 generateMap(options?: SourceMapOptions): SourceMap;
136 /**
137 * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
138 * Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
139 */
140 generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
141 getIndentString(): string;
142
143 /**
144 * Prefixes each line of the string with prefix.
145 * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
146 */
147 indent(options?: IndentOptions): MagicString;
148 /**
149 * Prefixes each line of the string with prefix.
150 * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
151 *
152 * The options argument can have an exclude property, which is an array of [start, end] character ranges.
153 * These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.
154 */
155 indent(indentStr?: string, options?: IndentOptions): MagicString;
156 indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
157
158 /**
159 * Moves the characters from `start and `end` to `index`.
160 */
161 move(start: number, end: number, index: number): MagicString;
162 /**
163 * Replaces the characters from `start` to `end` with `content`, along with the appended/prepended content in
164 * that range. The same restrictions as `s.remove()` apply.
165 *
166 * The fourth argument is optional. It can have a storeName property — if true, the original name will be stored
167 * for later inclusion in a sourcemap's names array — and a contentOnly property which determines whether only
168 * the content is overwritten, or anything that was appended/prepended to the range as well.
169 *
170 * It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
171 */
172 overwrite(start: number, end: number, content: string, options?: boolean | OverwriteOptions): MagicString;
173 /**
174 * Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply.
175 *
176 * The fourth argument is optional. It can have a storeName property — if true, the original name will be stored
177 * for later inclusion in a sourcemap's names array — and an overwrite property which determines whether only
178 * the content is overwritten, or anything that was appended/prepended to the range as well.
179 */
180 update(start: number, end: number, content: string, options?: boolean | UpdateOptions): MagicString;
181 /**
182 * Prepends the string with the specified content.
183 */
184 prepend(content: string): MagicString;
185 /**
186 * Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
187 */
188 prependLeft(index: number, content: string): MagicString;
189 /**
190 * Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
191 */
192 prependRight(index: number, content: string): MagicString;
193 /**
194 * Removes the characters from `start` to `end` (of the original string, **not** the generated string).
195 * Removing the same content twice, or making removals that partially overlap, will cause an error.
196 */
197 remove(start: number, end: number): MagicString;
198 /**
199 * Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
200 * Throws error if the indices are for characters that were already removed.
201 */
202 slice(start: number, end: number): string;
203 /**
204 * Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
205 */
206 snip(start: number, end: number): MagicString;
207 /**
208 * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
209 */
210 trim(charType?: string): MagicString;
211 /**
212 * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
213 */
214 trimStart(charType?: string): MagicString;
215 /**
216 * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
217 */
218 trimEnd(charType?: string): MagicString;
219 /**
220 * Removes empty lines from the start and end.
221 */
222 trimLines(): MagicString;
223 /**
224 * String replacement with RegExp or string.
225 */
226 replace(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): MagicString;
227 /**
228 * Same as `s.replace`, but replace all matched strings instead of just one.
229 */
230 replaceAll(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): MagicString;
231
232 lastChar(): string;
233 lastLine(): string;
234 /**
235 * Returns true if the resulting source is empty (disregarding white space).
236 */
237 isEmpty(): boolean;
238 length(): number;
239
240 /**
241 * Indicates if the string has been changed.
242 */
243 hasChanged(): boolean;
244
245 original: string;
246 /**
247 * Returns the generated string.
248 */
249 toString(): string;
250}