@m2c2kit/build-helpers 0.3.28 → 0.3.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1418 -134
  2. package/package.json +9 -9
package/dist/index.js CHANGED
@@ -5589,100 +5589,91 @@ const selectAll = getSelectorFunc((query, elems, options) => query === boolbaseE
5589
5589
  ? []
5590
5590
  : findAll(query, elems, options));
5591
5591
 
5592
- const comma = ','.charCodeAt(0);
5593
- const semicolon = ';'.charCodeAt(0);
5594
- const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
5595
- const intToChar = new Uint8Array(64); // 64 possible chars.
5596
- const charToInt = new Uint8Array(128); // z is 122 in ASCII
5592
+ // src/vlq.ts
5593
+ var comma = ",".charCodeAt(0);
5594
+ var semicolon = ";".charCodeAt(0);
5595
+ var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5596
+ var intToChar = new Uint8Array(64);
5597
+ var charToInt = new Uint8Array(128);
5597
5598
  for (let i = 0; i < chars.length; i++) {
5598
- const c = chars.charCodeAt(i);
5599
- intToChar[i] = c;
5600
- charToInt[c] = i;
5599
+ const c = chars.charCodeAt(i);
5600
+ intToChar[i] = c;
5601
+ charToInt[c] = i;
5601
5602
  }
5602
5603
  function encodeInteger(builder, num, relative) {
5603
- let delta = num - relative;
5604
- delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
5605
- do {
5606
- let clamped = delta & 0b011111;
5607
- delta >>>= 5;
5608
- if (delta > 0)
5609
- clamped |= 0b100000;
5610
- builder.write(intToChar[clamped]);
5611
- } while (delta > 0);
5612
- return num;
5613
- }
5614
-
5615
- const bufLength = 1024 * 16;
5616
- // Provide a fallback for older environments.
5617
- const td = typeof TextDecoder !== 'undefined'
5618
- ? /* #__PURE__ */ new TextDecoder()
5619
- : typeof Buffer !== 'undefined'
5620
- ? {
5621
- decode(buf) {
5622
- const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
5623
- return out.toString();
5624
- },
5625
- }
5626
- : {
5627
- decode(buf) {
5628
- let out = '';
5629
- for (let i = 0; i < buf.length; i++) {
5630
- out += String.fromCharCode(buf[i]);
5631
- }
5632
- return out;
5633
- },
5634
- };
5635
- class StringWriter {
5636
- constructor() {
5637
- this.pos = 0;
5638
- this.out = '';
5639
- this.buffer = new Uint8Array(bufLength);
5640
- }
5641
- write(v) {
5642
- const { buffer } = this;
5643
- buffer[this.pos++] = v;
5644
- if (this.pos === bufLength) {
5645
- this.out += td.decode(buffer);
5646
- this.pos = 0;
5647
- }
5604
+ let delta = num - relative;
5605
+ delta = delta < 0 ? -delta << 1 | 1 : delta << 1;
5606
+ do {
5607
+ let clamped = delta & 31;
5608
+ delta >>>= 5;
5609
+ if (delta > 0) clamped |= 32;
5610
+ builder.write(intToChar[clamped]);
5611
+ } while (delta > 0);
5612
+ return num;
5613
+ }
5614
+
5615
+ // src/strings.ts
5616
+ var bufLength = 1024 * 16;
5617
+ var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
5618
+ decode(buf) {
5619
+ const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
5620
+ return out.toString();
5621
+ }
5622
+ } : {
5623
+ decode(buf) {
5624
+ let out = "";
5625
+ for (let i = 0; i < buf.length; i++) {
5626
+ out += String.fromCharCode(buf[i]);
5648
5627
  }
5649
- flush() {
5650
- const { buffer, out, pos } = this;
5651
- return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
5628
+ return out;
5629
+ }
5630
+ };
5631
+ var StringWriter = class {
5632
+ constructor() {
5633
+ this.pos = 0;
5634
+ this.out = "";
5635
+ this.buffer = new Uint8Array(bufLength);
5636
+ }
5637
+ write(v) {
5638
+ const { buffer } = this;
5639
+ buffer[this.pos++] = v;
5640
+ if (this.pos === bufLength) {
5641
+ this.out += td.decode(buffer);
5642
+ this.pos = 0;
5652
5643
  }
5653
- }
5644
+ }
5645
+ flush() {
5646
+ const { buffer, out, pos } = this;
5647
+ return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
5648
+ }
5649
+ };
5654
5650
  function encode(decoded) {
5655
- const writer = new StringWriter();
5656
- let sourcesIndex = 0;
5657
- let sourceLine = 0;
5658
- let sourceColumn = 0;
5659
- let namesIndex = 0;
5660
- for (let i = 0; i < decoded.length; i++) {
5661
- const line = decoded[i];
5662
- if (i > 0)
5663
- writer.write(semicolon);
5664
- if (line.length === 0)
5665
- continue;
5666
- let genColumn = 0;
5667
- for (let j = 0; j < line.length; j++) {
5668
- const segment = line[j];
5669
- if (j > 0)
5670
- writer.write(comma);
5671
- genColumn = encodeInteger(writer, segment[0], genColumn);
5672
- if (segment.length === 1)
5673
- continue;
5674
- sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
5675
- sourceLine = encodeInteger(writer, segment[2], sourceLine);
5676
- sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
5677
- if (segment.length === 4)
5678
- continue;
5679
- namesIndex = encodeInteger(writer, segment[4], namesIndex);
5680
- }
5651
+ const writer = new StringWriter();
5652
+ let sourcesIndex = 0;
5653
+ let sourceLine = 0;
5654
+ let sourceColumn = 0;
5655
+ let namesIndex = 0;
5656
+ for (let i = 0; i < decoded.length; i++) {
5657
+ const line = decoded[i];
5658
+ if (i > 0) writer.write(semicolon);
5659
+ if (line.length === 0) continue;
5660
+ let genColumn = 0;
5661
+ for (let j = 0; j < line.length; j++) {
5662
+ const segment = line[j];
5663
+ if (j > 0) writer.write(comma);
5664
+ genColumn = encodeInteger(writer, segment[0], genColumn);
5665
+ if (segment.length === 1) continue;
5666
+ sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
5667
+ sourceLine = encodeInteger(writer, segment[2], sourceLine);
5668
+ sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
5669
+ if (segment.length === 4) continue;
5670
+ namesIndex = encodeInteger(writer, segment[4], namesIndex);
5681
5671
  }
5682
- return writer.flush();
5672
+ }
5673
+ return writer.flush();
5683
5674
  }
5684
5675
 
5685
- class BitSet {
5676
+ let BitSet$1 = class BitSet {
5686
5677
  constructor(arg) {
5687
5678
  this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
5688
5679
  }
@@ -5694,9 +5685,9 @@ class BitSet {
5694
5685
  has(n) {
5695
5686
  return !!(this.bits[n >> 5] & (1 << (n & 31)));
5696
5687
  }
5697
- }
5688
+ };
5698
5689
 
5699
- class Chunk {
5690
+ let Chunk$1 = class Chunk {
5700
5691
  constructor(start, end, content) {
5701
5692
  this.start = start;
5702
5693
  this.end = end;
@@ -5873,9 +5864,9 @@ class Chunk {
5873
5864
  if (this.outro.length) return true;
5874
5865
  }
5875
5866
  }
5876
- }
5867
+ };
5877
5868
 
5878
- function getBtoa() {
5869
+ function getBtoa$1() {
5879
5870
  if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
5880
5871
  return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
5881
5872
  } else if (typeof Buffer === 'function') {
@@ -5887,9 +5878,9 @@ function getBtoa() {
5887
5878
  }
5888
5879
  }
5889
5880
 
5890
- const btoa = /*#__PURE__*/ getBtoa();
5881
+ const btoa$1 = /*#__PURE__*/ getBtoa$1();
5891
5882
 
5892
- class SourceMap {
5883
+ let SourceMap$1 = class SourceMap {
5893
5884
  constructor(properties) {
5894
5885
  this.version = 3;
5895
5886
  this.file = properties.file;
@@ -5910,11 +5901,11 @@ class SourceMap {
5910
5901
  }
5911
5902
 
5912
5903
  toUrl() {
5913
- return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
5904
+ return 'data:application/json;charset=utf-8;base64,' + btoa$1(this.toString());
5914
5905
  }
5915
- }
5906
+ };
5916
5907
 
5917
- function guessIndent(code) {
5908
+ function guessIndent$1(code) {
5918
5909
  const lines = code.split('\n');
5919
5910
 
5920
5911
  const tabbed = lines.filter((line) => /^\t+/.test(line));
@@ -5940,7 +5931,7 @@ function guessIndent(code) {
5940
5931
  return new Array(min + 1).join(' ');
5941
5932
  }
5942
5933
 
5943
- function getRelativePath(from, to) {
5934
+ function getRelativePath$1(from, to) {
5944
5935
  const fromParts = from.split(/[/\\]/);
5945
5936
  const toParts = to.split(/[/\\]/);
5946
5937
 
@@ -5959,13 +5950,13 @@ function getRelativePath(from, to) {
5959
5950
  return fromParts.concat(toParts).join('/');
5960
5951
  }
5961
5952
 
5962
- const toString = Object.prototype.toString;
5953
+ const toString$1 = Object.prototype.toString;
5963
5954
 
5964
- function isObject(thing) {
5965
- return toString.call(thing) === '[object Object]';
5955
+ function isObject$1(thing) {
5956
+ return toString$1.call(thing) === '[object Object]';
5966
5957
  }
5967
5958
 
5968
- function getLocator(source) {
5959
+ function getLocator$1(source) {
5969
5960
  const originalLines = source.split('\n');
5970
5961
  const lineOffsets = [];
5971
5962
 
@@ -5991,9 +5982,9 @@ function getLocator(source) {
5991
5982
  };
5992
5983
  }
5993
5984
 
5994
- const wordRegex = /\w/;
5985
+ const wordRegex$1 = /\w/;
5995
5986
 
5996
- class Mappings {
5987
+ let Mappings$1 = class Mappings {
5997
5988
  constructor(hires) {
5998
5989
  this.hires = hires;
5999
5990
  this.generatedCodeLine = 0;
@@ -6061,7 +6052,7 @@ class Mappings {
6061
6052
 
6062
6053
  if (this.hires === 'boundary') {
6063
6054
  // in hires "boundary", group segments per word boundary than per char
6064
- if (wordRegex.test(original[originalCharIndex])) {
6055
+ if (wordRegex$1.test(original[originalCharIndex])) {
6065
6056
  // for first char in the boundary found, start the boundary by pushing a segment
6066
6057
  if (!charInHiresBoundary) {
6067
6058
  this.rawSegments.push(segment);
@@ -6103,19 +6094,19 @@ class Mappings {
6103
6094
 
6104
6095
  this.generatedCodeColumn += lines[lines.length - 1].length;
6105
6096
  }
6106
- }
6097
+ };
6107
6098
 
6108
- const n = '\n';
6099
+ const n$1 = '\n';
6109
6100
 
6110
- const warned = {
6101
+ const warned$1 = {
6111
6102
  insertLeft: false,
6112
6103
  insertRight: false,
6113
6104
  storeName: false,
6114
6105
  };
6115
6106
 
6116
- class MagicString {
6107
+ let MagicString$1 = class MagicString {
6117
6108
  constructor(string, options = {}) {
6118
- const chunk = new Chunk(0, string.length, string);
6109
+ const chunk = new Chunk$1(0, string.length, string);
6119
6110
 
6120
6111
  Object.defineProperties(this, {
6121
6112
  original: { writable: true, value: string },
@@ -6128,7 +6119,7 @@ class MagicString {
6128
6119
  byEnd: { writable: true, value: {} },
6129
6120
  filename: { writable: true, value: options.filename },
6130
6121
  indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
6131
- sourcemapLocations: { writable: true, value: new BitSet() },
6122
+ sourcemapLocations: { writable: true, value: new BitSet$1() },
6132
6123
  storedNames: { writable: true, value: {} },
6133
6124
  indentStr: { writable: true, value: undefined },
6134
6125
  ignoreList: { writable: true, value: options.ignoreList },
@@ -6213,7 +6204,7 @@ class MagicString {
6213
6204
  cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
6214
6205
  }
6215
6206
 
6216
- cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
6207
+ cloned.sourcemapLocations = new BitSet$1(this.sourcemapLocations);
6217
6208
 
6218
6209
  cloned.intro = this.intro;
6219
6210
  cloned.outro = this.outro;
@@ -6226,9 +6217,9 @@ class MagicString {
6226
6217
 
6227
6218
  const sourceIndex = 0;
6228
6219
  const names = Object.keys(this.storedNames);
6229
- const mappings = new Mappings(options.hires);
6220
+ const mappings = new Mappings$1(options.hires);
6230
6221
 
6231
- const locate = getLocator(this.original);
6222
+ const locate = getLocator$1(this.original);
6232
6223
 
6233
6224
  if (this.intro) {
6234
6225
  mappings.advance(this.intro);
@@ -6253,10 +6244,14 @@ class MagicString {
6253
6244
  if (chunk.outro.length) mappings.advance(chunk.outro);
6254
6245
  });
6255
6246
 
6247
+ if (this.outro) {
6248
+ mappings.advance(this.outro);
6249
+ }
6250
+
6256
6251
  return {
6257
6252
  file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
6258
6253
  sources: [
6259
- options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
6254
+ options.source ? getRelativePath$1(options.file || '', options.source) : options.file || '',
6260
6255
  ],
6261
6256
  sourcesContent: options.includeContent ? [this.original] : undefined,
6262
6257
  names,
@@ -6266,12 +6261,12 @@ class MagicString {
6266
6261
  }
6267
6262
 
6268
6263
  generateMap(options) {
6269
- return new SourceMap(this.generateDecodedMap(options));
6264
+ return new SourceMap$1(this.generateDecodedMap(options));
6270
6265
  }
6271
6266
 
6272
6267
  _ensureindentStr() {
6273
6268
  if (this.indentStr === undefined) {
6274
- this.indentStr = guessIndent(this.original);
6269
+ this.indentStr = guessIndent$1(this.original);
6275
6270
  }
6276
6271
  }
6277
6272
 
@@ -6288,7 +6283,7 @@ class MagicString {
6288
6283
  indent(indentStr, options) {
6289
6284
  const pattern = /^[^\r\n]/gm;
6290
6285
 
6291
- if (isObject(indentStr)) {
6286
+ if (isObject$1(indentStr)) {
6292
6287
  options = indentStr;
6293
6288
  indentStr = undefined;
6294
6289
  }
@@ -6380,22 +6375,22 @@ class MagicString {
6380
6375
  }
6381
6376
 
6382
6377
  insertLeft(index, content) {
6383
- if (!warned.insertLeft) {
6378
+ if (!warned$1.insertLeft) {
6384
6379
  console.warn(
6385
6380
  'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
6386
6381
  );
6387
- warned.insertLeft = true;
6382
+ warned$1.insertLeft = true;
6388
6383
  }
6389
6384
 
6390
6385
  return this.appendLeft(index, content);
6391
6386
  }
6392
6387
 
6393
6388
  insertRight(index, content) {
6394
- if (!warned.insertRight) {
6389
+ if (!warned$1.insertRight) {
6395
6390
  console.warn(
6396
6391
  'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
6397
6392
  );
6398
- warned.insertRight = true;
6393
+ warned$1.insertRight = true;
6399
6394
  }
6400
6395
 
6401
6396
  return this.prependRight(index, content);
@@ -6468,11 +6463,11 @@ class MagicString {
6468
6463
  this._split(end);
6469
6464
 
6470
6465
  if (options === true) {
6471
- if (!warned.storeName) {
6466
+ if (!warned$1.storeName) {
6472
6467
  console.warn(
6473
6468
  'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
6474
6469
  );
6475
- warned.storeName = true;
6470
+ warned$1.storeName = true;
6476
6471
  }
6477
6472
 
6478
6473
  options = { storeName: true };
@@ -6505,7 +6500,7 @@ class MagicString {
6505
6500
  first.edit(content, storeName, !overwrite);
6506
6501
  } else {
6507
6502
  // must be inserting at the end
6508
- const newChunk = new Chunk(start, end, '').edit(content, storeName);
6503
+ const newChunk = new Chunk$1(start, end, '').edit(content, storeName);
6509
6504
 
6510
6505
  // TODO last chunk in the array may not be the last chunk, if it's moved...
6511
6506
  last.next = newChunk;
@@ -6624,30 +6619,30 @@ class MagicString {
6624
6619
  }
6625
6620
 
6626
6621
  lastLine() {
6627
- let lineIndex = this.outro.lastIndexOf(n);
6622
+ let lineIndex = this.outro.lastIndexOf(n$1);
6628
6623
  if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
6629
6624
  let lineStr = this.outro;
6630
6625
  let chunk = this.lastChunk;
6631
6626
  do {
6632
6627
  if (chunk.outro.length > 0) {
6633
- lineIndex = chunk.outro.lastIndexOf(n);
6628
+ lineIndex = chunk.outro.lastIndexOf(n$1);
6634
6629
  if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
6635
6630
  lineStr = chunk.outro + lineStr;
6636
6631
  }
6637
6632
 
6638
6633
  if (chunk.content.length > 0) {
6639
- lineIndex = chunk.content.lastIndexOf(n);
6634
+ lineIndex = chunk.content.lastIndexOf(n$1);
6640
6635
  if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
6641
6636
  lineStr = chunk.content + lineStr;
6642
6637
  }
6643
6638
 
6644
6639
  if (chunk.intro.length > 0) {
6645
- lineIndex = chunk.intro.lastIndexOf(n);
6640
+ lineIndex = chunk.intro.lastIndexOf(n$1);
6646
6641
  if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
6647
6642
  lineStr = chunk.intro + lineStr;
6648
6643
  }
6649
6644
  } while ((chunk = chunk.previous));
6650
- lineIndex = this.intro.lastIndexOf(n);
6645
+ lineIndex = this.intro.lastIndexOf(n$1);
6651
6646
  if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
6652
6647
  return this.intro + lineStr;
6653
6648
  }
@@ -6719,19 +6714,25 @@ class MagicString {
6719
6714
  if (this.byStart[index] || this.byEnd[index]) return;
6720
6715
 
6721
6716
  let chunk = this.lastSearchedChunk;
6717
+ let previousChunk = chunk;
6722
6718
  const searchForward = index > chunk.end;
6723
6719
 
6724
6720
  while (chunk) {
6725
6721
  if (chunk.contains(index)) return this._splitChunk(chunk, index);
6726
6722
 
6727
6723
  chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
6724
+
6725
+ // Prevent infinite loop (e.g. via empty chunks, where start === end)
6726
+ if (chunk === previousChunk) return;
6727
+
6728
+ previousChunk = chunk;
6728
6729
  }
6729
6730
  }
6730
6731
 
6731
6732
  _splitChunk(chunk, index) {
6732
6733
  if (chunk.edited && chunk.content.length) {
6733
6734
  // zero-length edited chunks are a special case (overlapping replacements)
6734
- const loc = getLocator(this.original)(index);
6735
+ const loc = getLocator$1(this.original)(index);
6735
6736
  throw new Error(
6736
6737
  `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
6737
6738
  );
@@ -6912,7 +6913,12 @@ class MagicString {
6912
6913
  const index = original.indexOf(string);
6913
6914
 
6914
6915
  if (index !== -1) {
6915
- this.overwrite(index, index + string.length, replacement);
6916
+ if (typeof replacement === 'function') {
6917
+ replacement = replacement(string, index, original);
6918
+ }
6919
+ if (string !== replacement) {
6920
+ this.overwrite(index, index + string.length, replacement);
6921
+ }
6916
6922
  }
6917
6923
 
6918
6924
  return this;
@@ -6935,7 +6941,11 @@ class MagicString {
6935
6941
  index = original.indexOf(string, index + stringLength)
6936
6942
  ) {
6937
6943
  const previous = original.slice(index, index + stringLength);
6938
- if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
6944
+ let _replacement = replacement;
6945
+ if (typeof replacement === 'function') {
6946
+ _replacement = replacement(previous, index, original);
6947
+ }
6948
+ if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);
6939
6949
  }
6940
6950
 
6941
6951
  return this;
@@ -6954,7 +6964,7 @@ class MagicString {
6954
6964
 
6955
6965
  return this._replaceRegexp(searchValue, replacement);
6956
6966
  }
6957
- }
6967
+ };
6958
6968
 
6959
6969
  const HASH_CHARACTER_LENGTH = 16;
6960
6970
  function hashM2c2kitAssets(rootDir, cwd = "") {
@@ -6963,7 +6973,7 @@ function hashM2c2kitAssets(rootDir, cwd = "") {
6963
6973
  name: "hash-m2c2kit-assets",
6964
6974
  renderChunk: {
6965
6975
  handler(code) {
6966
- const magicString = new MagicString(code);
6976
+ const magicString = new MagicString$1(code);
6967
6977
  magicString.replace(
6968
6978
  new RegExp("__NO_M2C2KIT_MANIFEST_JSON_URL__", "g"),
6969
6979
  "manifest.json"
@@ -7168,7 +7178,7 @@ function restoreImportMeta(pattern = "import_meta = {};", replacement = "import_
7168
7178
  name: "restore-import-meta",
7169
7179
  renderChunk: {
7170
7180
  handler(code) {
7171
- const magicString = new MagicString(code);
7181
+ const magicString = new MagicString$1(code);
7172
7182
  magicString.replace(new RegExp(pattern, "g"), replacement);
7173
7183
  return {
7174
7184
  code: magicString.toString(),
@@ -23043,7 +23053,7 @@ function addModuleMetadata() {
23043
23053
  transform: {
23044
23054
  async handler(code) {
23045
23055
  const pkg = JSON.parse(await readFile("./package.json", "utf8"));
23046
- const magicString = new MagicString(code);
23056
+ const magicString = new MagicString$1(code);
23047
23057
  magicString.replace(
23048
23058
  new RegExp(
23049
23059
  "moduleMetadata:\\s*Constants.MODULE_METADATA_PLACEHOLDER",
@@ -23060,6 +23070,1280 @@ function addModuleMetadata() {
23060
23070
  };
23061
23071
  }
23062
23072
 
23073
+ class BitSet {
23074
+ constructor(arg) {
23075
+ this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
23076
+ }
23077
+
23078
+ add(n) {
23079
+ this.bits[n >> 5] |= 1 << (n & 31);
23080
+ }
23081
+
23082
+ has(n) {
23083
+ return !!(this.bits[n >> 5] & (1 << (n & 31)));
23084
+ }
23085
+ }
23086
+
23087
+ class Chunk {
23088
+ constructor(start, end, content) {
23089
+ this.start = start;
23090
+ this.end = end;
23091
+ this.original = content;
23092
+
23093
+ this.intro = '';
23094
+ this.outro = '';
23095
+
23096
+ this.content = content;
23097
+ this.storeName = false;
23098
+ this.edited = false;
23099
+
23100
+ {
23101
+ this.previous = null;
23102
+ this.next = null;
23103
+ }
23104
+ }
23105
+
23106
+ appendLeft(content) {
23107
+ this.outro += content;
23108
+ }
23109
+
23110
+ appendRight(content) {
23111
+ this.intro = this.intro + content;
23112
+ }
23113
+
23114
+ clone() {
23115
+ const chunk = new Chunk(this.start, this.end, this.original);
23116
+
23117
+ chunk.intro = this.intro;
23118
+ chunk.outro = this.outro;
23119
+ chunk.content = this.content;
23120
+ chunk.storeName = this.storeName;
23121
+ chunk.edited = this.edited;
23122
+
23123
+ return chunk;
23124
+ }
23125
+
23126
+ contains(index) {
23127
+ return this.start < index && index < this.end;
23128
+ }
23129
+
23130
+ eachNext(fn) {
23131
+ let chunk = this;
23132
+ while (chunk) {
23133
+ fn(chunk);
23134
+ chunk = chunk.next;
23135
+ }
23136
+ }
23137
+
23138
+ eachPrevious(fn) {
23139
+ let chunk = this;
23140
+ while (chunk) {
23141
+ fn(chunk);
23142
+ chunk = chunk.previous;
23143
+ }
23144
+ }
23145
+
23146
+ edit(content, storeName, contentOnly) {
23147
+ this.content = content;
23148
+ if (!contentOnly) {
23149
+ this.intro = '';
23150
+ this.outro = '';
23151
+ }
23152
+ this.storeName = storeName;
23153
+
23154
+ this.edited = true;
23155
+
23156
+ return this;
23157
+ }
23158
+
23159
+ prependLeft(content) {
23160
+ this.outro = content + this.outro;
23161
+ }
23162
+
23163
+ prependRight(content) {
23164
+ this.intro = content + this.intro;
23165
+ }
23166
+
23167
+ reset() {
23168
+ this.intro = '';
23169
+ this.outro = '';
23170
+ if (this.edited) {
23171
+ this.content = this.original;
23172
+ this.storeName = false;
23173
+ this.edited = false;
23174
+ }
23175
+ }
23176
+
23177
+ split(index) {
23178
+ const sliceIndex = index - this.start;
23179
+
23180
+ const originalBefore = this.original.slice(0, sliceIndex);
23181
+ const originalAfter = this.original.slice(sliceIndex);
23182
+
23183
+ this.original = originalBefore;
23184
+
23185
+ const newChunk = new Chunk(index, this.end, originalAfter);
23186
+ newChunk.outro = this.outro;
23187
+ this.outro = '';
23188
+
23189
+ this.end = index;
23190
+
23191
+ if (this.edited) {
23192
+ // after split we should save the edit content record into the correct chunk
23193
+ // to make sure sourcemap correct
23194
+ // For example:
23195
+ // ' test'.trim()
23196
+ // split -> ' ' + 'test'
23197
+ // ✔️ edit -> '' + 'test'
23198
+ // ✖️ edit -> 'test' + ''
23199
+ // TODO is this block necessary?...
23200
+ newChunk.edit('', false);
23201
+ this.content = '';
23202
+ } else {
23203
+ this.content = originalBefore;
23204
+ }
23205
+
23206
+ newChunk.next = this.next;
23207
+ if (newChunk.next) newChunk.next.previous = newChunk;
23208
+ newChunk.previous = this;
23209
+ this.next = newChunk;
23210
+
23211
+ return newChunk;
23212
+ }
23213
+
23214
+ toString() {
23215
+ return this.intro + this.content + this.outro;
23216
+ }
23217
+
23218
+ trimEnd(rx) {
23219
+ this.outro = this.outro.replace(rx, '');
23220
+ if (this.outro.length) return true;
23221
+
23222
+ const trimmed = this.content.replace(rx, '');
23223
+
23224
+ if (trimmed.length) {
23225
+ if (trimmed !== this.content) {
23226
+ this.split(this.start + trimmed.length).edit('', undefined, true);
23227
+ if (this.edited) {
23228
+ // save the change, if it has been edited
23229
+ this.edit(trimmed, this.storeName, true);
23230
+ }
23231
+ }
23232
+ return true;
23233
+ } else {
23234
+ this.edit('', undefined, true);
23235
+
23236
+ this.intro = this.intro.replace(rx, '');
23237
+ if (this.intro.length) return true;
23238
+ }
23239
+ }
23240
+
23241
+ trimStart(rx) {
23242
+ this.intro = this.intro.replace(rx, '');
23243
+ if (this.intro.length) return true;
23244
+
23245
+ const trimmed = this.content.replace(rx, '');
23246
+
23247
+ if (trimmed.length) {
23248
+ if (trimmed !== this.content) {
23249
+ const newChunk = this.split(this.end - trimmed.length);
23250
+ if (this.edited) {
23251
+ // save the change, if it has been edited
23252
+ newChunk.edit(trimmed, this.storeName, true);
23253
+ }
23254
+ this.edit('', undefined, true);
23255
+ }
23256
+ return true;
23257
+ } else {
23258
+ this.edit('', undefined, true);
23259
+
23260
+ this.outro = this.outro.replace(rx, '');
23261
+ if (this.outro.length) return true;
23262
+ }
23263
+ }
23264
+ }
23265
+
23266
+ function getBtoa() {
23267
+ if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
23268
+ return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
23269
+ } else if (typeof Buffer === 'function') {
23270
+ return (str) => Buffer.from(str, 'utf-8').toString('base64');
23271
+ } else {
23272
+ return () => {
23273
+ throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
23274
+ };
23275
+ }
23276
+ }
23277
+
23278
+ const btoa = /*#__PURE__*/ getBtoa();
23279
+
23280
+ class SourceMap {
23281
+ constructor(properties) {
23282
+ this.version = 3;
23283
+ this.file = properties.file;
23284
+ this.sources = properties.sources;
23285
+ this.sourcesContent = properties.sourcesContent;
23286
+ this.names = properties.names;
23287
+ this.mappings = encode(properties.mappings);
23288
+ if (typeof properties.x_google_ignoreList !== 'undefined') {
23289
+ this.x_google_ignoreList = properties.x_google_ignoreList;
23290
+ }
23291
+ if (typeof properties.debugId !== 'undefined') {
23292
+ this.debugId = properties.debugId;
23293
+ }
23294
+ }
23295
+
23296
+ toString() {
23297
+ return JSON.stringify(this);
23298
+ }
23299
+
23300
+ toUrl() {
23301
+ return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
23302
+ }
23303
+ }
23304
+
23305
+ function guessIndent(code) {
23306
+ const lines = code.split('\n');
23307
+
23308
+ const tabbed = lines.filter((line) => /^\t+/.test(line));
23309
+ const spaced = lines.filter((line) => /^ {2,}/.test(line));
23310
+
23311
+ if (tabbed.length === 0 && spaced.length === 0) {
23312
+ return null;
23313
+ }
23314
+
23315
+ // More lines tabbed than spaced? Assume tabs, and
23316
+ // default to tabs in the case of a tie (or nothing
23317
+ // to go on)
23318
+ if (tabbed.length >= spaced.length) {
23319
+ return '\t';
23320
+ }
23321
+
23322
+ // Otherwise, we need to guess the multiple
23323
+ const min = spaced.reduce((previous, current) => {
23324
+ const numSpaces = /^ +/.exec(current)[0].length;
23325
+ return Math.min(numSpaces, previous);
23326
+ }, Infinity);
23327
+
23328
+ return new Array(min + 1).join(' ');
23329
+ }
23330
+
23331
+ function getRelativePath(from, to) {
23332
+ const fromParts = from.split(/[/\\]/);
23333
+ const toParts = to.split(/[/\\]/);
23334
+
23335
+ fromParts.pop(); // get dirname
23336
+
23337
+ while (fromParts[0] === toParts[0]) {
23338
+ fromParts.shift();
23339
+ toParts.shift();
23340
+ }
23341
+
23342
+ if (fromParts.length) {
23343
+ let i = fromParts.length;
23344
+ while (i--) fromParts[i] = '..';
23345
+ }
23346
+
23347
+ return fromParts.concat(toParts).join('/');
23348
+ }
23349
+
23350
+ const toString = Object.prototype.toString;
23351
+
23352
+ function isObject(thing) {
23353
+ return toString.call(thing) === '[object Object]';
23354
+ }
23355
+
23356
+ function getLocator(source) {
23357
+ const originalLines = source.split('\n');
23358
+ const lineOffsets = [];
23359
+
23360
+ for (let i = 0, pos = 0; i < originalLines.length; i++) {
23361
+ lineOffsets.push(pos);
23362
+ pos += originalLines[i].length + 1;
23363
+ }
23364
+
23365
+ return function locate(index) {
23366
+ let i = 0;
23367
+ let j = lineOffsets.length;
23368
+ while (i < j) {
23369
+ const m = (i + j) >> 1;
23370
+ if (index < lineOffsets[m]) {
23371
+ j = m;
23372
+ } else {
23373
+ i = m + 1;
23374
+ }
23375
+ }
23376
+ const line = i - 1;
23377
+ const column = index - lineOffsets[line];
23378
+ return { line, column };
23379
+ };
23380
+ }
23381
+
23382
+ const wordRegex = /\w/;
23383
+
23384
+ class Mappings {
23385
+ constructor(hires) {
23386
+ this.hires = hires;
23387
+ this.generatedCodeLine = 0;
23388
+ this.generatedCodeColumn = 0;
23389
+ this.raw = [];
23390
+ this.rawSegments = this.raw[this.generatedCodeLine] = [];
23391
+ this.pending = null;
23392
+ }
23393
+
23394
+ addEdit(sourceIndex, content, loc, nameIndex) {
23395
+ if (content.length) {
23396
+ const contentLengthMinusOne = content.length - 1;
23397
+ let contentLineEnd = content.indexOf('\n', 0);
23398
+ let previousContentLineEnd = -1;
23399
+ // Loop through each line in the content and add a segment, but stop if the last line is empty,
23400
+ // else code afterwards would fill one line too many
23401
+ while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
23402
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
23403
+ if (nameIndex >= 0) {
23404
+ segment.push(nameIndex);
23405
+ }
23406
+ this.rawSegments.push(segment);
23407
+
23408
+ this.generatedCodeLine += 1;
23409
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
23410
+ this.generatedCodeColumn = 0;
23411
+
23412
+ previousContentLineEnd = contentLineEnd;
23413
+ contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
23414
+ }
23415
+
23416
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
23417
+ if (nameIndex >= 0) {
23418
+ segment.push(nameIndex);
23419
+ }
23420
+ this.rawSegments.push(segment);
23421
+
23422
+ this.advance(content.slice(previousContentLineEnd + 1));
23423
+ } else if (this.pending) {
23424
+ this.rawSegments.push(this.pending);
23425
+ this.advance(content);
23426
+ }
23427
+
23428
+ this.pending = null;
23429
+ }
23430
+
23431
+ addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
23432
+ let originalCharIndex = chunk.start;
23433
+ let first = true;
23434
+ // when iterating each char, check if it's in a word boundary
23435
+ let charInHiresBoundary = false;
23436
+
23437
+ while (originalCharIndex < chunk.end) {
23438
+ if (original[originalCharIndex] === '\n') {
23439
+ loc.line += 1;
23440
+ loc.column = 0;
23441
+ this.generatedCodeLine += 1;
23442
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
23443
+ this.generatedCodeColumn = 0;
23444
+ first = true;
23445
+ charInHiresBoundary = false;
23446
+ } else {
23447
+ if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
23448
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
23449
+
23450
+ if (this.hires === 'boundary') {
23451
+ // in hires "boundary", group segments per word boundary than per char
23452
+ if (wordRegex.test(original[originalCharIndex])) {
23453
+ // for first char in the boundary found, start the boundary by pushing a segment
23454
+ if (!charInHiresBoundary) {
23455
+ this.rawSegments.push(segment);
23456
+ charInHiresBoundary = true;
23457
+ }
23458
+ } else {
23459
+ // for non-word char, end the boundary by pushing a segment
23460
+ this.rawSegments.push(segment);
23461
+ charInHiresBoundary = false;
23462
+ }
23463
+ } else {
23464
+ this.rawSegments.push(segment);
23465
+ }
23466
+ }
23467
+
23468
+ loc.column += 1;
23469
+ this.generatedCodeColumn += 1;
23470
+ first = false;
23471
+ }
23472
+
23473
+ originalCharIndex += 1;
23474
+ }
23475
+
23476
+ this.pending = null;
23477
+ }
23478
+
23479
+ advance(str) {
23480
+ if (!str) return;
23481
+
23482
+ const lines = str.split('\n');
23483
+
23484
+ if (lines.length > 1) {
23485
+ for (let i = 0; i < lines.length - 1; i++) {
23486
+ this.generatedCodeLine++;
23487
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
23488
+ }
23489
+ this.generatedCodeColumn = 0;
23490
+ }
23491
+
23492
+ this.generatedCodeColumn += lines[lines.length - 1].length;
23493
+ }
23494
+ }
23495
+
23496
+ const n = '\n';
23497
+
23498
+ const warned = {
23499
+ insertLeft: false,
23500
+ insertRight: false,
23501
+ storeName: false,
23502
+ };
23503
+
23504
+ class MagicString {
23505
+ constructor(string, options = {}) {
23506
+ const chunk = new Chunk(0, string.length, string);
23507
+
23508
+ Object.defineProperties(this, {
23509
+ original: { writable: true, value: string },
23510
+ outro: { writable: true, value: '' },
23511
+ intro: { writable: true, value: '' },
23512
+ firstChunk: { writable: true, value: chunk },
23513
+ lastChunk: { writable: true, value: chunk },
23514
+ lastSearchedChunk: { writable: true, value: chunk },
23515
+ byStart: { writable: true, value: {} },
23516
+ byEnd: { writable: true, value: {} },
23517
+ filename: { writable: true, value: options.filename },
23518
+ indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
23519
+ sourcemapLocations: { writable: true, value: new BitSet() },
23520
+ storedNames: { writable: true, value: {} },
23521
+ indentStr: { writable: true, value: undefined },
23522
+ ignoreList: { writable: true, value: options.ignoreList },
23523
+ offset: { writable: true, value: options.offset || 0 },
23524
+ });
23525
+
23526
+ this.byStart[0] = chunk;
23527
+ this.byEnd[string.length] = chunk;
23528
+ }
23529
+
23530
+ addSourcemapLocation(char) {
23531
+ this.sourcemapLocations.add(char);
23532
+ }
23533
+
23534
+ append(content) {
23535
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
23536
+
23537
+ this.outro += content;
23538
+ return this;
23539
+ }
23540
+
23541
+ appendLeft(index, content) {
23542
+ index = index + this.offset;
23543
+
23544
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23545
+
23546
+ this._split(index);
23547
+
23548
+ const chunk = this.byEnd[index];
23549
+
23550
+ if (chunk) {
23551
+ chunk.appendLeft(content);
23552
+ } else {
23553
+ this.intro += content;
23554
+ }
23555
+ return this;
23556
+ }
23557
+
23558
+ appendRight(index, content) {
23559
+ index = index + this.offset;
23560
+
23561
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23562
+
23563
+ this._split(index);
23564
+
23565
+ const chunk = this.byStart[index];
23566
+
23567
+ if (chunk) {
23568
+ chunk.appendRight(content);
23569
+ } else {
23570
+ this.outro += content;
23571
+ }
23572
+ return this;
23573
+ }
23574
+
23575
+ clone() {
23576
+ const cloned = new MagicString(this.original, { filename: this.filename, offset: this.offset });
23577
+
23578
+ let originalChunk = this.firstChunk;
23579
+ let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
23580
+
23581
+ while (originalChunk) {
23582
+ cloned.byStart[clonedChunk.start] = clonedChunk;
23583
+ cloned.byEnd[clonedChunk.end] = clonedChunk;
23584
+
23585
+ const nextOriginalChunk = originalChunk.next;
23586
+ const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
23587
+
23588
+ if (nextClonedChunk) {
23589
+ clonedChunk.next = nextClonedChunk;
23590
+ nextClonedChunk.previous = clonedChunk;
23591
+
23592
+ clonedChunk = nextClonedChunk;
23593
+ }
23594
+
23595
+ originalChunk = nextOriginalChunk;
23596
+ }
23597
+
23598
+ cloned.lastChunk = clonedChunk;
23599
+
23600
+ if (this.indentExclusionRanges) {
23601
+ cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
23602
+ }
23603
+
23604
+ cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
23605
+
23606
+ cloned.intro = this.intro;
23607
+ cloned.outro = this.outro;
23608
+
23609
+ return cloned;
23610
+ }
23611
+
23612
+ generateDecodedMap(options) {
23613
+ options = options || {};
23614
+
23615
+ const sourceIndex = 0;
23616
+ const names = Object.keys(this.storedNames);
23617
+ const mappings = new Mappings(options.hires);
23618
+
23619
+ const locate = getLocator(this.original);
23620
+
23621
+ if (this.intro) {
23622
+ mappings.advance(this.intro);
23623
+ }
23624
+
23625
+ this.firstChunk.eachNext((chunk) => {
23626
+ const loc = locate(chunk.start);
23627
+
23628
+ if (chunk.intro.length) mappings.advance(chunk.intro);
23629
+
23630
+ if (chunk.edited) {
23631
+ mappings.addEdit(
23632
+ sourceIndex,
23633
+ chunk.content,
23634
+ loc,
23635
+ chunk.storeName ? names.indexOf(chunk.original) : -1,
23636
+ );
23637
+ } else {
23638
+ mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
23639
+ }
23640
+
23641
+ if (chunk.outro.length) mappings.advance(chunk.outro);
23642
+ });
23643
+
23644
+ return {
23645
+ file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
23646
+ sources: [
23647
+ options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
23648
+ ],
23649
+ sourcesContent: options.includeContent ? [this.original] : undefined,
23650
+ names,
23651
+ mappings: mappings.raw,
23652
+ x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
23653
+ };
23654
+ }
23655
+
23656
+ generateMap(options) {
23657
+ return new SourceMap(this.generateDecodedMap(options));
23658
+ }
23659
+
23660
+ _ensureindentStr() {
23661
+ if (this.indentStr === undefined) {
23662
+ this.indentStr = guessIndent(this.original);
23663
+ }
23664
+ }
23665
+
23666
+ _getRawIndentString() {
23667
+ this._ensureindentStr();
23668
+ return this.indentStr;
23669
+ }
23670
+
23671
+ getIndentString() {
23672
+ this._ensureindentStr();
23673
+ return this.indentStr === null ? '\t' : this.indentStr;
23674
+ }
23675
+
23676
+ indent(indentStr, options) {
23677
+ const pattern = /^[^\r\n]/gm;
23678
+
23679
+ if (isObject(indentStr)) {
23680
+ options = indentStr;
23681
+ indentStr = undefined;
23682
+ }
23683
+
23684
+ if (indentStr === undefined) {
23685
+ this._ensureindentStr();
23686
+ indentStr = this.indentStr || '\t';
23687
+ }
23688
+
23689
+ if (indentStr === '') return this; // noop
23690
+
23691
+ options = options || {};
23692
+
23693
+ // Process exclusion ranges
23694
+ const isExcluded = {};
23695
+
23696
+ if (options.exclude) {
23697
+ const exclusions =
23698
+ typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
23699
+ exclusions.forEach((exclusion) => {
23700
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
23701
+ isExcluded[i] = true;
23702
+ }
23703
+ });
23704
+ }
23705
+
23706
+ let shouldIndentNextCharacter = options.indentStart !== false;
23707
+ const replacer = (match) => {
23708
+ if (shouldIndentNextCharacter) return `${indentStr}${match}`;
23709
+ shouldIndentNextCharacter = true;
23710
+ return match;
23711
+ };
23712
+
23713
+ this.intro = this.intro.replace(pattern, replacer);
23714
+
23715
+ let charIndex = 0;
23716
+ let chunk = this.firstChunk;
23717
+
23718
+ while (chunk) {
23719
+ const end = chunk.end;
23720
+
23721
+ if (chunk.edited) {
23722
+ if (!isExcluded[charIndex]) {
23723
+ chunk.content = chunk.content.replace(pattern, replacer);
23724
+
23725
+ if (chunk.content.length) {
23726
+ shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
23727
+ }
23728
+ }
23729
+ } else {
23730
+ charIndex = chunk.start;
23731
+
23732
+ while (charIndex < end) {
23733
+ if (!isExcluded[charIndex]) {
23734
+ const char = this.original[charIndex];
23735
+
23736
+ if (char === '\n') {
23737
+ shouldIndentNextCharacter = true;
23738
+ } else if (char !== '\r' && shouldIndentNextCharacter) {
23739
+ shouldIndentNextCharacter = false;
23740
+
23741
+ if (charIndex === chunk.start) {
23742
+ chunk.prependRight(indentStr);
23743
+ } else {
23744
+ this._splitChunk(chunk, charIndex);
23745
+ chunk = chunk.next;
23746
+ chunk.prependRight(indentStr);
23747
+ }
23748
+ }
23749
+ }
23750
+
23751
+ charIndex += 1;
23752
+ }
23753
+ }
23754
+
23755
+ charIndex = chunk.end;
23756
+ chunk = chunk.next;
23757
+ }
23758
+
23759
+ this.outro = this.outro.replace(pattern, replacer);
23760
+
23761
+ return this;
23762
+ }
23763
+
23764
+ insert() {
23765
+ throw new Error(
23766
+ 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
23767
+ );
23768
+ }
23769
+
23770
+ insertLeft(index, content) {
23771
+ if (!warned.insertLeft) {
23772
+ console.warn(
23773
+ 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
23774
+ );
23775
+ warned.insertLeft = true;
23776
+ }
23777
+
23778
+ return this.appendLeft(index, content);
23779
+ }
23780
+
23781
+ insertRight(index, content) {
23782
+ if (!warned.insertRight) {
23783
+ console.warn(
23784
+ 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
23785
+ );
23786
+ warned.insertRight = true;
23787
+ }
23788
+
23789
+ return this.prependRight(index, content);
23790
+ }
23791
+
23792
+ move(start, end, index) {
23793
+ start = start + this.offset;
23794
+ end = end + this.offset;
23795
+ index = index + this.offset;
23796
+
23797
+ if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
23798
+
23799
+ this._split(start);
23800
+ this._split(end);
23801
+ this._split(index);
23802
+
23803
+ const first = this.byStart[start];
23804
+ const last = this.byEnd[end];
23805
+
23806
+ const oldLeft = first.previous;
23807
+ const oldRight = last.next;
23808
+
23809
+ const newRight = this.byStart[index];
23810
+ if (!newRight && last === this.lastChunk) return this;
23811
+ const newLeft = newRight ? newRight.previous : this.lastChunk;
23812
+
23813
+ if (oldLeft) oldLeft.next = oldRight;
23814
+ if (oldRight) oldRight.previous = oldLeft;
23815
+
23816
+ if (newLeft) newLeft.next = first;
23817
+ if (newRight) newRight.previous = last;
23818
+
23819
+ if (!first.previous) this.firstChunk = last.next;
23820
+ if (!last.next) {
23821
+ this.lastChunk = first.previous;
23822
+ this.lastChunk.next = null;
23823
+ }
23824
+
23825
+ first.previous = newLeft;
23826
+ last.next = newRight || null;
23827
+
23828
+ if (!newLeft) this.firstChunk = first;
23829
+ if (!newRight) this.lastChunk = last;
23830
+ return this;
23831
+ }
23832
+
23833
+ overwrite(start, end, content, options) {
23834
+ options = options || {};
23835
+ return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
23836
+ }
23837
+
23838
+ update(start, end, content, options) {
23839
+ start = start + this.offset;
23840
+ end = end + this.offset;
23841
+
23842
+ if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
23843
+
23844
+ if (this.original.length !== 0) {
23845
+ while (start < 0) start += this.original.length;
23846
+ while (end < 0) end += this.original.length;
23847
+ }
23848
+
23849
+ if (end > this.original.length) throw new Error('end is out of bounds');
23850
+ if (start === end)
23851
+ throw new Error(
23852
+ 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
23853
+ );
23854
+
23855
+ this._split(start);
23856
+ this._split(end);
23857
+
23858
+ if (options === true) {
23859
+ if (!warned.storeName) {
23860
+ console.warn(
23861
+ 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
23862
+ );
23863
+ warned.storeName = true;
23864
+ }
23865
+
23866
+ options = { storeName: true };
23867
+ }
23868
+ const storeName = options !== undefined ? options.storeName : false;
23869
+ const overwrite = options !== undefined ? options.overwrite : false;
23870
+
23871
+ if (storeName) {
23872
+ const original = this.original.slice(start, end);
23873
+ Object.defineProperty(this.storedNames, original, {
23874
+ writable: true,
23875
+ value: true,
23876
+ enumerable: true,
23877
+ });
23878
+ }
23879
+
23880
+ const first = this.byStart[start];
23881
+ const last = this.byEnd[end];
23882
+
23883
+ if (first) {
23884
+ let chunk = first;
23885
+ while (chunk !== last) {
23886
+ if (chunk.next !== this.byStart[chunk.end]) {
23887
+ throw new Error('Cannot overwrite across a split point');
23888
+ }
23889
+ chunk = chunk.next;
23890
+ chunk.edit('', false);
23891
+ }
23892
+
23893
+ first.edit(content, storeName, !overwrite);
23894
+ } else {
23895
+ // must be inserting at the end
23896
+ const newChunk = new Chunk(start, end, '').edit(content, storeName);
23897
+
23898
+ // TODO last chunk in the array may not be the last chunk, if it's moved...
23899
+ last.next = newChunk;
23900
+ newChunk.previous = last;
23901
+ }
23902
+ return this;
23903
+ }
23904
+
23905
+ prepend(content) {
23906
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
23907
+
23908
+ this.intro = content + this.intro;
23909
+ return this;
23910
+ }
23911
+
23912
+ prependLeft(index, content) {
23913
+ index = index + this.offset;
23914
+
23915
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23916
+
23917
+ this._split(index);
23918
+
23919
+ const chunk = this.byEnd[index];
23920
+
23921
+ if (chunk) {
23922
+ chunk.prependLeft(content);
23923
+ } else {
23924
+ this.intro = content + this.intro;
23925
+ }
23926
+ return this;
23927
+ }
23928
+
23929
+ prependRight(index, content) {
23930
+ index = index + this.offset;
23931
+
23932
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23933
+
23934
+ this._split(index);
23935
+
23936
+ const chunk = this.byStart[index];
23937
+
23938
+ if (chunk) {
23939
+ chunk.prependRight(content);
23940
+ } else {
23941
+ this.outro = content + this.outro;
23942
+ }
23943
+ return this;
23944
+ }
23945
+
23946
+ remove(start, end) {
23947
+ start = start + this.offset;
23948
+ end = end + this.offset;
23949
+
23950
+ if (this.original.length !== 0) {
23951
+ while (start < 0) start += this.original.length;
23952
+ while (end < 0) end += this.original.length;
23953
+ }
23954
+
23955
+ if (start === end) return this;
23956
+
23957
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
23958
+ if (start > end) throw new Error('end must be greater than start');
23959
+
23960
+ this._split(start);
23961
+ this._split(end);
23962
+
23963
+ let chunk = this.byStart[start];
23964
+
23965
+ while (chunk) {
23966
+ chunk.intro = '';
23967
+ chunk.outro = '';
23968
+ chunk.edit('');
23969
+
23970
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
23971
+ }
23972
+ return this;
23973
+ }
23974
+
23975
+ reset(start, end) {
23976
+ start = start + this.offset;
23977
+ end = end + this.offset;
23978
+
23979
+ if (this.original.length !== 0) {
23980
+ while (start < 0) start += this.original.length;
23981
+ while (end < 0) end += this.original.length;
23982
+ }
23983
+
23984
+ if (start === end) return this;
23985
+
23986
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
23987
+ if (start > end) throw new Error('end must be greater than start');
23988
+
23989
+ this._split(start);
23990
+ this._split(end);
23991
+
23992
+ let chunk = this.byStart[start];
23993
+
23994
+ while (chunk) {
23995
+ chunk.reset();
23996
+
23997
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
23998
+ }
23999
+ return this;
24000
+ }
24001
+
24002
+ lastChar() {
24003
+ if (this.outro.length) return this.outro[this.outro.length - 1];
24004
+ let chunk = this.lastChunk;
24005
+ do {
24006
+ if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
24007
+ if (chunk.content.length) return chunk.content[chunk.content.length - 1];
24008
+ if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
24009
+ } while ((chunk = chunk.previous));
24010
+ if (this.intro.length) return this.intro[this.intro.length - 1];
24011
+ return '';
24012
+ }
24013
+
24014
+ lastLine() {
24015
+ let lineIndex = this.outro.lastIndexOf(n);
24016
+ if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
24017
+ let lineStr = this.outro;
24018
+ let chunk = this.lastChunk;
24019
+ do {
24020
+ if (chunk.outro.length > 0) {
24021
+ lineIndex = chunk.outro.lastIndexOf(n);
24022
+ if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
24023
+ lineStr = chunk.outro + lineStr;
24024
+ }
24025
+
24026
+ if (chunk.content.length > 0) {
24027
+ lineIndex = chunk.content.lastIndexOf(n);
24028
+ if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
24029
+ lineStr = chunk.content + lineStr;
24030
+ }
24031
+
24032
+ if (chunk.intro.length > 0) {
24033
+ lineIndex = chunk.intro.lastIndexOf(n);
24034
+ if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
24035
+ lineStr = chunk.intro + lineStr;
24036
+ }
24037
+ } while ((chunk = chunk.previous));
24038
+ lineIndex = this.intro.lastIndexOf(n);
24039
+ if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
24040
+ return this.intro + lineStr;
24041
+ }
24042
+
24043
+ slice(start = 0, end = this.original.length - this.offset) {
24044
+ start = start + this.offset;
24045
+ end = end + this.offset;
24046
+
24047
+ if (this.original.length !== 0) {
24048
+ while (start < 0) start += this.original.length;
24049
+ while (end < 0) end += this.original.length;
24050
+ }
24051
+
24052
+ let result = '';
24053
+
24054
+ // find start chunk
24055
+ let chunk = this.firstChunk;
24056
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
24057
+ // found end chunk before start
24058
+ if (chunk.start < end && chunk.end >= end) {
24059
+ return result;
24060
+ }
24061
+
24062
+ chunk = chunk.next;
24063
+ }
24064
+
24065
+ if (chunk && chunk.edited && chunk.start !== start)
24066
+ throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
24067
+
24068
+ const startChunk = chunk;
24069
+ while (chunk) {
24070
+ if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
24071
+ result += chunk.intro;
24072
+ }
24073
+
24074
+ const containsEnd = chunk.start < end && chunk.end >= end;
24075
+ if (containsEnd && chunk.edited && chunk.end !== end)
24076
+ throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
24077
+
24078
+ const sliceStart = startChunk === chunk ? start - chunk.start : 0;
24079
+ const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
24080
+
24081
+ result += chunk.content.slice(sliceStart, sliceEnd);
24082
+
24083
+ if (chunk.outro && (!containsEnd || chunk.end === end)) {
24084
+ result += chunk.outro;
24085
+ }
24086
+
24087
+ if (containsEnd) {
24088
+ break;
24089
+ }
24090
+
24091
+ chunk = chunk.next;
24092
+ }
24093
+
24094
+ return result;
24095
+ }
24096
+
24097
+ // TODO deprecate this? not really very useful
24098
+ snip(start, end) {
24099
+ const clone = this.clone();
24100
+ clone.remove(0, start);
24101
+ clone.remove(end, clone.original.length);
24102
+
24103
+ return clone;
24104
+ }
24105
+
24106
+ _split(index) {
24107
+ if (this.byStart[index] || this.byEnd[index]) return;
24108
+
24109
+ let chunk = this.lastSearchedChunk;
24110
+ const searchForward = index > chunk.end;
24111
+
24112
+ while (chunk) {
24113
+ if (chunk.contains(index)) return this._splitChunk(chunk, index);
24114
+
24115
+ chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
24116
+ }
24117
+ }
24118
+
24119
+ _splitChunk(chunk, index) {
24120
+ if (chunk.edited && chunk.content.length) {
24121
+ // zero-length edited chunks are a special case (overlapping replacements)
24122
+ const loc = getLocator(this.original)(index);
24123
+ throw new Error(
24124
+ `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
24125
+ );
24126
+ }
24127
+
24128
+ const newChunk = chunk.split(index);
24129
+
24130
+ this.byEnd[index] = chunk;
24131
+ this.byStart[index] = newChunk;
24132
+ this.byEnd[newChunk.end] = newChunk;
24133
+
24134
+ if (chunk === this.lastChunk) this.lastChunk = newChunk;
24135
+
24136
+ this.lastSearchedChunk = chunk;
24137
+ return true;
24138
+ }
24139
+
24140
+ toString() {
24141
+ let str = this.intro;
24142
+
24143
+ let chunk = this.firstChunk;
24144
+ while (chunk) {
24145
+ str += chunk.toString();
24146
+ chunk = chunk.next;
24147
+ }
24148
+
24149
+ return str + this.outro;
24150
+ }
24151
+
24152
+ isEmpty() {
24153
+ let chunk = this.firstChunk;
24154
+ do {
24155
+ if (
24156
+ (chunk.intro.length && chunk.intro.trim()) ||
24157
+ (chunk.content.length && chunk.content.trim()) ||
24158
+ (chunk.outro.length && chunk.outro.trim())
24159
+ )
24160
+ return false;
24161
+ } while ((chunk = chunk.next));
24162
+ return true;
24163
+ }
24164
+
24165
+ length() {
24166
+ let chunk = this.firstChunk;
24167
+ let length = 0;
24168
+ do {
24169
+ length += chunk.intro.length + chunk.content.length + chunk.outro.length;
24170
+ } while ((chunk = chunk.next));
24171
+ return length;
24172
+ }
24173
+
24174
+ trimLines() {
24175
+ return this.trim('[\\r\\n]');
24176
+ }
24177
+
24178
+ trim(charType) {
24179
+ return this.trimStart(charType).trimEnd(charType);
24180
+ }
24181
+
24182
+ trimEndAborted(charType) {
24183
+ const rx = new RegExp((charType || '\\s') + '+$');
24184
+
24185
+ this.outro = this.outro.replace(rx, '');
24186
+ if (this.outro.length) return true;
24187
+
24188
+ let chunk = this.lastChunk;
24189
+
24190
+ do {
24191
+ const end = chunk.end;
24192
+ const aborted = chunk.trimEnd(rx);
24193
+
24194
+ // if chunk was trimmed, we have a new lastChunk
24195
+ if (chunk.end !== end) {
24196
+ if (this.lastChunk === chunk) {
24197
+ this.lastChunk = chunk.next;
24198
+ }
24199
+
24200
+ this.byEnd[chunk.end] = chunk;
24201
+ this.byStart[chunk.next.start] = chunk.next;
24202
+ this.byEnd[chunk.next.end] = chunk.next;
24203
+ }
24204
+
24205
+ if (aborted) return true;
24206
+ chunk = chunk.previous;
24207
+ } while (chunk);
24208
+
24209
+ return false;
24210
+ }
24211
+
24212
+ trimEnd(charType) {
24213
+ this.trimEndAborted(charType);
24214
+ return this;
24215
+ }
24216
+ trimStartAborted(charType) {
24217
+ const rx = new RegExp('^' + (charType || '\\s') + '+');
24218
+
24219
+ this.intro = this.intro.replace(rx, '');
24220
+ if (this.intro.length) return true;
24221
+
24222
+ let chunk = this.firstChunk;
24223
+
24224
+ do {
24225
+ const end = chunk.end;
24226
+ const aborted = chunk.trimStart(rx);
24227
+
24228
+ if (chunk.end !== end) {
24229
+ // special case...
24230
+ if (chunk === this.lastChunk) this.lastChunk = chunk.next;
24231
+
24232
+ this.byEnd[chunk.end] = chunk;
24233
+ this.byStart[chunk.next.start] = chunk.next;
24234
+ this.byEnd[chunk.next.end] = chunk.next;
24235
+ }
24236
+
24237
+ if (aborted) return true;
24238
+ chunk = chunk.next;
24239
+ } while (chunk);
24240
+
24241
+ return false;
24242
+ }
24243
+
24244
+ trimStart(charType) {
24245
+ this.trimStartAborted(charType);
24246
+ return this;
24247
+ }
24248
+
24249
+ hasChanged() {
24250
+ return this.original !== this.toString();
24251
+ }
24252
+
24253
+ _replaceRegexp(searchValue, replacement) {
24254
+ function getReplacement(match, str) {
24255
+ if (typeof replacement === 'string') {
24256
+ return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
24257
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
24258
+ if (i === '$') return '$';
24259
+ if (i === '&') return match[0];
24260
+ const num = +i;
24261
+ if (num < match.length) return match[+i];
24262
+ return `$${i}`;
24263
+ });
24264
+ } else {
24265
+ return replacement(...match, match.index, str, match.groups);
24266
+ }
24267
+ }
24268
+ function matchAll(re, str) {
24269
+ let match;
24270
+ const matches = [];
24271
+ while ((match = re.exec(str))) {
24272
+ matches.push(match);
24273
+ }
24274
+ return matches;
24275
+ }
24276
+ if (searchValue.global) {
24277
+ const matches = matchAll(searchValue, this.original);
24278
+ matches.forEach((match) => {
24279
+ if (match.index != null) {
24280
+ const replacement = getReplacement(match, this.original);
24281
+ if (replacement !== match[0]) {
24282
+ this.overwrite(match.index, match.index + match[0].length, replacement);
24283
+ }
24284
+ }
24285
+ });
24286
+ } else {
24287
+ const match = this.original.match(searchValue);
24288
+ if (match && match.index != null) {
24289
+ const replacement = getReplacement(match, this.original);
24290
+ if (replacement !== match[0]) {
24291
+ this.overwrite(match.index, match.index + match[0].length, replacement);
24292
+ }
24293
+ }
24294
+ }
24295
+ return this;
24296
+ }
24297
+
24298
+ _replaceString(string, replacement) {
24299
+ const { original } = this;
24300
+ const index = original.indexOf(string);
24301
+
24302
+ if (index !== -1) {
24303
+ this.overwrite(index, index + string.length, replacement);
24304
+ }
24305
+
24306
+ return this;
24307
+ }
24308
+
24309
+ replace(searchValue, replacement) {
24310
+ if (typeof searchValue === 'string') {
24311
+ return this._replaceString(searchValue, replacement);
24312
+ }
24313
+
24314
+ return this._replaceRegexp(searchValue, replacement);
24315
+ }
24316
+
24317
+ _replaceAllString(string, replacement) {
24318
+ const { original } = this;
24319
+ const stringLength = string.length;
24320
+ for (
24321
+ let index = original.indexOf(string);
24322
+ index !== -1;
24323
+ index = original.indexOf(string, index + stringLength)
24324
+ ) {
24325
+ const previous = original.slice(index, index + stringLength);
24326
+ if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
24327
+ }
24328
+
24329
+ return this;
24330
+ }
24331
+
24332
+ replaceAll(searchValue, replacement) {
24333
+ if (typeof searchValue === 'string') {
24334
+ return this._replaceAllString(searchValue, replacement);
24335
+ }
24336
+
24337
+ if (!searchValue.global) {
24338
+ throw new TypeError(
24339
+ 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
24340
+ );
24341
+ }
24342
+
24343
+ return this._replaceRegexp(searchValue, replacement);
24344
+ }
24345
+ }
24346
+
23063
24347
  var utils = {};
23064
24348
 
23065
24349
  var constants;