magic-string.cjs.js 38 KB

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