@m2c2kit/build-helpers 0.3.19 → 0.3.20

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 +1322 -68
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -5402,7 +5402,7 @@ function encode(decoded) {
5402
5402
  return writer.flush();
5403
5403
  }
5404
5404
 
5405
- class BitSet {
5405
+ let BitSet$1 = class BitSet {
5406
5406
  constructor(arg) {
5407
5407
  this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
5408
5408
  }
@@ -5414,9 +5414,9 @@ class BitSet {
5414
5414
  has(n) {
5415
5415
  return !!(this.bits[n >> 5] & (1 << (n & 31)));
5416
5416
  }
5417
- }
5417
+ };
5418
5418
 
5419
- class Chunk {
5419
+ let Chunk$1 = class Chunk {
5420
5420
  constructor(start, end, content) {
5421
5421
  this.start = start;
5422
5422
  this.end = end;
@@ -5593,9 +5593,9 @@ class Chunk {
5593
5593
  if (this.outro.length) return true;
5594
5594
  }
5595
5595
  }
5596
- }
5596
+ };
5597
5597
 
5598
- function getBtoa() {
5598
+ function getBtoa$1() {
5599
5599
  if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
5600
5600
  return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
5601
5601
  } else if (typeof Buffer === 'function') {
@@ -5607,9 +5607,9 @@ function getBtoa() {
5607
5607
  }
5608
5608
  }
5609
5609
 
5610
- const btoa = /*#__PURE__*/ getBtoa();
5610
+ const btoa$1 = /*#__PURE__*/ getBtoa$1();
5611
5611
 
5612
- class SourceMap {
5612
+ let SourceMap$1 = class SourceMap {
5613
5613
  constructor(properties) {
5614
5614
  this.version = 3;
5615
5615
  this.file = properties.file;
@@ -5627,11 +5627,11 @@ class SourceMap {
5627
5627
  }
5628
5628
 
5629
5629
  toUrl() {
5630
- return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
5630
+ return 'data:application/json;charset=utf-8;base64,' + btoa$1(this.toString());
5631
5631
  }
5632
- }
5632
+ };
5633
5633
 
5634
- function guessIndent(code) {
5634
+ function guessIndent$1(code) {
5635
5635
  const lines = code.split('\n');
5636
5636
 
5637
5637
  const tabbed = lines.filter((line) => /^\t+/.test(line));
@@ -5657,7 +5657,7 @@ function guessIndent(code) {
5657
5657
  return new Array(min + 1).join(' ');
5658
5658
  }
5659
5659
 
5660
- function getRelativePath(from, to) {
5660
+ function getRelativePath$1(from, to) {
5661
5661
  const fromParts = from.split(/[/\\]/);
5662
5662
  const toParts = to.split(/[/\\]/);
5663
5663
 
@@ -5676,13 +5676,13 @@ function getRelativePath(from, to) {
5676
5676
  return fromParts.concat(toParts).join('/');
5677
5677
  }
5678
5678
 
5679
- const toString = Object.prototype.toString;
5679
+ const toString$1 = Object.prototype.toString;
5680
5680
 
5681
- function isObject(thing) {
5682
- return toString.call(thing) === '[object Object]';
5681
+ function isObject$1(thing) {
5682
+ return toString$1.call(thing) === '[object Object]';
5683
5683
  }
5684
5684
 
5685
- function getLocator(source) {
5685
+ function getLocator$1(source) {
5686
5686
  const originalLines = source.split('\n');
5687
5687
  const lineOffsets = [];
5688
5688
 
@@ -5708,9 +5708,9 @@ function getLocator(source) {
5708
5708
  };
5709
5709
  }
5710
5710
 
5711
- const wordRegex = /\w/;
5711
+ const wordRegex$1 = /\w/;
5712
5712
 
5713
- class Mappings {
5713
+ let Mappings$1 = class Mappings {
5714
5714
  constructor(hires) {
5715
5715
  this.hires = hires;
5716
5716
  this.generatedCodeLine = 0;
@@ -5764,27 +5764,6 @@ class Mappings {
5764
5764
  let charInHiresBoundary = false;
5765
5765
 
5766
5766
  while (originalCharIndex < chunk.end) {
5767
- if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
5768
- const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
5769
-
5770
- if (this.hires === 'boundary') {
5771
- // in hires "boundary", group segments per word boundary than per char
5772
- if (wordRegex.test(original[originalCharIndex])) {
5773
- // for first char in the boundary found, start the boundary by pushing a segment
5774
- if (!charInHiresBoundary) {
5775
- this.rawSegments.push(segment);
5776
- charInHiresBoundary = true;
5777
- }
5778
- } else {
5779
- // for non-word char, end the boundary by pushing a segment
5780
- this.rawSegments.push(segment);
5781
- charInHiresBoundary = false;
5782
- }
5783
- } else {
5784
- this.rawSegments.push(segment);
5785
- }
5786
- }
5787
-
5788
5767
  if (original[originalCharIndex] === '\n') {
5789
5768
  loc.line += 1;
5790
5769
  loc.column = 0;
@@ -5793,6 +5772,27 @@ class Mappings {
5793
5772
  this.generatedCodeColumn = 0;
5794
5773
  first = true;
5795
5774
  } else {
5775
+ if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
5776
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
5777
+
5778
+ if (this.hires === 'boundary') {
5779
+ // in hires "boundary", group segments per word boundary than per char
5780
+ if (wordRegex$1.test(original[originalCharIndex])) {
5781
+ // for first char in the boundary found, start the boundary by pushing a segment
5782
+ if (!charInHiresBoundary) {
5783
+ this.rawSegments.push(segment);
5784
+ charInHiresBoundary = true;
5785
+ }
5786
+ } else {
5787
+ // for non-word char, end the boundary by pushing a segment
5788
+ this.rawSegments.push(segment);
5789
+ charInHiresBoundary = false;
5790
+ }
5791
+ } else {
5792
+ this.rawSegments.push(segment);
5793
+ }
5794
+ }
5795
+
5796
5796
  loc.column += 1;
5797
5797
  this.generatedCodeColumn += 1;
5798
5798
  first = false;
@@ -5819,19 +5819,19 @@ class Mappings {
5819
5819
 
5820
5820
  this.generatedCodeColumn += lines[lines.length - 1].length;
5821
5821
  }
5822
- }
5822
+ };
5823
5823
 
5824
- const n = '\n';
5824
+ const n$1 = '\n';
5825
5825
 
5826
- const warned = {
5826
+ const warned$1 = {
5827
5827
  insertLeft: false,
5828
5828
  insertRight: false,
5829
5829
  storeName: false,
5830
5830
  };
5831
5831
 
5832
- class MagicString {
5832
+ let MagicString$1 = class MagicString {
5833
5833
  constructor(string, options = {}) {
5834
- const chunk = new Chunk(0, string.length, string);
5834
+ const chunk = new Chunk$1(0, string.length, string);
5835
5835
 
5836
5836
  Object.defineProperties(this, {
5837
5837
  original: { writable: true, value: string },
@@ -5844,7 +5844,7 @@ class MagicString {
5844
5844
  byEnd: { writable: true, value: {} },
5845
5845
  filename: { writable: true, value: options.filename },
5846
5846
  indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
5847
- sourcemapLocations: { writable: true, value: new BitSet() },
5847
+ sourcemapLocations: { writable: true, value: new BitSet$1() },
5848
5848
  storedNames: { writable: true, value: {} },
5849
5849
  indentStr: { writable: true, value: undefined },
5850
5850
  ignoreList: { writable: true, value: options.ignoreList },
@@ -5924,7 +5924,7 @@ class MagicString {
5924
5924
  cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
5925
5925
  }
5926
5926
 
5927
- cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
5927
+ cloned.sourcemapLocations = new BitSet$1(this.sourcemapLocations);
5928
5928
 
5929
5929
  cloned.intro = this.intro;
5930
5930
  cloned.outro = this.outro;
@@ -5937,9 +5937,9 @@ class MagicString {
5937
5937
 
5938
5938
  const sourceIndex = 0;
5939
5939
  const names = Object.keys(this.storedNames);
5940
- const mappings = new Mappings(options.hires);
5940
+ const mappings = new Mappings$1(options.hires);
5941
5941
 
5942
- const locate = getLocator(this.original);
5942
+ const locate = getLocator$1(this.original);
5943
5943
 
5944
5944
  if (this.intro) {
5945
5945
  mappings.advance(this.intro);
@@ -5967,7 +5967,7 @@ class MagicString {
5967
5967
  return {
5968
5968
  file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
5969
5969
  sources: [
5970
- options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
5970
+ options.source ? getRelativePath$1(options.file || '', options.source) : options.file || '',
5971
5971
  ],
5972
5972
  sourcesContent: options.includeContent ? [this.original] : undefined,
5973
5973
  names,
@@ -5977,12 +5977,12 @@ class MagicString {
5977
5977
  }
5978
5978
 
5979
5979
  generateMap(options) {
5980
- return new SourceMap(this.generateDecodedMap(options));
5980
+ return new SourceMap$1(this.generateDecodedMap(options));
5981
5981
  }
5982
5982
 
5983
5983
  _ensureindentStr() {
5984
5984
  if (this.indentStr === undefined) {
5985
- this.indentStr = guessIndent(this.original);
5985
+ this.indentStr = guessIndent$1(this.original);
5986
5986
  }
5987
5987
  }
5988
5988
 
@@ -5999,7 +5999,7 @@ class MagicString {
5999
5999
  indent(indentStr, options) {
6000
6000
  const pattern = /^[^\r\n]/gm;
6001
6001
 
6002
- if (isObject(indentStr)) {
6002
+ if (isObject$1(indentStr)) {
6003
6003
  options = indentStr;
6004
6004
  indentStr = undefined;
6005
6005
  }
@@ -6091,22 +6091,22 @@ class MagicString {
6091
6091
  }
6092
6092
 
6093
6093
  insertLeft(index, content) {
6094
- if (!warned.insertLeft) {
6094
+ if (!warned$1.insertLeft) {
6095
6095
  console.warn(
6096
6096
  'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
6097
6097
  ); // eslint-disable-line no-console
6098
- warned.insertLeft = true;
6098
+ warned$1.insertLeft = true;
6099
6099
  }
6100
6100
 
6101
6101
  return this.appendLeft(index, content);
6102
6102
  }
6103
6103
 
6104
6104
  insertRight(index, content) {
6105
- if (!warned.insertRight) {
6105
+ if (!warned$1.insertRight) {
6106
6106
  console.warn(
6107
6107
  'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
6108
6108
  ); // eslint-disable-line no-console
6109
- warned.insertRight = true;
6109
+ warned$1.insertRight = true;
6110
6110
  }
6111
6111
 
6112
6112
  return this.prependRight(index, content);
@@ -6172,11 +6172,11 @@ class MagicString {
6172
6172
  this._split(end);
6173
6173
 
6174
6174
  if (options === true) {
6175
- if (!warned.storeName) {
6175
+ if (!warned$1.storeName) {
6176
6176
  console.warn(
6177
6177
  'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
6178
6178
  ); // eslint-disable-line no-console
6179
- warned.storeName = true;
6179
+ warned$1.storeName = true;
6180
6180
  }
6181
6181
 
6182
6182
  options = { storeName: true };
@@ -6209,7 +6209,7 @@ class MagicString {
6209
6209
  first.edit(content, storeName, !overwrite);
6210
6210
  } else {
6211
6211
  // must be inserting at the end
6212
- const newChunk = new Chunk(start, end, '').edit(content, storeName);
6212
+ const newChunk = new Chunk$1(start, end, '').edit(content, storeName);
6213
6213
 
6214
6214
  // TODO last chunk in the array may not be the last chunk, if it's moved...
6215
6215
  last.next = newChunk;
@@ -6318,30 +6318,30 @@ class MagicString {
6318
6318
  }
6319
6319
 
6320
6320
  lastLine() {
6321
- let lineIndex = this.outro.lastIndexOf(n);
6321
+ let lineIndex = this.outro.lastIndexOf(n$1);
6322
6322
  if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
6323
6323
  let lineStr = this.outro;
6324
6324
  let chunk = this.lastChunk;
6325
6325
  do {
6326
6326
  if (chunk.outro.length > 0) {
6327
- lineIndex = chunk.outro.lastIndexOf(n);
6327
+ lineIndex = chunk.outro.lastIndexOf(n$1);
6328
6328
  if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
6329
6329
  lineStr = chunk.outro + lineStr;
6330
6330
  }
6331
6331
 
6332
6332
  if (chunk.content.length > 0) {
6333
- lineIndex = chunk.content.lastIndexOf(n);
6333
+ lineIndex = chunk.content.lastIndexOf(n$1);
6334
6334
  if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
6335
6335
  lineStr = chunk.content + lineStr;
6336
6336
  }
6337
6337
 
6338
6338
  if (chunk.intro.length > 0) {
6339
- lineIndex = chunk.intro.lastIndexOf(n);
6339
+ lineIndex = chunk.intro.lastIndexOf(n$1);
6340
6340
  if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
6341
6341
  lineStr = chunk.intro + lineStr;
6342
6342
  }
6343
6343
  } while ((chunk = chunk.previous));
6344
- lineIndex = this.intro.lastIndexOf(n);
6344
+ lineIndex = this.intro.lastIndexOf(n$1);
6345
6345
  if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
6346
6346
  return this.intro + lineStr;
6347
6347
  }
@@ -6422,7 +6422,7 @@ class MagicString {
6422
6422
  _splitChunk(chunk, index) {
6423
6423
  if (chunk.edited && chunk.content.length) {
6424
6424
  // zero-length edited chunks are a special case (overlapping replacements)
6425
- const loc = getLocator(this.original)(index);
6425
+ const loc = getLocator$1(this.original)(index);
6426
6426
  throw new Error(
6427
6427
  `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
6428
6428
  );
@@ -6654,7 +6654,7 @@ class MagicString {
6654
6654
 
6655
6655
  return this._replaceRegexp(searchValue, replacement);
6656
6656
  }
6657
- }
6657
+ };
6658
6658
 
6659
6659
  const HASH_CHARACTER_LENGTH = 16;
6660
6660
  function hashM2c2kitAssets(rootDir, cwd = "") {
@@ -6663,7 +6663,7 @@ function hashM2c2kitAssets(rootDir, cwd = "") {
6663
6663
  name: "hash-m2c2kit-assets",
6664
6664
  renderChunk: {
6665
6665
  handler(code) {
6666
- const magicString = new MagicString(code);
6666
+ const magicString = new MagicString$1(code);
6667
6667
  magicString.replace(
6668
6668
  new RegExp("__NO_M2C2KIT_MANIFEST_JSON_URL__", "g"),
6669
6669
  "manifest.json"
@@ -6868,7 +6868,7 @@ function restoreImportMeta(pattern = "import_meta = {};", replacement = "import_
6868
6868
  name: "restore-import-meta",
6869
6869
  renderChunk: {
6870
6870
  handler(code) {
6871
- const magicString = new MagicString(code);
6871
+ const magicString = new MagicString$1(code);
6872
6872
  magicString.replace(new RegExp(pattern, "g"), replacement);
6873
6873
  return {
6874
6874
  code: magicString.toString(),
@@ -22619,7 +22619,7 @@ function addModuleMetadata() {
22619
22619
  transform: {
22620
22620
  async handler(code) {
22621
22621
  const pkg = JSON.parse(await readFile("./package.json", "utf8"));
22622
- const magicString = new MagicString(code);
22622
+ const magicString = new MagicString$1(code);
22623
22623
  magicString.replace(
22624
22624
  new RegExp(
22625
22625
  "moduleMetadata:\\s*Constants.MODULE_METADATA_PLACEHOLDER",
@@ -22636,6 +22636,1260 @@ function addModuleMetadata() {
22636
22636
  };
22637
22637
  }
22638
22638
 
22639
+ class BitSet {
22640
+ constructor(arg) {
22641
+ this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
22642
+ }
22643
+
22644
+ add(n) {
22645
+ this.bits[n >> 5] |= 1 << (n & 31);
22646
+ }
22647
+
22648
+ has(n) {
22649
+ return !!(this.bits[n >> 5] & (1 << (n & 31)));
22650
+ }
22651
+ }
22652
+
22653
+ class Chunk {
22654
+ constructor(start, end, content) {
22655
+ this.start = start;
22656
+ this.end = end;
22657
+ this.original = content;
22658
+
22659
+ this.intro = '';
22660
+ this.outro = '';
22661
+
22662
+ this.content = content;
22663
+ this.storeName = false;
22664
+ this.edited = false;
22665
+
22666
+ {
22667
+ this.previous = null;
22668
+ this.next = null;
22669
+ }
22670
+ }
22671
+
22672
+ appendLeft(content) {
22673
+ this.outro += content;
22674
+ }
22675
+
22676
+ appendRight(content) {
22677
+ this.intro = this.intro + content;
22678
+ }
22679
+
22680
+ clone() {
22681
+ const chunk = new Chunk(this.start, this.end, this.original);
22682
+
22683
+ chunk.intro = this.intro;
22684
+ chunk.outro = this.outro;
22685
+ chunk.content = this.content;
22686
+ chunk.storeName = this.storeName;
22687
+ chunk.edited = this.edited;
22688
+
22689
+ return chunk;
22690
+ }
22691
+
22692
+ contains(index) {
22693
+ return this.start < index && index < this.end;
22694
+ }
22695
+
22696
+ eachNext(fn) {
22697
+ let chunk = this;
22698
+ while (chunk) {
22699
+ fn(chunk);
22700
+ chunk = chunk.next;
22701
+ }
22702
+ }
22703
+
22704
+ eachPrevious(fn) {
22705
+ let chunk = this;
22706
+ while (chunk) {
22707
+ fn(chunk);
22708
+ chunk = chunk.previous;
22709
+ }
22710
+ }
22711
+
22712
+ edit(content, storeName, contentOnly) {
22713
+ this.content = content;
22714
+ if (!contentOnly) {
22715
+ this.intro = '';
22716
+ this.outro = '';
22717
+ }
22718
+ this.storeName = storeName;
22719
+
22720
+ this.edited = true;
22721
+
22722
+ return this;
22723
+ }
22724
+
22725
+ prependLeft(content) {
22726
+ this.outro = content + this.outro;
22727
+ }
22728
+
22729
+ prependRight(content) {
22730
+ this.intro = content + this.intro;
22731
+ }
22732
+
22733
+ reset() {
22734
+ this.intro = '';
22735
+ this.outro = '';
22736
+ if (this.edited) {
22737
+ this.content = this.original;
22738
+ this.storeName = false;
22739
+ this.edited = false;
22740
+ }
22741
+ }
22742
+
22743
+ split(index) {
22744
+ const sliceIndex = index - this.start;
22745
+
22746
+ const originalBefore = this.original.slice(0, sliceIndex);
22747
+ const originalAfter = this.original.slice(sliceIndex);
22748
+
22749
+ this.original = originalBefore;
22750
+
22751
+ const newChunk = new Chunk(index, this.end, originalAfter);
22752
+ newChunk.outro = this.outro;
22753
+ this.outro = '';
22754
+
22755
+ this.end = index;
22756
+
22757
+ if (this.edited) {
22758
+ // after split we should save the edit content record into the correct chunk
22759
+ // to make sure sourcemap correct
22760
+ // For example:
22761
+ // ' test'.trim()
22762
+ // split -> ' ' + 'test'
22763
+ // ✔️ edit -> '' + 'test'
22764
+ // ✖️ edit -> 'test' + ''
22765
+ // TODO is this block necessary?...
22766
+ newChunk.edit('', false);
22767
+ this.content = '';
22768
+ } else {
22769
+ this.content = originalBefore;
22770
+ }
22771
+
22772
+ newChunk.next = this.next;
22773
+ if (newChunk.next) newChunk.next.previous = newChunk;
22774
+ newChunk.previous = this;
22775
+ this.next = newChunk;
22776
+
22777
+ return newChunk;
22778
+ }
22779
+
22780
+ toString() {
22781
+ return this.intro + this.content + this.outro;
22782
+ }
22783
+
22784
+ trimEnd(rx) {
22785
+ this.outro = this.outro.replace(rx, '');
22786
+ if (this.outro.length) return true;
22787
+
22788
+ const trimmed = this.content.replace(rx, '');
22789
+
22790
+ if (trimmed.length) {
22791
+ if (trimmed !== this.content) {
22792
+ this.split(this.start + trimmed.length).edit('', undefined, true);
22793
+ if (this.edited) {
22794
+ // save the change, if it has been edited
22795
+ this.edit(trimmed, this.storeName, true);
22796
+ }
22797
+ }
22798
+ return true;
22799
+ } else {
22800
+ this.edit('', undefined, true);
22801
+
22802
+ this.intro = this.intro.replace(rx, '');
22803
+ if (this.intro.length) return true;
22804
+ }
22805
+ }
22806
+
22807
+ trimStart(rx) {
22808
+ this.intro = this.intro.replace(rx, '');
22809
+ if (this.intro.length) return true;
22810
+
22811
+ const trimmed = this.content.replace(rx, '');
22812
+
22813
+ if (trimmed.length) {
22814
+ if (trimmed !== this.content) {
22815
+ const newChunk = this.split(this.end - trimmed.length);
22816
+ if (this.edited) {
22817
+ // save the change, if it has been edited
22818
+ newChunk.edit(trimmed, this.storeName, true);
22819
+ }
22820
+ this.edit('', undefined, true);
22821
+ }
22822
+ return true;
22823
+ } else {
22824
+ this.edit('', undefined, true);
22825
+
22826
+ this.outro = this.outro.replace(rx, '');
22827
+ if (this.outro.length) return true;
22828
+ }
22829
+ }
22830
+ }
22831
+
22832
+ function getBtoa() {
22833
+ if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
22834
+ return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
22835
+ } else if (typeof Buffer === 'function') {
22836
+ return (str) => Buffer.from(str, 'utf-8').toString('base64');
22837
+ } else {
22838
+ return () => {
22839
+ throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
22840
+ };
22841
+ }
22842
+ }
22843
+
22844
+ const btoa = /*#__PURE__*/ getBtoa();
22845
+
22846
+ class SourceMap {
22847
+ constructor(properties) {
22848
+ this.version = 3;
22849
+ this.file = properties.file;
22850
+ this.sources = properties.sources;
22851
+ this.sourcesContent = properties.sourcesContent;
22852
+ this.names = properties.names;
22853
+ this.mappings = encode(properties.mappings);
22854
+ if (typeof properties.x_google_ignoreList !== 'undefined') {
22855
+ this.x_google_ignoreList = properties.x_google_ignoreList;
22856
+ }
22857
+ }
22858
+
22859
+ toString() {
22860
+ return JSON.stringify(this);
22861
+ }
22862
+
22863
+ toUrl() {
22864
+ return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
22865
+ }
22866
+ }
22867
+
22868
+ function guessIndent(code) {
22869
+ const lines = code.split('\n');
22870
+
22871
+ const tabbed = lines.filter((line) => /^\t+/.test(line));
22872
+ const spaced = lines.filter((line) => /^ {2,}/.test(line));
22873
+
22874
+ if (tabbed.length === 0 && spaced.length === 0) {
22875
+ return null;
22876
+ }
22877
+
22878
+ // More lines tabbed than spaced? Assume tabs, and
22879
+ // default to tabs in the case of a tie (or nothing
22880
+ // to go on)
22881
+ if (tabbed.length >= spaced.length) {
22882
+ return '\t';
22883
+ }
22884
+
22885
+ // Otherwise, we need to guess the multiple
22886
+ const min = spaced.reduce((previous, current) => {
22887
+ const numSpaces = /^ +/.exec(current)[0].length;
22888
+ return Math.min(numSpaces, previous);
22889
+ }, Infinity);
22890
+
22891
+ return new Array(min + 1).join(' ');
22892
+ }
22893
+
22894
+ function getRelativePath(from, to) {
22895
+ const fromParts = from.split(/[/\\]/);
22896
+ const toParts = to.split(/[/\\]/);
22897
+
22898
+ fromParts.pop(); // get dirname
22899
+
22900
+ while (fromParts[0] === toParts[0]) {
22901
+ fromParts.shift();
22902
+ toParts.shift();
22903
+ }
22904
+
22905
+ if (fromParts.length) {
22906
+ let i = fromParts.length;
22907
+ while (i--) fromParts[i] = '..';
22908
+ }
22909
+
22910
+ return fromParts.concat(toParts).join('/');
22911
+ }
22912
+
22913
+ const toString = Object.prototype.toString;
22914
+
22915
+ function isObject(thing) {
22916
+ return toString.call(thing) === '[object Object]';
22917
+ }
22918
+
22919
+ function getLocator(source) {
22920
+ const originalLines = source.split('\n');
22921
+ const lineOffsets = [];
22922
+
22923
+ for (let i = 0, pos = 0; i < originalLines.length; i++) {
22924
+ lineOffsets.push(pos);
22925
+ pos += originalLines[i].length + 1;
22926
+ }
22927
+
22928
+ return function locate(index) {
22929
+ let i = 0;
22930
+ let j = lineOffsets.length;
22931
+ while (i < j) {
22932
+ const m = (i + j) >> 1;
22933
+ if (index < lineOffsets[m]) {
22934
+ j = m;
22935
+ } else {
22936
+ i = m + 1;
22937
+ }
22938
+ }
22939
+ const line = i - 1;
22940
+ const column = index - lineOffsets[line];
22941
+ return { line, column };
22942
+ };
22943
+ }
22944
+
22945
+ const wordRegex = /\w/;
22946
+
22947
+ class Mappings {
22948
+ constructor(hires) {
22949
+ this.hires = hires;
22950
+ this.generatedCodeLine = 0;
22951
+ this.generatedCodeColumn = 0;
22952
+ this.raw = [];
22953
+ this.rawSegments = this.raw[this.generatedCodeLine] = [];
22954
+ this.pending = null;
22955
+ }
22956
+
22957
+ addEdit(sourceIndex, content, loc, nameIndex) {
22958
+ if (content.length) {
22959
+ const contentLengthMinusOne = content.length - 1;
22960
+ let contentLineEnd = content.indexOf('\n', 0);
22961
+ let previousContentLineEnd = -1;
22962
+ // Loop through each line in the content and add a segment, but stop if the last line is empty,
22963
+ // else code afterwards would fill one line too many
22964
+ while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
22965
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
22966
+ if (nameIndex >= 0) {
22967
+ segment.push(nameIndex);
22968
+ }
22969
+ this.rawSegments.push(segment);
22970
+
22971
+ this.generatedCodeLine += 1;
22972
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
22973
+ this.generatedCodeColumn = 0;
22974
+
22975
+ previousContentLineEnd = contentLineEnd;
22976
+ contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
22977
+ }
22978
+
22979
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
22980
+ if (nameIndex >= 0) {
22981
+ segment.push(nameIndex);
22982
+ }
22983
+ this.rawSegments.push(segment);
22984
+
22985
+ this.advance(content.slice(previousContentLineEnd + 1));
22986
+ } else if (this.pending) {
22987
+ this.rawSegments.push(this.pending);
22988
+ this.advance(content);
22989
+ }
22990
+
22991
+ this.pending = null;
22992
+ }
22993
+
22994
+ addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
22995
+ let originalCharIndex = chunk.start;
22996
+ let first = true;
22997
+ // when iterating each char, check if it's in a word boundary
22998
+ let charInHiresBoundary = false;
22999
+
23000
+ while (originalCharIndex < chunk.end) {
23001
+ if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
23002
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
23003
+
23004
+ if (this.hires === 'boundary') {
23005
+ // in hires "boundary", group segments per word boundary than per char
23006
+ if (wordRegex.test(original[originalCharIndex])) {
23007
+ // for first char in the boundary found, start the boundary by pushing a segment
23008
+ if (!charInHiresBoundary) {
23009
+ this.rawSegments.push(segment);
23010
+ charInHiresBoundary = true;
23011
+ }
23012
+ } else {
23013
+ // for non-word char, end the boundary by pushing a segment
23014
+ this.rawSegments.push(segment);
23015
+ charInHiresBoundary = false;
23016
+ }
23017
+ } else {
23018
+ this.rawSegments.push(segment);
23019
+ }
23020
+ }
23021
+
23022
+ if (original[originalCharIndex] === '\n') {
23023
+ loc.line += 1;
23024
+ loc.column = 0;
23025
+ this.generatedCodeLine += 1;
23026
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
23027
+ this.generatedCodeColumn = 0;
23028
+ first = true;
23029
+ } else {
23030
+ loc.column += 1;
23031
+ this.generatedCodeColumn += 1;
23032
+ first = false;
23033
+ }
23034
+
23035
+ originalCharIndex += 1;
23036
+ }
23037
+
23038
+ this.pending = null;
23039
+ }
23040
+
23041
+ advance(str) {
23042
+ if (!str) return;
23043
+
23044
+ const lines = str.split('\n');
23045
+
23046
+ if (lines.length > 1) {
23047
+ for (let i = 0; i < lines.length - 1; i++) {
23048
+ this.generatedCodeLine++;
23049
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
23050
+ }
23051
+ this.generatedCodeColumn = 0;
23052
+ }
23053
+
23054
+ this.generatedCodeColumn += lines[lines.length - 1].length;
23055
+ }
23056
+ }
23057
+
23058
+ const n = '\n';
23059
+
23060
+ const warned = {
23061
+ insertLeft: false,
23062
+ insertRight: false,
23063
+ storeName: false,
23064
+ };
23065
+
23066
+ class MagicString {
23067
+ constructor(string, options = {}) {
23068
+ const chunk = new Chunk(0, string.length, string);
23069
+
23070
+ Object.defineProperties(this, {
23071
+ original: { writable: true, value: string },
23072
+ outro: { writable: true, value: '' },
23073
+ intro: { writable: true, value: '' },
23074
+ firstChunk: { writable: true, value: chunk },
23075
+ lastChunk: { writable: true, value: chunk },
23076
+ lastSearchedChunk: { writable: true, value: chunk },
23077
+ byStart: { writable: true, value: {} },
23078
+ byEnd: { writable: true, value: {} },
23079
+ filename: { writable: true, value: options.filename },
23080
+ indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
23081
+ sourcemapLocations: { writable: true, value: new BitSet() },
23082
+ storedNames: { writable: true, value: {} },
23083
+ indentStr: { writable: true, value: undefined },
23084
+ ignoreList: { writable: true, value: options.ignoreList },
23085
+ });
23086
+
23087
+ this.byStart[0] = chunk;
23088
+ this.byEnd[string.length] = chunk;
23089
+ }
23090
+
23091
+ addSourcemapLocation(char) {
23092
+ this.sourcemapLocations.add(char);
23093
+ }
23094
+
23095
+ append(content) {
23096
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
23097
+
23098
+ this.outro += content;
23099
+ return this;
23100
+ }
23101
+
23102
+ appendLeft(index, content) {
23103
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23104
+
23105
+ this._split(index);
23106
+
23107
+ const chunk = this.byEnd[index];
23108
+
23109
+ if (chunk) {
23110
+ chunk.appendLeft(content);
23111
+ } else {
23112
+ this.intro += content;
23113
+ }
23114
+ return this;
23115
+ }
23116
+
23117
+ appendRight(index, content) {
23118
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23119
+
23120
+ this._split(index);
23121
+
23122
+ const chunk = this.byStart[index];
23123
+
23124
+ if (chunk) {
23125
+ chunk.appendRight(content);
23126
+ } else {
23127
+ this.outro += content;
23128
+ }
23129
+ return this;
23130
+ }
23131
+
23132
+ clone() {
23133
+ const cloned = new MagicString(this.original, { filename: this.filename });
23134
+
23135
+ let originalChunk = this.firstChunk;
23136
+ let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
23137
+
23138
+ while (originalChunk) {
23139
+ cloned.byStart[clonedChunk.start] = clonedChunk;
23140
+ cloned.byEnd[clonedChunk.end] = clonedChunk;
23141
+
23142
+ const nextOriginalChunk = originalChunk.next;
23143
+ const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
23144
+
23145
+ if (nextClonedChunk) {
23146
+ clonedChunk.next = nextClonedChunk;
23147
+ nextClonedChunk.previous = clonedChunk;
23148
+
23149
+ clonedChunk = nextClonedChunk;
23150
+ }
23151
+
23152
+ originalChunk = nextOriginalChunk;
23153
+ }
23154
+
23155
+ cloned.lastChunk = clonedChunk;
23156
+
23157
+ if (this.indentExclusionRanges) {
23158
+ cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
23159
+ }
23160
+
23161
+ cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
23162
+
23163
+ cloned.intro = this.intro;
23164
+ cloned.outro = this.outro;
23165
+
23166
+ return cloned;
23167
+ }
23168
+
23169
+ generateDecodedMap(options) {
23170
+ options = options || {};
23171
+
23172
+ const sourceIndex = 0;
23173
+ const names = Object.keys(this.storedNames);
23174
+ const mappings = new Mappings(options.hires);
23175
+
23176
+ const locate = getLocator(this.original);
23177
+
23178
+ if (this.intro) {
23179
+ mappings.advance(this.intro);
23180
+ }
23181
+
23182
+ this.firstChunk.eachNext((chunk) => {
23183
+ const loc = locate(chunk.start);
23184
+
23185
+ if (chunk.intro.length) mappings.advance(chunk.intro);
23186
+
23187
+ if (chunk.edited) {
23188
+ mappings.addEdit(
23189
+ sourceIndex,
23190
+ chunk.content,
23191
+ loc,
23192
+ chunk.storeName ? names.indexOf(chunk.original) : -1,
23193
+ );
23194
+ } else {
23195
+ mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
23196
+ }
23197
+
23198
+ if (chunk.outro.length) mappings.advance(chunk.outro);
23199
+ });
23200
+
23201
+ return {
23202
+ file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
23203
+ sources: [
23204
+ options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
23205
+ ],
23206
+ sourcesContent: options.includeContent ? [this.original] : undefined,
23207
+ names,
23208
+ mappings: mappings.raw,
23209
+ x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
23210
+ };
23211
+ }
23212
+
23213
+ generateMap(options) {
23214
+ return new SourceMap(this.generateDecodedMap(options));
23215
+ }
23216
+
23217
+ _ensureindentStr() {
23218
+ if (this.indentStr === undefined) {
23219
+ this.indentStr = guessIndent(this.original);
23220
+ }
23221
+ }
23222
+
23223
+ _getRawIndentString() {
23224
+ this._ensureindentStr();
23225
+ return this.indentStr;
23226
+ }
23227
+
23228
+ getIndentString() {
23229
+ this._ensureindentStr();
23230
+ return this.indentStr === null ? '\t' : this.indentStr;
23231
+ }
23232
+
23233
+ indent(indentStr, options) {
23234
+ const pattern = /^[^\r\n]/gm;
23235
+
23236
+ if (isObject(indentStr)) {
23237
+ options = indentStr;
23238
+ indentStr = undefined;
23239
+ }
23240
+
23241
+ if (indentStr === undefined) {
23242
+ this._ensureindentStr();
23243
+ indentStr = this.indentStr || '\t';
23244
+ }
23245
+
23246
+ if (indentStr === '') return this; // noop
23247
+
23248
+ options = options || {};
23249
+
23250
+ // Process exclusion ranges
23251
+ const isExcluded = {};
23252
+
23253
+ if (options.exclude) {
23254
+ const exclusions =
23255
+ typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
23256
+ exclusions.forEach((exclusion) => {
23257
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
23258
+ isExcluded[i] = true;
23259
+ }
23260
+ });
23261
+ }
23262
+
23263
+ let shouldIndentNextCharacter = options.indentStart !== false;
23264
+ const replacer = (match) => {
23265
+ if (shouldIndentNextCharacter) return `${indentStr}${match}`;
23266
+ shouldIndentNextCharacter = true;
23267
+ return match;
23268
+ };
23269
+
23270
+ this.intro = this.intro.replace(pattern, replacer);
23271
+
23272
+ let charIndex = 0;
23273
+ let chunk = this.firstChunk;
23274
+
23275
+ while (chunk) {
23276
+ const end = chunk.end;
23277
+
23278
+ if (chunk.edited) {
23279
+ if (!isExcluded[charIndex]) {
23280
+ chunk.content = chunk.content.replace(pattern, replacer);
23281
+
23282
+ if (chunk.content.length) {
23283
+ shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
23284
+ }
23285
+ }
23286
+ } else {
23287
+ charIndex = chunk.start;
23288
+
23289
+ while (charIndex < end) {
23290
+ if (!isExcluded[charIndex]) {
23291
+ const char = this.original[charIndex];
23292
+
23293
+ if (char === '\n') {
23294
+ shouldIndentNextCharacter = true;
23295
+ } else if (char !== '\r' && shouldIndentNextCharacter) {
23296
+ shouldIndentNextCharacter = false;
23297
+
23298
+ if (charIndex === chunk.start) {
23299
+ chunk.prependRight(indentStr);
23300
+ } else {
23301
+ this._splitChunk(chunk, charIndex);
23302
+ chunk = chunk.next;
23303
+ chunk.prependRight(indentStr);
23304
+ }
23305
+ }
23306
+ }
23307
+
23308
+ charIndex += 1;
23309
+ }
23310
+ }
23311
+
23312
+ charIndex = chunk.end;
23313
+ chunk = chunk.next;
23314
+ }
23315
+
23316
+ this.outro = this.outro.replace(pattern, replacer);
23317
+
23318
+ return this;
23319
+ }
23320
+
23321
+ insert() {
23322
+ throw new Error(
23323
+ 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
23324
+ );
23325
+ }
23326
+
23327
+ insertLeft(index, content) {
23328
+ if (!warned.insertLeft) {
23329
+ console.warn(
23330
+ 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
23331
+ ); // eslint-disable-line no-console
23332
+ warned.insertLeft = true;
23333
+ }
23334
+
23335
+ return this.appendLeft(index, content);
23336
+ }
23337
+
23338
+ insertRight(index, content) {
23339
+ if (!warned.insertRight) {
23340
+ console.warn(
23341
+ 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
23342
+ ); // eslint-disable-line no-console
23343
+ warned.insertRight = true;
23344
+ }
23345
+
23346
+ return this.prependRight(index, content);
23347
+ }
23348
+
23349
+ move(start, end, index) {
23350
+ if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
23351
+
23352
+ this._split(start);
23353
+ this._split(end);
23354
+ this._split(index);
23355
+
23356
+ const first = this.byStart[start];
23357
+ const last = this.byEnd[end];
23358
+
23359
+ const oldLeft = first.previous;
23360
+ const oldRight = last.next;
23361
+
23362
+ const newRight = this.byStart[index];
23363
+ if (!newRight && last === this.lastChunk) return this;
23364
+ const newLeft = newRight ? newRight.previous : this.lastChunk;
23365
+
23366
+ if (oldLeft) oldLeft.next = oldRight;
23367
+ if (oldRight) oldRight.previous = oldLeft;
23368
+
23369
+ if (newLeft) newLeft.next = first;
23370
+ if (newRight) newRight.previous = last;
23371
+
23372
+ if (!first.previous) this.firstChunk = last.next;
23373
+ if (!last.next) {
23374
+ this.lastChunk = first.previous;
23375
+ this.lastChunk.next = null;
23376
+ }
23377
+
23378
+ first.previous = newLeft;
23379
+ last.next = newRight || null;
23380
+
23381
+ if (!newLeft) this.firstChunk = first;
23382
+ if (!newRight) this.lastChunk = last;
23383
+ return this;
23384
+ }
23385
+
23386
+ overwrite(start, end, content, options) {
23387
+ options = options || {};
23388
+ return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
23389
+ }
23390
+
23391
+ update(start, end, content, options) {
23392
+ if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
23393
+
23394
+ if (this.original.length !== 0) {
23395
+ while (start < 0) start += this.original.length;
23396
+ while (end < 0) end += this.original.length;
23397
+ }
23398
+
23399
+ if (end > this.original.length) throw new Error('end is out of bounds');
23400
+ if (start === end)
23401
+ throw new Error(
23402
+ 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
23403
+ );
23404
+
23405
+ this._split(start);
23406
+ this._split(end);
23407
+
23408
+ if (options === true) {
23409
+ if (!warned.storeName) {
23410
+ console.warn(
23411
+ 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
23412
+ ); // eslint-disable-line no-console
23413
+ warned.storeName = true;
23414
+ }
23415
+
23416
+ options = { storeName: true };
23417
+ }
23418
+ const storeName = options !== undefined ? options.storeName : false;
23419
+ const overwrite = options !== undefined ? options.overwrite : false;
23420
+
23421
+ if (storeName) {
23422
+ const original = this.original.slice(start, end);
23423
+ Object.defineProperty(this.storedNames, original, {
23424
+ writable: true,
23425
+ value: true,
23426
+ enumerable: true,
23427
+ });
23428
+ }
23429
+
23430
+ const first = this.byStart[start];
23431
+ const last = this.byEnd[end];
23432
+
23433
+ if (first) {
23434
+ let chunk = first;
23435
+ while (chunk !== last) {
23436
+ if (chunk.next !== this.byStart[chunk.end]) {
23437
+ throw new Error('Cannot overwrite across a split point');
23438
+ }
23439
+ chunk = chunk.next;
23440
+ chunk.edit('', false);
23441
+ }
23442
+
23443
+ first.edit(content, storeName, !overwrite);
23444
+ } else {
23445
+ // must be inserting at the end
23446
+ const newChunk = new Chunk(start, end, '').edit(content, storeName);
23447
+
23448
+ // TODO last chunk in the array may not be the last chunk, if it's moved...
23449
+ last.next = newChunk;
23450
+ newChunk.previous = last;
23451
+ }
23452
+ return this;
23453
+ }
23454
+
23455
+ prepend(content) {
23456
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
23457
+
23458
+ this.intro = content + this.intro;
23459
+ return this;
23460
+ }
23461
+
23462
+ prependLeft(index, content) {
23463
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23464
+
23465
+ this._split(index);
23466
+
23467
+ const chunk = this.byEnd[index];
23468
+
23469
+ if (chunk) {
23470
+ chunk.prependLeft(content);
23471
+ } else {
23472
+ this.intro = content + this.intro;
23473
+ }
23474
+ return this;
23475
+ }
23476
+
23477
+ prependRight(index, content) {
23478
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
23479
+
23480
+ this._split(index);
23481
+
23482
+ const chunk = this.byStart[index];
23483
+
23484
+ if (chunk) {
23485
+ chunk.prependRight(content);
23486
+ } else {
23487
+ this.outro = content + this.outro;
23488
+ }
23489
+ return this;
23490
+ }
23491
+
23492
+ remove(start, end) {
23493
+ if (this.original.length !== 0) {
23494
+ while (start < 0) start += this.original.length;
23495
+ while (end < 0) end += this.original.length;
23496
+ }
23497
+
23498
+ if (start === end) return this;
23499
+
23500
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
23501
+ if (start > end) throw new Error('end must be greater than start');
23502
+
23503
+ this._split(start);
23504
+ this._split(end);
23505
+
23506
+ let chunk = this.byStart[start];
23507
+
23508
+ while (chunk) {
23509
+ chunk.intro = '';
23510
+ chunk.outro = '';
23511
+ chunk.edit('');
23512
+
23513
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
23514
+ }
23515
+ return this;
23516
+ }
23517
+
23518
+ reset(start, end) {
23519
+ if (this.original.length !== 0) {
23520
+ while (start < 0) start += this.original.length;
23521
+ while (end < 0) end += this.original.length;
23522
+ }
23523
+
23524
+ if (start === end) return this;
23525
+
23526
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
23527
+ if (start > end) throw new Error('end must be greater than start');
23528
+
23529
+ this._split(start);
23530
+ this._split(end);
23531
+
23532
+ let chunk = this.byStart[start];
23533
+
23534
+ while (chunk) {
23535
+ chunk.reset();
23536
+
23537
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
23538
+ }
23539
+ return this;
23540
+ }
23541
+
23542
+ lastChar() {
23543
+ if (this.outro.length) return this.outro[this.outro.length - 1];
23544
+ let chunk = this.lastChunk;
23545
+ do {
23546
+ if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
23547
+ if (chunk.content.length) return chunk.content[chunk.content.length - 1];
23548
+ if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
23549
+ } while ((chunk = chunk.previous));
23550
+ if (this.intro.length) return this.intro[this.intro.length - 1];
23551
+ return '';
23552
+ }
23553
+
23554
+ lastLine() {
23555
+ let lineIndex = this.outro.lastIndexOf(n);
23556
+ if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
23557
+ let lineStr = this.outro;
23558
+ let chunk = this.lastChunk;
23559
+ do {
23560
+ if (chunk.outro.length > 0) {
23561
+ lineIndex = chunk.outro.lastIndexOf(n);
23562
+ if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
23563
+ lineStr = chunk.outro + lineStr;
23564
+ }
23565
+
23566
+ if (chunk.content.length > 0) {
23567
+ lineIndex = chunk.content.lastIndexOf(n);
23568
+ if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
23569
+ lineStr = chunk.content + lineStr;
23570
+ }
23571
+
23572
+ if (chunk.intro.length > 0) {
23573
+ lineIndex = chunk.intro.lastIndexOf(n);
23574
+ if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
23575
+ lineStr = chunk.intro + lineStr;
23576
+ }
23577
+ } while ((chunk = chunk.previous));
23578
+ lineIndex = this.intro.lastIndexOf(n);
23579
+ if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
23580
+ return this.intro + lineStr;
23581
+ }
23582
+
23583
+ slice(start = 0, end = this.original.length) {
23584
+ if (this.original.length !== 0) {
23585
+ while (start < 0) start += this.original.length;
23586
+ while (end < 0) end += this.original.length;
23587
+ }
23588
+
23589
+ let result = '';
23590
+
23591
+ // find start chunk
23592
+ let chunk = this.firstChunk;
23593
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
23594
+ // found end chunk before start
23595
+ if (chunk.start < end && chunk.end >= end) {
23596
+ return result;
23597
+ }
23598
+
23599
+ chunk = chunk.next;
23600
+ }
23601
+
23602
+ if (chunk && chunk.edited && chunk.start !== start)
23603
+ throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
23604
+
23605
+ const startChunk = chunk;
23606
+ while (chunk) {
23607
+ if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
23608
+ result += chunk.intro;
23609
+ }
23610
+
23611
+ const containsEnd = chunk.start < end && chunk.end >= end;
23612
+ if (containsEnd && chunk.edited && chunk.end !== end)
23613
+ throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
23614
+
23615
+ const sliceStart = startChunk === chunk ? start - chunk.start : 0;
23616
+ const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
23617
+
23618
+ result += chunk.content.slice(sliceStart, sliceEnd);
23619
+
23620
+ if (chunk.outro && (!containsEnd || chunk.end === end)) {
23621
+ result += chunk.outro;
23622
+ }
23623
+
23624
+ if (containsEnd) {
23625
+ break;
23626
+ }
23627
+
23628
+ chunk = chunk.next;
23629
+ }
23630
+
23631
+ return result;
23632
+ }
23633
+
23634
+ // TODO deprecate this? not really very useful
23635
+ snip(start, end) {
23636
+ const clone = this.clone();
23637
+ clone.remove(0, start);
23638
+ clone.remove(end, clone.original.length);
23639
+
23640
+ return clone;
23641
+ }
23642
+
23643
+ _split(index) {
23644
+ if (this.byStart[index] || this.byEnd[index]) return;
23645
+
23646
+ let chunk = this.lastSearchedChunk;
23647
+ const searchForward = index > chunk.end;
23648
+
23649
+ while (chunk) {
23650
+ if (chunk.contains(index)) return this._splitChunk(chunk, index);
23651
+
23652
+ chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
23653
+ }
23654
+ }
23655
+
23656
+ _splitChunk(chunk, index) {
23657
+ if (chunk.edited && chunk.content.length) {
23658
+ // zero-length edited chunks are a special case (overlapping replacements)
23659
+ const loc = getLocator(this.original)(index);
23660
+ throw new Error(
23661
+ `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
23662
+ );
23663
+ }
23664
+
23665
+ const newChunk = chunk.split(index);
23666
+
23667
+ this.byEnd[index] = chunk;
23668
+ this.byStart[index] = newChunk;
23669
+ this.byEnd[newChunk.end] = newChunk;
23670
+
23671
+ if (chunk === this.lastChunk) this.lastChunk = newChunk;
23672
+
23673
+ this.lastSearchedChunk = chunk;
23674
+ return true;
23675
+ }
23676
+
23677
+ toString() {
23678
+ let str = this.intro;
23679
+
23680
+ let chunk = this.firstChunk;
23681
+ while (chunk) {
23682
+ str += chunk.toString();
23683
+ chunk = chunk.next;
23684
+ }
23685
+
23686
+ return str + this.outro;
23687
+ }
23688
+
23689
+ isEmpty() {
23690
+ let chunk = this.firstChunk;
23691
+ do {
23692
+ if (
23693
+ (chunk.intro.length && chunk.intro.trim()) ||
23694
+ (chunk.content.length && chunk.content.trim()) ||
23695
+ (chunk.outro.length && chunk.outro.trim())
23696
+ )
23697
+ return false;
23698
+ } while ((chunk = chunk.next));
23699
+ return true;
23700
+ }
23701
+
23702
+ length() {
23703
+ let chunk = this.firstChunk;
23704
+ let length = 0;
23705
+ do {
23706
+ length += chunk.intro.length + chunk.content.length + chunk.outro.length;
23707
+ } while ((chunk = chunk.next));
23708
+ return length;
23709
+ }
23710
+
23711
+ trimLines() {
23712
+ return this.trim('[\\r\\n]');
23713
+ }
23714
+
23715
+ trim(charType) {
23716
+ return this.trimStart(charType).trimEnd(charType);
23717
+ }
23718
+
23719
+ trimEndAborted(charType) {
23720
+ const rx = new RegExp((charType || '\\s') + '+$');
23721
+
23722
+ this.outro = this.outro.replace(rx, '');
23723
+ if (this.outro.length) return true;
23724
+
23725
+ let chunk = this.lastChunk;
23726
+
23727
+ do {
23728
+ const end = chunk.end;
23729
+ const aborted = chunk.trimEnd(rx);
23730
+
23731
+ // if chunk was trimmed, we have a new lastChunk
23732
+ if (chunk.end !== end) {
23733
+ if (this.lastChunk === chunk) {
23734
+ this.lastChunk = chunk.next;
23735
+ }
23736
+
23737
+ this.byEnd[chunk.end] = chunk;
23738
+ this.byStart[chunk.next.start] = chunk.next;
23739
+ this.byEnd[chunk.next.end] = chunk.next;
23740
+ }
23741
+
23742
+ if (aborted) return true;
23743
+ chunk = chunk.previous;
23744
+ } while (chunk);
23745
+
23746
+ return false;
23747
+ }
23748
+
23749
+ trimEnd(charType) {
23750
+ this.trimEndAborted(charType);
23751
+ return this;
23752
+ }
23753
+ trimStartAborted(charType) {
23754
+ const rx = new RegExp('^' + (charType || '\\s') + '+');
23755
+
23756
+ this.intro = this.intro.replace(rx, '');
23757
+ if (this.intro.length) return true;
23758
+
23759
+ let chunk = this.firstChunk;
23760
+
23761
+ do {
23762
+ const end = chunk.end;
23763
+ const aborted = chunk.trimStart(rx);
23764
+
23765
+ if (chunk.end !== end) {
23766
+ // special case...
23767
+ if (chunk === this.lastChunk) this.lastChunk = chunk.next;
23768
+
23769
+ this.byEnd[chunk.end] = chunk;
23770
+ this.byStart[chunk.next.start] = chunk.next;
23771
+ this.byEnd[chunk.next.end] = chunk.next;
23772
+ }
23773
+
23774
+ if (aborted) return true;
23775
+ chunk = chunk.next;
23776
+ } while (chunk);
23777
+
23778
+ return false;
23779
+ }
23780
+
23781
+ trimStart(charType) {
23782
+ this.trimStartAborted(charType);
23783
+ return this;
23784
+ }
23785
+
23786
+ hasChanged() {
23787
+ return this.original !== this.toString();
23788
+ }
23789
+
23790
+ _replaceRegexp(searchValue, replacement) {
23791
+ function getReplacement(match, str) {
23792
+ if (typeof replacement === 'string') {
23793
+ return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
23794
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
23795
+ if (i === '$') return '$';
23796
+ if (i === '&') return match[0];
23797
+ const num = +i;
23798
+ if (num < match.length) return match[+i];
23799
+ return `$${i}`;
23800
+ });
23801
+ } else {
23802
+ return replacement(...match, match.index, str, match.groups);
23803
+ }
23804
+ }
23805
+ function matchAll(re, str) {
23806
+ let match;
23807
+ const matches = [];
23808
+ while ((match = re.exec(str))) {
23809
+ matches.push(match);
23810
+ }
23811
+ return matches;
23812
+ }
23813
+ if (searchValue.global) {
23814
+ const matches = matchAll(searchValue, this.original);
23815
+ matches.forEach((match) => {
23816
+ if (match.index != null) {
23817
+ const replacement = getReplacement(match, this.original);
23818
+ if (replacement !== match[0]) {
23819
+ this.overwrite(
23820
+ match.index,
23821
+ match.index + match[0].length,
23822
+ replacement
23823
+ );
23824
+ }
23825
+ }
23826
+ });
23827
+ } else {
23828
+ const match = this.original.match(searchValue);
23829
+ if (match && match.index != null) {
23830
+ const replacement = getReplacement(match, this.original);
23831
+ if (replacement !== match[0]) {
23832
+ this.overwrite(
23833
+ match.index,
23834
+ match.index + match[0].length,
23835
+ replacement
23836
+ );
23837
+ }
23838
+ }
23839
+ }
23840
+ return this;
23841
+ }
23842
+
23843
+ _replaceString(string, replacement) {
23844
+ const { original } = this;
23845
+ const index = original.indexOf(string);
23846
+
23847
+ if (index !== -1) {
23848
+ this.overwrite(index, index + string.length, replacement);
23849
+ }
23850
+
23851
+ return this;
23852
+ }
23853
+
23854
+ replace(searchValue, replacement) {
23855
+ if (typeof searchValue === 'string') {
23856
+ return this._replaceString(searchValue, replacement);
23857
+ }
23858
+
23859
+ return this._replaceRegexp(searchValue, replacement);
23860
+ }
23861
+
23862
+ _replaceAllString(string, replacement) {
23863
+ const { original } = this;
23864
+ const stringLength = string.length;
23865
+ for (
23866
+ let index = original.indexOf(string);
23867
+ index !== -1;
23868
+ index = original.indexOf(string, index + stringLength)
23869
+ ) {
23870
+ const previous = original.slice(index, index + stringLength);
23871
+ if (previous !== replacement)
23872
+ this.overwrite(index, index + stringLength, replacement);
23873
+ }
23874
+
23875
+ return this;
23876
+ }
23877
+
23878
+ replaceAll(searchValue, replacement) {
23879
+ if (typeof searchValue === 'string') {
23880
+ return this._replaceAllString(searchValue, replacement);
23881
+ }
23882
+
23883
+ if (!searchValue.global) {
23884
+ throw new TypeError(
23885
+ 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
23886
+ );
23887
+ }
23888
+
23889
+ return this._replaceRegexp(searchValue, replacement);
23890
+ }
23891
+ }
23892
+
22639
23893
  var utils = {};
22640
23894
 
22641
23895
  var constants;