magic-string.es.d.mts 9.9 KB

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