magic-string.umd.js 42 KB

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