magic-string.umd.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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. return {
  545. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  546. sources: [
  547. options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
  548. ],
  549. sourcesContent: options.includeContent ? [this.original] : undefined,
  550. names,
  551. mappings: mappings.raw,
  552. x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
  553. };
  554. }
  555. generateMap(options) {
  556. return new SourceMap(this.generateDecodedMap(options));
  557. }
  558. _ensureindentStr() {
  559. if (this.indentStr === undefined) {
  560. this.indentStr = guessIndent(this.original);
  561. }
  562. }
  563. _getRawIndentString() {
  564. this._ensureindentStr();
  565. return this.indentStr;
  566. }
  567. getIndentString() {
  568. this._ensureindentStr();
  569. return this.indentStr === null ? '\t' : this.indentStr;
  570. }
  571. indent(indentStr, options) {
  572. const pattern = /^[^\r\n]/gm;
  573. if (isObject(indentStr)) {
  574. options = indentStr;
  575. indentStr = undefined;
  576. }
  577. if (indentStr === undefined) {
  578. this._ensureindentStr();
  579. indentStr = this.indentStr || '\t';
  580. }
  581. if (indentStr === '') return this; // noop
  582. options = options || {};
  583. // Process exclusion ranges
  584. const isExcluded = {};
  585. if (options.exclude) {
  586. const exclusions =
  587. typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
  588. exclusions.forEach((exclusion) => {
  589. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  590. isExcluded[i] = true;
  591. }
  592. });
  593. }
  594. let shouldIndentNextCharacter = options.indentStart !== false;
  595. const replacer = (match) => {
  596. if (shouldIndentNextCharacter) return `${indentStr}${match}`;
  597. shouldIndentNextCharacter = true;
  598. return match;
  599. };
  600. this.intro = this.intro.replace(pattern, replacer);
  601. let charIndex = 0;
  602. let chunk = this.firstChunk;
  603. while (chunk) {
  604. const end = chunk.end;
  605. if (chunk.edited) {
  606. if (!isExcluded[charIndex]) {
  607. chunk.content = chunk.content.replace(pattern, replacer);
  608. if (chunk.content.length) {
  609. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
  610. }
  611. }
  612. } else {
  613. charIndex = chunk.start;
  614. while (charIndex < end) {
  615. if (!isExcluded[charIndex]) {
  616. const char = this.original[charIndex];
  617. if (char === '\n') {
  618. shouldIndentNextCharacter = true;
  619. } else if (char !== '\r' && shouldIndentNextCharacter) {
  620. shouldIndentNextCharacter = false;
  621. if (charIndex === chunk.start) {
  622. chunk.prependRight(indentStr);
  623. } else {
  624. this._splitChunk(chunk, charIndex);
  625. chunk = chunk.next;
  626. chunk.prependRight(indentStr);
  627. }
  628. }
  629. }
  630. charIndex += 1;
  631. }
  632. }
  633. charIndex = chunk.end;
  634. chunk = chunk.next;
  635. }
  636. this.outro = this.outro.replace(pattern, replacer);
  637. return this;
  638. }
  639. insert() {
  640. throw new Error(
  641. 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
  642. );
  643. }
  644. insertLeft(index, content) {
  645. if (!warned.insertLeft) {
  646. console.warn(
  647. 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
  648. );
  649. warned.insertLeft = true;
  650. }
  651. return this.appendLeft(index, content);
  652. }
  653. insertRight(index, content) {
  654. if (!warned.insertRight) {
  655. console.warn(
  656. 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
  657. );
  658. warned.insertRight = true;
  659. }
  660. return this.prependRight(index, content);
  661. }
  662. move(start, end, index) {
  663. start = start + this.offset;
  664. end = end + this.offset;
  665. index = index + this.offset;
  666. if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
  667. this._split(start);
  668. this._split(end);
  669. this._split(index);
  670. const first = this.byStart[start];
  671. const last = this.byEnd[end];
  672. const oldLeft = first.previous;
  673. const oldRight = last.next;
  674. const newRight = this.byStart[index];
  675. if (!newRight && last === this.lastChunk) return this;
  676. const newLeft = newRight ? newRight.previous : this.lastChunk;
  677. if (oldLeft) oldLeft.next = oldRight;
  678. if (oldRight) oldRight.previous = oldLeft;
  679. if (newLeft) newLeft.next = first;
  680. if (newRight) newRight.previous = last;
  681. if (!first.previous) this.firstChunk = last.next;
  682. if (!last.next) {
  683. this.lastChunk = first.previous;
  684. this.lastChunk.next = null;
  685. }
  686. first.previous = newLeft;
  687. last.next = newRight || null;
  688. if (!newLeft) this.firstChunk = first;
  689. if (!newRight) this.lastChunk = last;
  690. return this;
  691. }
  692. overwrite(start, end, content, options) {
  693. options = options || {};
  694. return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
  695. }
  696. update(start, end, content, options) {
  697. start = start + this.offset;
  698. end = end + this.offset;
  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. );
  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. index = index + this.offset;
  758. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  759. this._split(index);
  760. const chunk = this.byEnd[index];
  761. if (chunk) {
  762. chunk.prependLeft(content);
  763. } else {
  764. this.intro = content + this.intro;
  765. }
  766. return this;
  767. }
  768. prependRight(index, content) {
  769. index = index + this.offset;
  770. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  771. this._split(index);
  772. const chunk = this.byStart[index];
  773. if (chunk) {
  774. chunk.prependRight(content);
  775. } else {
  776. this.outro = content + this.outro;
  777. }
  778. return this;
  779. }
  780. remove(start, end) {
  781. start = start + this.offset;
  782. end = end + this.offset;
  783. if (this.original.length !== 0) {
  784. while (start < 0) start += this.original.length;
  785. while (end < 0) end += this.original.length;
  786. }
  787. if (start === end) return this;
  788. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  789. if (start > end) throw new Error('end must be greater than start');
  790. this._split(start);
  791. this._split(end);
  792. let chunk = this.byStart[start];
  793. while (chunk) {
  794. chunk.intro = '';
  795. chunk.outro = '';
  796. chunk.edit('');
  797. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  798. }
  799. return this;
  800. }
  801. reset(start, end) {
  802. start = start + this.offset;
  803. end = end + this.offset;
  804. if (this.original.length !== 0) {
  805. while (start < 0) start += this.original.length;
  806. while (end < 0) end += this.original.length;
  807. }
  808. if (start === end) return this;
  809. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  810. if (start > end) throw new Error('end must be greater than start');
  811. this._split(start);
  812. this._split(end);
  813. let chunk = this.byStart[start];
  814. while (chunk) {
  815. chunk.reset();
  816. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  817. }
  818. return this;
  819. }
  820. lastChar() {
  821. if (this.outro.length) return this.outro[this.outro.length - 1];
  822. let chunk = this.lastChunk;
  823. do {
  824. if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
  825. if (chunk.content.length) return chunk.content[chunk.content.length - 1];
  826. if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
  827. } while ((chunk = chunk.previous));
  828. if (this.intro.length) return this.intro[this.intro.length - 1];
  829. return '';
  830. }
  831. lastLine() {
  832. let lineIndex = this.outro.lastIndexOf(n);
  833. if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
  834. let lineStr = this.outro;
  835. let chunk = this.lastChunk;
  836. do {
  837. if (chunk.outro.length > 0) {
  838. lineIndex = chunk.outro.lastIndexOf(n);
  839. if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
  840. lineStr = chunk.outro + lineStr;
  841. }
  842. if (chunk.content.length > 0) {
  843. lineIndex = chunk.content.lastIndexOf(n);
  844. if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
  845. lineStr = chunk.content + lineStr;
  846. }
  847. if (chunk.intro.length > 0) {
  848. lineIndex = chunk.intro.lastIndexOf(n);
  849. if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
  850. lineStr = chunk.intro + lineStr;
  851. }
  852. } while ((chunk = chunk.previous));
  853. lineIndex = this.intro.lastIndexOf(n);
  854. if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
  855. return this.intro + lineStr;
  856. }
  857. slice(start = 0, end = this.original.length - this.offset) {
  858. start = start + this.offset;
  859. end = end + this.offset;
  860. if (this.original.length !== 0) {
  861. while (start < 0) start += this.original.length;
  862. while (end < 0) end += this.original.length;
  863. }
  864. let result = '';
  865. // find start chunk
  866. let chunk = this.firstChunk;
  867. while (chunk && (chunk.start > start || chunk.end <= start)) {
  868. // found end chunk before start
  869. if (chunk.start < end && chunk.end >= end) {
  870. return result;
  871. }
  872. chunk = chunk.next;
  873. }
  874. if (chunk && chunk.edited && chunk.start !== start)
  875. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  876. const startChunk = chunk;
  877. while (chunk) {
  878. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  879. result += chunk.intro;
  880. }
  881. const containsEnd = chunk.start < end && chunk.end >= end;
  882. if (containsEnd && chunk.edited && chunk.end !== end)
  883. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  884. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  885. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  886. result += chunk.content.slice(sliceStart, sliceEnd);
  887. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  888. result += chunk.outro;
  889. }
  890. if (containsEnd) {
  891. break;
  892. }
  893. chunk = chunk.next;
  894. }
  895. return result;
  896. }
  897. // TODO deprecate this? not really very useful
  898. snip(start, end) {
  899. const clone = this.clone();
  900. clone.remove(0, start);
  901. clone.remove(end, clone.original.length);
  902. return clone;
  903. }
  904. _split(index) {
  905. if (this.byStart[index] || this.byEnd[index]) return;
  906. let chunk = this.lastSearchedChunk;
  907. let previousChunk = chunk;
  908. const searchForward = index > chunk.end;
  909. while (chunk) {
  910. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  911. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  912. // Prevent infinite loop (e.g. via empty chunks, where start === end)
  913. if (chunk === previousChunk) return;
  914. previousChunk = chunk;
  915. }
  916. }
  917. _splitChunk(chunk, index) {
  918. if (chunk.edited && chunk.content.length) {
  919. // zero-length edited chunks are a special case (overlapping replacements)
  920. const loc = getLocator(this.original)(index);
  921. throw new Error(
  922. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  923. );
  924. }
  925. const newChunk = chunk.split(index);
  926. this.byEnd[index] = chunk;
  927. this.byStart[index] = newChunk;
  928. this.byEnd[newChunk.end] = newChunk;
  929. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  930. this.lastSearchedChunk = chunk;
  931. return true;
  932. }
  933. toString() {
  934. let str = this.intro;
  935. let chunk = this.firstChunk;
  936. while (chunk) {
  937. str += chunk.toString();
  938. chunk = chunk.next;
  939. }
  940. return str + this.outro;
  941. }
  942. isEmpty() {
  943. let chunk = this.firstChunk;
  944. do {
  945. if (
  946. (chunk.intro.length && chunk.intro.trim()) ||
  947. (chunk.content.length && chunk.content.trim()) ||
  948. (chunk.outro.length && chunk.outro.trim())
  949. )
  950. return false;
  951. } while ((chunk = chunk.next));
  952. return true;
  953. }
  954. length() {
  955. let chunk = this.firstChunk;
  956. let length = 0;
  957. do {
  958. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  959. } while ((chunk = chunk.next));
  960. return length;
  961. }
  962. trimLines() {
  963. return this.trim('[\\r\\n]');
  964. }
  965. trim(charType) {
  966. return this.trimStart(charType).trimEnd(charType);
  967. }
  968. trimEndAborted(charType) {
  969. const rx = new RegExp((charType || '\\s') + '+$');
  970. this.outro = this.outro.replace(rx, '');
  971. if (this.outro.length) return true;
  972. let chunk = this.lastChunk;
  973. do {
  974. const end = chunk.end;
  975. const aborted = chunk.trimEnd(rx);
  976. // if chunk was trimmed, we have a new lastChunk
  977. if (chunk.end !== end) {
  978. if (this.lastChunk === chunk) {
  979. this.lastChunk = chunk.next;
  980. }
  981. this.byEnd[chunk.end] = chunk;
  982. this.byStart[chunk.next.start] = chunk.next;
  983. this.byEnd[chunk.next.end] = chunk.next;
  984. }
  985. if (aborted) return true;
  986. chunk = chunk.previous;
  987. } while (chunk);
  988. return false;
  989. }
  990. trimEnd(charType) {
  991. this.trimEndAborted(charType);
  992. return this;
  993. }
  994. trimStartAborted(charType) {
  995. const rx = new RegExp('^' + (charType || '\\s') + '+');
  996. this.intro = this.intro.replace(rx, '');
  997. if (this.intro.length) return true;
  998. let chunk = this.firstChunk;
  999. do {
  1000. const end = chunk.end;
  1001. const aborted = chunk.trimStart(rx);
  1002. if (chunk.end !== end) {
  1003. // special case...
  1004. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  1005. this.byEnd[chunk.end] = chunk;
  1006. this.byStart[chunk.next.start] = chunk.next;
  1007. this.byEnd[chunk.next.end] = chunk.next;
  1008. }
  1009. if (aborted) return true;
  1010. chunk = chunk.next;
  1011. } while (chunk);
  1012. return false;
  1013. }
  1014. trimStart(charType) {
  1015. this.trimStartAborted(charType);
  1016. return this;
  1017. }
  1018. hasChanged() {
  1019. return this.original !== this.toString();
  1020. }
  1021. _replaceRegexp(searchValue, replacement) {
  1022. function getReplacement(match, str) {
  1023. if (typeof replacement === 'string') {
  1024. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  1025. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  1026. if (i === '$') return '$';
  1027. if (i === '&') return match[0];
  1028. const num = +i;
  1029. if (num < match.length) return match[+i];
  1030. return `$${i}`;
  1031. });
  1032. } else {
  1033. return replacement(...match, match.index, str, match.groups);
  1034. }
  1035. }
  1036. function matchAll(re, str) {
  1037. let match;
  1038. const matches = [];
  1039. while ((match = re.exec(str))) {
  1040. matches.push(match);
  1041. }
  1042. return matches;
  1043. }
  1044. if (searchValue.global) {
  1045. const matches = matchAll(searchValue, this.original);
  1046. matches.forEach((match) => {
  1047. if (match.index != null) {
  1048. const replacement = getReplacement(match, this.original);
  1049. if (replacement !== match[0]) {
  1050. this.overwrite(match.index, match.index + match[0].length, replacement);
  1051. }
  1052. }
  1053. });
  1054. } else {
  1055. const match = this.original.match(searchValue);
  1056. if (match && match.index != null) {
  1057. const replacement = getReplacement(match, this.original);
  1058. if (replacement !== match[0]) {
  1059. this.overwrite(match.index, match.index + match[0].length, replacement);
  1060. }
  1061. }
  1062. }
  1063. return this;
  1064. }
  1065. _replaceString(string, replacement) {
  1066. const { original } = this;
  1067. const index = original.indexOf(string);
  1068. if (index !== -1) {
  1069. this.overwrite(index, index + string.length, replacement);
  1070. }
  1071. return this;
  1072. }
  1073. replace(searchValue, replacement) {
  1074. if (typeof searchValue === 'string') {
  1075. return this._replaceString(searchValue, replacement);
  1076. }
  1077. return this._replaceRegexp(searchValue, replacement);
  1078. }
  1079. _replaceAllString(string, replacement) {
  1080. const { original } = this;
  1081. const stringLength = string.length;
  1082. for (
  1083. let index = original.indexOf(string);
  1084. index !== -1;
  1085. index = original.indexOf(string, index + stringLength)
  1086. ) {
  1087. const previous = original.slice(index, index + stringLength);
  1088. if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
  1089. }
  1090. return this;
  1091. }
  1092. replaceAll(searchValue, replacement) {
  1093. if (typeof searchValue === 'string') {
  1094. return this._replaceAllString(searchValue, replacement);
  1095. }
  1096. if (!searchValue.global) {
  1097. throw new TypeError(
  1098. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  1099. );
  1100. }
  1101. return this._replaceRegexp(searchValue, replacement);
  1102. }
  1103. }
  1104. const hasOwnProp = Object.prototype.hasOwnProperty;
  1105. class Bundle {
  1106. constructor(options = {}) {
  1107. this.intro = options.intro || '';
  1108. this.separator = options.separator !== undefined ? options.separator : '\n';
  1109. this.sources = [];
  1110. this.uniqueSources = [];
  1111. this.uniqueSourceIndexByFilename = {};
  1112. }
  1113. addSource(source) {
  1114. if (source instanceof MagicString) {
  1115. return this.addSource({
  1116. content: source,
  1117. filename: source.filename,
  1118. separator: this.separator,
  1119. });
  1120. }
  1121. if (!isObject(source) || !source.content) {
  1122. throw new Error(
  1123. 'bundle.addSource() takes an object with a `content` property, which should be an instance of MagicString, and an optional `filename`',
  1124. );
  1125. }
  1126. ['filename', 'ignoreList', 'indentExclusionRanges', 'separator'].forEach((option) => {
  1127. if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
  1128. });
  1129. if (source.separator === undefined) {
  1130. // TODO there's a bunch of this sort of thing, needs cleaning up
  1131. source.separator = this.separator;
  1132. }
  1133. if (source.filename) {
  1134. if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
  1135. this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
  1136. this.uniqueSources.push({ filename: source.filename, content: source.content.original });
  1137. } else {
  1138. const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
  1139. if (source.content.original !== uniqueSource.content) {
  1140. throw new Error(`Illegal source: same filename (${source.filename}), different contents`);
  1141. }
  1142. }
  1143. }
  1144. this.sources.push(source);
  1145. return this;
  1146. }
  1147. append(str, options) {
  1148. this.addSource({
  1149. content: new MagicString(str),
  1150. separator: (options && options.separator) || '',
  1151. });
  1152. return this;
  1153. }
  1154. clone() {
  1155. const bundle = new Bundle({
  1156. intro: this.intro,
  1157. separator: this.separator,
  1158. });
  1159. this.sources.forEach((source) => {
  1160. bundle.addSource({
  1161. filename: source.filename,
  1162. content: source.content.clone(),
  1163. separator: source.separator,
  1164. });
  1165. });
  1166. return bundle;
  1167. }
  1168. generateDecodedMap(options = {}) {
  1169. const names = [];
  1170. let x_google_ignoreList = undefined;
  1171. this.sources.forEach((source) => {
  1172. Object.keys(source.content.storedNames).forEach((name) => {
  1173. if (!~names.indexOf(name)) names.push(name);
  1174. });
  1175. });
  1176. const mappings = new Mappings(options.hires);
  1177. if (this.intro) {
  1178. mappings.advance(this.intro);
  1179. }
  1180. this.sources.forEach((source, i) => {
  1181. if (i > 0) {
  1182. mappings.advance(this.separator);
  1183. }
  1184. const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1;
  1185. const magicString = source.content;
  1186. const locate = getLocator(magicString.original);
  1187. if (magicString.intro) {
  1188. mappings.advance(magicString.intro);
  1189. }
  1190. magicString.firstChunk.eachNext((chunk) => {
  1191. const loc = locate(chunk.start);
  1192. if (chunk.intro.length) mappings.advance(chunk.intro);
  1193. if (source.filename) {
  1194. if (chunk.edited) {
  1195. mappings.addEdit(
  1196. sourceIndex,
  1197. chunk.content,
  1198. loc,
  1199. chunk.storeName ? names.indexOf(chunk.original) : -1,
  1200. );
  1201. } else {
  1202. mappings.addUneditedChunk(
  1203. sourceIndex,
  1204. chunk,
  1205. magicString.original,
  1206. loc,
  1207. magicString.sourcemapLocations,
  1208. );
  1209. }
  1210. } else {
  1211. mappings.advance(chunk.content);
  1212. }
  1213. if (chunk.outro.length) mappings.advance(chunk.outro);
  1214. });
  1215. if (magicString.outro) {
  1216. mappings.advance(magicString.outro);
  1217. }
  1218. if (source.ignoreList && sourceIndex !== -1) {
  1219. if (x_google_ignoreList === undefined) {
  1220. x_google_ignoreList = [];
  1221. }
  1222. x_google_ignoreList.push(sourceIndex);
  1223. }
  1224. });
  1225. return {
  1226. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  1227. sources: this.uniqueSources.map((source) => {
  1228. return options.file ? getRelativePath(options.file, source.filename) : source.filename;
  1229. }),
  1230. sourcesContent: this.uniqueSources.map((source) => {
  1231. return options.includeContent ? source.content : null;
  1232. }),
  1233. names,
  1234. mappings: mappings.raw,
  1235. x_google_ignoreList,
  1236. };
  1237. }
  1238. generateMap(options) {
  1239. return new SourceMap(this.generateDecodedMap(options));
  1240. }
  1241. getIndentString() {
  1242. const indentStringCounts = {};
  1243. this.sources.forEach((source) => {
  1244. const indentStr = source.content._getRawIndentString();
  1245. if (indentStr === null) return;
  1246. if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
  1247. indentStringCounts[indentStr] += 1;
  1248. });
  1249. return (
  1250. Object.keys(indentStringCounts).sort((a, b) => {
  1251. return indentStringCounts[a] - indentStringCounts[b];
  1252. })[0] || '\t'
  1253. );
  1254. }
  1255. indent(indentStr) {
  1256. if (!arguments.length) {
  1257. indentStr = this.getIndentString();
  1258. }
  1259. if (indentStr === '') return this; // noop
  1260. let trailingNewline = !this.intro || this.intro.slice(-1) === '\n';
  1261. this.sources.forEach((source, i) => {
  1262. const separator = source.separator !== undefined ? source.separator : this.separator;
  1263. const indentStart = trailingNewline || (i > 0 && /\r?\n$/.test(separator));
  1264. source.content.indent(indentStr, {
  1265. exclude: source.indentExclusionRanges,
  1266. indentStart, //: trailingNewline || /\r?\n$/.test( separator ) //true///\r?\n/.test( separator )
  1267. });
  1268. trailingNewline = source.content.lastChar() === '\n';
  1269. });
  1270. if (this.intro) {
  1271. this.intro =
  1272. indentStr +
  1273. this.intro.replace(/^[^\n]/gm, (match, index) => {
  1274. return index > 0 ? indentStr + match : match;
  1275. });
  1276. }
  1277. return this;
  1278. }
  1279. prepend(str) {
  1280. this.intro = str + this.intro;
  1281. return this;
  1282. }
  1283. toString() {
  1284. const body = this.sources
  1285. .map((source, i) => {
  1286. const separator = source.separator !== undefined ? source.separator : this.separator;
  1287. const str = (i > 0 ? separator : '') + source.content.toString();
  1288. return str;
  1289. })
  1290. .join('');
  1291. return this.intro + body;
  1292. }
  1293. isEmpty() {
  1294. if (this.intro.length && this.intro.trim()) return false;
  1295. if (this.sources.some((source) => !source.content.isEmpty())) return false;
  1296. return true;
  1297. }
  1298. length() {
  1299. return this.sources.reduce(
  1300. (length, source) => length + source.content.length(),
  1301. this.intro.length,
  1302. );
  1303. }
  1304. trimLines() {
  1305. return this.trim('[\\r\\n]');
  1306. }
  1307. trim(charType) {
  1308. return this.trimStart(charType).trimEnd(charType);
  1309. }
  1310. trimStart(charType) {
  1311. const rx = new RegExp('^' + (charType || '\\s') + '+');
  1312. this.intro = this.intro.replace(rx, '');
  1313. if (!this.intro) {
  1314. let source;
  1315. let i = 0;
  1316. do {
  1317. source = this.sources[i++];
  1318. if (!source) {
  1319. break;
  1320. }
  1321. } while (!source.content.trimStartAborted(charType));
  1322. }
  1323. return this;
  1324. }
  1325. trimEnd(charType) {
  1326. const rx = new RegExp((charType || '\\s') + '+$');
  1327. let source;
  1328. let i = this.sources.length - 1;
  1329. do {
  1330. source = this.sources[i--];
  1331. if (!source) {
  1332. this.intro = this.intro.replace(rx, '');
  1333. break;
  1334. }
  1335. } while (!source.content.trimEndAborted(charType));
  1336. return this;
  1337. }
  1338. }
  1339. MagicString.Bundle = Bundle;
  1340. MagicString.SourceMap = SourceMap;
  1341. MagicString.default = MagicString; // work around TypeScript bug https://github.com/Rich-Harris/magic-string/pull/121
  1342. return MagicString;
  1343. }));
  1344. //# sourceMappingURL=magic-string.umd.js.map