rollup 4.66.0 → 4.67.4

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.
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  @license
3
- Rollup.js v4.66.0
4
- Wed, 28 Oct 2026 13:27:47 GMT - commit 4de8104f55fe5c0df06aab54c1f9f2967dbe66ac
3
+ Rollup.js v4.67.4
4
+ Sun, 01 Nov 2026 07:42:20 GMT - commit 18b5cb60e55926f6d917cc1aeb17fd81d0da5cc6
5
5
 
6
6
  https://github.com/rollup/rollup
7
7
 
@@ -9,7 +9,7 @@
9
9
  */
10
10
  import { EMPTY_OBJECT, ExportDefaultDeclaration as ExportDefaultDeclaration$1, CallExpression as CallExpression$1, EMPTY_ARRAY, LOGLEVEL_WARN, logUnusedExternalImports, ANNOTATION_KEY, INVALID_ANNOTATION_KEY, ArrowFunctionExpression as ArrowFunctionExpression$1, FunctionExpression as FunctionExpression$1, Identifier as Identifier$1, ExpressionStatement as ExpressionStatement$1, AwaitExpression as AwaitExpression$1, MemberExpression as MemberExpression$1, ObjectExpression as ObjectExpression$1, Property as Property$1, Program as Program$1, logIllegalImportReassignment, BLANK, logRedeclarationError, StaticBlock as StaticBlock$1, CatchClause as CatchClause$1, logDuplicateArgumentNameError, logModuleLevelDirective, ReturnStatement as ReturnStatement$1, VariableDeclarator as VariableDeclarator$1, logMissingExport, normalize, getImportPath, error, logSourcePhaseFormatUnsupported, logMissingNodeBuiltins, logReservedNamespace, logIllegalIdentifierAsName, logMissingNameOptionForIifeExport, logMissingNameOptionForUmdExport, RestElement as RestElement$1, logConstVariableReassignError, EMPTY_SET, logCannotCallNamespace, logEval, BlockStatement as BlockStatement$1, getRollupError, logModuleParseError, logParseError, LOGLEVEL_INFO, logFirstSideEffect, locate, logInvalidAnnotation, logThisIsUndefined, getAstBuffer, convertAnnotations, FIXED_STRINGS, convertNode as convertNode$1, isAbsolute as isAbsolute$1, logImportAttributeIsInvalid, logImportOptionsAreInvalid, logMissingEntryExport, logSyntheticNamedExportsNeedNamespaceExport, logDuplicateExportError, logInvalidSourcemapForError, augmentCodeLocation, logInconsistentImportAttributes, logMissingJsxExport, logNamespaceConflict, logAmbiguousExternalNamespaces, logShimmedExport, parseAst, TemplateLiteral as TemplateLiteral$1, Literal as Literal$1, logCircularReexport, logInvalidFormatForTopLevelAwait, logAddonNotGenerated, logIncompatibleExportOptionValue, logMixedExport, logFailedValidation, isPathFragment, logCyclicCrossChunkReexport, getAliasName, logUnexpectedNamedImport, relative as relative$1, logUnexpectedNamespaceReexport, logEmptyChunk, logMissingGlobalName, logOptimizeChunkStatus, logSourcemapBroken, logConflictingSourcemapSources, logErrorInHook, logChunkInvalid, logInvalidOption, URL_OUTPUT_FORMAT, URL_OUTPUT_DIR, URL_OUTPUT_SOURCEMAPFILE, URL_OUTPUT_AMD_ID, logFileNameOutsideOutputDirectory, logCannotAssignModuleToChunk, logCircularChunk, logUnknownOption, printQuotedStringList, LOGLEVEL_ERROR, logLevelPriority, LOGLEVEL_DEBUG, logAnonymousPluginCache, logDuplicatePluginName, logInvalidSetAssetSourceCall, logPluginError, logNoTransformMapOrAstWithoutCode, warnDeprecation, URL_TRANSFORM, relativeId, logBadLoader, logNonExternalSourcePhaseImport, logInternalIdCannotBeExternal, isRelative, logUnresolvedImport, logUnresolvedImportTreatedAsExternal, logExternalSyntheticExports, logUnresolvedEntry, logUnresolvedImplicitDependant, logExternalModulesCannotBeIncludedInManualChunks, logEntryCannotBeExternal, logImplicitDependantCannotBeExternal, logExternalModulesCannotBeTransformedToModules, URL_LOAD, logNoAssetSourceSet, logFileReferenceIdNotFoundForFilename, logAssetReferenceIdNotFoundForSetSource, logAssetSourceAlreadySet, logInvalidRollupPhaseForChunkEmission, logChunkNotGeneratedForFileName, logAssetNotFinalisedForFileName, logFileNameConflict, URL_GENERATEBUNDLE, logInvalidLogPosition, logInputHookInOutputPlugin, logInvalidAddonPluginHook, logInvalidFunctionPluginHook, logImplicitDependantIsNotIncluded, logCircularDependency, augmentLogMessage, URL_JSX, URL_TREESHAKE, URL_TREESHAKE_MODULESIDEEFFECTS, URL_OUTPUT_INLINEDYNAMICIMPORTS, URL_PRESERVEENTRYSIGNATURES, URL_OUTPUT_GENERATEDCODE, isValidUrl, addTrailingSlashIfMissed, URL_OUTPUT_SOURCEMAPBASEURL, URL_OUTPUT_MANUALCHUNKS, logInvalidExportOptionValue, URL_OUTPUT_AMD_BASEPATH, URL_OUTPUT_INTEROP, URL_OUTPUT_EXTERNALIMPORTATTRIBUTES, logAlreadyClosed, logMissingFileOrDirOption, logCannotEmitFromOptionsHook, URL_WATCH } from './parseAst.js';
11
11
  import { relative, dirname, basename, extname, resolve as resolve$1, join } from 'node:path';
12
- import { posix, isAbsolute, resolve, win32 } from 'path';
12
+ import { isAbsolute, posix, win32, resolve } from 'path';
13
13
  import { parseAsync, xxhashBase16, xxhashBase64Url, xxhashBase36 } from '../../native.js';
14
14
  import process$1, { env } from 'node:process';
15
15
  import { performance } from 'node:perf_hooks';
@@ -27,7 +27,7 @@ function _mergeNamespaces(n, m) {
27
27
  return Object.defineProperty(n, Symbol.toStringTag, { value: 'Module' });
28
28
  }
29
29
 
30
- var version = "4.66.0";
30
+ var version = "4.67.4";
31
31
  const pkg = {
32
32
  version: version};
33
33
 
@@ -232,6 +232,17 @@ var BitSet = class BitSet {
232
232
  has(n) {
233
233
  return !!(this.bits[n >> 5] & 1 << (n & 31));
234
234
  }
235
+ forEach(fn) {
236
+ const bits = this.bits;
237
+ for (let w = 0; w < bits.length; w++) {
238
+ let word = bits[w];
239
+ while (word) {
240
+ const lowest = word & -word;
241
+ fn((w << 5) + 31 - Math.clz32(lowest));
242
+ word ^= lowest;
243
+ }
244
+ }
245
+ }
235
246
  };
236
247
  //#endregion
237
248
  //#region src/Chunk.ts
@@ -245,6 +256,8 @@ var Chunk$1 = class Chunk {
245
256
  this.content = content;
246
257
  this.storeName = false;
247
258
  this.edited = false;
259
+ this.replacesUntil = -1;
260
+ this.replacedFrom = -1;
248
261
  this.previous = null;
249
262
  this.next = null;
250
263
  }
@@ -261,6 +274,8 @@ var Chunk$1 = class Chunk {
261
274
  chunk.content = this.content;
262
275
  chunk.storeName = this.storeName;
263
276
  chunk.edited = this.edited;
277
+ chunk.replacesUntil = this.replacesUntil;
278
+ chunk.replacedFrom = this.replacedFrom;
264
279
  return chunk;
265
280
  }
266
281
  contains(index) {
@@ -301,6 +316,8 @@ var Chunk$1 = class Chunk {
301
316
  reset() {
302
317
  this.intro = "";
303
318
  this.outro = "";
319
+ this.replacesUntil = -1;
320
+ this.replacedFrom = -1;
304
321
  if (this.edited) {
305
322
  this.content = this.original;
306
323
  this.storeName = false;
@@ -318,7 +335,7 @@ var Chunk$1 = class Chunk {
318
335
  this.end = index;
319
336
  if (this.edited) {
320
337
  newChunk.edit("", false, true);
321
- this.content = "";
338
+ newChunk.replacedFrom = this.replacedFrom;
322
339
  } else this.content = originalBefore;
323
340
  newChunk.next = this.next;
324
341
  if (newChunk.next) newChunk.next.previous = newChunk;
@@ -582,8 +599,8 @@ var Mappings = class {
582
599
  this.generatedCodeColumn = 0;
583
600
  this.raw = [];
584
601
  this.rawSegments = this.raw[this.generatedCodeLine] = [];
585
- this.rawRangeMappings = [];
586
- this.rawRangeMappingsIndices = this.rawRangeMappings[this.generatedCodeLine] = [];
602
+ this.rawRangeMappingsIndices = [];
603
+ this.rawRangeMappings = hires === "experimental-range" ? [this.rawRangeMappingsIndices] : void 0;
587
604
  this.encoder = encoder;
588
605
  }
589
606
  nextLine() {
@@ -596,7 +613,28 @@ var Mappings = class {
596
613
  this.generatedCodeLine += 1;
597
614
  }
598
615
  this.generatedCodeColumn = 0;
599
- this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
616
+ if (this.rawRangeMappings) this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
617
+ }
618
+ /**
619
+ * Emits the mappings for one MagicString: its intro, every chunk (with the
620
+ * chunk's own intro/outro) and its outro. `sourceIndex === -1` means the
621
+ * string has no source of its own, so its content only advances the
622
+ * generated position without mapping anything.
623
+ */
624
+ addMagicString(magicString, sourceIndex, nameIndex) {
625
+ const { original } = magicString;
626
+ const locate = getLocator(original);
627
+ if (magicString.intro) this.advance(magicString.intro);
628
+ magicString.firstChunk.eachNext((chunk) => {
629
+ if (chunk.intro.length) this.advance(chunk.intro);
630
+ if (sourceIndex === -1) this.advance(chunk.content);
631
+ else if (chunk.edited) {
632
+ const name = chunk.storeName ? nameIndex.get(original.slice(chunk.start, chunk.replacesUntil)) : void 0;
633
+ this.addEdit(sourceIndex, chunk.content, locate(chunk.start), name ?? -1);
634
+ } else this.addUneditedChunk(sourceIndex, chunk, original, locate(chunk.start), magicString.sourcemapLocations);
635
+ if (chunk.outro.length) this.advance(chunk.outro);
636
+ });
637
+ if (magicString.outro) this.advance(magicString.outro);
600
638
  }
601
639
  addEdit(sourceIndex, content, loc, nameIndex) {
602
640
  if (content.length) {
@@ -811,6 +849,9 @@ function expandReplacement(replacement, matched, position, str, captures, namedC
811
849
  return result;
812
850
  }
813
851
  var MagicString = class MagicString {
852
+ /**
853
+ * Wraps `string` for editing; every index passed to the editing methods refers to this original string.
854
+ */
814
855
  constructor(string, options = {}) {
815
856
  const chunk = new Chunk$1(0, string.length, string);
816
857
  Object.defineProperties(this, {
@@ -860,7 +901,7 @@ var MagicString = class MagicString {
860
901
  },
861
902
  storedNames: {
862
903
  writable: true,
863
- value: {}
904
+ value: /* @__PURE__ */ new Set()
864
905
  },
865
906
  indentStr: {
866
907
  writable: true,
@@ -900,7 +941,8 @@ var MagicString = class MagicString {
900
941
  * See also `s.prependLeft(...)`.
901
942
  */
902
943
  appendLeft(index, content) {
903
- index = index + this.offset;
944
+ index = this._resolveIndex(index);
945
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
904
946
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
905
947
  this._split(index);
906
948
  const chunk = this.byEnd.get(index);
@@ -914,7 +956,8 @@ var MagicString = class MagicString {
914
956
  * See also `s.prependRight(...)`.
915
957
  */
916
958
  appendRight(index, content) {
917
- index = index + this.offset;
959
+ index = this._resolveIndex(index);
960
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
918
961
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
919
962
  this._split(index);
920
963
  const chunk = this.byStart.get(index);
@@ -923,7 +966,10 @@ var MagicString = class MagicString {
923
966
  return this;
924
967
  }
925
968
  /**
926
- * Does what you'd expect.
969
+ * Returns an independent copy: the chunk list, string-level intro/outro,
970
+ * sourcemap locations, stored names, indent exclusion ranges, `offset`,
971
+ * `filename` and `ignoreList` are all copied; later edits to either string
972
+ * do not affect the other.
927
973
  */
928
974
  clone() {
929
975
  const cloned = new MagicString(this.original, {
@@ -948,7 +994,7 @@ var MagicString = class MagicString {
948
994
  cloned.lastChunk = clonedChunk;
949
995
  if (this.indentExclusionRanges) cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
950
996
  cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
951
- cloned.storedNames = { ...this.storedNames };
997
+ cloned.storedNames = new Set(this.storedNames);
952
998
  cloned.intro = this.intro;
953
999
  cloned.outro = this.outro;
954
1000
  cloned.hasMovedChunks = this.hasMovedChunks;
@@ -984,18 +1030,8 @@ var MagicString = class MagicString {
984
1030
  }
985
1031
  /** @internal */
986
1032
  _generateMappings(mappings) {
987
- const sourceIndex = 0;
988
- const names = Object.keys(this.storedNames);
989
- const locate = getLocator(this.original);
990
- if (this.intro) mappings.advance(this.intro);
991
- this.firstChunk.eachNext((chunk) => {
992
- const loc = locate(chunk.start);
993
- if (chunk.intro.length) mappings.advance(chunk.intro);
994
- if (chunk.edited) mappings.addEdit(sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf(chunk.original) : -1);
995
- else mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
996
- if (chunk.outro.length) mappings.advance(chunk.outro);
997
- });
998
- if (this.outro) mappings.advance(this.outro);
1033
+ const names = [...this.storedNames];
1034
+ mappings.addMagicString(this, 0, new Map(names.map((name, i) => [name, i])));
999
1035
  return names;
1000
1036
  }
1001
1037
  /** @internal */
@@ -1017,9 +1053,13 @@ var MagicString = class MagicString {
1017
1053
  this._ensureindentStr();
1018
1054
  return this.indentStr;
1019
1055
  }
1056
+ /**
1057
+ * Returns the indentation string guessed from the original content, falling back to a single tab character.
1058
+ * This is the value `s.indent()` uses when no prefix is supplied.
1059
+ */
1020
1060
  getIndentString() {
1021
1061
  this._ensureindentStr();
1022
- return this.indentStr === null ? " " : this.indentStr;
1062
+ return this.indentStr ?? " ";
1023
1063
  }
1024
1064
  indent(indentStr, options) {
1025
1065
  const pattern = /^[^\r\n]/gm;
@@ -1034,9 +1074,9 @@ var MagicString = class MagicString {
1034
1074
  if (indentStr === "") return this;
1035
1075
  const resolvedIndentStr = indentStr;
1036
1076
  options = options || {};
1037
- const isExcluded = {};
1077
+ const isExcluded = new BitSet();
1038
1078
  if (options.exclude) (typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude).forEach((exclusion) => {
1039
- for (let i = exclusion[0]; i < exclusion[1]; i += 1) isExcluded[i] = true;
1079
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) isExcluded.add(i);
1040
1080
  });
1041
1081
  let shouldIndentNextCharacter = options.indentStart !== false;
1042
1082
  const indentPiece = (str) => {
@@ -1059,13 +1099,14 @@ var MagicString = class MagicString {
1059
1099
  };
1060
1100
  while (chunk) {
1061
1101
  const end = chunk.end;
1062
- if (!isExcluded[chunk.start]) chunk.intro = indentPiece(chunk.intro);
1102
+ if (!isExcluded.has(chunk.start)) chunk.intro = indentPiece(chunk.intro);
1063
1103
  if (chunk.edited) {
1064
- if (!isExcluded[charIndex]) chunk.content = indentPiece(chunk.content);
1104
+ if (!isExcluded.has(chunk.start)) chunk.content = indentPiece(chunk.content);
1105
+ else if (chunk.content) shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
1065
1106
  } else if (options.exclude) {
1066
1107
  charIndex = chunk.start;
1067
1108
  while (charIndex < end) {
1068
- if (!isExcluded[charIndex]) {
1109
+ if (!isExcluded.has(charIndex)) {
1069
1110
  const char = this.original.charCodeAt(charIndex);
1070
1111
  if (char === NEWLINE_CHAR) shouldIndentNextCharacter = true;
1071
1112
  else if (char !== CR_CHAR && shouldIndentNextCharacter) indentAt(charIndex);
@@ -1091,7 +1132,7 @@ var MagicString = class MagicString {
1091
1132
  charIndex += 1;
1092
1133
  }
1093
1134
  }
1094
- if (!isExcluded[chunk.end - 1]) chunk.outro = indentPiece(chunk.outro);
1135
+ if (!isExcluded.has(chunk.end - 1)) chunk.outro = indentPiece(chunk.outro);
1095
1136
  charIndex = chunk.end;
1096
1137
  chunk = chunk.next;
1097
1138
  }
@@ -1128,16 +1169,19 @@ var MagicString = class MagicString {
1128
1169
  * mirroring the `appendLeft`/`appendRight` distinction.
1129
1170
  */
1130
1171
  move(start, end, index, affinity = "right") {
1131
- start = start + this.offset;
1132
- end = end + this.offset;
1133
- index = index + this.offset;
1172
+ start = this._resolveIndex(start);
1173
+ end = this._resolveIndex(end);
1174
+ index = this._resolveIndex(index);
1134
1175
  if (start === end) return this;
1176
+ if (start < 0 || end > this.original.length || index < 0 || index > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1177
+ if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
1135
1178
  if (index >= start && index <= end) throw new MagicStringError("cannot move a selection inside itself");
1136
1179
  this._split(start);
1137
1180
  this._split(end);
1138
1181
  this._split(index);
1139
1182
  const first = this.byStart.get(start);
1140
1183
  const last = this.byEnd.get(end);
1184
+ if (!first || !last) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1141
1185
  if (this.hasMovedChunks) {
1142
1186
  let cursor = first;
1143
1187
  while (cursor !== last) {
@@ -1172,9 +1216,9 @@ var MagicString = class MagicString {
1172
1216
  if (oldRight) oldRight.previous = oldLeft;
1173
1217
  if (newLeft) newLeft.next = first;
1174
1218
  if (newRight) newRight.previous = last;
1175
- if (!first.previous) this.firstChunk = last.next;
1176
- if (!last.next) {
1177
- this.lastChunk = first.previous;
1219
+ if (!oldLeft) this.firstChunk = oldRight;
1220
+ if (!oldRight) {
1221
+ this.lastChunk = oldLeft;
1178
1222
  this.lastChunk.next = null;
1179
1223
  }
1180
1224
  first.previous = newLeft;
@@ -1195,6 +1239,16 @@ var MagicString = class MagicString {
1195
1239
  * It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
1196
1240
  */
1197
1241
  overwrite(start, end, content, options) {
1242
+ if (options === true) {
1243
+ if (!warned.storeName) {
1244
+ console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string");
1245
+ warned.storeName = true;
1246
+ }
1247
+ return this.update(start, end, content, {
1248
+ storeName: true,
1249
+ overwrite: true
1250
+ });
1251
+ }
1198
1252
  const optionObject = typeof options === "object" && options ? options : {};
1199
1253
  return this.update(start, end, content, {
1200
1254
  ...optionObject,
@@ -1209,13 +1263,9 @@ var MagicString = class MagicString {
1209
1263
  * the content is overwritten, or anything that was appended/prepended to the range as well.
1210
1264
  */
1211
1265
  update(start, end, content, options) {
1212
- start = start + this.offset;
1213
- end = end + this.offset;
1266
+ start = this._resolveIndex(start);
1267
+ end = this._resolveIndex(end);
1214
1268
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1215
- if (this.original.length !== 0) {
1216
- if (start < 0) start = Math.max(0, start + this.original.length);
1217
- if (end < 0) end = Math.max(0, end + this.original.length);
1218
- }
1219
1269
  if (start < 0) throw new MagicStringError(`start ${start} is out of bounds`);
1220
1270
  if (end > this.original.length) throw new MagicStringError(`end ${end} is out of bounds`);
1221
1271
  if (start === end) throw new MagicStringError(`cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`);
@@ -1232,30 +1282,21 @@ var MagicString = class MagicString {
1232
1282
  const optionObject = typeof options === "object" && options ? options : {};
1233
1283
  const storeName = optionObject.storeName || false;
1234
1284
  const overwrite = optionObject.overwrite || false;
1235
- if (storeName) {
1236
- const original = this.original.slice(start, end);
1237
- Object.defineProperty(this.storedNames, original, {
1238
- writable: true,
1239
- value: true,
1240
- enumerable: true
1241
- });
1242
- }
1285
+ if (storeName) this.storedNames.add(this.original.slice(start, end));
1243
1286
  const first = this.byStart.get(start);
1244
- const last = this.byEnd.get(end);
1245
- /* v8 ignore else -- unreachable: a valid start/end always yields a `first` chunk */
1246
- if (first) {
1247
- let chunk = first;
1248
- while (chunk !== last) {
1249
- if (chunk.next !== this.byStart.get(chunk.end)) throw new MagicStringError("cannot overwrite across a split point");
1250
- chunk = chunk.next;
1251
- chunk.edit("", false, !overwrite);
1252
- }
1253
- first.edit(content, storeName, !overwrite);
1254
- } else {
1255
- const newChunk = new Chunk$1(start, end, "").edit(content, storeName);
1256
- last.next = newChunk;
1257
- newChunk.previous = last;
1287
+ for (let chunk = first; chunk.end < end;) {
1288
+ const next = chunk.next;
1289
+ if (next === null || next !== this.byStart.get(chunk.end)) throw new MagicStringError("cannot overwrite across a split point");
1290
+ chunk = next;
1291
+ }
1292
+ for (let chunk = first.next; chunk !== null && chunk.start < end; chunk = chunk.next) {
1293
+ chunk.edit("", false, !overwrite);
1294
+ chunk.replacesUntil = -1;
1295
+ chunk.replacedFrom = start;
1258
1296
  }
1297
+ first.edit(content, storeName, !overwrite);
1298
+ first.replacesUntil = end;
1299
+ first.replacedFrom = -1;
1259
1300
  return this;
1260
1301
  }
1261
1302
  /**
@@ -1270,7 +1311,8 @@ var MagicString = class MagicString {
1270
1311
  * Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
1271
1312
  */
1272
1313
  prependLeft(index, content) {
1273
- index = index + this.offset;
1314
+ index = this._resolveIndex(index);
1315
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
1274
1316
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1275
1317
  this._split(index);
1276
1318
  const chunk = this.byEnd.get(index);
@@ -1282,7 +1324,8 @@ var MagicString = class MagicString {
1282
1324
  * Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
1283
1325
  */
1284
1326
  prependRight(index, content) {
1285
- index = index + this.offset;
1327
+ index = this._resolveIndex(index);
1328
+ if (index < 0 || index > this.original.length) throw new MagicStringError(`index ${index} is out of bounds`);
1286
1329
  if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
1287
1330
  this._split(index);
1288
1331
  const chunk = this.byStart.get(index);
@@ -1298,12 +1341,8 @@ var MagicString = class MagicString {
1298
1341
  * Repeated removals and removals with partially overlapping ranges are allowed.
1299
1342
  */
1300
1343
  remove(start, end) {
1301
- start = start + this.offset;
1302
- end = end + this.offset;
1303
- if (this.original.length !== 0) {
1304
- if (start < 0) start = Math.max(0, start + this.original.length);
1305
- if (end < 0) end = Math.max(0, end + this.original.length);
1306
- }
1344
+ start = this._resolveIndex(start);
1345
+ end = this._resolveIndex(end);
1307
1346
  if (start === end) return this;
1308
1347
  if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1309
1348
  if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
@@ -1314,7 +1353,9 @@ var MagicString = class MagicString {
1314
1353
  if (chunk.start > start) chunk.intro = "";
1315
1354
  if (chunk.end < end) chunk.outro = "";
1316
1355
  chunk.edit("", false, true);
1317
- chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
1356
+ chunk.replacesUntil = -1;
1357
+ chunk.replacedFrom = -1;
1358
+ chunk = end > chunk.end ? this.byStart.get(chunk.end) : void 0;
1318
1359
  }
1319
1360
  return this;
1320
1361
  }
@@ -1322,12 +1363,8 @@ var MagicString = class MagicString {
1322
1363
  * Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).
1323
1364
  */
1324
1365
  reset(start, end) {
1325
- start = start + this.offset;
1326
- end = end + this.offset;
1327
- if (this.original.length !== 0) {
1328
- if (start < 0) start = Math.max(0, start + this.original.length);
1329
- if (end < 0) end = Math.max(0, end + this.original.length);
1330
- }
1366
+ start = this._resolveIndex(start);
1367
+ end = this._resolveIndex(end);
1331
1368
  if (start === end) return this;
1332
1369
  if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}–${end} is out of bounds`);
1333
1370
  if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
@@ -1336,10 +1373,13 @@ var MagicString = class MagicString {
1336
1373
  let chunk = this.byStart.get(start);
1337
1374
  while (chunk) {
1338
1375
  chunk.reset();
1339
- chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
1376
+ chunk = end > chunk.end ? this.byStart.get(chunk.end) : void 0;
1340
1377
  }
1341
1378
  return this;
1342
1379
  }
1380
+ /**
1381
+ * Returns the last character of the generated string, or `''` if it is empty.
1382
+ */
1343
1383
  lastChar() {
1344
1384
  if (this.outro.length) return this.outro[this.outro.length - 1];
1345
1385
  let chunk = this.lastChunk;
@@ -1352,49 +1392,57 @@ var MagicString = class MagicString {
1352
1392
  if (this.intro.length) return this.intro[this.intro.length - 1];
1353
1393
  return "";
1354
1394
  }
1395
+ /**
1396
+ * Returns everything after the last newline in the generated string, or the whole string if there is no newline.
1397
+ */
1355
1398
  lastLine() {
1356
1399
  let lineIndex = this.outro.lastIndexOf(n);
1357
- if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
1400
+ if (lineIndex !== -1) return this.outro.slice(lineIndex + 1);
1358
1401
  let lineStr = this.outro;
1359
1402
  let chunk = this.lastChunk;
1360
1403
  while (chunk) {
1361
1404
  if (chunk.outro.length > 0) {
1362
1405
  lineIndex = chunk.outro.lastIndexOf(n);
1363
- if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
1406
+ if (lineIndex !== -1) return chunk.outro.slice(lineIndex + 1) + lineStr;
1364
1407
  lineStr = chunk.outro + lineStr;
1365
1408
  }
1366
1409
  if (chunk.content.length > 0) {
1367
1410
  lineIndex = chunk.content.lastIndexOf(n);
1368
- if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
1411
+ if (lineIndex !== -1) return chunk.content.slice(lineIndex + 1) + lineStr;
1369
1412
  lineStr = chunk.content + lineStr;
1370
1413
  }
1371
1414
  if (chunk.intro.length > 0) {
1372
1415
  lineIndex = chunk.intro.lastIndexOf(n);
1373
- if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
1416
+ if (lineIndex !== -1) return chunk.intro.slice(lineIndex + 1) + lineStr;
1374
1417
  lineStr = chunk.intro + lineStr;
1375
1418
  }
1376
1419
  chunk = chunk.previous;
1377
1420
  }
1378
1421
  lineIndex = this.intro.lastIndexOf(n);
1379
- if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
1422
+ if (lineIndex !== -1) return this.intro.slice(lineIndex + 1) + lineStr;
1380
1423
  return this.intro + lineStr;
1381
1424
  }
1382
1425
  /**
1383
- * Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
1384
- * Throws error if the indices are for characters that were already removed.
1426
+ * Returns the content of the generated string that corresponds to the slice
1427
+ * between `start` and `end` of the original string, walking the generated
1428
+ * string from the chunk holding `start` to the chunk holding `end`. Throws
1429
+ * if either index falls inside replaced content. After `move()`, a range
1430
+ * whose end now comes before its start in the output yields `''`.
1385
1431
  */
1386
1432
  slice(start = 0, end = this.original.length - this.offset) {
1387
- start = start + this.offset;
1388
- end = end + this.offset;
1389
- if (this.original.length !== 0) {
1390
- if (start < 0) start = Math.max(0, start + this.original.length);
1391
- if (end < 0) end = Math.max(0, end + this.original.length);
1392
- }
1433
+ start = this._resolveIndex(start);
1434
+ end = this._resolveIndex(end);
1393
1435
  let result = "";
1394
- let chunk = this.firstChunk;
1395
- while (chunk && (chunk.start > start || chunk.end <= start)) {
1396
- if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
1397
- chunk = chunk.next;
1436
+ let chunk;
1437
+ if (this.hasMovedChunks) {
1438
+ chunk = this.firstChunk;
1439
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
1440
+ if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
1441
+ chunk = chunk.next;
1442
+ }
1443
+ } else {
1444
+ if (start >= end) return result;
1445
+ chunk = this._chunkAt(start);
1398
1446
  }
1399
1447
  if (chunk && chunk.edited && chunk.start !== start) throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`);
1400
1448
  const startChunk = chunk;
@@ -1417,10 +1465,39 @@ var MagicString = class MagicString {
1417
1465
  */
1418
1466
  snip(start, end) {
1419
1467
  const clone = this.clone();
1420
- clone.remove(0, start);
1421
- clone.remove(end, clone.original.length);
1468
+ clone.remove(-clone.offset, start);
1469
+ clone.remove(end, clone.original.length - clone.offset);
1422
1470
  return clone;
1423
1471
  }
1472
+ /**
1473
+ * Applies `offset`, resolves a negative index from the end of `original` as
1474
+ * `update`/`remove` always have, and rejects anything that is not an integer.
1475
+ * Bounds are checked by the caller, because the methods report them differently.
1476
+ *
1477
+ * @internal
1478
+ */
1479
+ _resolveIndex(index) {
1480
+ const resolved = index + this.offset;
1481
+ if (!Number.isInteger(resolved)) throw new MagicStringError(`index must be an integer, got ${index}`);
1482
+ if (resolved < 0 && this.original.length !== 0) return Math.max(0, resolved + this.original.length);
1483
+ return resolved;
1484
+ }
1485
+ /**
1486
+ * The chunk whose original range holds `index` (`chunk.start <= index < chunk.end`),
1487
+ * or `undefined` when `index` is at or past the end. Searches from the last
1488
+ * visited chunk so that scans over nearby indices stay cheap.
1489
+ *
1490
+ * @internal
1491
+ */
1492
+ _chunkAt(index) {
1493
+ let chunk = this.byStart.get(index);
1494
+ if (chunk) return chunk;
1495
+ chunk = this.lastSearchedChunk;
1496
+ const searchForward = index >= chunk.end;
1497
+ while (chunk && !chunk.contains(index)) chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
1498
+ if (chunk) this.lastSearchedChunk = chunk;
1499
+ return chunk;
1500
+ }
1424
1501
  /** @internal */
1425
1502
  _split(index) {
1426
1503
  if (this.byStart.get(index) || this.byEnd.get(index)) return;
@@ -1473,6 +1550,11 @@ var MagicString = class MagicString {
1473
1550
  if (this.outro.length && this.outro.trim()) return false;
1474
1551
  return true;
1475
1552
  }
1553
+ /**
1554
+ * Returns the length of the generated string, counting only content attached to the original characters.
1555
+ * Content outside the string body (`s.prepend()`/`s.append()`, and inserts before the first or after the last
1556
+ * character) is not counted, so this can differ from `s.toString().length`.
1557
+ */
1476
1558
  length() {
1477
1559
  let chunk = this.firstChunk;
1478
1560
  let length = 0;
@@ -1504,10 +1586,11 @@ var MagicString = class MagicString {
1504
1586
  const end = chunk.end;
1505
1587
  const aborted = chunk.trimEnd(rx);
1506
1588
  if (chunk.end !== end) {
1507
- if (this.lastChunk === chunk) this.lastChunk = chunk.next;
1589
+ const next = chunk.next;
1590
+ if (this.lastChunk === chunk) this.lastChunk = next;
1508
1591
  this.byEnd.set(chunk.end, chunk);
1509
- this.byStart.set(chunk.next.start, chunk.next);
1510
- this.byEnd.set(chunk.next.end, chunk.next);
1592
+ this.byStart.set(next.start, next);
1593
+ this.byEnd.set(next.end, next);
1511
1594
  }
1512
1595
  if (aborted) return true;
1513
1596
  chunk = chunk.previous;
@@ -1532,10 +1615,11 @@ var MagicString = class MagicString {
1532
1615
  const end = chunk.end;
1533
1616
  const aborted = chunk.trimStart(rx);
1534
1617
  if (chunk.end !== end) {
1535
- if (chunk === this.lastChunk) this.lastChunk = chunk.next;
1618
+ const next = chunk.next;
1619
+ if (chunk === this.lastChunk) this.lastChunk = next;
1536
1620
  this.byEnd.set(chunk.end, chunk);
1537
- this.byStart.set(chunk.next.start, chunk.next);
1538
- this.byEnd.set(chunk.next.end, chunk.next);
1621
+ this.byStart.set(next.start, next);
1622
+ this.byEnd.set(next.end, next);
1539
1623
  }
1540
1624
  if (aborted) return true;
1541
1625
  chunk = chunk.next;
@@ -1577,30 +1661,29 @@ var MagicString = class MagicString {
1577
1661
  * @internal
1578
1662
  */
1579
1663
  _hasRemovedContent(start, end) {
1580
- let chunk = this.byStart.get(start);
1581
- if (!chunk) {
1582
- chunk = this.lastSearchedChunk;
1583
- const searchForward = start >= chunk.end;
1584
- while (chunk && !chunk.contains(start)) chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
1585
- if (chunk) this.lastSearchedChunk = chunk;
1664
+ const atBoundary = this.byStart.get(start);
1665
+ if (atBoundary && start === end) {
1666
+ const previous = this.byEnd.get(start);
1667
+ return !!previous && previous.content === "" && atBoundary.content === "";
1586
1668
  }
1669
+ let chunk = this._chunkAt(start);
1587
1670
  while (chunk && chunk.start < end) {
1588
- if (chunk.content === "") return true;
1671
+ if (chunk.content === "" && (chunk.replacedFrom < start || this.byStart.get(chunk.replacedFrom).replacesUntil < chunk.end)) return true;
1589
1672
  chunk = this.byStart.get(chunk.end);
1590
1673
  }
1591
1674
  return false;
1592
1675
  }
1593
1676
  /** @internal */
1594
1677
  _replaceRegexp(searchValue, replacement) {
1595
- function getReplacement(match, str) {
1596
- if (typeof replacement === "string") return expandReplacement(replacement, match[0], match.index, str, match.slice(1), match.groups);
1597
- else return match.groups === void 0 ? replacement(match[0], ...match.slice(1), match.index, str) : replacement(match[0], ...match.slice(1), match.index, str, match.groups);
1678
+ function getReplacement(match, index, str) {
1679
+ if (typeof replacement === "string") return expandReplacement(replacement, match[0], index, str, match.slice(1), match.groups);
1680
+ else return match.groups === void 0 ? replacement(match[0], ...match.slice(1), index, str) : replacement(match[0], ...match.slice(1), index, str, match.groups);
1598
1681
  }
1599
1682
  const replaceMatch = (match) => {
1600
1683
  /* v8 ignore next 2 -- `match.index` is always defined for matches from `matchAll` */
1601
1684
  if (match.index == null) return false;
1602
1685
  if (this._hasRemovedContent(match.index, match.index + match[0].length)) return false;
1603
- const replacement = getReplacement(match, this.original);
1686
+ const replacement = getReplacement(match, match.index, this.original);
1604
1687
  if (replacement === match[0]) return true;
1605
1688
  if (match[0].length === 0) this.appendRight(match.index - this.offset, replacement);
1606
1689
  else this.overwrite(match.index - this.offset, match.index + match[0].length - this.offset, replacement);
@@ -1682,7 +1765,7 @@ var Bundle$1 = class Bundle {
1682
1765
  this.separator = options.separator !== void 0 ? options.separator : "\n";
1683
1766
  this.sources = [];
1684
1767
  this.uniqueSources = [];
1685
- this.uniqueSourceIndexByFilename = {};
1768
+ this.uniqueSourceIndexByFilename = /* @__PURE__ */ new Map();
1686
1769
  }
1687
1770
  /**
1688
1771
  * Adds the specified source to the bundle, which can either be a `MagicString` object directly,
@@ -1701,30 +1784,31 @@ var Bundle$1 = class Bundle {
1701
1784
  separator: this.separator
1702
1785
  });
1703
1786
  if (!isObject(source) || !source.content) throw new MagicStringError("addSource() requires a `content` property that is a MagicString");
1704
- [
1705
- "filename",
1706
- "ignoreList",
1707
- "indentExclusionRanges",
1708
- "separator"
1709
- ].forEach((option) => {
1710
- if (!hasOwnProp.call(source, option)) source[option] = source.content[option];
1711
- });
1712
- if (source.separator === void 0) source.separator = this.separator;
1713
- if (source.filename) {
1714
- if (!hasOwnProp.call(this.uniqueSourceIndexByFilename, source.filename)) {
1715
- this.uniqueSourceIndexByFilename[source.filename] = this.uniqueSources.length;
1787
+ const content = source.content;
1788
+ const record = {
1789
+ content,
1790
+ filename: hasOwnProp.call(source, "filename") ? source.filename : content.filename,
1791
+ ignoreList: hasOwnProp.call(source, "ignoreList") ? source.ignoreList : content.ignoreList,
1792
+ indentExclusionRanges: hasOwnProp.call(source, "indentExclusionRanges") ? source.indentExclusionRanges : content.indentExclusionRanges,
1793
+ separator: source.separator ?? this.separator
1794
+ };
1795
+ if (record.filename) {
1796
+ const index = this.uniqueSourceIndexByFilename.get(record.filename);
1797
+ if (index === void 0) {
1798
+ this.uniqueSourceIndexByFilename.set(record.filename, this.uniqueSources.length);
1716
1799
  this.uniqueSources.push({
1717
- filename: source.filename,
1718
- content: source.content.original
1800
+ filename: record.filename,
1801
+ content: content.original
1719
1802
  });
1720
- } else {
1721
- const uniqueSource = this.uniqueSources[this.uniqueSourceIndexByFilename[source.filename]];
1722
- if (source.content.original !== uniqueSource.content) throw new MagicStringError(`duplicate filename "${source.filename}" with different content, use unique filenames`);
1723
- }
1803
+ } else if (content.original !== this.uniqueSources[index].content) throw new MagicStringError(`duplicate filename "${record.filename}" with different content, use unique filenames`);
1724
1804
  }
1725
- this.sources.push(source);
1805
+ this.sources.push(record);
1726
1806
  return this;
1727
1807
  }
1808
+ /**
1809
+ * Appends `str` as a new source without a filename. Only `options.separator` is read, and it defaults to `''`
1810
+ * rather than to the bundle separator.
1811
+ */
1728
1812
  append(str, options) {
1729
1813
  this.addSource({
1730
1814
  content: new MagicString(str),
@@ -1732,6 +1816,10 @@ var Bundle$1 = class Bundle {
1732
1816
  });
1733
1817
  return this;
1734
1818
  }
1819
+ /**
1820
+ * Returns an independent copy of the bundle: every source's `MagicString` is cloned, so later edits to either
1821
+ * bundle do not affect the other.
1822
+ */
1735
1823
  clone() {
1736
1824
  const bundle = new Bundle({
1737
1825
  intro: this.intro,
@@ -1767,12 +1855,12 @@ var Bundle$1 = class Bundle {
1767
1855
  const byStart = /* @__PURE__ */ new Map();
1768
1856
  const byEnd = /* @__PURE__ */ new Map();
1769
1857
  let hasMovedChunks = false;
1770
- this.sources.forEach((source, i) => {
1858
+ for (const [i, source] of this.sources.entries()) {
1771
1859
  const magicString = source.content;
1772
- const separator = i > 0 ? source.separator !== void 0 ? source.separator : this.separator : "";
1860
+ const separator = i > 0 ? source.separator : "";
1773
1861
  if (magicString.original.length === 0) {
1774
1862
  pending += separator + magicString.toString();
1775
- return;
1863
+ continue;
1776
1864
  }
1777
1865
  pending += separator + magicString.intro;
1778
1866
  let sourceFirst = null;
@@ -1782,6 +1870,8 @@ var Bundle$1 = class Bundle {
1782
1870
  const chunk = originalChunk.clone();
1783
1871
  chunk.start += offset;
1784
1872
  chunk.end += offset;
1873
+ if (chunk.replacesUntil !== -1) chunk.replacesUntil += offset;
1874
+ if (chunk.replacedFrom !== -1) chunk.replacedFrom += offset;
1785
1875
  chunk.previous = previous;
1786
1876
  chunk.next = null;
1787
1877
  if (previous) previous.next = chunk;
@@ -1800,18 +1890,14 @@ var Bundle$1 = class Bundle {
1800
1890
  sourceFirst.previous = last;
1801
1891
  } else first = sourceFirst;
1802
1892
  last = sourceLast;
1803
- for (let index = 0; index < magicString.original.length; index += 1) if (magicString.sourcemapLocations.has(index)) combined.sourcemapLocations.add(index + offset);
1804
- Object.keys(magicString.storedNames).forEach((name) => {
1805
- Object.defineProperty(combined.storedNames, name, {
1806
- writable: true,
1807
- value: true,
1808
- enumerable: true
1809
- });
1893
+ magicString.sourcemapLocations.forEach((index) => {
1894
+ if (index < magicString.original.length) combined.sourcemapLocations.add(index + offset);
1810
1895
  });
1896
+ magicString.storedNames.forEach((name) => combined.storedNames.add(name));
1811
1897
  if (magicString.hasMovedChunks) hasMovedChunks = true;
1812
1898
  offset += magicString.original.length;
1813
- });
1814
- if (!first) {
1899
+ }
1900
+ if (!first || !last) {
1815
1901
  combined.intro = pending;
1816
1902
  return combined;
1817
1903
  }
@@ -1824,6 +1910,10 @@ var Bundle$1 = class Bundle {
1824
1910
  combined.hasMovedChunks = hasMovedChunks;
1825
1911
  return combined;
1826
1912
  }
1913
+ /**
1914
+ * Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
1915
+ * Takes the same options as `generateMap`.
1916
+ */
1827
1917
  generateDecodedMap(options = {}) {
1828
1918
  const mappings = new Mappings(options.hires);
1829
1919
  const { names, x_google_ignoreList } = this._generateMappings(mappings);
@@ -1833,6 +1923,10 @@ var Bundle$1 = class Bundle {
1833
1923
  rangeMappings: mappings.rawRangeMappings
1834
1924
  };
1835
1925
  }
1926
+ /**
1927
+ * Generates a version 3 sourcemap covering every source that has a `filename`.
1928
+ * Options are as per `MagicString#generateMap`, except that `includeContent` can also be a per-source function.
1929
+ */
1836
1930
  generateMap(options = {}) {
1837
1931
  const encoder = new MappingsEncoder();
1838
1932
  const mappings = new Mappings(options.hires, encoder);
@@ -1845,32 +1939,16 @@ var Bundle$1 = class Bundle {
1845
1939
  }
1846
1940
  /** @internal */
1847
1941
  _generateMappings(mappings) {
1848
- const names = [];
1849
1942
  let x_google_ignoreList;
1850
- this.sources.forEach((source) => {
1851
- Object.keys(source.content.storedNames).forEach((name) => {
1852
- if (!names.includes(name)) names.push(name);
1853
- });
1854
- });
1943
+ const storedNames = /* @__PURE__ */ new Set();
1944
+ this.sources.forEach((source) => source.content.storedNames.forEach((name) => storedNames.add(name)));
1945
+ const names = [...storedNames];
1946
+ const nameIndex = new Map(names.map((name, i) => [name, i]));
1855
1947
  if (this.intro) mappings.advance(this.intro);
1856
1948
  this.sources.forEach((source, i) => {
1857
- if (i > 0)
1858
- /* v8 ignore next -- addSource always normalizes source.separator */
1859
- mappings.advance(source.separator !== void 0 ? source.separator : this.separator);
1860
- const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename[source.filename] : -1;
1861
- const magicString = source.content;
1862
- const locate = getLocator(magicString.original);
1863
- if (magicString.intro) mappings.advance(magicString.intro);
1864
- magicString.firstChunk.eachNext((chunk) => {
1865
- const loc = locate(chunk.start);
1866
- if (chunk.intro.length) mappings.advance(chunk.intro);
1867
- if (source.filename) {
1868
- if (chunk.edited) mappings.addEdit(sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf(chunk.original) : -1);
1869
- else mappings.addUneditedChunk(sourceIndex, chunk, magicString.original, loc, magicString.sourcemapLocations);
1870
- } else mappings.advance(chunk.content);
1871
- if (chunk.outro.length) mappings.advance(chunk.outro);
1872
- });
1873
- if (magicString.outro) mappings.advance(magicString.outro);
1949
+ if (i > 0) mappings.advance(source.separator);
1950
+ const sourceIndex = source.filename ? this.uniqueSourceIndexByFilename.get(source.filename) ?? -1 : -1;
1951
+ mappings.addMagicString(source.content, sourceIndex, nameIndex);
1874
1952
  if (source.ignoreList && sourceIndex !== -1) {
1875
1953
  if (x_google_ignoreList === void 0) x_google_ignoreList = [];
1876
1954
  x_google_ignoreList.push(sourceIndex);
@@ -1895,91 +1973,111 @@ var Bundle$1 = class Bundle {
1895
1973
  x_google_ignoreList
1896
1974
  };
1897
1975
  }
1976
+ /**
1977
+ * Returns the most common indent string among the sources, falling back to a tab.
1978
+ */
1898
1979
  getIndentString() {
1899
- const indentStringCounts = {};
1980
+ const indentStringCounts = /* @__PURE__ */ new Map();
1900
1981
  this.sources.forEach((source) => {
1901
1982
  const indentStr = source.content._getRawIndentString();
1902
- if (indentStr === null) return;
1903
- if (!indentStringCounts[indentStr]) indentStringCounts[indentStr] = 0;
1904
- indentStringCounts[indentStr] += 1;
1983
+ if (indentStr == null) return;
1984
+ indentStringCounts.set(indentStr, (indentStringCounts.get(indentStr) ?? 0) + 1);
1905
1985
  });
1906
- return Object.keys(indentStringCounts).sort((a, b) => {
1907
- return indentStringCounts[b] - indentStringCounts[a];
1908
- })[0] || " ";
1986
+ return [...indentStringCounts.entries()].sort(([, a], [, b]) => b - a)[0]?.[0] ?? " ";
1909
1987
  }
1988
+ /**
1989
+ * Prefixes each line of every source and of the intro with `indentStr`, guessed with `getIndentString()`
1990
+ * when not supplied. Each source's `indentExclusionRanges` are left unindented.
1991
+ */
1910
1992
  indent(indentStr) {
1911
1993
  if (!arguments.length) indentStr = this.getIndentString();
1912
1994
  if (indentStr === "") return this;
1913
1995
  let trailingNewline = !this.intro || this.intro.slice(-1) === "\n";
1914
1996
  this.sources.forEach((source, i) => {
1915
- /* v8 ignore next -- addSource always normalizes source.separator */
1916
- const separator = source.separator !== void 0 ? source.separator : this.separator;
1917
- const indentStart = trailingNewline || i > 0 && /\r?\n$/.test(separator);
1997
+ const indentStart = trailingNewline || i > 0 && /\r?\n$/.test(source.separator);
1918
1998
  source.content.indent(indentStr, {
1919
1999
  exclude: source.indentExclusionRanges,
1920
2000
  indentStart
1921
2001
  });
1922
2002
  trailingNewline = source.content.lastChar() === "\n";
1923
2003
  });
1924
- if (this.intro) this.intro = indentStr + this.intro.replace(/^[^\n]/gm, (match, index) => {
2004
+ if (this.intro) this.intro = indentStr + this.intro.replace(/^[^\r\n]/gm, (match, index) => {
1925
2005
  return index > 0 ? indentStr + match : match;
1926
2006
  });
1927
2007
  return this;
1928
2008
  }
2009
+ /**
2010
+ * Prepends `str` to the bundle's intro.
2011
+ */
1929
2012
  prepend(str) {
1930
2013
  this.intro = str + this.intro;
1931
2014
  return this;
1932
2015
  }
2016
+ /**
2017
+ * Returns the intro followed by every source's generated string, joined by their separators.
2018
+ */
1933
2019
  toString() {
1934
2020
  const body = this.sources.map((source, i) => {
1935
- /* v8 ignore next -- addSource always normalizes source.separator */
1936
- const separator = source.separator !== void 0 ? source.separator : this.separator;
1937
- return (i > 0 ? separator : "") + source.content.toString();
2021
+ return (i > 0 ? source.separator : "") + source.content.toString();
1938
2022
  }).join("");
1939
2023
  return this.intro + body;
1940
2024
  }
2025
+ /**
2026
+ * Returns true if the bundle's output is empty (disregarding white space).
2027
+ */
1941
2028
  isEmpty() {
1942
2029
  if (this.intro.length && this.intro.trim()) return false;
1943
2030
  if (this.sources.some((source, i) => {
1944
- /* v8 ignore next -- addSource always normalizes source.separator */
1945
- const separator = source.separator !== void 0 ? source.separator : this.separator;
1946
- return i > 0 && separator.trim() !== "" || !source.content.isEmpty();
2031
+ return i > 0 && source.separator.trim() !== "" || !source.content.isEmpty();
1947
2032
  })) return false;
1948
2033
  return true;
1949
2034
  }
2035
+ /**
2036
+ * Returns the length of the bundle's output; unlike `MagicString#length`, this equals `bundle.toString().length`.
2037
+ */
1950
2038
  length() {
1951
2039
  return this.sources.reduce((length, source, i) => {
1952
- /* v8 ignore next -- addSource always normalizes source.separator */
1953
- const separator = source.separator !== void 0 ? source.separator : this.separator;
1954
- return length + (i > 0 ? separator.length : 0) + source.content.toString().length;
2040
+ return length + (i > 0 ? source.separator.length : 0) + source.content.length() + source.content.intro.length + source.content.outro.length;
1955
2041
  }, this.intro.length);
1956
2042
  }
2043
+ /**
2044
+ * Removes empty lines from the start and end.
2045
+ */
1957
2046
  trimLines() {
1958
2047
  return this.trim("[\\r\\n]");
1959
2048
  }
2049
+ /**
2050
+ * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
2051
+ */
1960
2052
  trim(charType) {
1961
2053
  return this.trimStart(charType).trimEnd(charType);
1962
2054
  }
2055
+ /**
2056
+ * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
2057
+ */
1963
2058
  trimStart(charType) {
1964
2059
  const rx = new RegExp(`^${charType || "\\s"}+`);
1965
2060
  this.intro = this.intro.replace(rx, "");
1966
2061
  if (!this.intro) for (let i = 0; i < this.sources.length; i += 1) {
1967
2062
  const source = this.sources[i];
1968
2063
  if (i > 0) {
1969
- source.separator = (source.separator !== void 0 ? source.separator : this.separator).replace(rx, "");
2064
+ source.separator = source.separator.replace(rx, "");
1970
2065
  if (source.separator) break;
1971
2066
  }
1972
2067
  if (source.content.trimStartAborted(charType)) break;
1973
2068
  }
1974
2069
  return this;
1975
2070
  }
2071
+ /**
2072
+ * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
2073
+ */
1976
2074
  trimEnd(charType) {
1977
2075
  const rx = new RegExp(`${charType || "\\s"}+$`);
1978
2076
  for (let i = this.sources.length - 1; i >= 0; i -= 1) {
1979
2077
  const source = this.sources[i];
1980
2078
  if (source.content.trimEndAborted(charType)) return this;
1981
2079
  if (i > 0) {
1982
- source.separator = (source.separator !== void 0 ? source.separator : this.separator).replace(rx, "");
2080
+ source.separator = source.separator.replace(rx, "");
1983
2081
  if (source.separator) return this;
1984
2082
  }
1985
2083
  }
@@ -7548,12 +7646,19 @@ ExportDefaultDeclaration.prototype.includeNode = onlyIncludeSelfNoDeoptimize;
7548
7646
  ExportDefaultDeclaration.prototype.applyDeoptimizations = doNotDeoptimize;
7549
7647
 
7550
7648
  const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/;
7551
- const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g;
7552
- const backSlashRegEx = /\\/g;
7649
+ const escapeRegEx = /[\n\r'\\\u2028\u2029]/g;
7650
+ const escapeReplacements = {
7651
+ '\n': '\\n',
7652
+ '\r': '\\r',
7653
+ "'": "\\'",
7654
+ '\\': '\\\\',
7655
+ '\u2028': '\\u2028',
7656
+ '\u2029': '\\u2029'
7657
+ };
7553
7658
  function escapeId(id) {
7554
7659
  if (!needsEscapeRegEx.test(id))
7555
7660
  return id;
7556
- return id.replace(backSlashRegEx, '\\\\').replace(quoteNewlineRegEx, '\\$1');
7661
+ return id.replace(escapeRegEx, character => escapeReplacements[character]);
7557
7662
  }
7558
7663
 
7559
7664
  const INTEROP_DEFAULT_VARIABLE = '_interopDefault';
@@ -12319,17 +12424,29 @@ function getMatcherString$1(id, resolutionBase) {
12319
12424
  }
12320
12425
  const createFilter$1 = function createFilter(include, exclude, options) {
12321
12426
  const resolutionBase = options && options.resolve;
12322
- const getMatcher = (id) => id instanceof RegExp
12323
- ? id
12324
- : {
12427
+ const getMatcher = (id) => {
12428
+ if (id instanceof RegExp) {
12429
+ return id;
12430
+ }
12431
+ // Compiling a pattern costs far more than matching against it, so only recompile when the cwd it resolves against changes
12432
+ const compile = () => picomatch(getMatcherString$1(id, resolutionBase), { dot: true });
12433
+ // These patterns never resolve against the cwd, so there is no need to read or track it
12434
+ if (resolutionBase === false || isAbsolute(id) || id.startsWith('**')) {
12435
+ return { test: compile() };
12436
+ }
12437
+ let compiledCwd = process.cwd();
12438
+ let fn = compile();
12439
+ return {
12325
12440
  test: (what) => {
12326
- // this refactor is a tad overly verbose but makes for easy debugging
12327
- const pattern = getMatcherString$1(id, resolutionBase);
12328
- const fn = picomatch(pattern, { dot: true });
12329
- const result = fn(what);
12330
- return result;
12441
+ const cwd = process.cwd();
12442
+ if (cwd !== compiledCwd) {
12443
+ compiledCwd = cwd;
12444
+ fn = compile();
12445
+ }
12446
+ return fn(what);
12331
12447
  }
12332
12448
  };
12449
+ };
12333
12450
  const includeMatchers = ensureArray$1(include).map(getMatcher);
12334
12451
  const excludeMatchers = ensureArray$1(exclude).map(getMatcher);
12335
12452
  if (!includeMatchers.length && !excludeMatchers.length)