@timber-js/app 0.2.0-alpha.201 → 0.2.0-alpha.202

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.
@@ -50,8 +50,12 @@ export declare function hideCompilingOverlay(): void;
50
50
  *
51
51
  * The server receives 'timber:client-error' events, and echoes them
52
52
  * back as Vite '{ type: "error" }' payloads to trigger the overlay.
53
+ *
54
+ * Errors that fire before the Vite HMR WebSocket has connected are
55
+ * buffered and flushed once the connection is established, so
56
+ * `hot.send()` never throws `SendBeforeConnectError`. See TIM-1509.
53
57
  */
54
- export declare function setupClientErrorForwarding(hot: Pick<HotInterface, 'send'>): void;
58
+ export declare function setupClientErrorForwarding(hot: HotInterface): void;
55
59
  /**
56
60
  * Detect errors whose `digest` marks them as a framework control-flow signal
57
61
  * (RedirectSignal, DenySignal). These must never surface as dev overlays —
@@ -1 +1 @@
1
- {"version":3,"file":"browser-dev.d.ts","sourceRoot":"","sources":["../../src/client/browser-dev.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAMH,gDAAgD;AAChD,MAAM,WAAW,YAAY;IAC3B,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAAC;IAC1D,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1C;AAqDD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,GAAG,IAAI,CAwBxE;AAoED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAmB3C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAc3C;AAID;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,MAAM,CAAC,GAAG,IAAI,CAoChF;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAW9D"}
1
+ {"version":3,"file":"browser-dev.d.ts","sourceRoot":"","sources":["../../src/client/browser-dev.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAMH,gDAAgD;AAChD,MAAM,WAAW,YAAY;IAC3B,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,GAAG,IAAI,CAAC;IAC1D,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1C;AAqDD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,GAAG,IAAI,CAwBxE;AAoED;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAmB3C;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,IAAI,IAAI,CAc3C;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CAgElE;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAW9D"}
package/dist/index.js CHANGED
@@ -2715,11 +2715,11 @@ function renderEntryCss(entry, urlPrefix) {
2715
2715
  var comma = ",".charCodeAt(0);
2716
2716
  var semicolon = ";".charCodeAt(0);
2717
2717
  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2718
- var intToChar = /* @__PURE__ */ new Uint8Array(64);
2718
+ var intToChar$1 = /* @__PURE__ */ new Uint8Array(64);
2719
2719
  var charToInt = /* @__PURE__ */ new Uint8Array(128);
2720
2720
  for (let i = 0; i < chars.length; i++) {
2721
2721
  const c = chars.charCodeAt(i);
2722
- intToChar[i] = c;
2722
+ intToChar$1[i] = c;
2723
2723
  charToInt[c] = i;
2724
2724
  }
2725
2725
  function encodeInteger(builder, num) {
@@ -2727,7 +2727,7 @@ function encodeInteger(builder, num) {
2727
2727
  let clamped = num & 31;
2728
2728
  num >>>= 5;
2729
2729
  if (num > 0) clamped |= 32;
2730
- builder.write(intToChar[clamped]);
2730
+ builder.write(intToChar$1[clamped]);
2731
2731
  } while (num > 0);
2732
2732
  }
2733
2733
  function encodeSign(num) {
@@ -2806,7 +2806,7 @@ function encode(decoded) {
2806
2806
  return writer.flush();
2807
2807
  }
2808
2808
  //#endregion
2809
- //#region ../../node_modules/.pnpm/magic-string@1.3.1/node_modules/magic-string/dist/index.mjs
2809
+ //#region ../../node_modules/.pnpm/magic-string@1.4.1/node_modules/magic-string/dist/index.mjs
2810
2810
  var BitSet = class BitSet {
2811
2811
  constructor(arg) {
2812
2812
  this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
@@ -2978,7 +2978,7 @@ var SourceMap = class {
2978
2978
  this.sources = properties.sources;
2979
2979
  this.sourcesContent = properties.sourcesContent;
2980
2980
  this.names = properties.names;
2981
- this.mappings = encode(properties.mappings);
2981
+ this.mappings = typeof properties.mappings === "string" ? properties.mappings : encode(properties.mappings);
2982
2982
  if (typeof properties.x_google_ignoreList !== "undefined") this.x_google_ignoreList = properties.x_google_ignoreList;
2983
2983
  if (typeof properties.debugId !== "undefined") this.debugId = properties.debugId;
2984
2984
  if (typeof properties.rangeMappings !== "undefined") {
@@ -3052,12 +3052,100 @@ var toString = Object.prototype.toString;
3052
3052
  function isObject(thing) {
3053
3053
  return toString.call(thing) === "[object Object]";
3054
3054
  }
3055
+ var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3056
+ var intToChar = /* #__PURE__ */ (() => {
3057
+ const chars = /* @__PURE__ */ new Uint8Array(64);
3058
+ for (let i = 0; i < 64; i++) chars[i] = BASE64_CHARS.charCodeAt(i);
3059
+ return chars;
3060
+ })();
3061
+ var COMMA = 44;
3062
+ var SEMICOLON = 59;
3063
+ var BUFFER_SIZE = 16384;
3064
+ var FLUSH_THRESHOLD = 16348;
3065
+ var scratch = /* #__PURE__ */ new Uint8Array(BUFFER_SIZE);
3066
+ function writeVlq(pos, num) {
3067
+ num = num < 0 ? -num << 1 | 1 : num << 1;
3068
+ do {
3069
+ let clamped = num & 31;
3070
+ num >>>= 5;
3071
+ if (num > 0) clamped |= 32;
3072
+ scratch[pos++] = intToChar[clamped];
3073
+ } while (num > 0);
3074
+ return pos;
3075
+ }
3076
+ var decoder = /* #__PURE__ */ new TextDecoder();
3077
+ var MappingsEncoder = class {
3078
+ constructor() {
3079
+ this.out = "";
3080
+ this.pos = 0;
3081
+ this.lines = [];
3082
+ this.buffered = 0;
3083
+ this.needsComma = false;
3084
+ this.prevGenColumn = 0;
3085
+ this.prevSourceIndex = 0;
3086
+ this.prevSourceLine = 0;
3087
+ this.prevSourceColumn = 0;
3088
+ this.prevNameIndex = 0;
3089
+ }
3090
+ endLine(segments) {
3091
+ this.lines.push(segments);
3092
+ this.buffered += segments.length + 1;
3093
+ if (this.buffered >= 4096) this.drain(null);
3094
+ }
3095
+ segments(segments) {
3096
+ this.drain(segments);
3097
+ }
3098
+ finish(segments) {
3099
+ this.drain(segments);
3100
+ this.flush();
3101
+ return this.out;
3102
+ }
3103
+ drain(trailing) {
3104
+ const lines = this.lines;
3105
+ const lineCount = lines.length;
3106
+ for (let l = 0; l <= lineCount; l++) {
3107
+ const line = l < lineCount ? lines[l] : trailing;
3108
+ if (line === null) break;
3109
+ for (let i = 0; i < line.length; i++) {
3110
+ if (this.pos > FLUSH_THRESHOLD) this.flush();
3111
+ const segment = line[i];
3112
+ if (this.needsComma) scratch[this.pos++] = COMMA;
3113
+ this.needsComma = true;
3114
+ this.pos = writeVlq(this.pos, segment[0] - this.prevGenColumn);
3115
+ this.prevGenColumn = segment[0];
3116
+ this.pos = writeVlq(this.pos, segment[1] - this.prevSourceIndex);
3117
+ this.prevSourceIndex = segment[1];
3118
+ this.pos = writeVlq(this.pos, segment[2] - this.prevSourceLine);
3119
+ this.prevSourceLine = segment[2];
3120
+ this.pos = writeVlq(this.pos, segment[3] - this.prevSourceColumn);
3121
+ this.prevSourceColumn = segment[3];
3122
+ if (segment.length === 5) {
3123
+ this.pos = writeVlq(this.pos, segment[4] - this.prevNameIndex);
3124
+ this.prevNameIndex = segment[4];
3125
+ }
3126
+ }
3127
+ if (l < lineCount) {
3128
+ if (this.pos > FLUSH_THRESHOLD) this.flush();
3129
+ scratch[this.pos++] = SEMICOLON;
3130
+ this.needsComma = false;
3131
+ this.prevGenColumn = 0;
3132
+ }
3133
+ }
3134
+ lines.length = 0;
3135
+ this.buffered = 0;
3136
+ this.flush();
3137
+ }
3138
+ flush() {
3139
+ this.out += decoder.decode(scratch.subarray(0, this.pos));
3140
+ this.pos = 0;
3141
+ }
3142
+ };
3055
3143
  var NEWLINE_CHAR$1 = 10;
3056
3144
  function isWordCode(code) {
3057
3145
  return code >= 97 && code <= 122 || code >= 65 && code <= 90 || code >= 48 && code <= 57 || code === 95;
3058
3146
  }
3059
3147
  var Mappings = class {
3060
- constructor(hires) {
3148
+ constructor(hires, encoder = null) {
3061
3149
  this.hires = hires;
3062
3150
  this.generatedCodeLine = 0;
3063
3151
  this.generatedCodeColumn = 0;
@@ -3065,10 +3153,21 @@ var Mappings = class {
3065
3153
  this.rawSegments = this.raw[this.generatedCodeLine] = [];
3066
3154
  this.rawRangeMappings = [];
3067
3155
  this.rawRangeMappingsIndices = this.rawRangeMappings[this.generatedCodeLine] = [];
3068
- this.pending = null;
3156
+ this.encoder = encoder;
3157
+ }
3158
+ nextLine() {
3159
+ if (this.encoder === null) {
3160
+ this.generatedCodeLine += 1;
3161
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
3162
+ } else {
3163
+ this.encoder.endLine(this.rawSegments);
3164
+ this.rawSegments = [];
3165
+ this.generatedCodeLine += 1;
3166
+ }
3167
+ this.generatedCodeColumn = 0;
3168
+ this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
3069
3169
  }
3070
3170
  addEdit(sourceIndex, content, loc, nameIndex) {
3071
- /* v8 ignore else -- `pending` is never assigned a truthy value */
3072
3171
  if (content.length) {
3073
3172
  const contentLengthMinusOne = content.length - 1;
3074
3173
  let contentLineEnd = content.indexOf("\n", 0);
@@ -3082,10 +3181,7 @@ var Mappings = class {
3082
3181
  ];
3083
3182
  if (nameIndex >= 0) segment.push(nameIndex);
3084
3183
  this.rawSegments.push(segment);
3085
- this.generatedCodeLine += 1;
3086
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
3087
- this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
3088
- this.generatedCodeColumn = 0;
3184
+ this.nextLine();
3089
3185
  previousContentLineEnd = contentLineEnd;
3090
3186
  contentLineEnd = content.indexOf("\n", contentLineEnd + 1);
3091
3187
  }
@@ -3098,11 +3194,7 @@ var Mappings = class {
3098
3194
  if (nameIndex >= 0) segment.push(nameIndex);
3099
3195
  this.rawSegments.push(segment);
3100
3196
  this.advance(content.slice(previousContentLineEnd + 1));
3101
- } else if (this.pending) {
3102
- this.rawSegments.push(this.pending);
3103
- this.advance(content);
3104
3197
  }
3105
- this.pending = null;
3106
3198
  }
3107
3199
  addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
3108
3200
  const end = chunk.end;
@@ -3110,8 +3202,13 @@ var Mappings = class {
3110
3202
  if (this.hires) {
3111
3203
  const boundary = this.hires === "boundary";
3112
3204
  const experimentalRange = this.hires === "experimental-range";
3205
+ const encoder = experimentalRange ? null : this.encoder;
3113
3206
  let charInHiresBoundary = false;
3114
3207
  while (i < end) {
3208
+ if (encoder !== null && this.rawSegments.length >= 4096) {
3209
+ encoder.segments(this.rawSegments);
3210
+ this.rawSegments.length = 0;
3211
+ }
3115
3212
  if (experimentalRange && i + 1 >= end) this.rawSegments.push([
3116
3213
  this.generatedCodeColumn,
3117
3214
  sourceIndex,
@@ -3122,10 +3219,7 @@ var Mappings = class {
3122
3219
  if (code === NEWLINE_CHAR$1) {
3123
3220
  loc.line += 1;
3124
3221
  loc.column = 0;
3125
- this.generatedCodeLine += 1;
3126
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
3127
- this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
3128
- this.generatedCodeColumn = 0;
3222
+ this.nextLine();
3129
3223
  charInHiresBoundary = false;
3130
3224
  } else {
3131
3225
  if (boundary) {
@@ -3206,26 +3300,15 @@ var Mappings = class {
3206
3300
  if (newline === end) break;
3207
3301
  loc.line += 1;
3208
3302
  loc.column = 0;
3209
- this.generatedCodeLine += 1;
3210
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
3211
- this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
3212
- this.generatedCodeColumn = 0;
3303
+ this.nextLine();
3213
3304
  i = newline + 1;
3214
3305
  }
3215
3306
  }
3216
- this.pending = null;
3217
3307
  }
3218
3308
  advance(str) {
3219
3309
  if (!str) return;
3220
3310
  const lastNewline = str.lastIndexOf("\n");
3221
- if (lastNewline !== -1) {
3222
- for (let i = str.indexOf("\n"); i !== -1; i = str.indexOf("\n", i + 1)) {
3223
- this.generatedCodeLine++;
3224
- this.raw[this.generatedCodeLine] = this.rawSegments = [];
3225
- this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
3226
- }
3227
- this.generatedCodeColumn = 0;
3228
- }
3311
+ for (let i = str.indexOf("\n"); i !== -1; i = str.indexOf("\n", i + 1)) this.nextLine();
3229
3312
  this.generatedCodeColumn += str.length - lastNewline - 1;
3230
3313
  }
3231
3314
  };
@@ -3444,9 +3527,32 @@ var MagicString = class MagicString {
3444
3527
  */
3445
3528
  generateDecodedMap(options) {
3446
3529
  options = options || {};
3530
+ const mappings = new Mappings(options.hires);
3531
+ const names = this._generateMappings(mappings);
3532
+ return {
3533
+ ...this._mapProperties(options, names),
3534
+ mappings: mappings.raw,
3535
+ rangeMappings: mappings.rawRangeMappings
3536
+ };
3537
+ }
3538
+ /**
3539
+ * Generates a version 3 sourcemap.
3540
+ */
3541
+ generateMap(options) {
3542
+ options = options || {};
3543
+ const encoder = new MappingsEncoder();
3544
+ const mappings = new Mappings(options.hires, encoder);
3545
+ const names = this._generateMappings(mappings);
3546
+ return new SourceMap({
3547
+ ...this._mapProperties(options, names),
3548
+ mappings: encoder.finish(mappings.rawSegments),
3549
+ rangeMappings: mappings.rawRangeMappings
3550
+ });
3551
+ }
3552
+ /** @internal */
3553
+ _generateMappings(mappings) {
3447
3554
  const sourceIndex = 0;
3448
3555
  const names = Object.keys(this.storedNames);
3449
- const mappings = new Mappings(options.hires);
3450
3556
  const locate = getLocator(this.original);
3451
3557
  if (this.intro) mappings.advance(this.intro);
3452
3558
  this.firstChunk.eachNext((chunk) => {
@@ -3457,22 +3563,18 @@ var MagicString = class MagicString {
3457
3563
  if (chunk.outro.length) mappings.advance(chunk.outro);
3458
3564
  });
3459
3565
  if (this.outro) mappings.advance(this.outro);
3566
+ return names;
3567
+ }
3568
+ /** @internal */
3569
+ _mapProperties(options, names) {
3460
3570
  return {
3461
3571
  file: options.file ? options.file.split(/[/\\]/).pop() : void 0,
3462
3572
  sources: [options.source ? getRelativePath(options.file || "", options.source) : options.file || ""],
3463
3573
  sourcesContent: options.includeContent ? [this.original] : void 0,
3464
3574
  names,
3465
- mappings: mappings.raw,
3466
- x_google_ignoreList: this.ignoreList ? [sourceIndex] : void 0,
3467
- rangeMappings: mappings.rawRangeMappings
3575
+ x_google_ignoreList: this.ignoreList ? [0] : void 0
3468
3576
  };
3469
3577
  }
3470
- /**
3471
- * Generates a version 3 sourcemap.
3472
- */
3473
- generateMap(options) {
3474
- return new SourceMap(this.generateDecodedMap(options));
3475
- }
3476
3578
  /** @internal */
3477
3579
  _ensureindentStr() {
3478
3580
  if (this.indentStr === void 0) this.indentStr = guessIndent(this.original);
@@ -3585,8 +3687,14 @@ var MagicString = class MagicString {
3585
3687
  }
3586
3688
  /**
3587
3689
  * Moves the characters from `start` and `end` to `index`.
3690
+ *
3691
+ * `affinity` controls where the range is anchored at `index`. With the
3692
+ * default `'right'`, it is inserted before the content that starts at `index`;
3693
+ * with `'left'`, it is inserted after the content that ends at `index`. The
3694
+ * two differ only when other content has already been moved to that boundary,
3695
+ * mirroring the `appendLeft`/`appendRight` distinction.
3588
3696
  */
3589
- move(start, end, index) {
3697
+ move(start, end, index, affinity = "right") {
3590
3698
  start = start + this.offset;
3591
3699
  end = end + this.offset;
3592
3700
  index = index + this.offset;
@@ -3606,9 +3714,27 @@ var MagicString = class MagicString {
3606
3714
  }
3607
3715
  const oldLeft = first.previous;
3608
3716
  const oldRight = last.next;
3609
- const newRight = this.byStart.get(index);
3610
- if (!newRight && last === this.lastChunk) return this;
3611
- const newLeft = newRight ? newRight.previous : this.lastChunk;
3717
+ let newLeft;
3718
+ let newRight;
3719
+ if (affinity === "left") {
3720
+ newLeft = this.byEnd.get(index) ?? null;
3721
+ if (!newLeft) {
3722
+ if (first === this.firstChunk) return this;
3723
+ newRight = this.firstChunk;
3724
+ } else {
3725
+ if (newLeft.next === first) return this;
3726
+ newRight = newLeft.next;
3727
+ }
3728
+ } else {
3729
+ newRight = this.byStart.get(index) ?? null;
3730
+ if (!newRight) {
3731
+ if (last === this.lastChunk) return this;
3732
+ newLeft = this.lastChunk;
3733
+ } else {
3734
+ if (newRight.previous === last) return this;
3735
+ newLeft = newRight.previous;
3736
+ }
3737
+ }
3612
3738
  if (oldLeft) oldLeft.next = oldRight;
3613
3739
  if (oldRight) oldRight.previous = oldLeft;
3614
3740
  if (newLeft) newLeft.next = first;
@@ -3733,6 +3859,9 @@ var MagicString = class MagicString {
3733
3859
  }
3734
3860
  /**
3735
3861
  * Removes the characters from `start` to `end` (of the original string, **not** the generated string).
3862
+ * Content appended or prepended at positions strictly inside the range is removed with it, while
3863
+ * content attached at `start` or `end` is preserved — use `s.overwrite(start, end, '')` to remove
3864
+ * the range including its edge inserts.
3736
3865
  * Removing the same content twice, or making removals that partially overlap, will cause an error.
3737
3866
  */
3738
3867
  remove(start, end) {
@@ -3749,9 +3878,9 @@ var MagicString = class MagicString {
3749
3878
  this._split(end);
3750
3879
  let chunk = this.byStart.get(start);
3751
3880
  while (chunk) {
3752
- chunk.intro = "";
3753
- chunk.outro = "";
3754
- chunk.edit("");
3881
+ if (chunk.start > start) chunk.intro = "";
3882
+ if (chunk.end < end) chunk.outro = "";
3883
+ chunk.edit("", false, true);
3755
3884
  chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
3756
3885
  }
3757
3886
  return this;
@@ -3831,12 +3960,13 @@ var MagicString = class MagicString {
3831
3960
  let result = "";
3832
3961
  let chunk = this.firstChunk;
3833
3962
  while (chunk && (chunk.start > start || chunk.end <= start)) {
3834
- if (chunk.start < end && chunk.end >= end) return result;
3963
+ if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
3835
3964
  chunk = chunk.next;
3836
3965
  }
3837
3966
  if (chunk && chunk.edited && chunk.start !== start) throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`);
3838
3967
  const startChunk = chunk;
3839
3968
  while (chunk) {
3969
+ if (end === 0 && chunk.start === 0) break;
3840
3970
  if (chunk.intro && (startChunk !== chunk || chunk.start === start)) result += chunk.intro;
3841
3971
  const containsEnd = chunk.start < end && chunk.end >= end;
3842
3972
  if (containsEnd && chunk.edited && chunk.end !== end) throw new MagicStringError(`cannot use edited character ${end} as slice end anchor`);
@@ -3949,7 +4079,8 @@ var MagicString = class MagicString {
3949
4079
  if (aborted) return true;
3950
4080
  chunk = chunk.previous;
3951
4081
  } while (chunk);
3952
- return false;
4082
+ this.intro = this.intro.replace(rx, "");
4083
+ return this.intro.length > 0;
3953
4084
  }
3954
4085
  /**
3955
4086
  * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
@@ -3976,7 +4107,8 @@ var MagicString = class MagicString {
3976
4107
  if (aborted) return true;
3977
4108
  chunk = chunk.next;
3978
4109
  } while (chunk);
3979
- return false;
4110
+ this.outro = this.outro.replace(rx, "");
4111
+ return this.outro.length > 0;
3980
4112
  }
3981
4113
  /**
3982
4114
  * Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
@@ -4003,6 +4135,22 @@ var MagicString = class MagicString {
4003
4135
  }
4004
4136
  return outputIndex !== this.original.length;
4005
4137
  }
4138
+ /**
4139
+ * Whether the original range [start, end) has had any of its characters
4140
+ * removed. `replace`/`replaceAll` search `original`, so a match can land on
4141
+ * text that is no longer in the output - overwriting it would resurrect the
4142
+ * removed characters, so such matches are skipped instead.
4143
+ *
4144
+ * @internal
4145
+ */
4146
+ _hasRemovedContent(start, end) {
4147
+ let chunk = this.firstChunk;
4148
+ while (chunk) {
4149
+ if (chunk.content === "" && chunk.start < end && chunk.end > start) return true;
4150
+ chunk = chunk.next;
4151
+ }
4152
+ return false;
4153
+ }
4006
4154
  /** @internal */
4007
4155
  _replaceRegexp(searchValue, replacement) {
4008
4156
  function getReplacement(match, str) {
@@ -4011,32 +4159,42 @@ var MagicString = class MagicString {
4011
4159
  }
4012
4160
  const replaceMatch = (match) => {
4013
4161
  /* v8 ignore next 2 -- `match.index` is always defined for matches from `matchAll` */
4014
- if (match.index == null) return;
4162
+ if (match.index == null) return false;
4163
+ if (this._hasRemovedContent(match.index, match.index + match[0].length)) return false;
4015
4164
  const replacement = getReplacement(match, this.original);
4016
- if (replacement === match[0]) return;
4165
+ if (replacement === match[0]) return true;
4017
4166
  if (match[0].length === 0) this.appendRight(match.index, replacement);
4018
4167
  else this.overwrite(match.index, match.index + match[0].length, replacement);
4168
+ return true;
4019
4169
  };
4020
4170
  if (searchValue.global) {
4021
4171
  searchValue.lastIndex = 0;
4022
4172
  for (const match of this.original.matchAll(searchValue)) replaceMatch(match);
4023
4173
  } else {
4024
4174
  const match = this.original.match(searchValue);
4025
- if (match) replaceMatch(match);
4175
+ if (match && !replaceMatch(match)) {
4176
+ const global = new RegExp(searchValue.source, `${searchValue.flags}g`);
4177
+ for (const next of this.original.matchAll(global)) if (replaceMatch(next)) break;
4178
+ }
4026
4179
  }
4027
4180
  return this;
4028
4181
  }
4029
4182
  /** @internal */
4030
4183
  _replaceString(string, replacement) {
4031
4184
  const { original } = this;
4032
- const index = original.indexOf(string);
4033
- if (index !== -1) {
4185
+ let index = original.indexOf(string);
4186
+ while (index !== -1) {
4187
+ if (this._hasRemovedContent(index, index + string.length)) {
4188
+ index = original.indexOf(string, index + string.length);
4189
+ continue;
4190
+ }
4034
4191
  if (typeof replacement === "function") replacement = replacement(string, index, original);
4035
4192
  else replacement = expandReplacement(replacement, string, index, original, [], void 0);
4036
4193
  if (string !== replacement) {
4037
4194
  if (string.length === 0) this.appendRight(index, replacement);
4038
4195
  else this.overwrite(index, index + string.length, replacement);
4039
4196
  }
4197
+ break;
4040
4198
  }
4041
4199
  return this;
4042
4200
  }
@@ -4059,6 +4217,7 @@ var MagicString = class MagicString {
4059
4217
  return this;
4060
4218
  }
4061
4219
  for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
4220
+ if (this._hasRemovedContent(index, index + stringLength)) continue;
4062
4221
  const previous = original.slice(index, index + stringLength);
4063
4222
  const _replacement = typeof replacement === "function" ? replacement(previous, index, original) : expandReplacement(replacement, previous, index, original, [], void 0);
4064
4223
  if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);