vite 4.4.9 → 5.0.0-beta.1

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.
@@ -49,6 +49,8 @@ var UrlType;
49
49
  UrlType[UrlType["Absolute"] = 7] = "Absolute";
50
50
  })(UrlType || (UrlType = {}));
51
51
 
52
+ const comma = ','.charCodeAt(0);
53
+ const semicolon = ';'.charCodeAt(0);
52
54
  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
53
55
  const intToChar = new Uint8Array(64); // 64 possible chars.
54
56
  const charToInt = new Uint8Array(128); // z is 122 in ASCII
@@ -57,6 +59,83 @@ for (let i = 0; i < chars.length; i++) {
57
59
  intToChar[i] = c;
58
60
  charToInt[c] = i;
59
61
  }
62
+ // Provide a fallback for older environments.
63
+ const td = typeof TextDecoder !== 'undefined'
64
+ ? /* #__PURE__ */ new TextDecoder()
65
+ : typeof Buffer !== 'undefined'
66
+ ? {
67
+ decode(buf) {
68
+ const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
69
+ return out.toString();
70
+ },
71
+ }
72
+ : {
73
+ decode(buf) {
74
+ let out = '';
75
+ for (let i = 0; i < buf.length; i++) {
76
+ out += String.fromCharCode(buf[i]);
77
+ }
78
+ return out;
79
+ },
80
+ };
81
+ function encode(decoded) {
82
+ const state = new Int32Array(5);
83
+ const bufLength = 1024 * 16;
84
+ const subLength = bufLength - 36;
85
+ const buf = new Uint8Array(bufLength);
86
+ const sub = buf.subarray(0, subLength);
87
+ let pos = 0;
88
+ let out = '';
89
+ for (let i = 0; i < decoded.length; i++) {
90
+ const line = decoded[i];
91
+ if (i > 0) {
92
+ if (pos === bufLength) {
93
+ out += td.decode(buf);
94
+ pos = 0;
95
+ }
96
+ buf[pos++] = semicolon;
97
+ }
98
+ if (line.length === 0)
99
+ continue;
100
+ state[0] = 0;
101
+ for (let j = 0; j < line.length; j++) {
102
+ const segment = line[j];
103
+ // We can push up to 5 ints, each int can take at most 7 chars, and we
104
+ // may push a comma.
105
+ if (pos > subLength) {
106
+ out += td.decode(sub);
107
+ buf.copyWithin(0, subLength, pos);
108
+ pos -= subLength;
109
+ }
110
+ if (j > 0)
111
+ buf[pos++] = comma;
112
+ pos = encodeInteger(buf, pos, state, segment, 0); // genColumn
113
+ if (segment.length === 1)
114
+ continue;
115
+ pos = encodeInteger(buf, pos, state, segment, 1); // sourcesIndex
116
+ pos = encodeInteger(buf, pos, state, segment, 2); // sourceLine
117
+ pos = encodeInteger(buf, pos, state, segment, 3); // sourceColumn
118
+ if (segment.length === 4)
119
+ continue;
120
+ pos = encodeInteger(buf, pos, state, segment, 4); // namesIndex
121
+ }
122
+ }
123
+ return out + td.decode(buf.subarray(0, pos));
124
+ }
125
+ function encodeInteger(buf, pos, state, segment, j) {
126
+ const next = segment[j];
127
+ let num = next - state[j];
128
+ state[j] = next;
129
+ num = num < 0 ? (-num << 1) | 1 : num << 1;
130
+ do {
131
+ let clamped = num & 0b011111;
132
+ num >>>= 5;
133
+ if (num > 0)
134
+ clamped |= 0b100000;
135
+ buf[pos++] = intToChar[clamped];
136
+ } while (num > 0);
137
+ return pos;
138
+ }
60
139
 
61
140
  function getDefaultExportFromCjs (x) {
62
141
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -2875,7 +2954,7 @@ const scan = scan_1;
2875
2954
  const parse$1 = parse_1$1;
2876
2955
  const utils = utils$3;
2877
2956
  const constants = constants$2;
2878
- const isObject$1 = val => val && typeof val === 'object' && !Array.isArray(val);
2957
+ const isObject$2 = val => val && typeof val === 'object' && !Array.isArray(val);
2879
2958
 
2880
2959
  /**
2881
2960
  * Creates a matcher function from one or more glob patterns. The
@@ -2912,7 +2991,7 @@ const picomatch$1 = (glob, options, returnState = false) => {
2912
2991
  return arrayMatcher;
2913
2992
  }
2914
2993
 
2915
- const isState = isObject$1(glob) && glob.tokens && glob.input;
2994
+ const isState = isObject$2(glob) && glob.tokens && glob.input;
2916
2995
 
2917
2996
  if (glob === '' || (typeof glob !== 'string' && !isState)) {
2918
2997
  throw new TypeError('Expected pattern to be a non-empty string');
@@ -3344,6 +3423,12 @@ function fsPathFromId(id) {
3344
3423
  function fsPathFromUrl(url) {
3345
3424
  return fsPathFromId(cleanUrl(url));
3346
3425
  }
3426
+ function withTrailingSlash(path) {
3427
+ if (path[path.length - 1] !== '/') {
3428
+ return `${path}/`;
3429
+ }
3430
+ return path;
3431
+ }
3347
3432
  /**
3348
3433
  * Check if dir is a parent of file
3349
3434
  *
@@ -3354,9 +3439,7 @@ function fsPathFromUrl(url) {
3354
3439
  * @returns true if dir is a parent of file
3355
3440
  */
3356
3441
  function isParentDirectory(dir, file) {
3357
- if (dir[dir.length - 1] !== '/') {
3358
- dir = `${dir}/`;
3359
- }
3442
+ dir = withTrailingSlash(dir);
3360
3443
  return (file.startsWith(dir) ||
3361
3444
  (isCaseInsensitiveFS && file.toLowerCase().startsWith(dir.toLowerCase())));
3362
3445
  }
@@ -3377,7 +3460,12 @@ const postfixRE = /[?#].*$/s;
3377
3460
  function cleanUrl(url) {
3378
3461
  return url.replace(postfixRE, '');
3379
3462
  }
3380
- function isObject(value) {
3463
+ const trailingSeparatorRE = /[?&]$/;
3464
+ const timestampRE = /\bt=\d{13}&?\b/;
3465
+ function removeTimestampQuery(url) {
3466
+ return url.replace(timestampRE, '').replace(trailingSeparatorRE, '');
3467
+ }
3468
+ function isObject$1(value) {
3381
3469
  return Object.prototype.toString.call(value) === '[object Object]';
3382
3470
  }
3383
3471
  function tryStatSync(file) {
@@ -3451,7 +3539,7 @@ function mergeConfigRecursively(defaults, overrides, rootPath) {
3451
3539
  merged[key] = [...arraify(existing ?? []), ...arraify(value ?? [])];
3452
3540
  continue;
3453
3541
  }
3454
- if (isObject(existing) && isObject(value)) {
3542
+ if (isObject$1(existing) && isObject$1(value)) {
3455
3543
  merged[key] = mergeConfigRecursively(existing, value, rootPath ? `${rootPath}.${key}` : key);
3456
3544
  continue;
3457
3545
  }
@@ -3470,7 +3558,7 @@ function mergeAlias(a, b) {
3470
3558
  return b;
3471
3559
  if (!b)
3472
3560
  return a;
3473
- if (isObject(a) && isObject(b)) {
3561
+ if (isObject$1(a) && isObject$1(b)) {
3474
3562
  return { ...a, ...b };
3475
3563
  }
3476
3564
  // the order is flipped because the alias is resolved from top-down,
@@ -3518,6 +3606,7 @@ const isCSSRequest = (request) => CSS_LANGS_RE.test(request);
3518
3606
  // The cache needs to be reset on buildStart for watch mode to work correctly
3519
3607
  // Don't use this manualChunks strategy for ssr, lib mode, and 'umd' or 'iife'
3520
3608
  class SplitVendorChunkCache {
3609
+ cache;
3521
3610
  constructor() {
3522
3611
  this.cache = new Map();
3523
3612
  }
@@ -3641,7 +3730,7 @@ var Stats = fs$2.Stats;
3641
3730
  * @private
3642
3731
  */
3643
3732
 
3644
- var toString = Object.prototype.toString;
3733
+ var toString$1 = Object.prototype.toString;
3645
3734
 
3646
3735
  /**
3647
3736
  * Generate an entity tag.
@@ -3724,8 +3813,8 @@ function isstats (obj) {
3724
3813
 
3725
3814
  // quack quack
3726
3815
  return obj && typeof obj === 'object' &&
3727
- 'ctime' in obj && toString.call(obj.ctime) === '[object Date]' &&
3728
- 'mtime' in obj && toString.call(obj.mtime) === '[object Date]' &&
3816
+ 'ctime' in obj && toString$1.call(obj.ctime) === '[object Date]' &&
3817
+ 'mtime' in obj && toString$1.call(obj.mtime) === '[object Date]' &&
3729
3818
  'ino' in obj && typeof obj.ino === 'number' &&
3730
3819
  'size' in obj && typeof obj.size === 'number'
3731
3820
  }
@@ -3747,6 +3836,1173 @@ function stattag (stat) {
3747
3836
 
3748
3837
  var getEtag = /*@__PURE__*/getDefaultExportFromCjs(etag_1);
3749
3838
 
3839
+ class BitSet {
3840
+ constructor(arg) {
3841
+ this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
3842
+ }
3843
+
3844
+ add(n) {
3845
+ this.bits[n >> 5] |= 1 << (n & 31);
3846
+ }
3847
+
3848
+ has(n) {
3849
+ return !!(this.bits[n >> 5] & (1 << (n & 31)));
3850
+ }
3851
+ }
3852
+
3853
+ class Chunk {
3854
+ constructor(start, end, content) {
3855
+ this.start = start;
3856
+ this.end = end;
3857
+ this.original = content;
3858
+
3859
+ this.intro = '';
3860
+ this.outro = '';
3861
+
3862
+ this.content = content;
3863
+ this.storeName = false;
3864
+ this.edited = false;
3865
+
3866
+ {
3867
+ this.previous = null;
3868
+ this.next = null;
3869
+ }
3870
+ }
3871
+
3872
+ appendLeft(content) {
3873
+ this.outro += content;
3874
+ }
3875
+
3876
+ appendRight(content) {
3877
+ this.intro = this.intro + content;
3878
+ }
3879
+
3880
+ clone() {
3881
+ const chunk = new Chunk(this.start, this.end, this.original);
3882
+
3883
+ chunk.intro = this.intro;
3884
+ chunk.outro = this.outro;
3885
+ chunk.content = this.content;
3886
+ chunk.storeName = this.storeName;
3887
+ chunk.edited = this.edited;
3888
+
3889
+ return chunk;
3890
+ }
3891
+
3892
+ contains(index) {
3893
+ return this.start < index && index < this.end;
3894
+ }
3895
+
3896
+ eachNext(fn) {
3897
+ let chunk = this;
3898
+ while (chunk) {
3899
+ fn(chunk);
3900
+ chunk = chunk.next;
3901
+ }
3902
+ }
3903
+
3904
+ eachPrevious(fn) {
3905
+ let chunk = this;
3906
+ while (chunk) {
3907
+ fn(chunk);
3908
+ chunk = chunk.previous;
3909
+ }
3910
+ }
3911
+
3912
+ edit(content, storeName, contentOnly) {
3913
+ this.content = content;
3914
+ if (!contentOnly) {
3915
+ this.intro = '';
3916
+ this.outro = '';
3917
+ }
3918
+ this.storeName = storeName;
3919
+
3920
+ this.edited = true;
3921
+
3922
+ return this;
3923
+ }
3924
+
3925
+ prependLeft(content) {
3926
+ this.outro = content + this.outro;
3927
+ }
3928
+
3929
+ prependRight(content) {
3930
+ this.intro = content + this.intro;
3931
+ }
3932
+
3933
+ split(index) {
3934
+ const sliceIndex = index - this.start;
3935
+
3936
+ const originalBefore = this.original.slice(0, sliceIndex);
3937
+ const originalAfter = this.original.slice(sliceIndex);
3938
+
3939
+ this.original = originalBefore;
3940
+
3941
+ const newChunk = new Chunk(index, this.end, originalAfter);
3942
+ newChunk.outro = this.outro;
3943
+ this.outro = '';
3944
+
3945
+ this.end = index;
3946
+
3947
+ if (this.edited) {
3948
+ // TODO is this block necessary?...
3949
+ newChunk.edit('', false);
3950
+ this.content = '';
3951
+ } else {
3952
+ this.content = originalBefore;
3953
+ }
3954
+
3955
+ newChunk.next = this.next;
3956
+ if (newChunk.next) newChunk.next.previous = newChunk;
3957
+ newChunk.previous = this;
3958
+ this.next = newChunk;
3959
+
3960
+ return newChunk;
3961
+ }
3962
+
3963
+ toString() {
3964
+ return this.intro + this.content + this.outro;
3965
+ }
3966
+
3967
+ trimEnd(rx) {
3968
+ this.outro = this.outro.replace(rx, '');
3969
+ if (this.outro.length) return true;
3970
+
3971
+ const trimmed = this.content.replace(rx, '');
3972
+
3973
+ if (trimmed.length) {
3974
+ if (trimmed !== this.content) {
3975
+ this.split(this.start + trimmed.length).edit('', undefined, true);
3976
+ }
3977
+ return true;
3978
+ } else {
3979
+ this.edit('', undefined, true);
3980
+
3981
+ this.intro = this.intro.replace(rx, '');
3982
+ if (this.intro.length) return true;
3983
+ }
3984
+ }
3985
+
3986
+ trimStart(rx) {
3987
+ this.intro = this.intro.replace(rx, '');
3988
+ if (this.intro.length) return true;
3989
+
3990
+ const trimmed = this.content.replace(rx, '');
3991
+
3992
+ if (trimmed.length) {
3993
+ if (trimmed !== this.content) {
3994
+ this.split(this.end - trimmed.length);
3995
+ this.edit('', undefined, true);
3996
+ }
3997
+ return true;
3998
+ } else {
3999
+ this.edit('', undefined, true);
4000
+
4001
+ this.outro = this.outro.replace(rx, '');
4002
+ if (this.outro.length) return true;
4003
+ }
4004
+ }
4005
+ }
4006
+
4007
+ function getBtoa() {
4008
+ if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
4009
+ return (str) => window.btoa(unescape(encodeURIComponent(str)));
4010
+ } else if (typeof Buffer === 'function') {
4011
+ return (str) => Buffer.from(str, 'utf-8').toString('base64');
4012
+ } else {
4013
+ return () => {
4014
+ throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
4015
+ };
4016
+ }
4017
+ }
4018
+
4019
+ const btoa = /*#__PURE__*/ getBtoa();
4020
+
4021
+ class SourceMap {
4022
+ constructor(properties) {
4023
+ this.version = 3;
4024
+ this.file = properties.file;
4025
+ this.sources = properties.sources;
4026
+ this.sourcesContent = properties.sourcesContent;
4027
+ this.names = properties.names;
4028
+ this.mappings = encode(properties.mappings);
4029
+ if (typeof properties.x_google_ignoreList !== 'undefined') {
4030
+ this.x_google_ignoreList = properties.x_google_ignoreList;
4031
+ }
4032
+ }
4033
+
4034
+ toString() {
4035
+ return JSON.stringify(this);
4036
+ }
4037
+
4038
+ toUrl() {
4039
+ return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
4040
+ }
4041
+ }
4042
+
4043
+ function guessIndent(code) {
4044
+ const lines = code.split('\n');
4045
+
4046
+ const tabbed = lines.filter((line) => /^\t+/.test(line));
4047
+ const spaced = lines.filter((line) => /^ {2,}/.test(line));
4048
+
4049
+ if (tabbed.length === 0 && spaced.length === 0) {
4050
+ return null;
4051
+ }
4052
+
4053
+ // More lines tabbed than spaced? Assume tabs, and
4054
+ // default to tabs in the case of a tie (or nothing
4055
+ // to go on)
4056
+ if (tabbed.length >= spaced.length) {
4057
+ return '\t';
4058
+ }
4059
+
4060
+ // Otherwise, we need to guess the multiple
4061
+ const min = spaced.reduce((previous, current) => {
4062
+ const numSpaces = /^ +/.exec(current)[0].length;
4063
+ return Math.min(numSpaces, previous);
4064
+ }, Infinity);
4065
+
4066
+ return new Array(min + 1).join(' ');
4067
+ }
4068
+
4069
+ function getRelativePath(from, to) {
4070
+ const fromParts = from.split(/[/\\]/);
4071
+ const toParts = to.split(/[/\\]/);
4072
+
4073
+ fromParts.pop(); // get dirname
4074
+
4075
+ while (fromParts[0] === toParts[0]) {
4076
+ fromParts.shift();
4077
+ toParts.shift();
4078
+ }
4079
+
4080
+ if (fromParts.length) {
4081
+ let i = fromParts.length;
4082
+ while (i--) fromParts[i] = '..';
4083
+ }
4084
+
4085
+ return fromParts.concat(toParts).join('/');
4086
+ }
4087
+
4088
+ const toString = Object.prototype.toString;
4089
+
4090
+ function isObject(thing) {
4091
+ return toString.call(thing) === '[object Object]';
4092
+ }
4093
+
4094
+ function getLocator(source) {
4095
+ const originalLines = source.split('\n');
4096
+ const lineOffsets = [];
4097
+
4098
+ for (let i = 0, pos = 0; i < originalLines.length; i++) {
4099
+ lineOffsets.push(pos);
4100
+ pos += originalLines[i].length + 1;
4101
+ }
4102
+
4103
+ return function locate(index) {
4104
+ let i = 0;
4105
+ let j = lineOffsets.length;
4106
+ while (i < j) {
4107
+ const m = (i + j) >> 1;
4108
+ if (index < lineOffsets[m]) {
4109
+ j = m;
4110
+ } else {
4111
+ i = m + 1;
4112
+ }
4113
+ }
4114
+ const line = i - 1;
4115
+ const column = index - lineOffsets[line];
4116
+ return { line, column };
4117
+ };
4118
+ }
4119
+
4120
+ const wordRegex = /\w/;
4121
+
4122
+ class Mappings {
4123
+ constructor(hires) {
4124
+ this.hires = hires;
4125
+ this.generatedCodeLine = 0;
4126
+ this.generatedCodeColumn = 0;
4127
+ this.raw = [];
4128
+ this.rawSegments = this.raw[this.generatedCodeLine] = [];
4129
+ this.pending = null;
4130
+ }
4131
+
4132
+ addEdit(sourceIndex, content, loc, nameIndex) {
4133
+ if (content.length) {
4134
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
4135
+ if (nameIndex >= 0) {
4136
+ segment.push(nameIndex);
4137
+ }
4138
+ this.rawSegments.push(segment);
4139
+ } else if (this.pending) {
4140
+ this.rawSegments.push(this.pending);
4141
+ }
4142
+
4143
+ this.advance(content);
4144
+ this.pending = null;
4145
+ }
4146
+
4147
+ addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
4148
+ let originalCharIndex = chunk.start;
4149
+ let first = true;
4150
+ // when iterating each char, check if it's in a word boundary
4151
+ let charInHiresBoundary = false;
4152
+
4153
+ while (originalCharIndex < chunk.end) {
4154
+ if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
4155
+ const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
4156
+
4157
+ if (this.hires === 'boundary') {
4158
+ // in hires "boundary", group segments per word boundary than per char
4159
+ if (wordRegex.test(original[originalCharIndex])) {
4160
+ // for first char in the boundary found, start the boundary by pushing a segment
4161
+ if (!charInHiresBoundary) {
4162
+ this.rawSegments.push(segment);
4163
+ charInHiresBoundary = true;
4164
+ }
4165
+ } else {
4166
+ // for non-word char, end the boundary by pushing a segment
4167
+ this.rawSegments.push(segment);
4168
+ charInHiresBoundary = false;
4169
+ }
4170
+ } else {
4171
+ this.rawSegments.push(segment);
4172
+ }
4173
+ }
4174
+
4175
+ if (original[originalCharIndex] === '\n') {
4176
+ loc.line += 1;
4177
+ loc.column = 0;
4178
+ this.generatedCodeLine += 1;
4179
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
4180
+ this.generatedCodeColumn = 0;
4181
+ first = true;
4182
+ } else {
4183
+ loc.column += 1;
4184
+ this.generatedCodeColumn += 1;
4185
+ first = false;
4186
+ }
4187
+
4188
+ originalCharIndex += 1;
4189
+ }
4190
+
4191
+ this.pending = null;
4192
+ }
4193
+
4194
+ advance(str) {
4195
+ if (!str) return;
4196
+
4197
+ const lines = str.split('\n');
4198
+
4199
+ if (lines.length > 1) {
4200
+ for (let i = 0; i < lines.length - 1; i++) {
4201
+ this.generatedCodeLine++;
4202
+ this.raw[this.generatedCodeLine] = this.rawSegments = [];
4203
+ }
4204
+ this.generatedCodeColumn = 0;
4205
+ }
4206
+
4207
+ this.generatedCodeColumn += lines[lines.length - 1].length;
4208
+ }
4209
+ }
4210
+
4211
+ const n = '\n';
4212
+
4213
+ const warned = {
4214
+ insertLeft: false,
4215
+ insertRight: false,
4216
+ storeName: false,
4217
+ };
4218
+
4219
+ class MagicString {
4220
+ constructor(string, options = {}) {
4221
+ const chunk = new Chunk(0, string.length, string);
4222
+
4223
+ Object.defineProperties(this, {
4224
+ original: { writable: true, value: string },
4225
+ outro: { writable: true, value: '' },
4226
+ intro: { writable: true, value: '' },
4227
+ firstChunk: { writable: true, value: chunk },
4228
+ lastChunk: { writable: true, value: chunk },
4229
+ lastSearchedChunk: { writable: true, value: chunk },
4230
+ byStart: { writable: true, value: {} },
4231
+ byEnd: { writable: true, value: {} },
4232
+ filename: { writable: true, value: options.filename },
4233
+ indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
4234
+ sourcemapLocations: { writable: true, value: new BitSet() },
4235
+ storedNames: { writable: true, value: {} },
4236
+ indentStr: { writable: true, value: undefined },
4237
+ ignoreList: { writable: true, value: options.ignoreList },
4238
+ });
4239
+
4240
+ this.byStart[0] = chunk;
4241
+ this.byEnd[string.length] = chunk;
4242
+ }
4243
+
4244
+ addSourcemapLocation(char) {
4245
+ this.sourcemapLocations.add(char);
4246
+ }
4247
+
4248
+ append(content) {
4249
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
4250
+
4251
+ this.outro += content;
4252
+ return this;
4253
+ }
4254
+
4255
+ appendLeft(index, content) {
4256
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
4257
+
4258
+ this._split(index);
4259
+
4260
+ const chunk = this.byEnd[index];
4261
+
4262
+ if (chunk) {
4263
+ chunk.appendLeft(content);
4264
+ } else {
4265
+ this.intro += content;
4266
+ }
4267
+ return this;
4268
+ }
4269
+
4270
+ appendRight(index, content) {
4271
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
4272
+
4273
+ this._split(index);
4274
+
4275
+ const chunk = this.byStart[index];
4276
+
4277
+ if (chunk) {
4278
+ chunk.appendRight(content);
4279
+ } else {
4280
+ this.outro += content;
4281
+ }
4282
+ return this;
4283
+ }
4284
+
4285
+ clone() {
4286
+ const cloned = new MagicString(this.original, { filename: this.filename });
4287
+
4288
+ let originalChunk = this.firstChunk;
4289
+ let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
4290
+
4291
+ while (originalChunk) {
4292
+ cloned.byStart[clonedChunk.start] = clonedChunk;
4293
+ cloned.byEnd[clonedChunk.end] = clonedChunk;
4294
+
4295
+ const nextOriginalChunk = originalChunk.next;
4296
+ const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
4297
+
4298
+ if (nextClonedChunk) {
4299
+ clonedChunk.next = nextClonedChunk;
4300
+ nextClonedChunk.previous = clonedChunk;
4301
+
4302
+ clonedChunk = nextClonedChunk;
4303
+ }
4304
+
4305
+ originalChunk = nextOriginalChunk;
4306
+ }
4307
+
4308
+ cloned.lastChunk = clonedChunk;
4309
+
4310
+ if (this.indentExclusionRanges) {
4311
+ cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
4312
+ }
4313
+
4314
+ cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
4315
+
4316
+ cloned.intro = this.intro;
4317
+ cloned.outro = this.outro;
4318
+
4319
+ return cloned;
4320
+ }
4321
+
4322
+ generateDecodedMap(options) {
4323
+ options = options || {};
4324
+
4325
+ const sourceIndex = 0;
4326
+ const names = Object.keys(this.storedNames);
4327
+ const mappings = new Mappings(options.hires);
4328
+
4329
+ const locate = getLocator(this.original);
4330
+
4331
+ if (this.intro) {
4332
+ mappings.advance(this.intro);
4333
+ }
4334
+
4335
+ this.firstChunk.eachNext((chunk) => {
4336
+ const loc = locate(chunk.start);
4337
+
4338
+ if (chunk.intro.length) mappings.advance(chunk.intro);
4339
+
4340
+ if (chunk.edited) {
4341
+ mappings.addEdit(
4342
+ sourceIndex,
4343
+ chunk.content,
4344
+ loc,
4345
+ chunk.storeName ? names.indexOf(chunk.original) : -1,
4346
+ );
4347
+ } else {
4348
+ mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
4349
+ }
4350
+
4351
+ if (chunk.outro.length) mappings.advance(chunk.outro);
4352
+ });
4353
+
4354
+ return {
4355
+ file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
4356
+ sources: [
4357
+ options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
4358
+ ],
4359
+ sourcesContent: options.includeContent ? [this.original] : undefined,
4360
+ names,
4361
+ mappings: mappings.raw,
4362
+ x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
4363
+ };
4364
+ }
4365
+
4366
+ generateMap(options) {
4367
+ return new SourceMap(this.generateDecodedMap(options));
4368
+ }
4369
+
4370
+ _ensureindentStr() {
4371
+ if (this.indentStr === undefined) {
4372
+ this.indentStr = guessIndent(this.original);
4373
+ }
4374
+ }
4375
+
4376
+ _getRawIndentString() {
4377
+ this._ensureindentStr();
4378
+ return this.indentStr;
4379
+ }
4380
+
4381
+ getIndentString() {
4382
+ this._ensureindentStr();
4383
+ return this.indentStr === null ? '\t' : this.indentStr;
4384
+ }
4385
+
4386
+ indent(indentStr, options) {
4387
+ const pattern = /^[^\r\n]/gm;
4388
+
4389
+ if (isObject(indentStr)) {
4390
+ options = indentStr;
4391
+ indentStr = undefined;
4392
+ }
4393
+
4394
+ if (indentStr === undefined) {
4395
+ this._ensureindentStr();
4396
+ indentStr = this.indentStr || '\t';
4397
+ }
4398
+
4399
+ if (indentStr === '') return this; // noop
4400
+
4401
+ options = options || {};
4402
+
4403
+ // Process exclusion ranges
4404
+ const isExcluded = {};
4405
+
4406
+ if (options.exclude) {
4407
+ const exclusions =
4408
+ typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
4409
+ exclusions.forEach((exclusion) => {
4410
+ for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
4411
+ isExcluded[i] = true;
4412
+ }
4413
+ });
4414
+ }
4415
+
4416
+ let shouldIndentNextCharacter = options.indentStart !== false;
4417
+ const replacer = (match) => {
4418
+ if (shouldIndentNextCharacter) return `${indentStr}${match}`;
4419
+ shouldIndentNextCharacter = true;
4420
+ return match;
4421
+ };
4422
+
4423
+ this.intro = this.intro.replace(pattern, replacer);
4424
+
4425
+ let charIndex = 0;
4426
+ let chunk = this.firstChunk;
4427
+
4428
+ while (chunk) {
4429
+ const end = chunk.end;
4430
+
4431
+ if (chunk.edited) {
4432
+ if (!isExcluded[charIndex]) {
4433
+ chunk.content = chunk.content.replace(pattern, replacer);
4434
+
4435
+ if (chunk.content.length) {
4436
+ shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
4437
+ }
4438
+ }
4439
+ } else {
4440
+ charIndex = chunk.start;
4441
+
4442
+ while (charIndex < end) {
4443
+ if (!isExcluded[charIndex]) {
4444
+ const char = this.original[charIndex];
4445
+
4446
+ if (char === '\n') {
4447
+ shouldIndentNextCharacter = true;
4448
+ } else if (char !== '\r' && shouldIndentNextCharacter) {
4449
+ shouldIndentNextCharacter = false;
4450
+
4451
+ if (charIndex === chunk.start) {
4452
+ chunk.prependRight(indentStr);
4453
+ } else {
4454
+ this._splitChunk(chunk, charIndex);
4455
+ chunk = chunk.next;
4456
+ chunk.prependRight(indentStr);
4457
+ }
4458
+ }
4459
+ }
4460
+
4461
+ charIndex += 1;
4462
+ }
4463
+ }
4464
+
4465
+ charIndex = chunk.end;
4466
+ chunk = chunk.next;
4467
+ }
4468
+
4469
+ this.outro = this.outro.replace(pattern, replacer);
4470
+
4471
+ return this;
4472
+ }
4473
+
4474
+ insert() {
4475
+ throw new Error(
4476
+ 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
4477
+ );
4478
+ }
4479
+
4480
+ insertLeft(index, content) {
4481
+ if (!warned.insertLeft) {
4482
+ console.warn(
4483
+ 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
4484
+ ); // eslint-disable-line no-console
4485
+ warned.insertLeft = true;
4486
+ }
4487
+
4488
+ return this.appendLeft(index, content);
4489
+ }
4490
+
4491
+ insertRight(index, content) {
4492
+ if (!warned.insertRight) {
4493
+ console.warn(
4494
+ 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
4495
+ ); // eslint-disable-line no-console
4496
+ warned.insertRight = true;
4497
+ }
4498
+
4499
+ return this.prependRight(index, content);
4500
+ }
4501
+
4502
+ move(start, end, index) {
4503
+ if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
4504
+
4505
+ this._split(start);
4506
+ this._split(end);
4507
+ this._split(index);
4508
+
4509
+ const first = this.byStart[start];
4510
+ const last = this.byEnd[end];
4511
+
4512
+ const oldLeft = first.previous;
4513
+ const oldRight = last.next;
4514
+
4515
+ const newRight = this.byStart[index];
4516
+ if (!newRight && last === this.lastChunk) return this;
4517
+ const newLeft = newRight ? newRight.previous : this.lastChunk;
4518
+
4519
+ if (oldLeft) oldLeft.next = oldRight;
4520
+ if (oldRight) oldRight.previous = oldLeft;
4521
+
4522
+ if (newLeft) newLeft.next = first;
4523
+ if (newRight) newRight.previous = last;
4524
+
4525
+ if (!first.previous) this.firstChunk = last.next;
4526
+ if (!last.next) {
4527
+ this.lastChunk = first.previous;
4528
+ this.lastChunk.next = null;
4529
+ }
4530
+
4531
+ first.previous = newLeft;
4532
+ last.next = newRight || null;
4533
+
4534
+ if (!newLeft) this.firstChunk = first;
4535
+ if (!newRight) this.lastChunk = last;
4536
+ return this;
4537
+ }
4538
+
4539
+ overwrite(start, end, content, options) {
4540
+ options = options || {};
4541
+ return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
4542
+ }
4543
+
4544
+ update(start, end, content, options) {
4545
+ if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
4546
+
4547
+ while (start < 0) start += this.original.length;
4548
+ while (end < 0) end += this.original.length;
4549
+
4550
+ if (end > this.original.length) throw new Error('end is out of bounds');
4551
+ if (start === end)
4552
+ throw new Error(
4553
+ 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
4554
+ );
4555
+
4556
+ this._split(start);
4557
+ this._split(end);
4558
+
4559
+ if (options === true) {
4560
+ if (!warned.storeName) {
4561
+ console.warn(
4562
+ 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
4563
+ ); // eslint-disable-line no-console
4564
+ warned.storeName = true;
4565
+ }
4566
+
4567
+ options = { storeName: true };
4568
+ }
4569
+ const storeName = options !== undefined ? options.storeName : false;
4570
+ const overwrite = options !== undefined ? options.overwrite : false;
4571
+
4572
+ if (storeName) {
4573
+ const original = this.original.slice(start, end);
4574
+ Object.defineProperty(this.storedNames, original, {
4575
+ writable: true,
4576
+ value: true,
4577
+ enumerable: true,
4578
+ });
4579
+ }
4580
+
4581
+ const first = this.byStart[start];
4582
+ const last = this.byEnd[end];
4583
+
4584
+ if (first) {
4585
+ let chunk = first;
4586
+ while (chunk !== last) {
4587
+ if (chunk.next !== this.byStart[chunk.end]) {
4588
+ throw new Error('Cannot overwrite across a split point');
4589
+ }
4590
+ chunk = chunk.next;
4591
+ chunk.edit('', false);
4592
+ }
4593
+
4594
+ first.edit(content, storeName, !overwrite);
4595
+ } else {
4596
+ // must be inserting at the end
4597
+ const newChunk = new Chunk(start, end, '').edit(content, storeName);
4598
+
4599
+ // TODO last chunk in the array may not be the last chunk, if it's moved...
4600
+ last.next = newChunk;
4601
+ newChunk.previous = last;
4602
+ }
4603
+ return this;
4604
+ }
4605
+
4606
+ prepend(content) {
4607
+ if (typeof content !== 'string') throw new TypeError('outro content must be a string');
4608
+
4609
+ this.intro = content + this.intro;
4610
+ return this;
4611
+ }
4612
+
4613
+ prependLeft(index, content) {
4614
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
4615
+
4616
+ this._split(index);
4617
+
4618
+ const chunk = this.byEnd[index];
4619
+
4620
+ if (chunk) {
4621
+ chunk.prependLeft(content);
4622
+ } else {
4623
+ this.intro = content + this.intro;
4624
+ }
4625
+ return this;
4626
+ }
4627
+
4628
+ prependRight(index, content) {
4629
+ if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
4630
+
4631
+ this._split(index);
4632
+
4633
+ const chunk = this.byStart[index];
4634
+
4635
+ if (chunk) {
4636
+ chunk.prependRight(content);
4637
+ } else {
4638
+ this.outro = content + this.outro;
4639
+ }
4640
+ return this;
4641
+ }
4642
+
4643
+ remove(start, end) {
4644
+ while (start < 0) start += this.original.length;
4645
+ while (end < 0) end += this.original.length;
4646
+
4647
+ if (start === end) return this;
4648
+
4649
+ if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
4650
+ if (start > end) throw new Error('end must be greater than start');
4651
+
4652
+ this._split(start);
4653
+ this._split(end);
4654
+
4655
+ let chunk = this.byStart[start];
4656
+
4657
+ while (chunk) {
4658
+ chunk.intro = '';
4659
+ chunk.outro = '';
4660
+ chunk.edit('');
4661
+
4662
+ chunk = end > chunk.end ? this.byStart[chunk.end] : null;
4663
+ }
4664
+ return this;
4665
+ }
4666
+
4667
+ lastChar() {
4668
+ if (this.outro.length) return this.outro[this.outro.length - 1];
4669
+ let chunk = this.lastChunk;
4670
+ do {
4671
+ if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
4672
+ if (chunk.content.length) return chunk.content[chunk.content.length - 1];
4673
+ if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
4674
+ } while ((chunk = chunk.previous));
4675
+ if (this.intro.length) return this.intro[this.intro.length - 1];
4676
+ return '';
4677
+ }
4678
+
4679
+ lastLine() {
4680
+ let lineIndex = this.outro.lastIndexOf(n);
4681
+ if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
4682
+ let lineStr = this.outro;
4683
+ let chunk = this.lastChunk;
4684
+ do {
4685
+ if (chunk.outro.length > 0) {
4686
+ lineIndex = chunk.outro.lastIndexOf(n);
4687
+ if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
4688
+ lineStr = chunk.outro + lineStr;
4689
+ }
4690
+
4691
+ if (chunk.content.length > 0) {
4692
+ lineIndex = chunk.content.lastIndexOf(n);
4693
+ if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
4694
+ lineStr = chunk.content + lineStr;
4695
+ }
4696
+
4697
+ if (chunk.intro.length > 0) {
4698
+ lineIndex = chunk.intro.lastIndexOf(n);
4699
+ if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
4700
+ lineStr = chunk.intro + lineStr;
4701
+ }
4702
+ } while ((chunk = chunk.previous));
4703
+ lineIndex = this.intro.lastIndexOf(n);
4704
+ if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
4705
+ return this.intro + lineStr;
4706
+ }
4707
+
4708
+ slice(start = 0, end = this.original.length) {
4709
+ while (start < 0) start += this.original.length;
4710
+ while (end < 0) end += this.original.length;
4711
+
4712
+ let result = '';
4713
+
4714
+ // find start chunk
4715
+ let chunk = this.firstChunk;
4716
+ while (chunk && (chunk.start > start || chunk.end <= start)) {
4717
+ // found end chunk before start
4718
+ if (chunk.start < end && chunk.end >= end) {
4719
+ return result;
4720
+ }
4721
+
4722
+ chunk = chunk.next;
4723
+ }
4724
+
4725
+ if (chunk && chunk.edited && chunk.start !== start)
4726
+ throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
4727
+
4728
+ const startChunk = chunk;
4729
+ while (chunk) {
4730
+ if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
4731
+ result += chunk.intro;
4732
+ }
4733
+
4734
+ const containsEnd = chunk.start < end && chunk.end >= end;
4735
+ if (containsEnd && chunk.edited && chunk.end !== end)
4736
+ throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
4737
+
4738
+ const sliceStart = startChunk === chunk ? start - chunk.start : 0;
4739
+ const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
4740
+
4741
+ result += chunk.content.slice(sliceStart, sliceEnd);
4742
+
4743
+ if (chunk.outro && (!containsEnd || chunk.end === end)) {
4744
+ result += chunk.outro;
4745
+ }
4746
+
4747
+ if (containsEnd) {
4748
+ break;
4749
+ }
4750
+
4751
+ chunk = chunk.next;
4752
+ }
4753
+
4754
+ return result;
4755
+ }
4756
+
4757
+ // TODO deprecate this? not really very useful
4758
+ snip(start, end) {
4759
+ const clone = this.clone();
4760
+ clone.remove(0, start);
4761
+ clone.remove(end, clone.original.length);
4762
+
4763
+ return clone;
4764
+ }
4765
+
4766
+ _split(index) {
4767
+ if (this.byStart[index] || this.byEnd[index]) return;
4768
+
4769
+ let chunk = this.lastSearchedChunk;
4770
+ const searchForward = index > chunk.end;
4771
+
4772
+ while (chunk) {
4773
+ if (chunk.contains(index)) return this._splitChunk(chunk, index);
4774
+
4775
+ chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
4776
+ }
4777
+ }
4778
+
4779
+ _splitChunk(chunk, index) {
4780
+ if (chunk.edited && chunk.content.length) {
4781
+ // zero-length edited chunks are a special case (overlapping replacements)
4782
+ const loc = getLocator(this.original)(index);
4783
+ throw new Error(
4784
+ `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
4785
+ );
4786
+ }
4787
+
4788
+ const newChunk = chunk.split(index);
4789
+
4790
+ this.byEnd[index] = chunk;
4791
+ this.byStart[index] = newChunk;
4792
+ this.byEnd[newChunk.end] = newChunk;
4793
+
4794
+ if (chunk === this.lastChunk) this.lastChunk = newChunk;
4795
+
4796
+ this.lastSearchedChunk = chunk;
4797
+ return true;
4798
+ }
4799
+
4800
+ toString() {
4801
+ let str = this.intro;
4802
+
4803
+ let chunk = this.firstChunk;
4804
+ while (chunk) {
4805
+ str += chunk.toString();
4806
+ chunk = chunk.next;
4807
+ }
4808
+
4809
+ return str + this.outro;
4810
+ }
4811
+
4812
+ isEmpty() {
4813
+ let chunk = this.firstChunk;
4814
+ do {
4815
+ if (
4816
+ (chunk.intro.length && chunk.intro.trim()) ||
4817
+ (chunk.content.length && chunk.content.trim()) ||
4818
+ (chunk.outro.length && chunk.outro.trim())
4819
+ )
4820
+ return false;
4821
+ } while ((chunk = chunk.next));
4822
+ return true;
4823
+ }
4824
+
4825
+ length() {
4826
+ let chunk = this.firstChunk;
4827
+ let length = 0;
4828
+ do {
4829
+ length += chunk.intro.length + chunk.content.length + chunk.outro.length;
4830
+ } while ((chunk = chunk.next));
4831
+ return length;
4832
+ }
4833
+
4834
+ trimLines() {
4835
+ return this.trim('[\\r\\n]');
4836
+ }
4837
+
4838
+ trim(charType) {
4839
+ return this.trimStart(charType).trimEnd(charType);
4840
+ }
4841
+
4842
+ trimEndAborted(charType) {
4843
+ const rx = new RegExp((charType || '\\s') + '+$');
4844
+
4845
+ this.outro = this.outro.replace(rx, '');
4846
+ if (this.outro.length) return true;
4847
+
4848
+ let chunk = this.lastChunk;
4849
+
4850
+ do {
4851
+ const end = chunk.end;
4852
+ const aborted = chunk.trimEnd(rx);
4853
+
4854
+ // if chunk was trimmed, we have a new lastChunk
4855
+ if (chunk.end !== end) {
4856
+ if (this.lastChunk === chunk) {
4857
+ this.lastChunk = chunk.next;
4858
+ }
4859
+
4860
+ this.byEnd[chunk.end] = chunk;
4861
+ this.byStart[chunk.next.start] = chunk.next;
4862
+ this.byEnd[chunk.next.end] = chunk.next;
4863
+ }
4864
+
4865
+ if (aborted) return true;
4866
+ chunk = chunk.previous;
4867
+ } while (chunk);
4868
+
4869
+ return false;
4870
+ }
4871
+
4872
+ trimEnd(charType) {
4873
+ this.trimEndAborted(charType);
4874
+ return this;
4875
+ }
4876
+ trimStartAborted(charType) {
4877
+ const rx = new RegExp('^' + (charType || '\\s') + '+');
4878
+
4879
+ this.intro = this.intro.replace(rx, '');
4880
+ if (this.intro.length) return true;
4881
+
4882
+ let chunk = this.firstChunk;
4883
+
4884
+ do {
4885
+ const end = chunk.end;
4886
+ const aborted = chunk.trimStart(rx);
4887
+
4888
+ if (chunk.end !== end) {
4889
+ // special case...
4890
+ if (chunk === this.lastChunk) this.lastChunk = chunk.next;
4891
+
4892
+ this.byEnd[chunk.end] = chunk;
4893
+ this.byStart[chunk.next.start] = chunk.next;
4894
+ this.byEnd[chunk.next.end] = chunk.next;
4895
+ }
4896
+
4897
+ if (aborted) return true;
4898
+ chunk = chunk.next;
4899
+ } while (chunk);
4900
+
4901
+ return false;
4902
+ }
4903
+
4904
+ trimStart(charType) {
4905
+ this.trimStartAborted(charType);
4906
+ return this;
4907
+ }
4908
+
4909
+ hasChanged() {
4910
+ return this.original !== this.toString();
4911
+ }
4912
+
4913
+ _replaceRegexp(searchValue, replacement) {
4914
+ function getReplacement(match, str) {
4915
+ if (typeof replacement === 'string') {
4916
+ return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
4917
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
4918
+ if (i === '$') return '$';
4919
+ if (i === '&') return match[0];
4920
+ const num = +i;
4921
+ if (num < match.length) return match[+i];
4922
+ return `$${i}`;
4923
+ });
4924
+ } else {
4925
+ return replacement(...match, match.index, str, match.groups);
4926
+ }
4927
+ }
4928
+ function matchAll(re, str) {
4929
+ let match;
4930
+ const matches = [];
4931
+ while ((match = re.exec(str))) {
4932
+ matches.push(match);
4933
+ }
4934
+ return matches;
4935
+ }
4936
+ if (searchValue.global) {
4937
+ const matches = matchAll(searchValue, this.original);
4938
+ matches.forEach((match) => {
4939
+ if (match.index != null)
4940
+ this.overwrite(
4941
+ match.index,
4942
+ match.index + match[0].length,
4943
+ getReplacement(match, this.original),
4944
+ );
4945
+ });
4946
+ } else {
4947
+ const match = this.original.match(searchValue);
4948
+ if (match && match.index != null)
4949
+ this.overwrite(
4950
+ match.index,
4951
+ match.index + match[0].length,
4952
+ getReplacement(match, this.original),
4953
+ );
4954
+ }
4955
+ return this;
4956
+ }
4957
+
4958
+ _replaceString(string, replacement) {
4959
+ const { original } = this;
4960
+ const index = original.indexOf(string);
4961
+
4962
+ if (index !== -1) {
4963
+ this.overwrite(index, index + string.length, replacement);
4964
+ }
4965
+
4966
+ return this;
4967
+ }
4968
+
4969
+ replace(searchValue, replacement) {
4970
+ if (typeof searchValue === 'string') {
4971
+ return this._replaceString(searchValue, replacement);
4972
+ }
4973
+
4974
+ return this._replaceRegexp(searchValue, replacement);
4975
+ }
4976
+
4977
+ _replaceAllString(string, replacement) {
4978
+ const { original } = this;
4979
+ const stringLength = string.length;
4980
+ for (
4981
+ let index = original.indexOf(string);
4982
+ index !== -1;
4983
+ index = original.indexOf(string, index + stringLength)
4984
+ ) {
4985
+ this.overwrite(index, index + stringLength, replacement);
4986
+ }
4987
+
4988
+ return this;
4989
+ }
4990
+
4991
+ replaceAll(searchValue, replacement) {
4992
+ if (typeof searchValue === 'string') {
4993
+ return this._replaceAllString(searchValue, replacement);
4994
+ }
4995
+
4996
+ if (!searchValue.global) {
4997
+ throw new TypeError(
4998
+ 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
4999
+ );
5000
+ }
5001
+
5002
+ return this._replaceRegexp(searchValue, replacement);
5003
+ }
5004
+ }
5005
+
3750
5006
  const debug = createDebugger('vite:sourcemap', {
3751
5007
  onlyWhenFocused: true,
3752
5008
  });
@@ -3794,11 +5050,18 @@ function send(req, res, content, type, options) {
3794
5050
  }
3795
5051
  }
3796
5052
  // inject source map reference
3797
- if (map && map.mappings) {
5053
+ if (map && 'version' in map && map.mappings) {
3798
5054
  if (type === 'js' || type === 'css') {
3799
5055
  content = getCodeWithSourcemap(type, content.toString(), map);
3800
5056
  }
3801
5057
  }
5058
+ else {
5059
+ if (type === 'js' && (!map || map.mappings !== '')) {
5060
+ const urlWithoutTimestamp = removeTimestampQuery(req.url);
5061
+ const ms = new MagicString(content.toString());
5062
+ content = getCodeWithSourcemap(type, content.toString(), ms.generateMap({ source: urlWithoutTimestamp, hires: 'boundary' }));
5063
+ }
5064
+ }
3802
5065
  res.statusCode = 200;
3803
5066
  res.end(content);
3804
5067
  return;