@seybel/breakhound 0.0.0-stage → 1.0.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.
- package/LICENSE +21 -0
- package/README.md +388 -2
- package/bin/breakhound.cjs +25 -0
- package/dist/cli.js +7318 -0
- package/dist/index.cjs +34 -0
- package/dist/index.d.cts +254 -0
- package/dist/index.d.ts +254 -0
- package/dist/index.js +7 -0
- package/dist/inject.js +2236 -0
- package/dist/next.cjs +87 -0
- package/dist/next.d.cts +18 -0
- package/dist/next.d.ts +18 -0
- package/dist/next.js +56 -0
- package/dist/schema.json +530 -0
- package/dist/source-loader.cjs +1650 -0
- package/dist/vite.cjs +1689 -0
- package/dist/vite.d.cts +29 -0
- package/dist/vite.d.ts +29 -0
- package/dist/vite.js +1662 -0
- package/dist/webpack.cjs +75 -0
- package/dist/webpack.d.cts +23 -0
- package/dist/webpack.d.ts +23 -0
- package/dist/webpack.js +44 -0
- package/package.json +104 -4
|
@@ -0,0 +1,1650 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// ../source/src/transform.ts
|
|
4
|
+
var import_node_module = require("module");
|
|
5
|
+
var import_node_path = require("path");
|
|
6
|
+
var import_parser = require("@babel/parser");
|
|
7
|
+
|
|
8
|
+
// ../../node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
9
|
+
var comma = ",".charCodeAt(0);
|
|
10
|
+
var semicolon = ";".charCodeAt(0);
|
|
11
|
+
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
12
|
+
var intToChar = new Uint8Array(64);
|
|
13
|
+
var charToInt = new Uint8Array(128);
|
|
14
|
+
for (let i = 0; i < chars.length; i++) {
|
|
15
|
+
const c = chars.charCodeAt(i);
|
|
16
|
+
intToChar[i] = c;
|
|
17
|
+
charToInt[c] = i;
|
|
18
|
+
}
|
|
19
|
+
function encodeInteger(builder, num) {
|
|
20
|
+
do {
|
|
21
|
+
let clamped = num & 31;
|
|
22
|
+
num >>>= 5;
|
|
23
|
+
if (num > 0) clamped |= 32;
|
|
24
|
+
builder.write(intToChar[clamped]);
|
|
25
|
+
} while (num > 0);
|
|
26
|
+
}
|
|
27
|
+
function encodeSign(num) {
|
|
28
|
+
return num < 0 ? -num << 1 | 1 : num << 1;
|
|
29
|
+
}
|
|
30
|
+
var bufLength = 1024 * 16;
|
|
31
|
+
var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
|
|
32
|
+
decode(buf) {
|
|
33
|
+
const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
34
|
+
return out.toString();
|
|
35
|
+
}
|
|
36
|
+
} : {
|
|
37
|
+
decode(buf) {
|
|
38
|
+
let out = "";
|
|
39
|
+
for (let i = 0; i < buf.length; i++) {
|
|
40
|
+
out += String.fromCharCode(buf[i]);
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var StringWriter = class {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.pos = 0;
|
|
48
|
+
this.out = "";
|
|
49
|
+
this.buffer = new Uint8Array(bufLength);
|
|
50
|
+
}
|
|
51
|
+
write(v) {
|
|
52
|
+
const { buffer } = this;
|
|
53
|
+
buffer[this.pos++] = v;
|
|
54
|
+
if (this.pos === bufLength) {
|
|
55
|
+
this.out += td.decode(buffer);
|
|
56
|
+
this.pos = 0;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
flush() {
|
|
60
|
+
const { buffer, out, pos } = this;
|
|
61
|
+
return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
function encodeRangeMappings(decoded) {
|
|
65
|
+
if (decoded.length === 0) return "";
|
|
66
|
+
const writer = new StringWriter();
|
|
67
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
68
|
+
const indices = decoded[i];
|
|
69
|
+
if (i > 0) writer.write(semicolon);
|
|
70
|
+
let index = 0;
|
|
71
|
+
for (let j = 0; j < indices.length; j++) {
|
|
72
|
+
const offset = indices[j];
|
|
73
|
+
encodeInteger(writer, offset - index);
|
|
74
|
+
index = offset;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return writer.flush();
|
|
78
|
+
}
|
|
79
|
+
function encode(decoded) {
|
|
80
|
+
const writer = new StringWriter();
|
|
81
|
+
let sourcesIndex = 0;
|
|
82
|
+
let sourceLine = 0;
|
|
83
|
+
let sourceColumn = 0;
|
|
84
|
+
let namesIndex = 0;
|
|
85
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
86
|
+
const line = decoded[i];
|
|
87
|
+
if (i > 0) writer.write(semicolon);
|
|
88
|
+
if (line.length === 0) continue;
|
|
89
|
+
let genColumn = 0;
|
|
90
|
+
for (let j = 0; j < line.length; j++) {
|
|
91
|
+
const segment = line[j];
|
|
92
|
+
if (j > 0) writer.write(comma);
|
|
93
|
+
encodeInteger(writer, encodeSign(segment[0] - genColumn));
|
|
94
|
+
genColumn = segment[0];
|
|
95
|
+
if (segment.length === 1) continue;
|
|
96
|
+
encodeInteger(writer, encodeSign(segment[1] - sourcesIndex));
|
|
97
|
+
encodeInteger(writer, encodeSign(segment[2] - sourceLine));
|
|
98
|
+
encodeInteger(writer, encodeSign(segment[3] - sourceColumn));
|
|
99
|
+
sourcesIndex = segment[1];
|
|
100
|
+
sourceLine = segment[2];
|
|
101
|
+
sourceColumn = segment[3];
|
|
102
|
+
if (segment.length === 4) continue;
|
|
103
|
+
encodeInteger(writer, encodeSign(segment[4] - namesIndex));
|
|
104
|
+
namesIndex = segment[4];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return writer.flush();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ../../node_modules/magic-string/dist/index.mjs
|
|
111
|
+
var BitSet = class BitSet2 {
|
|
112
|
+
constructor(arg) {
|
|
113
|
+
this.bits = arg instanceof BitSet2 ? arg.bits.slice() : [];
|
|
114
|
+
}
|
|
115
|
+
add(n2) {
|
|
116
|
+
this.bits[n2 >> 5] |= 1 << (n2 & 31);
|
|
117
|
+
}
|
|
118
|
+
has(n2) {
|
|
119
|
+
return !!(this.bits[n2 >> 5] & 1 << (n2 & 31));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
var Chunk = class Chunk2 {
|
|
123
|
+
constructor(start, end, content) {
|
|
124
|
+
this.start = start;
|
|
125
|
+
this.end = end;
|
|
126
|
+
this.original = content;
|
|
127
|
+
this.intro = "";
|
|
128
|
+
this.outro = "";
|
|
129
|
+
this.content = content;
|
|
130
|
+
this.storeName = false;
|
|
131
|
+
this.edited = false;
|
|
132
|
+
this.previous = null;
|
|
133
|
+
this.next = null;
|
|
134
|
+
}
|
|
135
|
+
appendLeft(content) {
|
|
136
|
+
this.outro += content;
|
|
137
|
+
}
|
|
138
|
+
appendRight(content) {
|
|
139
|
+
this.intro = this.intro + content;
|
|
140
|
+
}
|
|
141
|
+
clone() {
|
|
142
|
+
const chunk = new Chunk2(this.start, this.end, this.original);
|
|
143
|
+
chunk.intro = this.intro;
|
|
144
|
+
chunk.outro = this.outro;
|
|
145
|
+
chunk.content = this.content;
|
|
146
|
+
chunk.storeName = this.storeName;
|
|
147
|
+
chunk.edited = this.edited;
|
|
148
|
+
return chunk;
|
|
149
|
+
}
|
|
150
|
+
contains(index) {
|
|
151
|
+
return this.start < index && index < this.end;
|
|
152
|
+
}
|
|
153
|
+
eachNext(fn) {
|
|
154
|
+
fn(this);
|
|
155
|
+
let chunk = this.next;
|
|
156
|
+
while (chunk) {
|
|
157
|
+
fn(chunk);
|
|
158
|
+
chunk = chunk.next;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
eachPrevious(fn) {
|
|
162
|
+
fn(this);
|
|
163
|
+
let chunk = this.previous;
|
|
164
|
+
while (chunk) {
|
|
165
|
+
fn(chunk);
|
|
166
|
+
chunk = chunk.previous;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
edit(content, storeName, contentOnly) {
|
|
170
|
+
this.content = content;
|
|
171
|
+
if (!contentOnly) {
|
|
172
|
+
this.intro = "";
|
|
173
|
+
this.outro = "";
|
|
174
|
+
}
|
|
175
|
+
this.storeName = storeName;
|
|
176
|
+
this.edited = true;
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
prependLeft(content) {
|
|
180
|
+
this.outro = content + this.outro;
|
|
181
|
+
}
|
|
182
|
+
prependRight(content) {
|
|
183
|
+
this.intro = content + this.intro;
|
|
184
|
+
}
|
|
185
|
+
reset() {
|
|
186
|
+
this.intro = "";
|
|
187
|
+
this.outro = "";
|
|
188
|
+
if (this.edited) {
|
|
189
|
+
this.content = this.original;
|
|
190
|
+
this.storeName = false;
|
|
191
|
+
this.edited = false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
split(index) {
|
|
195
|
+
const sliceIndex = index - this.start;
|
|
196
|
+
const originalBefore = this.original.slice(0, sliceIndex);
|
|
197
|
+
const originalAfter = this.original.slice(sliceIndex);
|
|
198
|
+
this.original = originalBefore;
|
|
199
|
+
const newChunk = new Chunk2(index, this.end, originalAfter);
|
|
200
|
+
newChunk.outro = this.outro;
|
|
201
|
+
this.outro = "";
|
|
202
|
+
this.end = index;
|
|
203
|
+
if (this.edited) {
|
|
204
|
+
newChunk.edit("", false, true);
|
|
205
|
+
this.content = "";
|
|
206
|
+
} else this.content = originalBefore;
|
|
207
|
+
newChunk.next = this.next;
|
|
208
|
+
if (newChunk.next) newChunk.next.previous = newChunk;
|
|
209
|
+
newChunk.previous = this;
|
|
210
|
+
this.next = newChunk;
|
|
211
|
+
return newChunk;
|
|
212
|
+
}
|
|
213
|
+
toString() {
|
|
214
|
+
return this.intro + this.content + this.outro;
|
|
215
|
+
}
|
|
216
|
+
trimEnd(rx) {
|
|
217
|
+
this.outro = this.outro.replace(rx, "");
|
|
218
|
+
if (this.outro.length) return true;
|
|
219
|
+
const trimmed = this.content.replace(rx, "");
|
|
220
|
+
if (trimmed.length) {
|
|
221
|
+
if (trimmed !== this.content) {
|
|
222
|
+
if (this.edited) this.edit(trimmed, this.storeName, true);
|
|
223
|
+
else this.split(this.start + trimmed.length).edit("", void 0, true);
|
|
224
|
+
}
|
|
225
|
+
return true;
|
|
226
|
+
} else {
|
|
227
|
+
this.edit("", void 0, true);
|
|
228
|
+
this.intro = this.intro.replace(rx, "");
|
|
229
|
+
if (this.intro.length) return true;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
trimStart(rx) {
|
|
233
|
+
this.intro = this.intro.replace(rx, "");
|
|
234
|
+
if (this.intro.length) return true;
|
|
235
|
+
const trimmed = this.content.replace(rx, "");
|
|
236
|
+
if (trimmed.length) {
|
|
237
|
+
if (trimmed !== this.content) {
|
|
238
|
+
if (this.edited) this.edit(trimmed, this.storeName, true);
|
|
239
|
+
else {
|
|
240
|
+
this.split(this.end - trimmed.length);
|
|
241
|
+
this.edit("", void 0, true);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return true;
|
|
245
|
+
} else {
|
|
246
|
+
this.edit("", void 0, true);
|
|
247
|
+
this.outro = this.outro.replace(rx, "");
|
|
248
|
+
if (this.outro.length) return true;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
var MagicStringError = class extends Error {
|
|
253
|
+
name = "MagicStringError";
|
|
254
|
+
constructor(message, options) {
|
|
255
|
+
super(`[MagicString] ${message}`, options);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
function getBtoa() {
|
|
259
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
|
|
260
|
+
const buffer = globalThis["Buffer"];
|
|
261
|
+
if (buffer) return (str) => buffer.from(str, "utf-8").toString("base64");
|
|
262
|
+
return () => {
|
|
263
|
+
throw new MagicStringError("unsupported environment: `btoa` or `Buffer` is required");
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
var btoa = /* @__PURE__ */ getBtoa();
|
|
267
|
+
var SourceMap = class {
|
|
268
|
+
constructor(properties) {
|
|
269
|
+
this.version = 3;
|
|
270
|
+
this.file = properties.file;
|
|
271
|
+
this.sources = properties.sources;
|
|
272
|
+
this.sourcesContent = properties.sourcesContent;
|
|
273
|
+
this.names = properties.names;
|
|
274
|
+
this.mappings = typeof properties.mappings === "string" ? properties.mappings : encode(properties.mappings);
|
|
275
|
+
if (typeof properties.x_google_ignoreList !== "undefined") this.x_google_ignoreList = properties.x_google_ignoreList;
|
|
276
|
+
if (typeof properties.debugId !== "undefined") this.debugId = properties.debugId;
|
|
277
|
+
if (typeof properties.rangeMappings !== "undefined") {
|
|
278
|
+
let shouldOutputRangeMapping = false;
|
|
279
|
+
for (const line of properties.rangeMappings) if (line.length !== 0) {
|
|
280
|
+
shouldOutputRangeMapping = true;
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
if (shouldOutputRangeMapping) this.rangeMappings = encodeRangeMappings(properties.rangeMappings);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Returns the equivalent of `JSON.stringify(map)`
|
|
288
|
+
*/
|
|
289
|
+
toString() {
|
|
290
|
+
return JSON.stringify(this);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Returns a DataURI containing the sourcemap. Useful for doing this sort of thing:
|
|
294
|
+
* `generateMap(options?: SourceMapOptions): SourceMap;`
|
|
295
|
+
*/
|
|
296
|
+
toUrl() {
|
|
297
|
+
return `data:application/json;charset=utf-8;base64,${btoa(this.toString())}`;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
function getLocator(source) {
|
|
301
|
+
const lineOffsets = [0];
|
|
302
|
+
for (let i = source.indexOf("\n"); i !== -1; i = source.indexOf("\n", i + 1)) lineOffsets.push(i + 1);
|
|
303
|
+
return function locate(index) {
|
|
304
|
+
let i = 0;
|
|
305
|
+
let j = lineOffsets.length;
|
|
306
|
+
while (i < j) {
|
|
307
|
+
const m = i + j >> 1;
|
|
308
|
+
if (index < lineOffsets[m]) j = m;
|
|
309
|
+
else i = m + 1;
|
|
310
|
+
}
|
|
311
|
+
const line = i - 1;
|
|
312
|
+
return {
|
|
313
|
+
line,
|
|
314
|
+
column: index - lineOffsets[line]
|
|
315
|
+
};
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function getRelativePath(from, to) {
|
|
319
|
+
const fromParts = from.split(/[/\\]/);
|
|
320
|
+
const toParts = to.split(/[/\\]/);
|
|
321
|
+
fromParts.pop();
|
|
322
|
+
while (fromParts[0] === toParts[0]) {
|
|
323
|
+
fromParts.shift();
|
|
324
|
+
toParts.shift();
|
|
325
|
+
}
|
|
326
|
+
if (fromParts.length) {
|
|
327
|
+
let i = fromParts.length;
|
|
328
|
+
while (i--) fromParts[i] = "..";
|
|
329
|
+
}
|
|
330
|
+
return fromParts.concat(toParts).join("/");
|
|
331
|
+
}
|
|
332
|
+
function guessIndent(code) {
|
|
333
|
+
const lines = code.split("\n");
|
|
334
|
+
const tabbed = lines.filter((line) => /^\t+/.test(line));
|
|
335
|
+
const spaced = lines.filter((line) => /^ {2,}/.test(line));
|
|
336
|
+
if (tabbed.length === 0 && spaced.length === 0) return null;
|
|
337
|
+
if (tabbed.length >= spaced.length) return " ";
|
|
338
|
+
const min = spaced.reduce((previous, current) => {
|
|
339
|
+
const numSpaces = /^ +/.exec(current)[0].length;
|
|
340
|
+
return Math.min(numSpaces, previous);
|
|
341
|
+
}, Infinity);
|
|
342
|
+
return " ".repeat(min);
|
|
343
|
+
}
|
|
344
|
+
var toString = Object.prototype.toString;
|
|
345
|
+
function isObject(thing) {
|
|
346
|
+
return toString.call(thing) === "[object Object]";
|
|
347
|
+
}
|
|
348
|
+
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
349
|
+
var intToChar2 = /* @__PURE__ */ (() => {
|
|
350
|
+
const chars2 = /* @__PURE__ */ new Uint8Array(64);
|
|
351
|
+
for (let i = 0; i < 64; i++) chars2[i] = BASE64_CHARS.charCodeAt(i);
|
|
352
|
+
return chars2;
|
|
353
|
+
})();
|
|
354
|
+
var COMMA = 44;
|
|
355
|
+
var SEMICOLON = 59;
|
|
356
|
+
var BUFFER_SIZE = 16384;
|
|
357
|
+
var FLUSH_THRESHOLD = 16348;
|
|
358
|
+
var scratch = /* @__PURE__ */ new Uint8Array(BUFFER_SIZE);
|
|
359
|
+
function writeVlq(pos, num) {
|
|
360
|
+
num = num < 0 ? -num << 1 | 1 : num << 1;
|
|
361
|
+
do {
|
|
362
|
+
let clamped = num & 31;
|
|
363
|
+
num >>>= 5;
|
|
364
|
+
if (num > 0) clamped |= 32;
|
|
365
|
+
scratch[pos++] = intToChar2[clamped];
|
|
366
|
+
} while (num > 0);
|
|
367
|
+
return pos;
|
|
368
|
+
}
|
|
369
|
+
var decoder = /* @__PURE__ */ new TextDecoder();
|
|
370
|
+
var MappingsEncoder = class {
|
|
371
|
+
constructor() {
|
|
372
|
+
this.out = "";
|
|
373
|
+
this.pos = 0;
|
|
374
|
+
this.lines = [];
|
|
375
|
+
this.buffered = 0;
|
|
376
|
+
this.needsComma = false;
|
|
377
|
+
this.prevGenColumn = 0;
|
|
378
|
+
this.prevSourceIndex = 0;
|
|
379
|
+
this.prevSourceLine = 0;
|
|
380
|
+
this.prevSourceColumn = 0;
|
|
381
|
+
this.prevNameIndex = 0;
|
|
382
|
+
}
|
|
383
|
+
endLine(segments) {
|
|
384
|
+
this.lines.push(segments);
|
|
385
|
+
this.buffered += segments.length + 1;
|
|
386
|
+
if (this.buffered >= 4096) this.drain(null);
|
|
387
|
+
}
|
|
388
|
+
segments(segments) {
|
|
389
|
+
this.drain(segments);
|
|
390
|
+
}
|
|
391
|
+
finish(segments) {
|
|
392
|
+
this.drain(segments);
|
|
393
|
+
this.flush();
|
|
394
|
+
return this.out;
|
|
395
|
+
}
|
|
396
|
+
drain(trailing) {
|
|
397
|
+
const lines = this.lines;
|
|
398
|
+
const lineCount = lines.length;
|
|
399
|
+
for (let l = 0; l <= lineCount; l++) {
|
|
400
|
+
const line = l < lineCount ? lines[l] : trailing;
|
|
401
|
+
if (line === null) break;
|
|
402
|
+
for (let i = 0; i < line.length; i++) {
|
|
403
|
+
if (this.pos > FLUSH_THRESHOLD) this.flush();
|
|
404
|
+
const segment = line[i];
|
|
405
|
+
if (this.needsComma) scratch[this.pos++] = COMMA;
|
|
406
|
+
this.needsComma = true;
|
|
407
|
+
this.pos = writeVlq(this.pos, segment[0] - this.prevGenColumn);
|
|
408
|
+
this.prevGenColumn = segment[0];
|
|
409
|
+
this.pos = writeVlq(this.pos, segment[1] - this.prevSourceIndex);
|
|
410
|
+
this.prevSourceIndex = segment[1];
|
|
411
|
+
this.pos = writeVlq(this.pos, segment[2] - this.prevSourceLine);
|
|
412
|
+
this.prevSourceLine = segment[2];
|
|
413
|
+
this.pos = writeVlq(this.pos, segment[3] - this.prevSourceColumn);
|
|
414
|
+
this.prevSourceColumn = segment[3];
|
|
415
|
+
if (segment.length === 5) {
|
|
416
|
+
this.pos = writeVlq(this.pos, segment[4] - this.prevNameIndex);
|
|
417
|
+
this.prevNameIndex = segment[4];
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (l < lineCount) {
|
|
421
|
+
if (this.pos > FLUSH_THRESHOLD) this.flush();
|
|
422
|
+
scratch[this.pos++] = SEMICOLON;
|
|
423
|
+
this.needsComma = false;
|
|
424
|
+
this.prevGenColumn = 0;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
lines.length = 0;
|
|
428
|
+
this.buffered = 0;
|
|
429
|
+
this.flush();
|
|
430
|
+
}
|
|
431
|
+
flush() {
|
|
432
|
+
this.out += decoder.decode(scratch.subarray(0, this.pos));
|
|
433
|
+
this.pos = 0;
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
var NEWLINE_CHAR$1 = 10;
|
|
437
|
+
function isWordCode(code) {
|
|
438
|
+
return code >= 97 && code <= 122 || code >= 65 && code <= 90 || code >= 48 && code <= 57 || code === 95;
|
|
439
|
+
}
|
|
440
|
+
var Mappings = class {
|
|
441
|
+
constructor(hires, encoder = null) {
|
|
442
|
+
this.hires = hires;
|
|
443
|
+
this.generatedCodeLine = 0;
|
|
444
|
+
this.generatedCodeColumn = 0;
|
|
445
|
+
this.raw = [];
|
|
446
|
+
this.rawSegments = this.raw[this.generatedCodeLine] = [];
|
|
447
|
+
this.rawRangeMappings = [];
|
|
448
|
+
this.rawRangeMappingsIndices = this.rawRangeMappings[this.generatedCodeLine] = [];
|
|
449
|
+
this.encoder = encoder;
|
|
450
|
+
}
|
|
451
|
+
nextLine() {
|
|
452
|
+
if (this.encoder === null) {
|
|
453
|
+
this.generatedCodeLine += 1;
|
|
454
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
455
|
+
} else {
|
|
456
|
+
this.encoder.endLine(this.rawSegments);
|
|
457
|
+
this.rawSegments = [];
|
|
458
|
+
this.generatedCodeLine += 1;
|
|
459
|
+
}
|
|
460
|
+
this.generatedCodeColumn = 0;
|
|
461
|
+
this.rawRangeMappings[this.generatedCodeLine] = this.rawRangeMappingsIndices = [];
|
|
462
|
+
}
|
|
463
|
+
addEdit(sourceIndex, content, loc, nameIndex) {
|
|
464
|
+
if (content.length) {
|
|
465
|
+
const contentLengthMinusOne = content.length - 1;
|
|
466
|
+
let contentLineEnd = content.indexOf("\n", 0);
|
|
467
|
+
let previousContentLineEnd = -1;
|
|
468
|
+
while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
|
|
469
|
+
const segment2 = [
|
|
470
|
+
this.generatedCodeColumn,
|
|
471
|
+
sourceIndex,
|
|
472
|
+
loc.line,
|
|
473
|
+
loc.column
|
|
474
|
+
];
|
|
475
|
+
if (nameIndex >= 0) segment2.push(nameIndex);
|
|
476
|
+
this.rawSegments.push(segment2);
|
|
477
|
+
this.nextLine();
|
|
478
|
+
previousContentLineEnd = contentLineEnd;
|
|
479
|
+
contentLineEnd = content.indexOf("\n", contentLineEnd + 1);
|
|
480
|
+
}
|
|
481
|
+
const segment = [
|
|
482
|
+
this.generatedCodeColumn,
|
|
483
|
+
sourceIndex,
|
|
484
|
+
loc.line,
|
|
485
|
+
loc.column
|
|
486
|
+
];
|
|
487
|
+
if (nameIndex >= 0) segment.push(nameIndex);
|
|
488
|
+
this.rawSegments.push(segment);
|
|
489
|
+
this.advance(content.slice(previousContentLineEnd + 1));
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
|
|
493
|
+
const end = chunk.end;
|
|
494
|
+
let i = chunk.start;
|
|
495
|
+
if (this.hires) {
|
|
496
|
+
const boundary = this.hires === "boundary";
|
|
497
|
+
const experimentalRange = this.hires === "experimental-range";
|
|
498
|
+
const encoder = experimentalRange ? null : this.encoder;
|
|
499
|
+
let charInHiresBoundary = false;
|
|
500
|
+
while (i < end) {
|
|
501
|
+
if (encoder !== null && this.rawSegments.length >= 4096) {
|
|
502
|
+
encoder.segments(this.rawSegments);
|
|
503
|
+
this.rawSegments.length = 0;
|
|
504
|
+
}
|
|
505
|
+
if (experimentalRange && i + 1 >= end) this.rawSegments.push([
|
|
506
|
+
this.generatedCodeColumn,
|
|
507
|
+
sourceIndex,
|
|
508
|
+
loc.line,
|
|
509
|
+
loc.column
|
|
510
|
+
]);
|
|
511
|
+
const code = original.charCodeAt(i);
|
|
512
|
+
if (code === NEWLINE_CHAR$1) {
|
|
513
|
+
loc.line += 1;
|
|
514
|
+
loc.column = 0;
|
|
515
|
+
this.nextLine();
|
|
516
|
+
charInHiresBoundary = false;
|
|
517
|
+
} else {
|
|
518
|
+
if (boundary) {
|
|
519
|
+
if (isWordCode(code)) {
|
|
520
|
+
if (!charInHiresBoundary) {
|
|
521
|
+
this.rawSegments.push([
|
|
522
|
+
this.generatedCodeColumn,
|
|
523
|
+
sourceIndex,
|
|
524
|
+
loc.line,
|
|
525
|
+
loc.column
|
|
526
|
+
]);
|
|
527
|
+
charInHiresBoundary = true;
|
|
528
|
+
}
|
|
529
|
+
} else {
|
|
530
|
+
this.rawSegments.push([
|
|
531
|
+
this.generatedCodeColumn,
|
|
532
|
+
sourceIndex,
|
|
533
|
+
loc.line,
|
|
534
|
+
loc.column
|
|
535
|
+
]);
|
|
536
|
+
charInHiresBoundary = false;
|
|
537
|
+
}
|
|
538
|
+
} else if (experimentalRange) {
|
|
539
|
+
if (i === chunk.start) {
|
|
540
|
+
this.rawRangeMappingsIndices.push(this.rawSegments.length);
|
|
541
|
+
this.rawSegments.push([
|
|
542
|
+
this.generatedCodeColumn,
|
|
543
|
+
sourceIndex,
|
|
544
|
+
loc.line,
|
|
545
|
+
loc.column
|
|
546
|
+
]);
|
|
547
|
+
}
|
|
548
|
+
} else this.rawSegments.push([
|
|
549
|
+
this.generatedCodeColumn,
|
|
550
|
+
sourceIndex,
|
|
551
|
+
loc.line,
|
|
552
|
+
loc.column
|
|
553
|
+
]);
|
|
554
|
+
loc.column += 1;
|
|
555
|
+
this.generatedCodeColumn += 1;
|
|
556
|
+
}
|
|
557
|
+
i += 1;
|
|
558
|
+
}
|
|
559
|
+
} else {
|
|
560
|
+
const bits = sourcemapLocations.bits;
|
|
561
|
+
while (i < end) {
|
|
562
|
+
let newline = original.indexOf("\n", i);
|
|
563
|
+
if (newline === -1 || newline > end) newline = end;
|
|
564
|
+
if (newline > i) {
|
|
565
|
+
this.rawSegments.push([
|
|
566
|
+
this.generatedCodeColumn,
|
|
567
|
+
sourceIndex,
|
|
568
|
+
loc.line,
|
|
569
|
+
loc.column
|
|
570
|
+
]);
|
|
571
|
+
for (let w = i + 1 >> 5, last = newline - 1 >> 5; w <= last; w++) {
|
|
572
|
+
let word = bits[w];
|
|
573
|
+
if (!word) continue;
|
|
574
|
+
const base = w << 5;
|
|
575
|
+
while (word) {
|
|
576
|
+
const lowest = word & -word;
|
|
577
|
+
const index = base + 31 - Math.clz32(lowest);
|
|
578
|
+
if (index > i && index < newline) {
|
|
579
|
+
const offset = index - i;
|
|
580
|
+
this.rawSegments.push([
|
|
581
|
+
this.generatedCodeColumn + offset,
|
|
582
|
+
sourceIndex,
|
|
583
|
+
loc.line,
|
|
584
|
+
loc.column + offset
|
|
585
|
+
]);
|
|
586
|
+
}
|
|
587
|
+
word ^= lowest;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
loc.column += newline - i;
|
|
591
|
+
this.generatedCodeColumn += newline - i;
|
|
592
|
+
}
|
|
593
|
+
if (newline === end) break;
|
|
594
|
+
loc.line += 1;
|
|
595
|
+
loc.column = 0;
|
|
596
|
+
this.nextLine();
|
|
597
|
+
i = newline + 1;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
advance(str) {
|
|
602
|
+
if (!str) return;
|
|
603
|
+
const lastNewline = str.lastIndexOf("\n");
|
|
604
|
+
for (let i = str.indexOf("\n"); i !== -1; i = str.indexOf("\n", i + 1)) this.nextLine();
|
|
605
|
+
this.generatedCodeColumn += str.length - lastNewline - 1;
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
var n = "\n";
|
|
609
|
+
var NEWLINE_CHAR = "\n".charCodeAt(0);
|
|
610
|
+
var CR_CHAR = "\r".charCodeAt(0);
|
|
611
|
+
var warned = {
|
|
612
|
+
insertLeft: false,
|
|
613
|
+
insertRight: false,
|
|
614
|
+
storeName: false
|
|
615
|
+
};
|
|
616
|
+
function expandReplacement(replacement, matched, position, str, captures, namedCaptures) {
|
|
617
|
+
if (!replacement.includes("$")) return replacement;
|
|
618
|
+
let result = "";
|
|
619
|
+
let index = 0;
|
|
620
|
+
while (index < replacement.length) {
|
|
621
|
+
const dollar = replacement.indexOf("$", index);
|
|
622
|
+
if (dollar === -1) {
|
|
623
|
+
result += replacement.slice(index);
|
|
624
|
+
break;
|
|
625
|
+
}
|
|
626
|
+
result += replacement.slice(index, dollar);
|
|
627
|
+
const char = replacement[dollar + 1];
|
|
628
|
+
let expansion = "$";
|
|
629
|
+
let consumed = 1;
|
|
630
|
+
if (char === "$") consumed = 2;
|
|
631
|
+
else if (char === "&") {
|
|
632
|
+
expansion = matched;
|
|
633
|
+
consumed = 2;
|
|
634
|
+
} else if (char === "`") {
|
|
635
|
+
expansion = str.slice(0, position);
|
|
636
|
+
consumed = 2;
|
|
637
|
+
} else if (char === "'") {
|
|
638
|
+
expansion = str.slice(position + matched.length);
|
|
639
|
+
consumed = 2;
|
|
640
|
+
} else if (char === "<" && namedCaptures !== void 0) {
|
|
641
|
+
const close = replacement.indexOf(">", dollar + 2);
|
|
642
|
+
if (close !== -1) {
|
|
643
|
+
expansion = namedCaptures[replacement.slice(dollar + 2, close)] ?? "";
|
|
644
|
+
consumed = close + 1 - dollar;
|
|
645
|
+
}
|
|
646
|
+
} else if (char >= "0" && char <= "9") {
|
|
647
|
+
const second = replacement[dollar + 2];
|
|
648
|
+
const double = second >= "0" && second <= "9" ? Number(char + second) : NaN;
|
|
649
|
+
const single = Number(char);
|
|
650
|
+
if (double >= 1 && double <= captures.length) {
|
|
651
|
+
expansion = captures[double - 1] ?? "";
|
|
652
|
+
consumed = 3;
|
|
653
|
+
} else if (single >= 1 && single <= captures.length) {
|
|
654
|
+
expansion = captures[single - 1] ?? "";
|
|
655
|
+
consumed = 2;
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
result += expansion;
|
|
659
|
+
index = dollar + consumed;
|
|
660
|
+
}
|
|
661
|
+
return result;
|
|
662
|
+
}
|
|
663
|
+
var MagicString = class MagicString2 {
|
|
664
|
+
constructor(string, options = {}) {
|
|
665
|
+
const chunk = new Chunk(0, string.length, string);
|
|
666
|
+
Object.defineProperties(this, {
|
|
667
|
+
original: {
|
|
668
|
+
writable: true,
|
|
669
|
+
value: string
|
|
670
|
+
},
|
|
671
|
+
outro: {
|
|
672
|
+
writable: true,
|
|
673
|
+
value: ""
|
|
674
|
+
},
|
|
675
|
+
intro: {
|
|
676
|
+
writable: true,
|
|
677
|
+
value: ""
|
|
678
|
+
},
|
|
679
|
+
firstChunk: {
|
|
680
|
+
writable: true,
|
|
681
|
+
value: chunk
|
|
682
|
+
},
|
|
683
|
+
lastChunk: {
|
|
684
|
+
writable: true,
|
|
685
|
+
value: chunk
|
|
686
|
+
},
|
|
687
|
+
lastSearchedChunk: {
|
|
688
|
+
writable: true,
|
|
689
|
+
value: chunk
|
|
690
|
+
},
|
|
691
|
+
byStart: {
|
|
692
|
+
writable: true,
|
|
693
|
+
value: /* @__PURE__ */ new Map([[0, chunk]])
|
|
694
|
+
},
|
|
695
|
+
byEnd: {
|
|
696
|
+
writable: true,
|
|
697
|
+
value: /* @__PURE__ */ new Map([[string.length, chunk]])
|
|
698
|
+
},
|
|
699
|
+
filename: {
|
|
700
|
+
writable: true,
|
|
701
|
+
value: options.filename
|
|
702
|
+
},
|
|
703
|
+
indentExclusionRanges: {
|
|
704
|
+
writable: true,
|
|
705
|
+
value: options.indentExclusionRanges
|
|
706
|
+
},
|
|
707
|
+
sourcemapLocations: {
|
|
708
|
+
writable: true,
|
|
709
|
+
value: new BitSet()
|
|
710
|
+
},
|
|
711
|
+
storedNames: {
|
|
712
|
+
writable: true,
|
|
713
|
+
value: {}
|
|
714
|
+
},
|
|
715
|
+
indentStr: {
|
|
716
|
+
writable: true,
|
|
717
|
+
value: void 0
|
|
718
|
+
},
|
|
719
|
+
ignoreList: {
|
|
720
|
+
writable: true,
|
|
721
|
+
value: options.ignoreList
|
|
722
|
+
},
|
|
723
|
+
offset: {
|
|
724
|
+
writable: true,
|
|
725
|
+
value: options.offset || 0
|
|
726
|
+
},
|
|
727
|
+
hasMovedChunks: {
|
|
728
|
+
writable: true,
|
|
729
|
+
value: false
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Adds the specified character index (with respect to the original string) to sourcemap mappings, if `hires` is false.
|
|
735
|
+
*/
|
|
736
|
+
addSourcemapLocation(char) {
|
|
737
|
+
this.sourcemapLocations.add(char);
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Appends the specified content to the end of the string.
|
|
741
|
+
*/
|
|
742
|
+
append(content) {
|
|
743
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
744
|
+
this.outro += content;
|
|
745
|
+
return this;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Appends the specified content at the index in the original string.
|
|
749
|
+
* If a range *ending* with index is subsequently moved, the insert will be moved with it.
|
|
750
|
+
* See also `s.prependLeft(...)`.
|
|
751
|
+
*/
|
|
752
|
+
appendLeft(index, content) {
|
|
753
|
+
index = index + this.offset;
|
|
754
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
755
|
+
this._split(index);
|
|
756
|
+
const chunk = this.byEnd.get(index);
|
|
757
|
+
if (chunk) chunk.appendLeft(content);
|
|
758
|
+
else this.intro += content;
|
|
759
|
+
return this;
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Appends the specified content at the index in the original string.
|
|
763
|
+
* If a range *starting* with index is subsequently moved, the insert will be moved with it.
|
|
764
|
+
* See also `s.prependRight(...)`.
|
|
765
|
+
*/
|
|
766
|
+
appendRight(index, content) {
|
|
767
|
+
index = index + this.offset;
|
|
768
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
769
|
+
this._split(index);
|
|
770
|
+
const chunk = this.byStart.get(index);
|
|
771
|
+
if (chunk) chunk.appendRight(content);
|
|
772
|
+
else this.outro += content;
|
|
773
|
+
return this;
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Does what you'd expect.
|
|
777
|
+
*/
|
|
778
|
+
clone() {
|
|
779
|
+
const cloned = new MagicString2(this.original, {
|
|
780
|
+
filename: this.filename,
|
|
781
|
+
ignoreList: this.ignoreList,
|
|
782
|
+
offset: this.offset
|
|
783
|
+
});
|
|
784
|
+
let originalChunk = this.firstChunk;
|
|
785
|
+
let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
|
|
786
|
+
while (originalChunk) {
|
|
787
|
+
cloned.byStart.set(clonedChunk.start, clonedChunk);
|
|
788
|
+
cloned.byEnd.set(clonedChunk.end, clonedChunk);
|
|
789
|
+
const nextOriginalChunk = originalChunk.next;
|
|
790
|
+
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
|
791
|
+
if (nextClonedChunk) {
|
|
792
|
+
clonedChunk.next = nextClonedChunk;
|
|
793
|
+
nextClonedChunk.previous = clonedChunk;
|
|
794
|
+
clonedChunk = nextClonedChunk;
|
|
795
|
+
}
|
|
796
|
+
originalChunk = nextOriginalChunk;
|
|
797
|
+
}
|
|
798
|
+
cloned.lastChunk = clonedChunk;
|
|
799
|
+
if (this.indentExclusionRanges) cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
|
800
|
+
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
|
801
|
+
cloned.storedNames = { ...this.storedNames };
|
|
802
|
+
cloned.intro = this.intro;
|
|
803
|
+
cloned.outro = this.outro;
|
|
804
|
+
cloned.hasMovedChunks = this.hasMovedChunks;
|
|
805
|
+
return cloned;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Generates a sourcemap object with raw mappings in array form, rather than encoded as a string.
|
|
809
|
+
* Useful if you need to manipulate the sourcemap further, but most of the time you will use `generateMap` instead.
|
|
810
|
+
*/
|
|
811
|
+
generateDecodedMap(options) {
|
|
812
|
+
options = options || {};
|
|
813
|
+
const mappings = new Mappings(options.hires);
|
|
814
|
+
const names = this._generateMappings(mappings);
|
|
815
|
+
return {
|
|
816
|
+
...this._mapProperties(options, names),
|
|
817
|
+
mappings: mappings.raw,
|
|
818
|
+
rangeMappings: mappings.rawRangeMappings
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Generates a version 3 sourcemap.
|
|
823
|
+
*/
|
|
824
|
+
generateMap(options) {
|
|
825
|
+
options = options || {};
|
|
826
|
+
const encoder = new MappingsEncoder();
|
|
827
|
+
const mappings = new Mappings(options.hires, encoder);
|
|
828
|
+
const names = this._generateMappings(mappings);
|
|
829
|
+
return new SourceMap({
|
|
830
|
+
...this._mapProperties(options, names),
|
|
831
|
+
mappings: encoder.finish(mappings.rawSegments),
|
|
832
|
+
rangeMappings: mappings.rawRangeMappings
|
|
833
|
+
});
|
|
834
|
+
}
|
|
835
|
+
/** @internal */
|
|
836
|
+
_generateMappings(mappings) {
|
|
837
|
+
const sourceIndex = 0;
|
|
838
|
+
const names = Object.keys(this.storedNames);
|
|
839
|
+
const locate = getLocator(this.original);
|
|
840
|
+
if (this.intro) mappings.advance(this.intro);
|
|
841
|
+
this.firstChunk.eachNext((chunk) => {
|
|
842
|
+
const loc = locate(chunk.start);
|
|
843
|
+
if (chunk.intro.length) mappings.advance(chunk.intro);
|
|
844
|
+
if (chunk.edited) mappings.addEdit(sourceIndex, chunk.content, loc, chunk.storeName ? names.indexOf(chunk.original) : -1);
|
|
845
|
+
else mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
|
|
846
|
+
if (chunk.outro.length) mappings.advance(chunk.outro);
|
|
847
|
+
});
|
|
848
|
+
if (this.outro) mappings.advance(this.outro);
|
|
849
|
+
return names;
|
|
850
|
+
}
|
|
851
|
+
/** @internal */
|
|
852
|
+
_mapProperties(options, names) {
|
|
853
|
+
return {
|
|
854
|
+
file: options.file ? options.file.split(/[/\\]/).pop() : void 0,
|
|
855
|
+
sources: [options.source ? getRelativePath(options.file || "", options.source) : options.file || ""],
|
|
856
|
+
sourcesContent: options.includeContent ? [this.original] : void 0,
|
|
857
|
+
names,
|
|
858
|
+
x_google_ignoreList: this.ignoreList ? [0] : void 0
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
/** @internal */
|
|
862
|
+
_ensureindentStr() {
|
|
863
|
+
if (this.indentStr === void 0) this.indentStr = guessIndent(this.original);
|
|
864
|
+
}
|
|
865
|
+
/** @internal */
|
|
866
|
+
_getRawIndentString() {
|
|
867
|
+
this._ensureindentStr();
|
|
868
|
+
return this.indentStr;
|
|
869
|
+
}
|
|
870
|
+
getIndentString() {
|
|
871
|
+
this._ensureindentStr();
|
|
872
|
+
return this.indentStr === null ? " " : this.indentStr;
|
|
873
|
+
}
|
|
874
|
+
indent(indentStr, options) {
|
|
875
|
+
const pattern = /^[^\r\n]/gm;
|
|
876
|
+
if (isObject(indentStr)) {
|
|
877
|
+
options = indentStr;
|
|
878
|
+
indentStr = void 0;
|
|
879
|
+
}
|
|
880
|
+
if (indentStr === void 0) {
|
|
881
|
+
this._ensureindentStr();
|
|
882
|
+
indentStr = this.indentStr || " ";
|
|
883
|
+
}
|
|
884
|
+
if (indentStr === "") return this;
|
|
885
|
+
const resolvedIndentStr = indentStr;
|
|
886
|
+
options = options || {};
|
|
887
|
+
const isExcluded = {};
|
|
888
|
+
if (options.exclude) (typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude).forEach((exclusion) => {
|
|
889
|
+
for (let i = exclusion[0]; i < exclusion[1]; i += 1) isExcluded[i] = true;
|
|
890
|
+
});
|
|
891
|
+
let shouldIndentNextCharacter = options.indentStart !== false;
|
|
892
|
+
const indentPiece = (str) => {
|
|
893
|
+
if (str === "") return str;
|
|
894
|
+
const indented = str.replace(pattern, (match, offset) => offset > 0 || shouldIndentNextCharacter ? `${resolvedIndentStr}${match}` : match);
|
|
895
|
+
shouldIndentNextCharacter = str[str.length - 1] === "\n";
|
|
896
|
+
return indented;
|
|
897
|
+
};
|
|
898
|
+
this.intro = indentPiece(this.intro);
|
|
899
|
+
let charIndex = 0;
|
|
900
|
+
let chunk = this.firstChunk;
|
|
901
|
+
const indentAt = (index) => {
|
|
902
|
+
shouldIndentNextCharacter = false;
|
|
903
|
+
if (index === chunk.start) chunk.appendRight(resolvedIndentStr);
|
|
904
|
+
else {
|
|
905
|
+
this._splitChunk(chunk, index);
|
|
906
|
+
chunk = chunk.next;
|
|
907
|
+
chunk.prependRight(resolvedIndentStr);
|
|
908
|
+
}
|
|
909
|
+
};
|
|
910
|
+
while (chunk) {
|
|
911
|
+
const end = chunk.end;
|
|
912
|
+
if (!isExcluded[chunk.start]) chunk.intro = indentPiece(chunk.intro);
|
|
913
|
+
if (chunk.edited) {
|
|
914
|
+
if (!isExcluded[charIndex]) chunk.content = indentPiece(chunk.content);
|
|
915
|
+
} else if (options.exclude) {
|
|
916
|
+
charIndex = chunk.start;
|
|
917
|
+
while (charIndex < end) {
|
|
918
|
+
if (!isExcluded[charIndex]) {
|
|
919
|
+
const char = this.original.charCodeAt(charIndex);
|
|
920
|
+
if (char === NEWLINE_CHAR) shouldIndentNextCharacter = true;
|
|
921
|
+
else if (char !== CR_CHAR && shouldIndentNextCharacter) indentAt(charIndex);
|
|
922
|
+
}
|
|
923
|
+
charIndex += 1;
|
|
924
|
+
}
|
|
925
|
+
} else {
|
|
926
|
+
charIndex = chunk.start;
|
|
927
|
+
while (charIndex < end) {
|
|
928
|
+
if (!shouldIndentNextCharacter) {
|
|
929
|
+
const nextLine = this.original.indexOf(n, charIndex);
|
|
930
|
+
if (nextLine === -1 || nextLine >= end) break;
|
|
931
|
+
shouldIndentNextCharacter = true;
|
|
932
|
+
charIndex = nextLine + 1;
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
const char = this.original.charCodeAt(charIndex);
|
|
936
|
+
if (char === NEWLINE_CHAR || char === CR_CHAR) {
|
|
937
|
+
charIndex += 1;
|
|
938
|
+
continue;
|
|
939
|
+
}
|
|
940
|
+
indentAt(charIndex);
|
|
941
|
+
charIndex += 1;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
if (!isExcluded[chunk.end - 1]) chunk.outro = indentPiece(chunk.outro);
|
|
945
|
+
charIndex = chunk.end;
|
|
946
|
+
chunk = chunk.next;
|
|
947
|
+
}
|
|
948
|
+
this.outro = indentPiece(this.outro);
|
|
949
|
+
return this;
|
|
950
|
+
}
|
|
951
|
+
/** @internal */
|
|
952
|
+
insert() {
|
|
953
|
+
throw new MagicStringError("insert() is deprecated, use appendLeft() or prependRight()");
|
|
954
|
+
}
|
|
955
|
+
/** @internal */
|
|
956
|
+
insertLeft(index, content) {
|
|
957
|
+
if (!warned.insertLeft) {
|
|
958
|
+
console.warn("magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead");
|
|
959
|
+
warned.insertLeft = true;
|
|
960
|
+
}
|
|
961
|
+
return this.appendLeft(index, content);
|
|
962
|
+
}
|
|
963
|
+
/** @internal */
|
|
964
|
+
insertRight(index, content) {
|
|
965
|
+
if (!warned.insertRight) {
|
|
966
|
+
console.warn("magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead");
|
|
967
|
+
warned.insertRight = true;
|
|
968
|
+
}
|
|
969
|
+
return this.prependRight(index, content);
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Moves the characters from `start` and `end` to `index`.
|
|
973
|
+
*
|
|
974
|
+
* `affinity` controls where the range is anchored at `index`. With the
|
|
975
|
+
* default `'right'`, it is inserted before the content that starts at `index`;
|
|
976
|
+
* with `'left'`, it is inserted after the content that ends at `index`. The
|
|
977
|
+
* two differ only when other content has already been moved to that boundary,
|
|
978
|
+
* mirroring the `appendLeft`/`appendRight` distinction.
|
|
979
|
+
*/
|
|
980
|
+
move(start, end, index, affinity = "right") {
|
|
981
|
+
start = start + this.offset;
|
|
982
|
+
end = end + this.offset;
|
|
983
|
+
index = index + this.offset;
|
|
984
|
+
if (start === end) return this;
|
|
985
|
+
if (index >= start && index <= end) throw new MagicStringError("cannot move a selection inside itself");
|
|
986
|
+
this._split(start);
|
|
987
|
+
this._split(end);
|
|
988
|
+
this._split(index);
|
|
989
|
+
const first = this.byStart.get(start);
|
|
990
|
+
const last = this.byEnd.get(end);
|
|
991
|
+
if (this.hasMovedChunks) {
|
|
992
|
+
let cursor = first;
|
|
993
|
+
while (cursor !== last) {
|
|
994
|
+
cursor = cursor.next;
|
|
995
|
+
if (!cursor || cursor.start < start || cursor.end > end) throw new MagicStringError(`cannot move ${start} to ${end} because an earlier move split that range`);
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
const oldLeft = first.previous;
|
|
999
|
+
const oldRight = last.next;
|
|
1000
|
+
let newLeft;
|
|
1001
|
+
let newRight;
|
|
1002
|
+
if (affinity === "left") {
|
|
1003
|
+
newLeft = this.byEnd.get(index) ?? null;
|
|
1004
|
+
if (!newLeft) {
|
|
1005
|
+
if (first === this.firstChunk) return this;
|
|
1006
|
+
newRight = this.firstChunk;
|
|
1007
|
+
} else {
|
|
1008
|
+
if (newLeft.next === first) return this;
|
|
1009
|
+
newRight = newLeft.next;
|
|
1010
|
+
}
|
|
1011
|
+
} else {
|
|
1012
|
+
newRight = this.byStart.get(index) ?? null;
|
|
1013
|
+
if (!newRight) {
|
|
1014
|
+
if (last === this.lastChunk) return this;
|
|
1015
|
+
newLeft = this.lastChunk;
|
|
1016
|
+
} else {
|
|
1017
|
+
if (newRight.previous === last) return this;
|
|
1018
|
+
newLeft = newRight.previous;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
if (oldLeft) oldLeft.next = oldRight;
|
|
1022
|
+
if (oldRight) oldRight.previous = oldLeft;
|
|
1023
|
+
if (newLeft) newLeft.next = first;
|
|
1024
|
+
if (newRight) newRight.previous = last;
|
|
1025
|
+
if (!first.previous) this.firstChunk = last.next;
|
|
1026
|
+
if (!last.next) {
|
|
1027
|
+
this.lastChunk = first.previous;
|
|
1028
|
+
this.lastChunk.next = null;
|
|
1029
|
+
}
|
|
1030
|
+
first.previous = newLeft;
|
|
1031
|
+
last.next = newRight || null;
|
|
1032
|
+
if (!newLeft) this.firstChunk = first;
|
|
1033
|
+
if (!newRight) this.lastChunk = last;
|
|
1034
|
+
this.hasMovedChunks = true;
|
|
1035
|
+
return this;
|
|
1036
|
+
}
|
|
1037
|
+
/**
|
|
1038
|
+
* Replaces the characters from `start` to `end` with `content`, along with the appended/prepended content in
|
|
1039
|
+
* that range. The same restrictions as `s.remove()` apply.
|
|
1040
|
+
*
|
|
1041
|
+
* The fourth argument is optional. It can have a storeName property - if true, the original name will be stored
|
|
1042
|
+
* for later inclusion in a sourcemap's names array - and a contentOnly property which determines whether only
|
|
1043
|
+
* the content is overwritten, or anything that was appended/prepended to the range as well.
|
|
1044
|
+
*
|
|
1045
|
+
* It may be preferred to use `s.update(...)` instead if you wish to avoid overwriting the appended/prepended content.
|
|
1046
|
+
*/
|
|
1047
|
+
overwrite(start, end, content, options) {
|
|
1048
|
+
const optionObject = typeof options === "object" && options ? options : {};
|
|
1049
|
+
return this.update(start, end, content, {
|
|
1050
|
+
...optionObject,
|
|
1051
|
+
overwrite: !optionObject.contentOnly
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
* Replaces the characters from `start` to `end` with `content`. The same restrictions as `s.remove()` apply.
|
|
1056
|
+
*
|
|
1057
|
+
* The fourth argument is optional. It can have a storeName property - if true, the original name will be stored
|
|
1058
|
+
* for later inclusion in a sourcemap's names array - and an overwrite property which determines whether only
|
|
1059
|
+
* the content is overwritten, or anything that was appended/prepended to the range as well.
|
|
1060
|
+
*/
|
|
1061
|
+
update(start, end, content, options) {
|
|
1062
|
+
start = start + this.offset;
|
|
1063
|
+
end = end + this.offset;
|
|
1064
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
1065
|
+
if (this.original.length !== 0) {
|
|
1066
|
+
if (start < 0) start = Math.max(0, start + this.original.length);
|
|
1067
|
+
if (end < 0) end = Math.max(0, end + this.original.length);
|
|
1068
|
+
}
|
|
1069
|
+
if (start < 0) throw new MagicStringError(`start ${start} is out of bounds`);
|
|
1070
|
+
if (end > this.original.length) throw new MagicStringError(`end ${end} is out of bounds`);
|
|
1071
|
+
if (start === end) throw new MagicStringError(`cannot overwrite a zero-length range at ${start}, use appendLeft() or prependRight()`);
|
|
1072
|
+
if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
|
|
1073
|
+
this._split(start);
|
|
1074
|
+
this._split(end);
|
|
1075
|
+
if (options === true) {
|
|
1076
|
+
if (!warned.storeName) {
|
|
1077
|
+
console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string");
|
|
1078
|
+
warned.storeName = true;
|
|
1079
|
+
}
|
|
1080
|
+
options = { storeName: true };
|
|
1081
|
+
}
|
|
1082
|
+
const optionObject = typeof options === "object" && options ? options : {};
|
|
1083
|
+
const storeName = optionObject.storeName || false;
|
|
1084
|
+
const overwrite = optionObject.overwrite || false;
|
|
1085
|
+
if (storeName) {
|
|
1086
|
+
const original = this.original.slice(start, end);
|
|
1087
|
+
Object.defineProperty(this.storedNames, original, {
|
|
1088
|
+
writable: true,
|
|
1089
|
+
value: true,
|
|
1090
|
+
enumerable: true
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
const first = this.byStart.get(start);
|
|
1094
|
+
const last = this.byEnd.get(end);
|
|
1095
|
+
if (first) {
|
|
1096
|
+
let chunk = first;
|
|
1097
|
+
while (chunk !== last) {
|
|
1098
|
+
if (chunk.next !== this.byStart.get(chunk.end)) throw new MagicStringError("cannot overwrite across a split point");
|
|
1099
|
+
chunk = chunk.next;
|
|
1100
|
+
chunk.edit("", false);
|
|
1101
|
+
}
|
|
1102
|
+
first.edit(content, storeName, !overwrite);
|
|
1103
|
+
} else {
|
|
1104
|
+
const newChunk = new Chunk(start, end, "").edit(content, storeName);
|
|
1105
|
+
last.next = newChunk;
|
|
1106
|
+
newChunk.previous = last;
|
|
1107
|
+
}
|
|
1108
|
+
return this;
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Prepends the string with the specified content.
|
|
1112
|
+
*/
|
|
1113
|
+
prepend(content) {
|
|
1114
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
1115
|
+
this.intro = content + this.intro;
|
|
1116
|
+
return this;
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Same as `s.appendLeft(...)`, except that the inserted content will go *before* any previous appends or prepends at index
|
|
1120
|
+
*/
|
|
1121
|
+
prependLeft(index, content) {
|
|
1122
|
+
index = index + this.offset;
|
|
1123
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
1124
|
+
this._split(index);
|
|
1125
|
+
const chunk = this.byEnd.get(index);
|
|
1126
|
+
if (chunk) chunk.prependLeft(content);
|
|
1127
|
+
else this.intro = content + this.intro;
|
|
1128
|
+
return this;
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* Same as `s.appendRight(...)`, except that the inserted content will go *before* any previous appends or prepends at `index`
|
|
1132
|
+
*/
|
|
1133
|
+
prependRight(index, content) {
|
|
1134
|
+
index = index + this.offset;
|
|
1135
|
+
if (typeof content !== "string") throw new MagicStringError(`content must be a string, got ${typeof content}`);
|
|
1136
|
+
this._split(index);
|
|
1137
|
+
const chunk = this.byStart.get(index);
|
|
1138
|
+
if (chunk) chunk.prependRight(content);
|
|
1139
|
+
else this.outro = content + this.outro;
|
|
1140
|
+
return this;
|
|
1141
|
+
}
|
|
1142
|
+
/**
|
|
1143
|
+
* Removes the characters from `start` to `end` (of the original string, **not** the generated string).
|
|
1144
|
+
* Content appended or prepended at positions strictly inside the range is removed with it, while
|
|
1145
|
+
* content attached at `start` or `end` is preserved — use `s.overwrite(start, end, '')` to remove
|
|
1146
|
+
* the range including its edge inserts.
|
|
1147
|
+
* Repeated removals and removals with partially overlapping ranges are allowed.
|
|
1148
|
+
*/
|
|
1149
|
+
remove(start, end) {
|
|
1150
|
+
start = start + this.offset;
|
|
1151
|
+
end = end + this.offset;
|
|
1152
|
+
if (this.original.length !== 0) {
|
|
1153
|
+
if (start < 0) start = Math.max(0, start + this.original.length);
|
|
1154
|
+
if (end < 0) end = Math.max(0, end + this.original.length);
|
|
1155
|
+
}
|
|
1156
|
+
if (start === end) return this;
|
|
1157
|
+
if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}\u2013${end} is out of bounds`);
|
|
1158
|
+
if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
|
|
1159
|
+
this._split(start);
|
|
1160
|
+
this._split(end);
|
|
1161
|
+
let chunk = this.byStart.get(start);
|
|
1162
|
+
while (chunk) {
|
|
1163
|
+
if (chunk.start > start) chunk.intro = "";
|
|
1164
|
+
if (chunk.end < end) chunk.outro = "";
|
|
1165
|
+
chunk.edit("", false, true);
|
|
1166
|
+
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
|
|
1167
|
+
}
|
|
1168
|
+
return this;
|
|
1169
|
+
}
|
|
1170
|
+
/**
|
|
1171
|
+
* Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).
|
|
1172
|
+
*/
|
|
1173
|
+
reset(start, end) {
|
|
1174
|
+
start = start + this.offset;
|
|
1175
|
+
end = end + this.offset;
|
|
1176
|
+
if (this.original.length !== 0) {
|
|
1177
|
+
if (start < 0) start = Math.max(0, start + this.original.length);
|
|
1178
|
+
if (end < 0) end = Math.max(0, end + this.original.length);
|
|
1179
|
+
}
|
|
1180
|
+
if (start === end) return this;
|
|
1181
|
+
if (start < 0 || end > this.original.length) throw new MagicStringError(`range ${start}\u2013${end} is out of bounds`);
|
|
1182
|
+
if (start > end) throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`);
|
|
1183
|
+
this._split(start);
|
|
1184
|
+
this._split(end);
|
|
1185
|
+
let chunk = this.byStart.get(start);
|
|
1186
|
+
while (chunk) {
|
|
1187
|
+
chunk.reset();
|
|
1188
|
+
chunk = end > chunk.end ? this.byStart.get(chunk.end) : null;
|
|
1189
|
+
}
|
|
1190
|
+
return this;
|
|
1191
|
+
}
|
|
1192
|
+
lastChar() {
|
|
1193
|
+
if (this.outro.length) return this.outro[this.outro.length - 1];
|
|
1194
|
+
let chunk = this.lastChunk;
|
|
1195
|
+
while (chunk) {
|
|
1196
|
+
if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
|
|
1197
|
+
if (chunk.content.length) return chunk.content[chunk.content.length - 1];
|
|
1198
|
+
if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
|
|
1199
|
+
chunk = chunk.previous;
|
|
1200
|
+
}
|
|
1201
|
+
if (this.intro.length) return this.intro[this.intro.length - 1];
|
|
1202
|
+
return "";
|
|
1203
|
+
}
|
|
1204
|
+
lastLine() {
|
|
1205
|
+
let lineIndex = this.outro.lastIndexOf(n);
|
|
1206
|
+
if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
|
|
1207
|
+
let lineStr = this.outro;
|
|
1208
|
+
let chunk = this.lastChunk;
|
|
1209
|
+
while (chunk) {
|
|
1210
|
+
if (chunk.outro.length > 0) {
|
|
1211
|
+
lineIndex = chunk.outro.lastIndexOf(n);
|
|
1212
|
+
if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
|
|
1213
|
+
lineStr = chunk.outro + lineStr;
|
|
1214
|
+
}
|
|
1215
|
+
if (chunk.content.length > 0) {
|
|
1216
|
+
lineIndex = chunk.content.lastIndexOf(n);
|
|
1217
|
+
if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
|
|
1218
|
+
lineStr = chunk.content + lineStr;
|
|
1219
|
+
}
|
|
1220
|
+
if (chunk.intro.length > 0) {
|
|
1221
|
+
lineIndex = chunk.intro.lastIndexOf(n);
|
|
1222
|
+
if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
|
|
1223
|
+
lineStr = chunk.intro + lineStr;
|
|
1224
|
+
}
|
|
1225
|
+
chunk = chunk.previous;
|
|
1226
|
+
}
|
|
1227
|
+
lineIndex = this.intro.lastIndexOf(n);
|
|
1228
|
+
if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
|
|
1229
|
+
return this.intro + lineStr;
|
|
1230
|
+
}
|
|
1231
|
+
/**
|
|
1232
|
+
* Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string.
|
|
1233
|
+
* Throws error if the indices are for characters that were already removed.
|
|
1234
|
+
*/
|
|
1235
|
+
slice(start = 0, end = this.original.length - this.offset) {
|
|
1236
|
+
start = start + this.offset;
|
|
1237
|
+
end = end + this.offset;
|
|
1238
|
+
if (this.original.length !== 0) {
|
|
1239
|
+
if (start < 0) start = Math.max(0, start + this.original.length);
|
|
1240
|
+
if (end < 0) end = Math.max(0, end + this.original.length);
|
|
1241
|
+
}
|
|
1242
|
+
let result = "";
|
|
1243
|
+
let chunk = this.firstChunk;
|
|
1244
|
+
while (chunk && (chunk.start > start || chunk.end <= start)) {
|
|
1245
|
+
if (chunk.start < end && chunk.end >= end || end === 0 && chunk.start === 0) return result;
|
|
1246
|
+
chunk = chunk.next;
|
|
1247
|
+
}
|
|
1248
|
+
if (chunk && chunk.edited && chunk.start !== start) throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`);
|
|
1249
|
+
const startChunk = chunk;
|
|
1250
|
+
while (chunk) {
|
|
1251
|
+
if (end === 0 && chunk.start === 0) break;
|
|
1252
|
+
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) result += chunk.intro;
|
|
1253
|
+
const containsEnd = chunk.start < end && chunk.end >= end;
|
|
1254
|
+
if (containsEnd && chunk.edited && chunk.end !== end) throw new MagicStringError(`cannot use edited character ${end} as slice end anchor`);
|
|
1255
|
+
const sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
|
1256
|
+
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
|
1257
|
+
result += chunk.content.slice(sliceStart, sliceEnd);
|
|
1258
|
+
if (chunk.outro && (!containsEnd || chunk.end === end)) result += chunk.outro;
|
|
1259
|
+
if (containsEnd) break;
|
|
1260
|
+
chunk = chunk.next;
|
|
1261
|
+
}
|
|
1262
|
+
return result;
|
|
1263
|
+
}
|
|
1264
|
+
/**
|
|
1265
|
+
* Returns a clone of `s`, with all content before the `start` and `end` characters of the original string removed.
|
|
1266
|
+
*/
|
|
1267
|
+
snip(start, end) {
|
|
1268
|
+
const clone = this.clone();
|
|
1269
|
+
clone.remove(0, start);
|
|
1270
|
+
clone.remove(end, clone.original.length);
|
|
1271
|
+
return clone;
|
|
1272
|
+
}
|
|
1273
|
+
/** @internal */
|
|
1274
|
+
_split(index) {
|
|
1275
|
+
if (this.byStart.get(index) || this.byEnd.get(index)) return;
|
|
1276
|
+
let chunk = this.lastSearchedChunk;
|
|
1277
|
+
let previousChunk = chunk;
|
|
1278
|
+
const searchForward = index > chunk.end;
|
|
1279
|
+
while (chunk) {
|
|
1280
|
+
if (chunk.contains(index)) return this._splitChunk(chunk, index);
|
|
1281
|
+
chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
|
|
1282
|
+
if (chunk === previousChunk) return;
|
|
1283
|
+
previousChunk = chunk;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
/** @internal */
|
|
1287
|
+
_splitChunk(chunk, index) {
|
|
1288
|
+
if (chunk.edited && chunk.content.length) {
|
|
1289
|
+
const loc = getLocator(this.original)(index);
|
|
1290
|
+
throw new MagicStringError(`cannot split a chunk that has already been edited (${loc.line}:${loc.column} \u2013 "${chunk.original}")`);
|
|
1291
|
+
}
|
|
1292
|
+
const newChunk = chunk.split(index);
|
|
1293
|
+
this.byEnd.set(index, chunk);
|
|
1294
|
+
this.byStart.set(index, newChunk);
|
|
1295
|
+
this.byEnd.set(newChunk.end, newChunk);
|
|
1296
|
+
if (chunk === this.lastChunk) this.lastChunk = newChunk;
|
|
1297
|
+
this.lastSearchedChunk = chunk;
|
|
1298
|
+
return true;
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Returns the generated string.
|
|
1302
|
+
*/
|
|
1303
|
+
toString() {
|
|
1304
|
+
let str = this.intro;
|
|
1305
|
+
let chunk = this.firstChunk;
|
|
1306
|
+
while (chunk) {
|
|
1307
|
+
str += chunk.toString();
|
|
1308
|
+
chunk = chunk.next;
|
|
1309
|
+
}
|
|
1310
|
+
return str + this.outro;
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
* Returns true if the resulting source is empty (disregarding white space).
|
|
1314
|
+
*/
|
|
1315
|
+
isEmpty() {
|
|
1316
|
+
if (this.intro.length && this.intro.trim()) return false;
|
|
1317
|
+
let chunk = this.firstChunk;
|
|
1318
|
+
while (chunk) {
|
|
1319
|
+
if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim()) return false;
|
|
1320
|
+
chunk = chunk.next;
|
|
1321
|
+
}
|
|
1322
|
+
if (this.outro.length && this.outro.trim()) return false;
|
|
1323
|
+
return true;
|
|
1324
|
+
}
|
|
1325
|
+
length() {
|
|
1326
|
+
let chunk = this.firstChunk;
|
|
1327
|
+
let length = 0;
|
|
1328
|
+
while (chunk) {
|
|
1329
|
+
length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
|
1330
|
+
chunk = chunk.next;
|
|
1331
|
+
}
|
|
1332
|
+
return length;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* Removes empty lines from the start and end.
|
|
1336
|
+
*/
|
|
1337
|
+
trimLines() {
|
|
1338
|
+
return this.trim("[\\r\\n]");
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start and end.
|
|
1342
|
+
*/
|
|
1343
|
+
trim(charType) {
|
|
1344
|
+
return this.trimStart(charType).trimEnd(charType);
|
|
1345
|
+
}
|
|
1346
|
+
/** @internal */
|
|
1347
|
+
trimEndAborted(charType) {
|
|
1348
|
+
const rx = new RegExp(`${charType || "\\s"}+$`);
|
|
1349
|
+
this.outro = this.outro.replace(rx, "");
|
|
1350
|
+
if (this.outro.length) return true;
|
|
1351
|
+
let chunk = this.lastChunk;
|
|
1352
|
+
do {
|
|
1353
|
+
const end = chunk.end;
|
|
1354
|
+
const aborted = chunk.trimEnd(rx);
|
|
1355
|
+
if (chunk.end !== end) {
|
|
1356
|
+
if (this.lastChunk === chunk) this.lastChunk = chunk.next;
|
|
1357
|
+
this.byEnd.set(chunk.end, chunk);
|
|
1358
|
+
this.byStart.set(chunk.next.start, chunk.next);
|
|
1359
|
+
this.byEnd.set(chunk.next.end, chunk.next);
|
|
1360
|
+
}
|
|
1361
|
+
if (aborted) return true;
|
|
1362
|
+
chunk = chunk.previous;
|
|
1363
|
+
} while (chunk);
|
|
1364
|
+
this.intro = this.intro.replace(rx, "");
|
|
1365
|
+
return this.intro.length > 0;
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the end.
|
|
1369
|
+
*/
|
|
1370
|
+
trimEnd(charType) {
|
|
1371
|
+
this.trimEndAborted(charType);
|
|
1372
|
+
return this;
|
|
1373
|
+
}
|
|
1374
|
+
/** @internal */
|
|
1375
|
+
trimStartAborted(charType) {
|
|
1376
|
+
const rx = new RegExp(`^${charType || "\\s"}+`);
|
|
1377
|
+
this.intro = this.intro.replace(rx, "");
|
|
1378
|
+
if (this.intro.length) return true;
|
|
1379
|
+
let chunk = this.firstChunk;
|
|
1380
|
+
do {
|
|
1381
|
+
const end = chunk.end;
|
|
1382
|
+
const aborted = chunk.trimStart(rx);
|
|
1383
|
+
if (chunk.end !== end) {
|
|
1384
|
+
if (chunk === this.lastChunk) this.lastChunk = chunk.next;
|
|
1385
|
+
this.byEnd.set(chunk.end, chunk);
|
|
1386
|
+
this.byStart.set(chunk.next.start, chunk.next);
|
|
1387
|
+
this.byEnd.set(chunk.next.end, chunk.next);
|
|
1388
|
+
}
|
|
1389
|
+
if (aborted) return true;
|
|
1390
|
+
chunk = chunk.next;
|
|
1391
|
+
} while (chunk);
|
|
1392
|
+
this.outro = this.outro.replace(rx, "");
|
|
1393
|
+
return this.outro.length > 0;
|
|
1394
|
+
}
|
|
1395
|
+
/**
|
|
1396
|
+
* Trims content matching `charType` (defaults to `\s`, i.e. whitespace) from the start.
|
|
1397
|
+
*/
|
|
1398
|
+
trimStart(charType) {
|
|
1399
|
+
this.trimStartAborted(charType);
|
|
1400
|
+
return this;
|
|
1401
|
+
}
|
|
1402
|
+
/**
|
|
1403
|
+
* Indicates if the string has been changed.
|
|
1404
|
+
*/
|
|
1405
|
+
hasChanged() {
|
|
1406
|
+
if (this.intro || this.outro) return true;
|
|
1407
|
+
let outputIndex = 0;
|
|
1408
|
+
let chunk = this.firstChunk;
|
|
1409
|
+
while (chunk) {
|
|
1410
|
+
if (chunk.intro || chunk.outro) return true;
|
|
1411
|
+
if (!chunk.edited && chunk.start === outputIndex) outputIndex = chunk.end;
|
|
1412
|
+
else {
|
|
1413
|
+
if (!this.original.startsWith(chunk.content, outputIndex)) return true;
|
|
1414
|
+
outputIndex += chunk.content.length;
|
|
1415
|
+
}
|
|
1416
|
+
chunk = chunk.next;
|
|
1417
|
+
}
|
|
1418
|
+
return outputIndex !== this.original.length;
|
|
1419
|
+
}
|
|
1420
|
+
/**
|
|
1421
|
+
* Whether the original range [start, end) has had any of its characters
|
|
1422
|
+
* removed. `replace`/`replaceAll` search `original`, so a match can land on
|
|
1423
|
+
* text that is no longer in the output - overwriting it would resurrect the
|
|
1424
|
+
* removed characters, so such matches are skipped instead.
|
|
1425
|
+
*
|
|
1426
|
+
* @internal
|
|
1427
|
+
*/
|
|
1428
|
+
_hasRemovedContent(start, end) {
|
|
1429
|
+
let chunk = this.byStart.get(start);
|
|
1430
|
+
if (!chunk) {
|
|
1431
|
+
chunk = this.lastSearchedChunk;
|
|
1432
|
+
const searchForward = start >= chunk.end;
|
|
1433
|
+
while (chunk && !chunk.contains(start)) chunk = searchForward ? this.byStart.get(chunk.end) : this.byEnd.get(chunk.start);
|
|
1434
|
+
if (chunk) this.lastSearchedChunk = chunk;
|
|
1435
|
+
}
|
|
1436
|
+
while (chunk && chunk.start < end) {
|
|
1437
|
+
if (chunk.content === "") return true;
|
|
1438
|
+
chunk = this.byStart.get(chunk.end);
|
|
1439
|
+
}
|
|
1440
|
+
return false;
|
|
1441
|
+
}
|
|
1442
|
+
/** @internal */
|
|
1443
|
+
_replaceRegexp(searchValue, replacement) {
|
|
1444
|
+
function getReplacement(match, str) {
|
|
1445
|
+
if (typeof replacement === "string") return expandReplacement(replacement, match[0], match.index, str, match.slice(1), match.groups);
|
|
1446
|
+
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);
|
|
1447
|
+
}
|
|
1448
|
+
const replaceMatch = (match) => {
|
|
1449
|
+
if (match.index == null) return false;
|
|
1450
|
+
if (this._hasRemovedContent(match.index, match.index + match[0].length)) return false;
|
|
1451
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1452
|
+
if (replacement2 === match[0]) return true;
|
|
1453
|
+
if (match[0].length === 0) this.appendRight(match.index, replacement2);
|
|
1454
|
+
else this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1455
|
+
return true;
|
|
1456
|
+
};
|
|
1457
|
+
if (searchValue.global) {
|
|
1458
|
+
searchValue.lastIndex = 0;
|
|
1459
|
+
for (const match of this.original.matchAll(searchValue)) replaceMatch(match);
|
|
1460
|
+
} else {
|
|
1461
|
+
const match = this.original.match(searchValue);
|
|
1462
|
+
if (match && !replaceMatch(match)) {
|
|
1463
|
+
const global = new RegExp(searchValue.source, `${searchValue.flags}g`);
|
|
1464
|
+
for (const next of this.original.matchAll(global)) if (replaceMatch(next)) break;
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return this;
|
|
1468
|
+
}
|
|
1469
|
+
/** @internal */
|
|
1470
|
+
_replaceString(string, replacement) {
|
|
1471
|
+
const { original } = this;
|
|
1472
|
+
let index = original.indexOf(string);
|
|
1473
|
+
while (index !== -1) {
|
|
1474
|
+
if (this._hasRemovedContent(index, index + string.length)) {
|
|
1475
|
+
index = original.indexOf(string, index + string.length);
|
|
1476
|
+
continue;
|
|
1477
|
+
}
|
|
1478
|
+
if (typeof replacement === "function") replacement = replacement(string, index, original);
|
|
1479
|
+
else replacement = expandReplacement(replacement, string, index, original, [], void 0);
|
|
1480
|
+
if (string !== replacement) {
|
|
1481
|
+
if (string.length === 0) this.appendRight(index, replacement);
|
|
1482
|
+
else this.overwrite(index, index + string.length, replacement);
|
|
1483
|
+
}
|
|
1484
|
+
break;
|
|
1485
|
+
}
|
|
1486
|
+
return this;
|
|
1487
|
+
}
|
|
1488
|
+
/**
|
|
1489
|
+
* String replacement with RegExp or string.
|
|
1490
|
+
*/
|
|
1491
|
+
replace(searchValue, replacement) {
|
|
1492
|
+
if (typeof searchValue === "string") return this._replaceString(searchValue, replacement);
|
|
1493
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1494
|
+
}
|
|
1495
|
+
/** @internal */
|
|
1496
|
+
_replaceAllString(string, replacement) {
|
|
1497
|
+
const { original } = this;
|
|
1498
|
+
const stringLength = string.length;
|
|
1499
|
+
if (stringLength === 0) {
|
|
1500
|
+
for (let index = 0; index <= original.length; index += 1) {
|
|
1501
|
+
if (this._hasRemovedContent(index, index)) continue;
|
|
1502
|
+
const _replacement = typeof replacement === "function" ? replacement("", index, original) : expandReplacement(replacement, "", index, original, [], void 0);
|
|
1503
|
+
if (_replacement !== "") this.appendRight(index, _replacement);
|
|
1504
|
+
}
|
|
1505
|
+
return this;
|
|
1506
|
+
}
|
|
1507
|
+
for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
|
|
1508
|
+
if (this._hasRemovedContent(index, index + stringLength)) continue;
|
|
1509
|
+
const previous = original.slice(index, index + stringLength);
|
|
1510
|
+
const _replacement = typeof replacement === "function" ? replacement(previous, index, original) : expandReplacement(replacement, previous, index, original, [], void 0);
|
|
1511
|
+
if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);
|
|
1512
|
+
}
|
|
1513
|
+
return this;
|
|
1514
|
+
}
|
|
1515
|
+
/**
|
|
1516
|
+
* Same as `s.replace`, but replace all matched strings instead of just one.
|
|
1517
|
+
*/
|
|
1518
|
+
replaceAll(searchValue, replacement) {
|
|
1519
|
+
if (typeof searchValue === "string") return this._replaceAllString(searchValue, replacement);
|
|
1520
|
+
if (!searchValue.global) throw new MagicStringError("replaceAll() requires a global RegExp");
|
|
1521
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
// ../source/src/transform.ts
|
|
1526
|
+
var SOURCE_ATTR = "data-bh-src";
|
|
1527
|
+
function walk(node, visit) {
|
|
1528
|
+
if (!node || typeof node !== "object") return;
|
|
1529
|
+
if (typeof node.type === "string" || typeof node.type === "number") visit(node);
|
|
1530
|
+
for (const key of Object.keys(node)) {
|
|
1531
|
+
if (key === "loc" || key === "start" || key === "end" || key === "parent") continue;
|
|
1532
|
+
const value = node[key];
|
|
1533
|
+
if (Array.isArray(value)) for (const child of value) walk(child, visit);
|
|
1534
|
+
else if (value && typeof value === "object") walk(value, visit);
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
function attr(file, line, column) {
|
|
1538
|
+
return ` ${SOURCE_ATTR}="${file}:${line}:${column}"`;
|
|
1539
|
+
}
|
|
1540
|
+
function jsx(code, file, typescript) {
|
|
1541
|
+
if (code.includes("@react-three/fiber")) return null;
|
|
1542
|
+
let ast;
|
|
1543
|
+
try {
|
|
1544
|
+
ast = (0, import_parser.parse)(code, {
|
|
1545
|
+
sourceType: "module",
|
|
1546
|
+
errorRecovery: true,
|
|
1547
|
+
plugins: typescript ? ["jsx", "typescript", "decorators-legacy"] : ["jsx"]
|
|
1548
|
+
});
|
|
1549
|
+
} catch {
|
|
1550
|
+
return null;
|
|
1551
|
+
}
|
|
1552
|
+
const s = new MagicString(code);
|
|
1553
|
+
let changed = false;
|
|
1554
|
+
walk(ast.program, (n2) => {
|
|
1555
|
+
if (n2.type !== "JSXOpeningElement" || n2.name?.type !== "JSXIdentifier") return;
|
|
1556
|
+
if (!/^[a-z]/.test(n2.name.name)) return;
|
|
1557
|
+
if (n2.attributes.some((a) => a.name?.name === SOURCE_ATTR)) return;
|
|
1558
|
+
s.appendLeft(n2.name.end, attr(file, n2.loc.start.line, n2.loc.start.column + 1));
|
|
1559
|
+
changed = true;
|
|
1560
|
+
});
|
|
1561
|
+
return changed ? s : null;
|
|
1562
|
+
}
|
|
1563
|
+
function projectRequire(root) {
|
|
1564
|
+
return (0, import_node_module.createRequire)((0, import_node_path.join)(root, "package.json"));
|
|
1565
|
+
}
|
|
1566
|
+
function vue(code, file, root) {
|
|
1567
|
+
let compiler;
|
|
1568
|
+
try {
|
|
1569
|
+
compiler = projectRequire(root)("vue/compiler-sfc");
|
|
1570
|
+
} catch {
|
|
1571
|
+
return null;
|
|
1572
|
+
}
|
|
1573
|
+
const { descriptor } = compiler.parse(code, { filename: file });
|
|
1574
|
+
const template = descriptor.template;
|
|
1575
|
+
if (!template?.ast || template.lang && template.lang !== "html") return null;
|
|
1576
|
+
const s = new MagicString(code);
|
|
1577
|
+
let changed = false;
|
|
1578
|
+
walk(template.ast, (n2) => {
|
|
1579
|
+
if (n2.type !== 1 || n2.tagType !== 0) return;
|
|
1580
|
+
if (n2.props?.some((p) => p.name === SOURCE_ATTR)) return;
|
|
1581
|
+
const at = n2.loc.start.offset + 1 + n2.tag.length;
|
|
1582
|
+
s.appendLeft(at, attr(file, n2.loc.start.line, n2.loc.start.column));
|
|
1583
|
+
changed = true;
|
|
1584
|
+
});
|
|
1585
|
+
return changed ? s : null;
|
|
1586
|
+
}
|
|
1587
|
+
function lineColumn(code, offset) {
|
|
1588
|
+
let line = 1;
|
|
1589
|
+
let last = -1;
|
|
1590
|
+
for (let i = code.indexOf("\n"); i !== -1 && i < offset; i = code.indexOf("\n", i + 1)) {
|
|
1591
|
+
line++;
|
|
1592
|
+
last = i;
|
|
1593
|
+
}
|
|
1594
|
+
return [line, offset - last];
|
|
1595
|
+
}
|
|
1596
|
+
function svelte(code, file, root) {
|
|
1597
|
+
let compiler;
|
|
1598
|
+
try {
|
|
1599
|
+
compiler = projectRequire(root)("svelte/compiler");
|
|
1600
|
+
} catch {
|
|
1601
|
+
return null;
|
|
1602
|
+
}
|
|
1603
|
+
let ast;
|
|
1604
|
+
try {
|
|
1605
|
+
ast = compiler.parse(code, { modern: true });
|
|
1606
|
+
} catch {
|
|
1607
|
+
return null;
|
|
1608
|
+
}
|
|
1609
|
+
const s = new MagicString(code);
|
|
1610
|
+
let changed = false;
|
|
1611
|
+
walk(ast.fragment, (n2) => {
|
|
1612
|
+
if (n2.type !== "RegularElement" && n2.type !== "SvelteElement") return;
|
|
1613
|
+
if (n2.type === "SvelteElement") return;
|
|
1614
|
+
if (n2.attributes?.some((a) => a.name === SOURCE_ATTR)) return;
|
|
1615
|
+
const [line, column] = lineColumn(code, n2.start);
|
|
1616
|
+
s.appendLeft(n2.start + 1 + n2.name.length, attr(file, line, column));
|
|
1617
|
+
changed = true;
|
|
1618
|
+
});
|
|
1619
|
+
return changed ? s : null;
|
|
1620
|
+
}
|
|
1621
|
+
function wantsFile(id) {
|
|
1622
|
+
if (id.includes("\0") || id.includes("?") || /[\\/]node_modules[\\/]/.test(id)) return false;
|
|
1623
|
+
return /\.(jsx|tsx|vue|svelte)$/.test(id);
|
|
1624
|
+
}
|
|
1625
|
+
function addSourceAttributes(code, id, root) {
|
|
1626
|
+
if (!wantsFile(id)) return null;
|
|
1627
|
+
const file = (0, import_node_path.relative)(root, id).split(import_node_path.sep).join("/");
|
|
1628
|
+
if (file.startsWith("..")) return null;
|
|
1629
|
+
const ext = (0, import_node_path.extname)(id);
|
|
1630
|
+
const s = ext === ".vue" ? vue(code, file, root) : ext === ".svelte" ? svelte(code, file, root) : jsx(code, file, ext === ".tsx");
|
|
1631
|
+
if (!s) return null;
|
|
1632
|
+
return {
|
|
1633
|
+
code: s.toString(),
|
|
1634
|
+
map: s.generateMap({ source: id, includeContent: true, hires: true })
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
// ../source/src/webpack-loader.ts
|
|
1639
|
+
function loader(source, map) {
|
|
1640
|
+
const root = this.getOptions?.()?.root ?? this.rootContext ?? process.cwd();
|
|
1641
|
+
const result = addSourceAttributes(source, this.resourcePath, root);
|
|
1642
|
+
if (!result) {
|
|
1643
|
+
this.callback(null, source, map);
|
|
1644
|
+
return;
|
|
1645
|
+
}
|
|
1646
|
+
this.callback(null, result.code, result.map);
|
|
1647
|
+
}
|
|
1648
|
+
|
|
1649
|
+
// src/source-loader.ts
|
|
1650
|
+
module.exports = loader;
|