36b8361e596c31fb75a159ddc8ab9945ab7a72c50743a9999c8314520f5b0f3c56d7146ec56fb1afdcf8d4c97e277d0861108c79856d82b48800c542869f04 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 interface UpdateOptions {
  90. storeName?: boolean;
  91. overwrite?: boolean;
  92. }
  93. export default class MagicString {
  94. constructor(str: string, options?: MagicStringOptions);
  95. /**
  96. * Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
  97. */
  98. addSourcemapLocation(char: number): void;
  99. /**
  100. * Appends the specified content to the end of the string.
  101. */
  102. append(content: string): MagicString;
  103. /**
  104. * Appends the specified content at the index in the original string.
  105. * If a range *ending* with index is subsequently moved, the insert will be moved with it.
  106. * See also `s.prependLeft(...)`.
  107. */
  108. appendLeft(index: number, content: string): MagicString;
  109. /**
  110. * Appends the specified content at the index in the original string.
  111. * If a range *starting* with index is subsequently moved, the insert will be moved with it.
  112. * See also `s.prependRight(...)`.
  113. */
  114. appendRight(index: number, content: string): MagicString;
  115. /**
  116. * Does what you'd expect.
  117. */
  118. clone(): MagicString;
  119. /**
  120. * Generates a version 3 sourcemap.
  121. */
  122. generateMap(options?: SourceMapOptions): SourceMap;
  123. /**
  124. * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
  125. * Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
  126. */
  127. generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
  128. getIndentString(): string;
  129. /**
  130. * Prefixes each line of the string with prefix.
  131. * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
  132. */
  133. indent(options?: IndentOptions): MagicString;
  134. /**
  135. * Prefixes each line of the string with prefix.
  136. * If prefix is not supplied, the indentation will be guessed from the original content, falling back to a single tab character.
  137. *
  138. * The options argument can have an exclude property, which is an array of [start, end] character ranges.
  139. * These ranges will be excluded from the indentation - useful for (e.g.) multiline strings.
  140. */
  141. indent(indentStr?: string, options?: IndentOptions): MagicString;
  142. indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
  143. /**
  144. * Moves the characters from `start and `end` to `index`.
  145. */
  146. move(start: number, end: number, index: number): MagicString;
  147. /**
  148. * Replaces the characters from `start` to `end` with `content`, along with the appended/prepended content in
  149. * that range. The same restrictions as `s.remove()` apply.
  150. *
  151. * The fourth argument is optional. It can have a storeName property — if true, the original name will be stored
  152. * for later inclusion in a sourcemap's names array — and a contentOnly property which determines whether only
  153. * the content is overwritten, or anything that was appended/prepended to the range as well.
  154. *
  155. * It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
  156. */
  157. overwrite(start: number, end: number, content: string, options?: boolean | OverwriteOptions): MagicString;
  158. /**
  159. * Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply.
  160. *
  161. * The fourth argument is optional. It can have a storeName property — if true, the original name will be stored
  162. * for later inclusion in a sourcemap's names array — and an overwrite property which determines whether only
  163. * the content is overwritten, or anything that was appended/prepended to the range as well.
  164. */
  165. update(start: number, end: number, content: string, options?: boolean | UpdateOptions): MagicString;
  166. /**
  167. * Prepends the string with the specified content.
  168. */
  169. prepend(content: string): MagicString;
  170. /**
  171. * Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
  172. */
  173. prependLeft(index: number, content: string): MagicString;
  174. /**
  175. * Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
  176. */
  177. prependRight(index: number, content: string): MagicString;
  178. /**
  179. * Removes the characters from `start` to `end` (of the original string, **not** the generated string).
  180. * Removing the same content twice, or making removals that partially overlap, will cause an error.
  181. */
  182. remove(start: number, end: number): MagicString;
  183. /**
  184. * Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
  185. * Throws error if the indices are for characters that were already removed.
  186. */
  187. slice(start: number, end: number): string;
  188. /**
  189. * Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
  190. */
  191. snip(start: number, end: number): MagicString;
  192. /**
  193. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
  194. */
  195. trim(charType?: string): MagicString;
  196. /**
  197. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
  198. */
  199. trimStart(charType?: string): MagicString;
  200. /**
  201. * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
  202. */
  203. trimEnd(charType?: string): MagicString;
  204. /**
  205. * Removes empty lines from the start and end.
  206. */
  207. trimLines(): MagicString;
  208. /**
  209. * String replacement with RegExp or string.
  210. */
  211. replace(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): MagicString;
  212. /**
  213. * Same as `s.replace`, but replace all matched strings instead of just one.
  214. */
  215. replaceAll(regex: RegExp | string, replacement: string | ((substring: string, ...args: any[]) => string)): MagicString;
  216. lastChar(): string;
  217. lastLine(): string;
  218. /**
  219. * Returns true if the resulting source is empty (disregarding white space).
  220. */
  221. isEmpty(): boolean;
  222. length(): number;
  223. /**
  224. * Indicates if the string has been changed.
  225. */
  226. hasChanged(): boolean;
  227. original: string;
  228. /**
  229. * Returns the generated string.
  230. */
  231. toString(): string;
  232. }