magic-string.es.mjs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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. const searchForward = index > chunk.end;
  822. while (chunk) {
  823. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  824. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  825. }
  826. }
  827. _splitChunk(chunk, index) {
  828. if (chunk.edited && chunk.content.length) {
  829. // zero-length edited chunks are a special case (overlapping replacements)
  830. const loc = getLocator(this.original)(index);
  831. throw new Error(
  832. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  833. );
  834. }
  835. const newChunk = chunk.split(index);
  836. this.byEnd[index] = chunk;
  837. this.byStart[index] = newChunk;
  838. this.byEnd[newChunk.end] = newChunk;
  839. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  840. this.lastSearchedChunk = chunk;
  841. return true;
  842. }
  843. toString() {
  844. let str = this.intro;
  845. let chunk = this.firstChunk;
  846. while (chunk) {
  847. str += chunk.toString();
  848. chunk = chunk.next;
  849. }
  850. return str + this.outro;
  851. }
  852. isEmpty() {
  853. let chunk = this.firstChunk;
  854. do {
  855. if (
  856. (chunk.intro.length && chunk.intro.trim()) ||
  857. (chunk.content.length && chunk.content.trim()) ||
  858. (chunk.outro.length && chunk.outro.trim())
  859. )
  860. return false;
  861. } while ((chunk = chunk.next));
  862. return true;
  863. }
  864. length() {
  865. let chunk = this.firstChunk;
  866. let length = 0;
  867. do {
  868. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  869. } while ((chunk = chunk.next));
  870. return length;
  871. }
  872. trimLines() {
  873. return this.trim('[\\r\\n]');
  874. }
  875. trim(charType) {
  876. return this.trimStart(charType).trimEnd(charType);
  877. }
  878. trimEndAborted(charType) {
  879. const rx = new RegExp((charType || '\\s') + '+$');
  880. this.outro = this.outro.replace(rx, '');
  881. if (this.outro.length) return true;
  882. let chunk = this.lastChunk;
  883. do {
  884. const end = chunk.end;
  885. const aborted = chunk.trimEnd(rx);
  886. // if chunk was trimmed, we have a new lastChunk
  887. if (chunk.end !== end) {
  888. if (this.lastChunk === chunk) {
  889. this.lastChunk = chunk.next;
  890. }
  891. this.byEnd[chunk.end] = chunk;
  892. this.byStart[chunk.next.start] = chunk.next;
  893. this.byEnd[chunk.next.end] = chunk.next;
  894. }
  895. if (aborted) return true;
  896. chunk = chunk.previous;
  897. } while (chunk);
  898. return false;
  899. }
  900. trimEnd(charType) {
  901. this.trimEndAborted(charType);
  902. return this;
  903. }
  904. trimStartAborted(charType) {
  905. const rx = new RegExp('^' + (charType || '\\s') + '+');
  906. this.intro = this.intro.replace(rx, '');
  907. if (this.intro.length) return true;
  908. let chunk = this.firstChunk;
  909. do {
  910. const end = chunk.end;
  911. const aborted = chunk.trimStart(rx);
  912. if (chunk.end !== end) {
  913. // special case...
  914. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  915. this.byEnd[chunk.end] = chunk;
  916. this.byStart[chunk.next.start] = chunk.next;
  917. this.byEnd[chunk.next.end] = chunk.next;
  918. }
  919. if (aborted) return true;
  920. chunk = chunk.next;
  921. } while (chunk);
  922. return false;
  923. }
  924. trimStart(charType) {
  925. this.trimStartAborted(charType);
  926. return this;
  927. }
  928. hasChanged() {
  929. return this.original !== this.toString();
  930. }
  931. _replaceRegexp(searchValue, replacement) {
  932. function getReplacement(match, str) {
  933. if (typeof replacement === 'string') {
  934. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  935. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  936. if (i === '$') return '$';
  937. if (i === '&') return match[0];
  938. const num = +i;
  939. if (num < match.length) return match[+i];
  940. return `$${i}`;
  941. });
  942. } else {
  943. return replacement(...match, match.index, str, match.groups);
  944. }
  945. }
  946. function matchAll(re, str) {
  947. let match;
  948. const matches = [];
  949. while ((match = re.exec(str))) {
  950. matches.push(match);
  951. }
  952. return matches;
  953. }
  954. if (searchValue.global) {
  955. const matches = matchAll(searchValue, this.original);
  956. matches.forEach((match) => {
  957. if (match.index != null) {
  958. const replacement = getReplacement(match, this.original);
  959. if (replacement !== match[0]) {
  960. this.overwrite(match.index, match.index + match[0].length, replacement);
  961. }
  962. }
  963. });
  964. } else {
  965. const match = this.original.match(searchValue);
  966. if (match && match.index != null) {
  967. const replacement = getReplacement(match, this.original);
  968. if (replacement !== match[0]) {
  969. this.overwrite(match.index, match.index + match[0].length, replacement);
  970. }
  971. }
  972. }
  973. return this;
  974. }
  975. _replaceString(string, replacement) {
  976. const { original } = this;
  977. const index = original.indexOf(string);
  978. if (index !== -1) {
  979. this.overwrite(index, index + string.length, replacement);
  980. }
  981. return this;
  982. }
  983. replace(searchValue, replacement) {
  984. if (typeof searchValue === 'string') {
  985. return this._replaceString(searchValue, replacement);
  986. }
  987. return this._replaceRegexp(searchValue, replacement);
  988. }
  989. _replaceAllString(string, replacement) {
  990. const { original } = this;
  991. const stringLength = string.length;
  992. for (
  993. let index = original.indexOf(string);
  994. index !== -1;
  995. index = original.indexOf(string, index + stringLength)
  996. ) {
  997. const previous = original.slice(index, index + stringLength);
  998. if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
  999. }
  1000. return this;
  1001. }
  1002. replaceAll(searchValue, replacement) {
  1003. if (typeof searchValue === 'string') {
  1004. return this._replaceAllString(searchValue, replacement);
  1005. }
  1006. if (!searchValue.global) {
  1007. throw new TypeError(
  1008. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  1009. );
  1010. }
  1011. return this._replaceRegexp(searchValue, replacement);
  1012. }
  1013. }
  1014. const hasOwnProp = Object.prototype.hasOwnProperty;
  1015. class Bundle {
  1016. constructor(options = {}) {
  1017. this.intro = options.intro || '';
  1018. this.separator = options.separator !== undefined ? options.separator : '\n';
  1019. this.sources = [];
  1020. this.uniqueSources = [];
  1021. this.uniqueSourceIndexByFilename = {};
  1022. }
  1023. addSource(source) {
  1024. if (source instanceof MagicString) {
  1025. return this.addSource({
  1026. content: source,
  1027. filename: source.filename,
  1028. separator: this.separator,
  1029. });
  1030. }
  1031. if (!isObject(source) || !source.content) {
  1032. throw new Error(
  1033. 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`',
  1034. );
  1035. }
  1036. ['filename', 'ignoreList', 'indentExclusionRanges', 'separator'].forEach((option) => {
  1037. if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
  1038. });
  1039. if (source.separator === undefined) {
  1040. // TODO there's a bunch of this sort of thing, needs cleaning up
  1041. source.separator = this.separator;
  1042. }
  1043. if (source.filename) {
  1044. if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
  1045. this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
  1046. this.uniqueSources.push({ filename: source.filename, content: source.content.original });
  1047. } else {
  1048. const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
  1049. if (source.content.original !== uniqueSource.content) {
  1050. throw new Error(`Illegal source: same filename (${source.filename}), different contents`);
  1051. }
  1052. }
  1053. }
  1054. this.sources.push(source);
  1055. return this;
  1056. }
  1057. append(str, options) {
  1058. this.addSource({
  1059. content: new MagicString(str),
  1060. separator: (options && options.separator) || '',
  1061. });
  1062. return this;
  1063. }
  1064. clone() {
  1065. const bundle = new Bundle({
  1066. intro: this.intro,
  1067. separator: this.separator,
  1068. });
  1069. this.sources.forEach((source) => {
  1070. bundle.addSource({
  1071. filename: source.filename,
  1072. content: source.content.clone(),
  1073. separator: source.separator,
  1074. });
  1075. });
  1076. return bundle;
  1077. }
  1078. generateDecodedMap(options = {}) {
  1079. const names = [];
  1080. let x_google_ignoreList = undefined;
  1081. this.sources.forEach((source) => {
  1082. Object.keys(source.content.storedNames).forEach((name) => {
  1083. if (!~names.indexOf(name)) names.push(name);
  1084. });
  1085. });
  1086. const mappings = new Mappings(options.hires);
  1087. if (this.intro) {
  1088. mappings.advance(this.intro);
  1089. }
  1090. this.sources.forEach((source, i) => {
  1091. if (i > 0) {
  1092. mappings.advance(this.separator);
  1093. }
  1094. const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1;
  1095. const magicString = source.content;
  1096. const locate = getLocator(magicString.original);
  1097. if (magicString.intro) {
  1098. mappings.advance(magicString.intro);
  1099. }
  1100. magicString.firstChunk.eachNext((chunk) => {
  1101. const loc = locate(chunk.start);
  1102. if (chunk.intro.length) mappings.advance(chunk.intro);
  1103. if (source.filename) {
  1104. if (chunk.edited) {
  1105. mappings.addEdit(
  1106. sourceIndex,
  1107. chunk.content,
  1108. loc,
  1109. chunk.storeName ? names.indexOf(chunk.original) : -1,
  1110. );
  1111. } else {
  1112. mappings.addUneditedChunk(
  1113. sourceIndex,
  1114. chunk,
  1115. magicString.original,
  1116. loc,
  1117. magicString.sourcemapLocations,
  1118. );
  1119. }
  1120. } else {
  1121. mappings.advance(chunk.content);
  1122. }
  1123. if (chunk.outro.length) mappings.advance(chunk.outro);
  1124. });
  1125. if (magicString.outro) {
  1126. mappings.advance(magicString.outro);
  1127. }
  1128. if (source.ignoreList && sourceIndex !== -1) {
  1129. if (x_google_ignoreList === undefined) {
  1130. x_google_ignoreList = [];
  1131. }
  1132. x_google_ignoreList.push(sourceIndex);
  1133. }
  1134. });
  1135. return {
  1136. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  1137. sources: this.uniqueSources.map((source) => {
  1138. return options.file ? getRelativePath(options.file, source.filename) : source.filename;
  1139. }),
  1140. sourcesContent: this.uniqueSources.map((source) => {
  1141. return options.includeContent ? source.content : null;
  1142. }),
  1143. names,
  1144. mappings: mappings.raw,
  1145. x_google_ignoreList,
  1146. };
  1147. }
  1148. generateMap(options) {
  1149. return new SourceMap(this.generateDecodedMap(options));
  1150. }
  1151. getIndentString() {
  1152. const indentStringCounts = {};
  1153. this.sources.forEach((source) => {
  1154. const indentStr = source.content._getRawIndentString();
  1155. if (indentStr === null) return;
  1156. if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
  1157. indentStringCounts[indentStr] += 1;
  1158. });
  1159. return (
  1160. Object.keys(indentStringCounts).sort((a, b) => {
  1161. return indentStringCounts[a] - indentStringCounts[b];
  1162. })[0] || '\t'
  1163. );
  1164. }
  1165. indent(indentStr) {
  1166. if (!arguments.length) {
  1167. indentStr = this.getIndentString();
  1168. }
  1169. if (indentStr === '') return this; // noop
  1170. let trailingNewline = !this.intro || this.intro.slice(-1) === '\n';
  1171. this.sources.forEach((source, i) => {
  1172. const separator = source.separator !== undefined ? source.separator : this.separator;
  1173. const indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator));
  1174. source.content.indent(indentStr, {
  1175. exclude: source.indentExclusionRanges,
  1176. indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
  1177. });
  1178. trailingNewline = source.content.lastChar() === '\n';
  1179. });
  1180. if (this.intro) {
  1181. this.intro =
  1182. indentStr +
  1183. this.intro.replace(/^[^\n]/gm, (match, index) => {
  1184. return index > 0 ? indentStr + match : match;
  1185. });
  1186. }
  1187. return this;
  1188. }
  1189. prepend(str) {
  1190. this.intro = str + this.intro;
  1191. return this;
  1192. }
  1193. toString() {
  1194. const body = this.sources
  1195. .map((source, i) => {
  1196. const separator = source.separator !== undefined ? source.separator : this.separator;
  1197. const str = (i > 0 ? separator : '') + source.content.toString();
  1198. return str;
  1199. })
  1200. .join('');
  1201. return this.intro + body;
  1202. }
  1203. isEmpty() {
  1204. if (this.intro.length && this.intro.trim()) return false;
  1205. if (this.sources.some((source) => !source.content.isEmpty())) return false;
  1206. return true;
  1207. }
  1208. length() {
  1209. return this.sources.reduce(
  1210. (length, source) => length + source.content.length(),
  1211. this.intro.length,
  1212. );
  1213. }
  1214. trimLines() {
  1215. return this.trim('[\\r\\n]');
  1216. }
  1217. trim(charType) {
  1218. return this.trimStart(charType).trimEnd(charType);
  1219. }
  1220. trimStart(charType) {
  1221. const rx = new RegExp('^' + (charType || '\\s') + '+');
  1222. this.intro = this.intro.replace(rx, '');
  1223. if (!this.intro) {
  1224. let source;
  1225. let i = 0;
  1226. do {
  1227. source = this.sources[i++];
  1228. if (!source) {
  1229. break;
  1230. }
  1231. } while (!source.content.trimStartAborted(charType));
  1232. }
  1233. return this;
  1234. }
  1235. trimEnd(charType) {
  1236. const rx = new RegExp((charType || '\\s') + '+$');
  1237. let source;
  1238. let i = this.sources.length - 1;
  1239. do {
  1240. source = this.sources[i--];
  1241. if (!source) {
  1242. this.intro = this.intro.replace(rx, '');
  1243. break;
  1244. }
  1245. } while (!source.content.trimEndAborted(charType));
  1246. return this;
  1247. }
  1248. }
  1249. export { Bundle, SourceMap, MagicString as default };
  1250. //# sourceMappingURL=magic-string.es.mjs.map