magic-string.es.mjs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. import { encode } from '@jridgewell/sourcemap-codec';
  2. class BitSet {
  3. constructor(arg) {
  4. this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
  5. }
  6. add(n) {
  7. this.bits[n >> 5] |= 1 << (n & 31);
  8. }
  9. has(n) {
  10. return !!(this.bits[n >> 5] & (1 << (n & 31)));
  11. }
  12. }
  13. class Chunk {
  14. constructor(start, end, content) {
  15. this.start = start;
  16. this.end = end;
  17. this.original = content;
  18. this.intro = '';
  19. this.outro = '';
  20. this.content = content;
  21. this.storeName = false;
  22. this.edited = false;
  23. {
  24. this.previous = null;
  25. this.next = null;
  26. }
  27. }
  28. appendLeft(content) {
  29. this.outro += content;
  30. }
  31. appendRight(content) {
  32. this.intro = this.intro + content;
  33. }
  34. clone() {
  35. const chunk = new Chunk(this.start, this.end, this.original);
  36. chunk.intro = this.intro;
  37. chunk.outro = this.outro;
  38. chunk.content = this.content;
  39. chunk.storeName = this.storeName;
  40. chunk.edited = this.edited;
  41. return chunk;
  42. }
  43. contains(index) {
  44. return this.start < index && index < this.end;
  45. }
  46. eachNext(fn) {
  47. let chunk = this;
  48. while (chunk) {
  49. fn(chunk);
  50. chunk = chunk.next;
  51. }
  52. }
  53. eachPrevious(fn) {
  54. let chunk = this;
  55. while (chunk) {
  56. fn(chunk);
  57. chunk = chunk.previous;
  58. }
  59. }
  60. edit(content, storeName, contentOnly) {
  61. this.content = content;
  62. if (!contentOnly) {
  63. this.intro = '';
  64. this.outro = '';
  65. }
  66. this.storeName = storeName;
  67. this.edited = true;
  68. return this;
  69. }
  70. prependLeft(content) {
  71. this.outro = content + this.outro;
  72. }
  73. prependRight(content) {
  74. this.intro = content + this.intro;
  75. }
  76. reset() {
  77. this.intro = '';
  78. this.outro = '';
  79. if (this.edited) {
  80. this.content = this.original;
  81. this.storeName = false;
  82. this.edited = false;
  83. }
  84. }
  85. split(index) {
  86. const sliceIndex = index - this.start;
  87. const originalBefore = this.original.slice(0, sliceIndex);
  88. const originalAfter = this.original.slice(sliceIndex);
  89. this.original = originalBefore;
  90. const newChunk = new Chunk(index, this.end, originalAfter);
  91. newChunk.outro = this.outro;
  92. this.outro = '';
  93. this.end = index;
  94. if (this.edited) {
  95. // after split we should save the edit content record into the correct chunk
  96. // to make sure sourcemap correct
  97. // For example:
  98. // ' test'.trim()
  99. // split -> ' ' + 'test'
  100. // ✔️ edit -> '' + 'test'
  101. // ✖️ edit -> 'test' + ''
  102. // TODO is this block necessary?...
  103. newChunk.edit('', false);
  104. this.content = '';
  105. } else {
  106. this.content = originalBefore;
  107. }
  108. newChunk.next = this.next;
  109. if (newChunk.next) newChunk.next.previous = newChunk;
  110. newChunk.previous = this;
  111. this.next = newChunk;
  112. return newChunk;
  113. }
  114. toString() {
  115. return this.intro + this.content + this.outro;
  116. }
  117. trimEnd(rx) {
  118. this.outro = this.outro.replace(rx, '');
  119. if (this.outro.length) return true;
  120. const trimmed = this.content.replace(rx, '');
  121. if (trimmed.length) {
  122. if (trimmed !== this.content) {
  123. this.split(this.start + trimmed.length).edit('', undefined, true);
  124. if (this.edited) {
  125. // save the change, if it has been edited
  126. this.edit(trimmed, this.storeName, true);
  127. }
  128. }
  129. return true;
  130. } else {
  131. this.edit('', undefined, true);
  132. this.intro = this.intro.replace(rx, '');
  133. if (this.intro.length) return true;
  134. }
  135. }
  136. trimStart(rx) {
  137. this.intro = this.intro.replace(rx, '');
  138. if (this.intro.length) return true;
  139. const trimmed = this.content.replace(rx, '');
  140. if (trimmed.length) {
  141. if (trimmed !== this.content) {
  142. const newChunk = this.split(this.end - trimmed.length);
  143. if (this.edited) {
  144. // save the change, if it has been edited
  145. newChunk.edit(trimmed, this.storeName, true);
  146. }
  147. this.edit('', undefined, true);
  148. }
  149. return true;
  150. } else {
  151. this.edit('', undefined, true);
  152. this.outro = this.outro.replace(rx, '');
  153. if (this.outro.length) return true;
  154. }
  155. }
  156. }
  157. function getBtoa() {
  158. if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
  159. return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
  160. } else if (typeof Buffer === 'function') {
  161. return (str) => Buffer.from(str, 'utf-8').toString('base64');
  162. } else {
  163. return () => {
  164. throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
  165. };
  166. }
  167. }
  168. const btoa = /*#__PURE__*/ getBtoa();
  169. class SourceMap {
  170. constructor(properties) {
  171. this.version = 3;
  172. this.file = properties.file;
  173. this.sources = properties.sources;
  174. this.sourcesContent = properties.sourcesContent;
  175. this.names = properties.names;
  176. this.mappings = encode(properties.mappings);
  177. if (typeof properties.x_google_ignoreList !== 'undefined') {
  178. this.x_google_ignoreList = properties.x_google_ignoreList;
  179. }
  180. if (typeof properties.debugId !== 'undefined') {
  181. this.debugId = properties.debugId;
  182. }
  183. }
  184. toString() {
  185. return JSON.stringify(this);
  186. }
  187. toUrl() {
  188. return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
  189. }
  190. }
  191. function guessIndent(code) {
  192. const lines = code.split('\n');
  193. const tabbed = lines.filter((line) => /^\t+/.test(line));
  194. const spaced = lines.filter((line) => /^ {2,}/.test(line));
  195. if (tabbed.length === 0 && spaced.length === 0) {
  196. return null;
  197. }
  198. // More lines tabbed than spaced? Assume tabs, and
  199. // default to tabs in the case of a tie (or nothing
  200. // to go on)
  201. if (tabbed.length >= spaced.length) {
  202. return '\t';
  203. }
  204. // Otherwise, we need to guess the multiple
  205. const min = spaced.reduce((previous, current) => {
  206. const numSpaces = /^ +/.exec(current)[0].length;
  207. return Math.min(numSpaces, previous);
  208. }, Infinity);
  209. return new Array(min + 1).join(' ');
  210. }
  211. function getRelativePath(from, to) {
  212. const fromParts = from.split(/[/\\]/);
  213. const toParts = to.split(/[/\\]/);
  214. fromParts.pop(); // get dirname
  215. while (fromParts[0] === toParts[0]) {
  216. fromParts.shift();
  217. toParts.shift();
  218. }
  219. if (fromParts.length) {
  220. let i = fromParts.length;
  221. while (i--) fromParts[i] = '..';
  222. }
  223. return fromParts.concat(toParts).join('/');
  224. }
  225. const toString = Object.prototype.toString;
  226. function isObject(thing) {
  227. return toString.call(thing) === '[object Object]';
  228. }
  229. function getLocator(source) {
  230. const originalLines = source.split('\n');
  231. const lineOffsets = [];
  232. for (let i = 0, pos = 0; i < originalLines.length; i++) {
  233. lineOffsets.push(pos);
  234. pos += originalLines[i].length + 1;
  235. }
  236. return function locate(index) {
  237. let i = 0;
  238. let j = lineOffsets.length;
  239. while (i < j) {
  240. const m = (i + j) >> 1;
  241. if (index < lineOffsets[m]) {
  242. j = m;
  243. } else {
  244. i = m + 1;
  245. }
  246. }
  247. const line = i - 1;
  248. const column = index - lineOffsets[line];
  249. return { line, column };
  250. };
  251. }
  252. const wordRegex = /\w/;
  253. class Mappings {
  254. constructor(hires) {
  255. this.hires = hires;
  256. this.generatedCodeLine = 0;
  257. this.generatedCodeColumn = 0;
  258. this.raw = [];
  259. this.rawSegments = this.raw[this.generatedCodeLine] = [];
  260. this.pending = null;
  261. }
  262. addEdit(sourceIndex, content, loc, nameIndex) {
  263. if (content.length) {
  264. const contentLengthMinusOne = content.length - 1;
  265. let contentLineEnd = content.indexOf('\n', 0);
  266. let previousContentLineEnd = -1;
  267. // Loop through each line in the content and add a segment, but stop if the last line is empty,
  268. // else code afterwards would fill one line too many
  269. while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
  270. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  271. if (nameIndex >= 0) {
  272. segment.push(nameIndex);
  273. }
  274. this.rawSegments.push(segment);
  275. this.generatedCodeLine += 1;
  276. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  277. this.generatedCodeColumn = 0;
  278. previousContentLineEnd = contentLineEnd;
  279. contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
  280. }
  281. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  282. if (nameIndex >= 0) {
  283. segment.push(nameIndex);
  284. }
  285. this.rawSegments.push(segment);
  286. this.advance(content.slice(previousContentLineEnd + 1));
  287. } else if (this.pending) {
  288. this.rawSegments.push(this.pending);
  289. this.advance(content);
  290. }
  291. this.pending = null;
  292. }
  293. addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
  294. let originalCharIndex = chunk.start;
  295. let first = true;
  296. // when iterating each char, check if it's in a word boundary
  297. let charInHiresBoundary = false;
  298. while (originalCharIndex < chunk.end) {
  299. if (original[originalCharIndex] === '\n') {
  300. loc.line += 1;
  301. loc.column = 0;
  302. this.generatedCodeLine += 1;
  303. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  304. this.generatedCodeColumn = 0;
  305. first = true;
  306. charInHiresBoundary = false;
  307. } else {
  308. if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
  309. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  310. if (this.hires === 'boundary') {
  311. // in hires "boundary", group segments per word boundary than per char
  312. if (wordRegex.test(original[originalCharIndex])) {
  313. // for first char in the boundary found, start the boundary by pushing a segment
  314. if (!charInHiresBoundary) {
  315. this.rawSegments.push(segment);
  316. charInHiresBoundary = true;
  317. }
  318. } else {
  319. // for non-word char, end the boundary by pushing a segment
  320. this.rawSegments.push(segment);
  321. charInHiresBoundary = false;
  322. }
  323. } else {
  324. this.rawSegments.push(segment);
  325. }
  326. }
  327. loc.column += 1;
  328. this.generatedCodeColumn += 1;
  329. first = false;
  330. }
  331. originalCharIndex += 1;
  332. }
  333. this.pending = null;
  334. }
  335. advance(str) {
  336. if (!str) return;
  337. const lines = str.split('\n');
  338. if (lines.length > 1) {
  339. for (let i = 0; i < lines.length - 1; i++) {
  340. this.generatedCodeLine++;
  341. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  342. }
  343. this.generatedCodeColumn = 0;
  344. }
  345. this.generatedCodeColumn += lines[lines.length - 1].length;
  346. }
  347. }
  348. const n = '\n';
  349. const warned = {
  350. insertLeft: false,
  351. insertRight: false,
  352. storeName: false,
  353. };
  354. class MagicString {
  355. constructor(string, options = {}) {
  356. const chunk = new Chunk(0, string.length, string);
  357. Object.defineProperties(this, {
  358. original: { writable: true, value: string },
  359. outro: { writable: true, value: '' },
  360. intro: { writable: true, value: '' },
  361. firstChunk: { writable: true, value: chunk },
  362. lastChunk: { writable: true, value: chunk },
  363. lastSearchedChunk: { writable: true, value: chunk },
  364. byStart: { writable: true, value: {} },
  365. byEnd: { writable: true, value: {} },
  366. filename: { writable: true, value: options.filename },
  367. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  368. sourcemapLocations: { writable: true, value: new BitSet() },
  369. storedNames: { writable: true, value: {} },
  370. indentStr: { writable: true, value: undefined },
  371. ignoreList: { writable: true, value: options.ignoreList },
  372. offset: { writable: true, value: options.offset || 0 },
  373. });
  374. this.byStart[0] = chunk;
  375. this.byEnd[string.length] = chunk;
  376. }
  377. addSourcemapLocation(char) {
  378. this.sourcemapLocations.add(char);
  379. }
  380. append(content) {
  381. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  382. this.outro += content;
  383. return this;
  384. }
  385. appendLeft(index, content) {
  386. index = index + this.offset;
  387. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  388. this._split(index);
  389. const chunk = this.byEnd[index];
  390. if (chunk) {
  391. chunk.appendLeft(content);
  392. } else {
  393. this.intro += content;
  394. }
  395. return this;
  396. }
  397. appendRight(index, content) {
  398. index = index + this.offset;
  399. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  400. this._split(index);
  401. const chunk = this.byStart[index];
  402. if (chunk) {
  403. chunk.appendRight(content);
  404. } else {
  405. this.outro += content;
  406. }
  407. return this;
  408. }
  409. clone() {
  410. const cloned = new MagicString(this.original, { filename: this.filename, offset: this.offset });
  411. let originalChunk = this.firstChunk;
  412. let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
  413. while (originalChunk) {
  414. cloned.byStart[clonedChunk.start] = clonedChunk;
  415. cloned.byEnd[clonedChunk.end] = clonedChunk;
  416. const nextOriginalChunk = originalChunk.next;
  417. const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  418. if (nextClonedChunk) {
  419. clonedChunk.next = nextClonedChunk;
  420. nextClonedChunk.previous = clonedChunk;
  421. clonedChunk = nextClonedChunk;
  422. }
  423. originalChunk = nextOriginalChunk;
  424. }
  425. cloned.lastChunk = clonedChunk;
  426. if (this.indentExclusionRanges) {
  427. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  428. }
  429. cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
  430. cloned.intro = this.intro;
  431. cloned.outro = this.outro;
  432. return cloned;
  433. }
  434. generateDecodedMap(options) {
  435. options = options || {};
  436. const sourceIndex = 0;
  437. const names = Object.keys(this.storedNames);
  438. const mappings = new Mappings(options.hires);
  439. const locate = getLocator(this.original);
  440. if (this.intro) {
  441. mappings.advance(this.intro);
  442. }
  443. this.firstChunk.eachNext((chunk) => {
  444. const loc = locate(chunk.start);
  445. if (chunk.intro.length) mappings.advance(chunk.intro);
  446. if (chunk.edited) {
  447. mappings.addEdit(
  448. sourceIndex,
  449. chunk.content,
  450. loc,
  451. chunk.storeName ? names.indexOf(chunk.original) : -1,
  452. );
  453. } else {
  454. mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
  455. }
  456. if (chunk.outro.length) mappings.advance(chunk.outro);
  457. });
  458. return {
  459. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  460. sources: [
  461. options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
  462. ],
  463. sourcesContent: options.includeContent ? [this.original] : undefined,
  464. names,
  465. mappings: mappings.raw,
  466. x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
  467. };
  468. }
  469. generateMap(options) {
  470. return new SourceMap(this.generateDecodedMap(options));
  471. }
  472. _ensureindentStr() {
  473. if (this.indentStr === undefined) {
  474. this.indentStr = guessIndent(this.original);
  475. }
  476. }
  477. _getRawIndentString() {
  478. this._ensureindentStr();
  479. return this.indentStr;
  480. }
  481. getIndentString() {
  482. this._ensureindentStr();
  483. return this.indentStr === null ? '\t' : this.indentStr;
  484. }
  485. indent(indentStr, options) {
  486. const pattern = /^[^\r\n]/gm;
  487. if (isObject(indentStr)) {
  488. options = indentStr;
  489. indentStr = undefined;
  490. }
  491. if (indentStr === undefined) {
  492. this._ensureindentStr();
  493. indentStr = this.indentStr || '\t';
  494. }
  495. if (indentStr === '') return this; // noop
  496. options = options || {};
  497. // Process exclusion ranges
  498. const isExcluded = {};
  499. if (options.exclude) {
  500. const exclusions =
  501. typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
  502. exclusions.forEach((exclusion) => {
  503. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  504. isExcluded[i] = true;
  505. }
  506. });
  507. }
  508. let shouldIndentNextCharacter = options.indentStart !== false;
  509. const replacer = (match) => {
  510. if (shouldIndentNextCharacter) return `${indentStr}${match}`;
  511. shouldIndentNextCharacter = true;
  512. return match;
  513. };
  514. this.intro = this.intro.replace(pattern, replacer);
  515. let charIndex = 0;
  516. let chunk = this.firstChunk;
  517. while (chunk) {
  518. const end = chunk.end;
  519. if (chunk.edited) {
  520. if (!isExcluded[charIndex]) {
  521. chunk.content = chunk.content.replace(pattern, replacer);
  522. if (chunk.content.length) {
  523. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
  524. }
  525. }
  526. } else {
  527. charIndex = chunk.start;
  528. while (charIndex < end) {
  529. if (!isExcluded[charIndex]) {
  530. const char = this.original[charIndex];
  531. if (char === '\n') {
  532. shouldIndentNextCharacter = true;
  533. } else if (char !== '\r' && shouldIndentNextCharacter) {
  534. shouldIndentNextCharacter = false;
  535. if (charIndex === chunk.start) {
  536. chunk.prependRight(indentStr);
  537. } else {
  538. this._splitChunk(chunk, charIndex);
  539. chunk = chunk.next;
  540. chunk.prependRight(indentStr);
  541. }
  542. }
  543. }
  544. charIndex += 1;
  545. }
  546. }
  547. charIndex = chunk.end;
  548. chunk = chunk.next;
  549. }
  550. this.outro = this.outro.replace(pattern, replacer);
  551. return this;
  552. }
  553. insert() {
  554. throw new Error(
  555. 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
  556. );
  557. }
  558. insertLeft(index, content) {
  559. if (!warned.insertLeft) {
  560. console.warn(
  561. 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
  562. );
  563. warned.insertLeft = true;
  564. }
  565. return this.appendLeft(index, content);
  566. }
  567. insertRight(index, content) {
  568. if (!warned.insertRight) {
  569. console.warn(
  570. 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
  571. );
  572. warned.insertRight = true;
  573. }
  574. return this.prependRight(index, content);
  575. }
  576. move(start, end, index) {
  577. start = start + this.offset;
  578. end = end + this.offset;
  579. index = index + this.offset;
  580. if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
  581. this._split(start);
  582. this._split(end);
  583. this._split(index);
  584. const first = this.byStart[start];
  585. const last = this.byEnd[end];
  586. const oldLeft = first.previous;
  587. const oldRight = last.next;
  588. const newRight = this.byStart[index];
  589. if (!newRight && last === this.lastChunk) return this;
  590. const newLeft = newRight ? newRight.previous : this.lastChunk;
  591. if (oldLeft) oldLeft.next = oldRight;
  592. if (oldRight) oldRight.previous = oldLeft;
  593. if (newLeft) newLeft.next = first;
  594. if (newRight) newRight.previous = last;
  595. if (!first.previous) this.firstChunk = last.next;
  596. if (!last.next) {
  597. this.lastChunk = first.previous;
  598. this.lastChunk.next = null;
  599. }
  600. first.previous = newLeft;
  601. last.next = newRight || null;
  602. if (!newLeft) this.firstChunk = first;
  603. if (!newRight) this.lastChunk = last;
  604. return this;
  605. }
  606. overwrite(start, end, content, options) {
  607. options = options || {};
  608. return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
  609. }
  610. update(start, end, content, options) {
  611. start = start + this.offset;
  612. end = end + this.offset;
  613. if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
  614. if (this.original.length !== 0) {
  615. while (start < 0) start += this.original.length;
  616. while (end < 0) end += this.original.length;
  617. }
  618. if (end > this.original.length) throw new Error('end is out of bounds');
  619. if (start === end)
  620. throw new Error(
  621. 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
  622. );
  623. this._split(start);
  624. this._split(end);
  625. if (options === true) {
  626. if (!warned.storeName) {
  627. console.warn(
  628. 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
  629. );
  630. warned.storeName = true;
  631. }
  632. options = { storeName: true };
  633. }
  634. const storeName = options !== undefined ? options.storeName : false;
  635. const overwrite = options !== undefined ? options.overwrite : false;
  636. if (storeName) {
  637. const original = this.original.slice(start, end);
  638. Object.defineProperty(this.storedNames, original, {
  639. writable: true,
  640. value: true,
  641. enumerable: true,
  642. });
  643. }
  644. const first = this.byStart[start];
  645. const last = this.byEnd[end];
  646. if (first) {
  647. let chunk = first;
  648. while (chunk !== last) {
  649. if (chunk.next !== this.byStart[chunk.end]) {
  650. throw new Error('Cannot overwrite across a split point');
  651. }
  652. chunk = chunk.next;
  653. chunk.edit('', false);
  654. }
  655. first.edit(content, storeName, !overwrite);
  656. } else {
  657. // must be inserting at the end
  658. const newChunk = new Chunk(start, end, '').edit(content, storeName);
  659. // TODO last chunk in the array may not be the last chunk, if it's moved...
  660. last.next = newChunk;
  661. newChunk.previous = last;
  662. }
  663. return this;
  664. }
  665. prepend(content) {
  666. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  667. this.intro = content + this.intro;
  668. return this;
  669. }
  670. prependLeft(index, content) {
  671. index = index + this.offset;
  672. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  673. this._split(index);
  674. const chunk = this.byEnd[index];
  675. if (chunk) {
  676. chunk.prependLeft(content);
  677. } else {
  678. this.intro = content + this.intro;
  679. }
  680. return this;
  681. }
  682. prependRight(index, content) {
  683. index = index + this.offset;
  684. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  685. this._split(index);
  686. const chunk = this.byStart[index];
  687. if (chunk) {
  688. chunk.prependRight(content);
  689. } else {
  690. this.outro = content + this.outro;
  691. }
  692. return this;
  693. }
  694. remove(start, end) {
  695. start = start + this.offset;
  696. end = end + this.offset;
  697. if (this.original.length !== 0) {
  698. while (start < 0) start += this.original.length;
  699. while (end < 0) end += this.original.length;
  700. }
  701. if (start === end) return this;
  702. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  703. if (start > end) throw new Error('end must be greater than start');
  704. this._split(start);
  705. this._split(end);
  706. let chunk = this.byStart[start];
  707. while (chunk) {
  708. chunk.intro = '';
  709. chunk.outro = '';
  710. chunk.edit('');
  711. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  712. }
  713. return this;
  714. }
  715. reset(start, end) {
  716. start = start + this.offset;
  717. end = end + this.offset;
  718. if (this.original.length !== 0) {
  719. while (start < 0) start += this.original.length;
  720. while (end < 0) end += this.original.length;
  721. }
  722. if (start === end) return this;
  723. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  724. if (start > end) throw new Error('end must be greater than start');
  725. this._split(start);
  726. this._split(end);
  727. let chunk = this.byStart[start];
  728. while (chunk) {
  729. chunk.reset();
  730. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  731. }
  732. return this;
  733. }
  734. lastChar() {
  735. if (this.outro.length) return this.outro[this.outro.length - 1];
  736. let chunk = this.lastChunk;
  737. do {
  738. if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
  739. if (chunk.content.length) return chunk.content[chunk.content.length - 1];
  740. if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
  741. } while ((chunk = chunk.previous));
  742. if (this.intro.length) return this.intro[this.intro.length - 1];
  743. return '';
  744. }
  745. lastLine() {
  746. let lineIndex = this.outro.lastIndexOf(n);
  747. if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
  748. let lineStr = this.outro;
  749. let chunk = this.lastChunk;
  750. do {
  751. if (chunk.outro.length > 0) {
  752. lineIndex = chunk.outro.lastIndexOf(n);
  753. if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
  754. lineStr = chunk.outro + lineStr;
  755. }
  756. if (chunk.content.length > 0) {
  757. lineIndex = chunk.content.lastIndexOf(n);
  758. if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
  759. lineStr = chunk.content + lineStr;
  760. }
  761. if (chunk.intro.length > 0) {
  762. lineIndex = chunk.intro.lastIndexOf(n);
  763. if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
  764. lineStr = chunk.intro + lineStr;
  765. }
  766. } while ((chunk = chunk.previous));
  767. lineIndex = this.intro.lastIndexOf(n);
  768. if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
  769. return this.intro + lineStr;
  770. }
  771. slice(start = 0, end = this.original.length - this.offset) {
  772. start = start + this.offset;
  773. end = end + this.offset;
  774. if (this.original.length !== 0) {
  775. while (start < 0) start += this.original.length;
  776. while (end < 0) end += this.original.length;
  777. }
  778. let result = '';
  779. // find start chunk
  780. let chunk = this.firstChunk;
  781. while (chunk && (chunk.start > start || chunk.end <= start)) {
  782. // found end chunk before start
  783. if (chunk.start < end && chunk.end >= end) {
  784. return result;
  785. }
  786. chunk = chunk.next;
  787. }
  788. if (chunk && chunk.edited && chunk.start !== start)
  789. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  790. const startChunk = chunk;
  791. while (chunk) {
  792. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  793. result += chunk.intro;
  794. }
  795. const containsEnd = chunk.start < end && chunk.end >= end;
  796. if (containsEnd && chunk.edited && chunk.end !== end)
  797. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  798. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  799. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  800. result += chunk.content.slice(sliceStart, sliceEnd);
  801. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  802. result += chunk.outro;
  803. }
  804. if (containsEnd) {
  805. break;
  806. }
  807. chunk = chunk.next;
  808. }
  809. return result;
  810. }
  811. // TODO deprecate this? not really very useful
  812. snip(start, end) {
  813. const clone = this.clone();
  814. clone.remove(0, start);
  815. clone.remove(end, clone.original.length);
  816. return clone;
  817. }
  818. _split(index) {
  819. if (this.byStart[index] || this.byEnd[index]) return;
  820. let chunk = this.lastSearchedChunk;
  821. let previousChunk = chunk;
  822. const searchForward = index > chunk.end;
  823. while (chunk) {
  824. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  825. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  826. // Prevent infinite loop (e.g. via empty chunks, where start === end)
  827. if (chunk === previousChunk) return;
  828. previousChunk = chunk;
  829. }
  830. }
  831. _splitChunk(chunk, index) {
  832. if (chunk.edited && chunk.content.length) {
  833. // zero-length edited chunks are a special case (overlapping replacements)
  834. const loc = getLocator(this.original)(index);
  835. throw new Error(
  836. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  837. );
  838. }
  839. const newChunk = chunk.split(index);
  840. this.byEnd[index] = chunk;
  841. this.byStart[index] = newChunk;
  842. this.byEnd[newChunk.end] = newChunk;
  843. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  844. this.lastSearchedChunk = chunk;
  845. return true;
  846. }
  847. toString() {
  848. let str = this.intro;
  849. let chunk = this.firstChunk;
  850. while (chunk) {
  851. str += chunk.toString();
  852. chunk = chunk.next;
  853. }
  854. return str + this.outro;
  855. }
  856. isEmpty() {
  857. let chunk = this.firstChunk;
  858. do {
  859. if (
  860. (chunk.intro.length && chunk.intro.trim()) ||
  861. (chunk.content.length && chunk.content.trim()) ||
  862. (chunk.outro.length && chunk.outro.trim())
  863. )
  864. return false;
  865. } while ((chunk = chunk.next));
  866. return true;
  867. }
  868. length() {
  869. let chunk = this.firstChunk;
  870. let length = 0;
  871. do {
  872. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  873. } while ((chunk = chunk.next));
  874. return length;
  875. }
  876. trimLines() {
  877. return this.trim('[\\r\\n]');
  878. }
  879. trim(charType) {
  880. return this.trimStart(charType).trimEnd(charType);
  881. }
  882. trimEndAborted(charType) {
  883. const rx = new RegExp((charType || '\\s') + '+$');
  884. this.outro = this.outro.replace(rx, '');
  885. if (this.outro.length) return true;
  886. let chunk = this.lastChunk;
  887. do {
  888. const end = chunk.end;
  889. const aborted = chunk.trimEnd(rx);
  890. // if chunk was trimmed, we have a new lastChunk
  891. if (chunk.end !== end) {
  892. if (this.lastChunk === chunk) {
  893. this.lastChunk = chunk.next;
  894. }
  895. this.byEnd[chunk.end] = chunk;
  896. this.byStart[chunk.next.start] = chunk.next;
  897. this.byEnd[chunk.next.end] = chunk.next;
  898. }
  899. if (aborted) return true;
  900. chunk = chunk.previous;
  901. } while (chunk);
  902. return false;
  903. }
  904. trimEnd(charType) {
  905. this.trimEndAborted(charType);
  906. return this;
  907. }
  908. trimStartAborted(charType) {
  909. const rx = new RegExp('^' + (charType || '\\s') + '+');
  910. this.intro = this.intro.replace(rx, '');
  911. if (this.intro.length) return true;
  912. let chunk = this.firstChunk;
  913. do {
  914. const end = chunk.end;
  915. const aborted = chunk.trimStart(rx);
  916. if (chunk.end !== end) {
  917. // special case...
  918. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  919. this.byEnd[chunk.end] = chunk;
  920. this.byStart[chunk.next.start] = chunk.next;
  921. this.byEnd[chunk.next.end] = chunk.next;
  922. }
  923. if (aborted) return true;
  924. chunk = chunk.next;
  925. } while (chunk);
  926. return false;
  927. }
  928. trimStart(charType) {
  929. this.trimStartAborted(charType);
  930. return this;
  931. }
  932. hasChanged() {
  933. return this.original !== this.toString();
  934. }
  935. _replaceRegexp(searchValue, replacement) {
  936. function getReplacement(match, str) {
  937. if (typeof replacement === 'string') {
  938. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  939. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  940. if (i === '$') return '$';
  941. if (i === '&') return match[0];
  942. const num = +i;
  943. if (num < match.length) return match[+i];
  944. return `$${i}`;
  945. });
  946. } else {
  947. return replacement(...match, match.index, str, match.groups);
  948. }
  949. }
  950. function matchAll(re, str) {
  951. let match;
  952. const matches = [];
  953. while ((match = re.exec(str))) {
  954. matches.push(match);
  955. }
  956. return matches;
  957. }
  958. if (searchValue.global) {
  959. const matches = matchAll(searchValue, this.original);
  960. matches.forEach((match) => {
  961. if (match.index != null) {
  962. const replacement = getReplacement(match, this.original);
  963. if (replacement !== match[0]) {
  964. this.overwrite(match.index, match.index + match[0].length, replacement);
  965. }
  966. }
  967. });
  968. } else {
  969. const match = this.original.match(searchValue);
  970. if (match && match.index != null) {
  971. const replacement = getReplacement(match, this.original);
  972. if (replacement !== match[0]) {
  973. this.overwrite(match.index, match.index + match[0].length, replacement);
  974. }
  975. }
  976. }
  977. return this;
  978. }
  979. _replaceString(string, replacement) {
  980. const { original } = this;
  981. const index = original.indexOf(string);
  982. if (index !== -1) {
  983. this.overwrite(index, index + string.length, replacement);
  984. }
  985. return this;
  986. }
  987. replace(searchValue, replacement) {
  988. if (typeof searchValue === 'string') {
  989. return this._replaceString(searchValue, replacement);
  990. }
  991. return this._replaceRegexp(searchValue, replacement);
  992. }
  993. _replaceAllString(string, replacement) {
  994. const { original } = this;
  995. const stringLength = string.length;
  996. for (
  997. let index = original.indexOf(string);
  998. index !== -1;
  999. index = original.indexOf(string, index + stringLength)
  1000. ) {
  1001. const previous = original.slice(index, index + stringLength);
  1002. if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
  1003. }
  1004. return this;
  1005. }
  1006. replaceAll(searchValue, replacement) {
  1007. if (typeof searchValue === 'string') {
  1008. return this._replaceAllString(searchValue, replacement);
  1009. }
  1010. if (!searchValue.global) {
  1011. throw new TypeError(
  1012. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  1013. );
  1014. }
  1015. return this._replaceRegexp(searchValue, replacement);
  1016. }
  1017. }
  1018. const hasOwnProp = Object.prototype.hasOwnProperty;
  1019. class Bundle {
  1020. constructor(options = {}) {
  1021. this.intro = options.intro || '';
  1022. this.separator = options.separator !== undefined ? options.separator : '\n';
  1023. this.sources = [];
  1024. this.uniqueSources = [];
  1025. this.uniqueSourceIndexByFilename = {};
  1026. }
  1027. addSource(source) {
  1028. if (source instanceof MagicString) {
  1029. return this.addSource({
  1030. content: source,
  1031. filename: source.filename,
  1032. separator: this.separator,
  1033. });
  1034. }
  1035. if (!isObject(source) || !source.content) {
  1036. throw new Error(
  1037. 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`',
  1038. );
  1039. }
  1040. ['filename', 'ignoreList', 'indentExclusionRanges', 'separator'].forEach((option) => {
  1041. if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
  1042. });
  1043. if (source.separator === undefined) {
  1044. // TODO there's a bunch of this sort of thing, needs cleaning up
  1045. source.separator = this.separator;
  1046. }
  1047. if (source.filename) {
  1048. if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
  1049. this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
  1050. this.uniqueSources.push({ filename: source.filename, content: source.content.original });
  1051. } else {
  1052. const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
  1053. if (source.content.original !== uniqueSource.content) {
  1054. throw new Error(`Illegal source: same filename (${source.filename}), different contents`);
  1055. }
  1056. }
  1057. }
  1058. this.sources.push(source);
  1059. return this;
  1060. }
  1061. append(str, options) {
  1062. this.addSource({
  1063. content: new MagicString(str),
  1064. separator: (options && options.separator) || '',
  1065. });
  1066. return this;
  1067. }
  1068. clone() {
  1069. const bundle = new Bundle({
  1070. intro: this.intro,
  1071. separator: this.separator,
  1072. });
  1073. this.sources.forEach((source) => {
  1074. bundle.addSource({
  1075. filename: source.filename,
  1076. content: source.content.clone(),
  1077. separator: source.separator,
  1078. });
  1079. });
  1080. return bundle;
  1081. }
  1082. generateDecodedMap(options = {}) {
  1083. const names = [];
  1084. let x_google_ignoreList = undefined;
  1085. this.sources.forEach((source) => {
  1086. Object.keys(source.content.storedNames).forEach((name) => {
  1087. if (!~names.indexOf(name)) names.push(name);
  1088. });
  1089. });
  1090. const mappings = new Mappings(options.hires);
  1091. if (this.intro) {
  1092. mappings.advance(this.intro);
  1093. }
  1094. this.sources.forEach((source, i) => {
  1095. if (i > 0) {
  1096. mappings.advance(this.separator);
  1097. }
  1098. const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1;
  1099. const magicString = source.content;
  1100. const locate = getLocator(magicString.original);
  1101. if (magicString.intro) {
  1102. mappings.advance(magicString.intro);
  1103. }
  1104. magicString.firstChunk.eachNext((chunk) => {
  1105. const loc = locate(chunk.start);
  1106. if (chunk.intro.length) mappings.advance(chunk.intro);
  1107. if (source.filename) {
  1108. if (chunk.edited) {
  1109. mappings.addEdit(
  1110. sourceIndex,
  1111. chunk.content,
  1112. loc,
  1113. chunk.storeName ? names.indexOf(chunk.original) : -1,
  1114. );
  1115. } else {
  1116. mappings.addUneditedChunk(
  1117. sourceIndex,
  1118. chunk,
  1119. magicString.original,
  1120. loc,
  1121. magicString.sourcemapLocations,
  1122. );
  1123. }
  1124. } else {
  1125. mappings.advance(chunk.content);
  1126. }
  1127. if (chunk.outro.length) mappings.advance(chunk.outro);
  1128. });
  1129. if (magicString.outro) {
  1130. mappings.advance(magicString.outro);
  1131. }
  1132. if (source.ignoreList && sourceIndex !== -1) {
  1133. if (x_google_ignoreList === undefined) {
  1134. x_google_ignoreList = [];
  1135. }
  1136. x_google_ignoreList.push(sourceIndex);
  1137. }
  1138. });
  1139. return {
  1140. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  1141. sources: this.uniqueSources.map((source) => {
  1142. return options.file ? getRelativePath(options.file, source.filename) : source.filename;
  1143. }),
  1144. sourcesContent: this.uniqueSources.map((source) => {
  1145. return options.includeContent ? source.content : null;
  1146. }),
  1147. names,
  1148. mappings: mappings.raw,
  1149. x_google_ignoreList,
  1150. };
  1151. }
  1152. generateMap(options) {
  1153. return new SourceMap(this.generateDecodedMap(options));
  1154. }
  1155. getIndentString() {
  1156. const indentStringCounts = {};
  1157. this.sources.forEach((source) => {
  1158. const indentStr = source.content._getRawIndentString();
  1159. if (indentStr === null) return;
  1160. if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
  1161. indentStringCounts[indentStr] += 1;
  1162. });
  1163. return (
  1164. Object.keys(indentStringCounts).sort((a, b) => {
  1165. return indentStringCounts[a] - indentStringCounts[b];
  1166. })[0] || '\t'
  1167. );
  1168. }
  1169. indent(indentStr) {
  1170. if (!arguments.length) {
  1171. indentStr = this.getIndentString();
  1172. }
  1173. if (indentStr === '') return this; // noop
  1174. let trailingNewline = !this.intro || this.intro.slice(-1) === '\n';
  1175. this.sources.forEach((source, i) => {
  1176. const separator = source.separator !== undefined ? source.separator : this.separator;
  1177. const indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator));
  1178. source.content.indent(indentStr, {
  1179. exclude: source.indentExclusionRanges,
  1180. indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
  1181. });
  1182. trailingNewline = source.content.lastChar() === '\n';
  1183. });
  1184. if (this.intro) {
  1185. this.intro =
  1186. indentStr +
  1187. this.intro.replace(/^[^\n]/gm, (match, index) => {
  1188. return index > 0 ? indentStr + match : match;
  1189. });
  1190. }
  1191. return this;
  1192. }
  1193. prepend(str) {
  1194. this.intro = str + this.intro;
  1195. return this;
  1196. }
  1197. toString() {
  1198. const body = this.sources
  1199. .map((source, i) => {
  1200. const separator = source.separator !== undefined ? source.separator : this.separator;
  1201. const str = (i > 0 ? separator : '') + source.content.toString();
  1202. return str;
  1203. })
  1204. .join('');
  1205. return this.intro + body;
  1206. }
  1207. isEmpty() {
  1208. if (this.intro.length && this.intro.trim()) return false;
  1209. if (this.sources.some((source) => !source.content.isEmpty())) return false;
  1210. return true;
  1211. }
  1212. length() {
  1213. return this.sources.reduce(
  1214. (length, source) => length + source.content.length(),
  1215. this.intro.length,
  1216. );
  1217. }
  1218. trimLines() {
  1219. return this.trim('[\\r\\n]');
  1220. }
  1221. trim(charType) {
  1222. return this.trimStart(charType).trimEnd(charType);
  1223. }
  1224. trimStart(charType) {
  1225. const rx = new RegExp('^' + (charType || '\\s') + '+');
  1226. this.intro = this.intro.replace(rx, '');
  1227. if (!this.intro) {
  1228. let source;
  1229. let i = 0;
  1230. do {
  1231. source = this.sources[i++];
  1232. if (!source) {
  1233. break;
  1234. }
  1235. } while (!source.content.trimStartAborted(charType));
  1236. }
  1237. return this;
  1238. }
  1239. trimEnd(charType) {
  1240. const rx = new RegExp((charType || '\\s') + '+$');
  1241. let source;
  1242. let i = this.sources.length - 1;
  1243. do {
  1244. source = this.sources[i--];
  1245. if (!source) {
  1246. this.intro = this.intro.replace(rx, '');
  1247. break;
  1248. }
  1249. } while (!source.content.trimEndAborted(charType));
  1250. return this;
  1251. }
  1252. }
  1253. export { Bundle, SourceMap, MagicString as default };
  1254. //# sourceMappingURL=magic-string.es.mjs.map