magic-string.umd.js 42 KB

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