index.d.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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[];
  36. names: string[];
  37. mappings: SourceMapSegment[][];
  38. }
  39. export class SourceMap {
  40. constructor(properties: DecodedSourceMap);
  41. version: number;
  42. file: string;
  43. sources: string[];
  44. sourcesContent: string[];
  45. names: string[];
  46. mappings: string;
  47. /**
  48. * Returns the equivalent of `JSON.stringify(map)`
  49. */
  50. toString(): string;
  51. /**
  52. * Returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
  53. * `generateMap(options?: SourceMapOptions): SourceMap;`
  54. */
  55. toUrl(): string;
  56. }
  57. export class Bundle {
  58. constructor(options?: BundleOptions);
  59. addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle;
  60. append(str: string, options?: BundleOptions): Bundle;
  61. clone(): Bundle;
  62. generateMap(options?: SourceMapOptions): SourceMap;
  63. generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
  64. getIndentString(): string;
  65. indent(indentStr?: string): Bundle;
  66. indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
  67. prepend(str: string): Bundle;
  68. toString(): string;
  69. trimLines(): Bundle;
  70. trim(charType?: string): Bundle;
  71. trimStart(charType?: string): Bundle;
  72. trimEnd(charType?: string): Bundle;
  73. isEmpty(): boolean;
  74. length(): number;
  75. }
  76. export type ExclusionRange = [ number, number ];
  77. export interface MagicStringOptions {
  78. filename?: string,
  79. indentExclusionRanges?: ExclusionRange | Array<ExclusionRange>;
  80. }
  81. export interface IndentOptions {
  82. exclude?: ExclusionRange | Array<ExclusionRange>;
  83. indentStart?: boolean;
  84. }
  85. export interface OverwriteOptions {
  86. storeName?: boolean;
  87. contentOnly?: boolean;
  88. }
  89. export default class MagicString {
  90. constructor(str: string, options?: MagicStringOptions);
  91. /**
  92. * Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
  93. */
  94. addSourcemapLocation(char: number): void;
  95. /**
  96. * Appends the specified content to the end of the string.
  97. */
  98. append(content: string): MagicString;
  99. /**
  100. * Appends the specified content at the index in the original string.
  101. * If a range *ending* with index is subsequently moved, the insert will be moved with it.
  102. * See also `s.prependLeft(...)`.
  103. */
  104. appendLeft(index: number, content: string): MagicString;
  105. /**
  106. * Appends the specified content at the index in the original string.
  107. * If a range *starting* with index is subsequently moved, the insert will be moved with it.
  108. * See also `s.prependRight(...)`.
  109. */
  110. appendRight(index: number, content: string): MagicString;
  111. /**
  112. * Does what you'd expect.
  113. */
  114. clone(): MagicString;
  115. /**
  116. * Generates a version 3 sourcemap.
  117. */
  118. generateMap(options?: SourceMapOptions): SourceMap;
  119. /**
  120. * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
  121. * Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
  122. */
  123. generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
  124. getIndentString(): string;
  125. /**
  126. * Prefixes each line of the string with prefix.
  127. * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
  128. */
  129. indent(options?: IndentOptions): MagicString;
  130. /**
  131. * Prefixes each line of the string with prefix.
  132. * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
  133. *
  134. * The options argument can have an exclude property, which is an array of [start, end] character ranges.
  135. * These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.
  136. */
  137. indent(indentStr?: string, options?: IndentOptions): MagicString;
  138. indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
  139. /**
  140. * Moves the characters from `start and `end` to `index`.
  141. */
  142. move(start: number, end: number, index: number): MagicString;
  143. /**
  144. * Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply.
  145. *
  146. * The fourth argument is optional. It can have a storeName property — if true, the original name will be stored
  147. * for later inclusion in a sourcemap's names array — and a contentOnly property which determines whether only
  148. * the content is overwritten, or anything that was appended/prepended to the range as well.
  149. */
  150. overwrite(start: number, end: number, content: string, options?: boolean | OverwriteOptions): MagicString;
  151. /**
  152. * Prepends the string with the specified content.
  153. */
  154. prepend(content: string): MagicString;
  155. /**
  156. * Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
  157. */
  158. prependLeft(index: number, content: string): MagicString;
  159. /**
  160. * Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
  161. */
  162. prependRight(index: number, content: string): MagicString;
  163. /**
  164. * Removes the characters from `start` to `end` (of the original string, **not** the generated string).
  165. * Removing the same content twice, or making removals that partially overlap, will cause an error.
  166. */
  167. remove(start: number, end: number): MagicString;
  168. /**
  169. * Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
  170. * Throws error if the indices are for characters that were already removed.
  171. */
  172. slice(start: number, end: number): string;
  173. /**
  174. * Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
  175. */
  176. snip(start: number, end: number): MagicString;
  177. /**
  178. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
  179. */
  180. trim(charType?: string): MagicString;
  181. /**
  182. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
  183. */
  184. trimStart(charType?: string): MagicString;
  185. /**
  186. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
  187. */
  188. trimEnd(charType?: string): MagicString;
  189. /**
  190. * Removes empty lines from the start and end.
  191. */
  192. trimLines(): MagicString;
  193. lastChar(): string;
  194. lastLine(): string;
  195. /**
  196. * Returns true if the resulting source is empty (disregarding white space).
  197. */
  198. isEmpty(): boolean;
  199. length(): number;
  200. original: string;
  201. /**
  202. * Returns the generated string.
  203. */
  204. toString(): string;
  205. }