magic-string.cjs.d.ts 9.8 KB

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