magic-string.es.mjs 38 KB

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