magic-string.umd.js 42 KB

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