magic-string.cjs.d.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. debugId?: string;
  52. /**
  53. * Returns the equivalent of `JSON.stringify(map)`
  54. */
  55. toString(): string;
  56. /**
  57. * Returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
  58. * `generateMap(options?: SourceMapOptions): SourceMap;`
  59. */
  60. toUrl(): string;
  61. }
  62. export class Bundle {
  63. constructor(options?: BundleOptions);
  64. /**
  65. * Adds the specified source to the bundle, which can either be a `MagicString` object directly,
  66. * or an options object that holds a magic string `content` property and optionally provides
  67. * a `filename` for the source within the bundle, as well as an optional `ignoreList` hint
  68. * (which defaults to `false`). The `filename` is used when constructing the source map for the
  69. * bundle, to identify this `source` in the source map's `sources` field. The `ignoreList` hint
  70. * is used to populate the `x_google_ignoreList` extension field in the source map, which is a
  71. * mechanism for tools to signal to debuggers that certain sources should be ignored by default
  72. * (depending on user preferences).
  73. */
  74. addSource(source: MagicString | { filename?: string, content: MagicString, ignoreList?: boolean }): this;
  75. append(str: string, options?: BundleOptions): this;
  76. clone(): this;
  77. generateMap(options?: SourceMapOptions): Omit<SourceMap, 'sourcesContent'> & { sourcesContent: Array<string | null> };
  78. generateDecodedMap(options?: SourceMapOptions): Omit<DecodedSourceMap, 'sourcesContent'> & { sourcesContent: Array<string | null> };
  79. getIndentString(): string;
  80. indent(indentStr?: string): this;
  81. indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
  82. prepend(str: string): this;
  83. toString(): string;
  84. trimLines(): this;
  85. trim(charType?: string): this;
  86. trimStart(charType?: string): this;
  87. trimEnd(charType?: string): this;
  88. isEmpty(): boolean;
  89. length(): number;
  90. }
  91. export type ExclusionRange = [ number, number ];
  92. export interface MagicStringOptions {
  93. filename?: string,
  94. indentExclusionRanges?: ExclusionRange | Array<ExclusionRange>;
  95. }
  96. export interface IndentOptions {
  97. exclude?: ExclusionRange | Array<ExclusionRange>;
  98. indentStart?: boolean;
  99. }
  100. export interface OverwriteOptions {
  101. storeName?: boolean;
  102. contentOnly?: boolean;
  103. }
  104. export interface UpdateOptions {
  105. storeName?: boolean;
  106. overwrite?: boolean;
  107. }
  108. export default class MagicString {
  109. constructor(str: string, options?: MagicStringOptions);
  110. /**
  111. * Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
  112. */
  113. addSourcemapLocation(char: number): void;
  114. /**
  115. * Appends the specified content to the end of the string.
  116. */
  117. append(content: string): this;
  118. /**
  119. * Appends the specified content at the index in the original string.
  120. * If a range *ending* with index is subsequently moved, the insert will be moved with it.
  121. * See also `s.prependLeft(...)`.
  122. */
  123. appendLeft(index: number, content: string): this;
  124. /**
  125. * Appends the specified content at the index in the original string.
  126. * If a range *starting* with index is subsequently moved, the insert will be moved with it.
  127. * See also `s.prependRight(...)`.
  128. */
  129. appendRight(index: number, content: string): this;
  130. /**
  131. * Does what you'd expect.
  132. */
  133. clone(): this;
  134. /**
  135. * Generates a version 3 sourcemap.
  136. */
  137. generateMap(options?: SourceMapOptions): SourceMap;
  138. /**
  139. * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
  140. * Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
  141. */
  142. generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
  143. getIndentString(): string;
  144. /**
  145. * Prefixes each line of the string with prefix.
  146. * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
  147. */
  148. indent(options?: IndentOptions): this;
  149. /**
  150. * Prefixes each line of the string with prefix.
  151. * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
  152. *
  153. * The options argument can have an exclude property, which is an array of [start, end] character ranges.
  154. * These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.
  155. */
  156. indent(indentStr?: string, options?: IndentOptions): this;
  157. indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
  158. /**
  159. * Moves the characters from `start` and `end` to `index`.
  160. */
  161. move(start: number, end: number, index: number): this;
  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): this;
  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): this;
  181. /**
  182. * Prepends the string with the specified content.
  183. */
  184. prepend(content: string): this;
  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): this;
  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): this;
  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): this;
  198. /**
  199. * Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).
  200. */
  201. reset(start: number, end: number): this;
  202. /**
  203. * Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
  204. * Throws error if the indices are for characters that were already removed.
  205. */
  206. slice(start: number, end: number): string;
  207. /**
  208. * Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
  209. */
  210. snip(start: number, end: number): this;
  211. /**
  212. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
  213. */
  214. trim(charType?: string): this;
  215. /**
  216. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
  217. */
  218. trimStart(charType?: string): this;
  219. /**
  220. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
  221. */
  222. trimEnd(charType?: string): this;
  223. /**
  224. * Removes empty lines from the start and end.
  225. */
  226. trimLines(): this;
  227. /**
  228. * String replacement with RegExp or string.
  229. */
  230. replace(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): this;
  231. /**
  232. * Same as `s.replace`, but replace all matched strings instead of just one.
  233. */
  234. replaceAll(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): this;
  235. lastChar(): string;
  236. lastLine(): string;
  237. /**
  238. * Returns true if the resulting source is empty (disregarding white space).
  239. */
  240. isEmpty(): boolean;
  241. length(): number;
  242. /**
  243. * Indicates if the string has been changed.
  244. */
  245. hasChanged(): boolean;
  246. original: string;
  247. /**
  248. * Returns the generated string.
  249. */
  250. toString(): string;
  251. }