index.d.ts 9.6 KB

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