vite-plugin-ai-annotator 1.0.2 → 1.1.0
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/README.md +42 -42
- package/dist/annotator/inspection.d.ts +8 -1
- package/dist/annotator/selection.d.ts +5 -2
- package/dist/annotator-toolbar.d.ts +10 -2
- package/dist/annotator-toolbar.js +354 -198
- package/dist/index.cjs +82 -62
- package/dist/mcp-stdio.d.ts +15 -0
- package/dist/rpc/define.d.ts +1 -1
- package/dist/utils/logger.d.ts +4 -4
- package/dist/vite-plugin.d.ts +6 -0
- package/dist/vite-plugin.js +1170 -5
- package/dist/vite-plugin.js.map +4 -4
- package/package.json +4 -3
package/dist/vite-plugin.js
CHANGED
|
@@ -1,8 +1,1155 @@
|
|
|
1
1
|
// src/vite-plugin.ts
|
|
2
2
|
import { spawn } from "node:child_process";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
4
|
+
import { dirname, join, relative } from "node:path";
|
|
5
5
|
import { existsSync } from "node:fs";
|
|
6
|
+
|
|
7
|
+
// node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
8
|
+
var comma = ",".charCodeAt(0);
|
|
9
|
+
var semicolon = ";".charCodeAt(0);
|
|
10
|
+
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
11
|
+
var intToChar = new Uint8Array(64);
|
|
12
|
+
var charToInt = new Uint8Array(128);
|
|
13
|
+
for (let i = 0; i < chars.length; i++) {
|
|
14
|
+
const c = chars.charCodeAt(i);
|
|
15
|
+
intToChar[i] = c;
|
|
16
|
+
charToInt[c] = i;
|
|
17
|
+
}
|
|
18
|
+
function encodeInteger(builder, num, relative2) {
|
|
19
|
+
let delta = num - relative2;
|
|
20
|
+
delta = delta < 0 ? -delta << 1 | 1 : delta << 1;
|
|
21
|
+
do {
|
|
22
|
+
let clamped = delta & 31;
|
|
23
|
+
delta >>>= 5;
|
|
24
|
+
if (delta > 0) clamped |= 32;
|
|
25
|
+
builder.write(intToChar[clamped]);
|
|
26
|
+
} while (delta > 0);
|
|
27
|
+
return num;
|
|
28
|
+
}
|
|
29
|
+
var bufLength = 1024 * 16;
|
|
30
|
+
var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
|
|
31
|
+
decode(buf) {
|
|
32
|
+
const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
33
|
+
return out.toString();
|
|
34
|
+
}
|
|
35
|
+
} : {
|
|
36
|
+
decode(buf) {
|
|
37
|
+
let out = "";
|
|
38
|
+
for (let i = 0; i < buf.length; i++) {
|
|
39
|
+
out += String.fromCharCode(buf[i]);
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var StringWriter = class {
|
|
45
|
+
constructor() {
|
|
46
|
+
this.pos = 0;
|
|
47
|
+
this.out = "";
|
|
48
|
+
this.buffer = new Uint8Array(bufLength);
|
|
49
|
+
}
|
|
50
|
+
write(v) {
|
|
51
|
+
const { buffer } = this;
|
|
52
|
+
buffer[this.pos++] = v;
|
|
53
|
+
if (this.pos === bufLength) {
|
|
54
|
+
this.out += td.decode(buffer);
|
|
55
|
+
this.pos = 0;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
flush() {
|
|
59
|
+
const { buffer, out, pos } = this;
|
|
60
|
+
return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
function encode(decoded) {
|
|
64
|
+
const writer = new StringWriter();
|
|
65
|
+
let sourcesIndex = 0;
|
|
66
|
+
let sourceLine = 0;
|
|
67
|
+
let sourceColumn = 0;
|
|
68
|
+
let namesIndex = 0;
|
|
69
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
70
|
+
const line = decoded[i];
|
|
71
|
+
if (i > 0) writer.write(semicolon);
|
|
72
|
+
if (line.length === 0) continue;
|
|
73
|
+
let genColumn = 0;
|
|
74
|
+
for (let j = 0; j < line.length; j++) {
|
|
75
|
+
const segment = line[j];
|
|
76
|
+
if (j > 0) writer.write(comma);
|
|
77
|
+
genColumn = encodeInteger(writer, segment[0], genColumn);
|
|
78
|
+
if (segment.length === 1) continue;
|
|
79
|
+
sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
|
|
80
|
+
sourceLine = encodeInteger(writer, segment[2], sourceLine);
|
|
81
|
+
sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
|
|
82
|
+
if (segment.length === 4) continue;
|
|
83
|
+
namesIndex = encodeInteger(writer, segment[4], namesIndex);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return writer.flush();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// node_modules/magic-string/dist/magic-string.es.mjs
|
|
90
|
+
var BitSet = class _BitSet {
|
|
91
|
+
constructor(arg) {
|
|
92
|
+
this.bits = arg instanceof _BitSet ? arg.bits.slice() : [];
|
|
93
|
+
}
|
|
94
|
+
add(n2) {
|
|
95
|
+
this.bits[n2 >> 5] |= 1 << (n2 & 31);
|
|
96
|
+
}
|
|
97
|
+
has(n2) {
|
|
98
|
+
return !!(this.bits[n2 >> 5] & 1 << (n2 & 31));
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
var Chunk = class _Chunk {
|
|
102
|
+
constructor(start, end, content) {
|
|
103
|
+
this.start = start;
|
|
104
|
+
this.end = end;
|
|
105
|
+
this.original = content;
|
|
106
|
+
this.intro = "";
|
|
107
|
+
this.outro = "";
|
|
108
|
+
this.content = content;
|
|
109
|
+
this.storeName = false;
|
|
110
|
+
this.edited = false;
|
|
111
|
+
{
|
|
112
|
+
this.previous = null;
|
|
113
|
+
this.next = null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
appendLeft(content) {
|
|
117
|
+
this.outro += content;
|
|
118
|
+
}
|
|
119
|
+
appendRight(content) {
|
|
120
|
+
this.intro = this.intro + content;
|
|
121
|
+
}
|
|
122
|
+
clone() {
|
|
123
|
+
const chunk = new _Chunk(this.start, this.end, this.original);
|
|
124
|
+
chunk.intro = this.intro;
|
|
125
|
+
chunk.outro = this.outro;
|
|
126
|
+
chunk.content = this.content;
|
|
127
|
+
chunk.storeName = this.storeName;
|
|
128
|
+
chunk.edited = this.edited;
|
|
129
|
+
return chunk;
|
|
130
|
+
}
|
|
131
|
+
contains(index) {
|
|
132
|
+
return this.start < index && index < this.end;
|
|
133
|
+
}
|
|
134
|
+
eachNext(fn) {
|
|
135
|
+
let chunk = this;
|
|
136
|
+
while (chunk) {
|
|
137
|
+
fn(chunk);
|
|
138
|
+
chunk = chunk.next;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
eachPrevious(fn) {
|
|
142
|
+
let chunk = this;
|
|
143
|
+
while (chunk) {
|
|
144
|
+
fn(chunk);
|
|
145
|
+
chunk = chunk.previous;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
edit(content, storeName, contentOnly) {
|
|
149
|
+
this.content = content;
|
|
150
|
+
if (!contentOnly) {
|
|
151
|
+
this.intro = "";
|
|
152
|
+
this.outro = "";
|
|
153
|
+
}
|
|
154
|
+
this.storeName = storeName;
|
|
155
|
+
this.edited = true;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
prependLeft(content) {
|
|
159
|
+
this.outro = content + this.outro;
|
|
160
|
+
}
|
|
161
|
+
prependRight(content) {
|
|
162
|
+
this.intro = content + this.intro;
|
|
163
|
+
}
|
|
164
|
+
reset() {
|
|
165
|
+
this.intro = "";
|
|
166
|
+
this.outro = "";
|
|
167
|
+
if (this.edited) {
|
|
168
|
+
this.content = this.original;
|
|
169
|
+
this.storeName = false;
|
|
170
|
+
this.edited = false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
split(index) {
|
|
174
|
+
const sliceIndex = index - this.start;
|
|
175
|
+
const originalBefore = this.original.slice(0, sliceIndex);
|
|
176
|
+
const originalAfter = this.original.slice(sliceIndex);
|
|
177
|
+
this.original = originalBefore;
|
|
178
|
+
const newChunk = new _Chunk(index, this.end, originalAfter);
|
|
179
|
+
newChunk.outro = this.outro;
|
|
180
|
+
this.outro = "";
|
|
181
|
+
this.end = index;
|
|
182
|
+
if (this.edited) {
|
|
183
|
+
newChunk.edit("", false);
|
|
184
|
+
this.content = "";
|
|
185
|
+
} else {
|
|
186
|
+
this.content = originalBefore;
|
|
187
|
+
}
|
|
188
|
+
newChunk.next = this.next;
|
|
189
|
+
if (newChunk.next) newChunk.next.previous = newChunk;
|
|
190
|
+
newChunk.previous = this;
|
|
191
|
+
this.next = newChunk;
|
|
192
|
+
return newChunk;
|
|
193
|
+
}
|
|
194
|
+
toString() {
|
|
195
|
+
return this.intro + this.content + this.outro;
|
|
196
|
+
}
|
|
197
|
+
trimEnd(rx) {
|
|
198
|
+
this.outro = this.outro.replace(rx, "");
|
|
199
|
+
if (this.outro.length) return true;
|
|
200
|
+
const trimmed = this.content.replace(rx, "");
|
|
201
|
+
if (trimmed.length) {
|
|
202
|
+
if (trimmed !== this.content) {
|
|
203
|
+
this.split(this.start + trimmed.length).edit("", void 0, true);
|
|
204
|
+
if (this.edited) {
|
|
205
|
+
this.edit(trimmed, this.storeName, true);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return true;
|
|
209
|
+
} else {
|
|
210
|
+
this.edit("", void 0, true);
|
|
211
|
+
this.intro = this.intro.replace(rx, "");
|
|
212
|
+
if (this.intro.length) return true;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
trimStart(rx) {
|
|
216
|
+
this.intro = this.intro.replace(rx, "");
|
|
217
|
+
if (this.intro.length) return true;
|
|
218
|
+
const trimmed = this.content.replace(rx, "");
|
|
219
|
+
if (trimmed.length) {
|
|
220
|
+
if (trimmed !== this.content) {
|
|
221
|
+
const newChunk = this.split(this.end - trimmed.length);
|
|
222
|
+
if (this.edited) {
|
|
223
|
+
newChunk.edit(trimmed, this.storeName, true);
|
|
224
|
+
}
|
|
225
|
+
this.edit("", void 0, true);
|
|
226
|
+
}
|
|
227
|
+
return true;
|
|
228
|
+
} else {
|
|
229
|
+
this.edit("", void 0, true);
|
|
230
|
+
this.outro = this.outro.replace(rx, "");
|
|
231
|
+
if (this.outro.length) return true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
function getBtoa() {
|
|
236
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
|
|
237
|
+
return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
|
|
238
|
+
} else if (typeof Buffer === "function") {
|
|
239
|
+
return (str) => Buffer.from(str, "utf-8").toString("base64");
|
|
240
|
+
} else {
|
|
241
|
+
return () => {
|
|
242
|
+
throw new Error("Unsupported environment: `window.btoa` or `Buffer` should be supported.");
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
var btoa = /* @__PURE__ */ getBtoa();
|
|
247
|
+
var SourceMap = class {
|
|
248
|
+
constructor(properties) {
|
|
249
|
+
this.version = 3;
|
|
250
|
+
this.file = properties.file;
|
|
251
|
+
this.sources = properties.sources;
|
|
252
|
+
this.sourcesContent = properties.sourcesContent;
|
|
253
|
+
this.names = properties.names;
|
|
254
|
+
this.mappings = encode(properties.mappings);
|
|
255
|
+
if (typeof properties.x_google_ignoreList !== "undefined") {
|
|
256
|
+
this.x_google_ignoreList = properties.x_google_ignoreList;
|
|
257
|
+
}
|
|
258
|
+
if (typeof properties.debugId !== "undefined") {
|
|
259
|
+
this.debugId = properties.debugId;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
toString() {
|
|
263
|
+
return JSON.stringify(this);
|
|
264
|
+
}
|
|
265
|
+
toUrl() {
|
|
266
|
+
return "data:application/json;charset=utf-8;base64," + btoa(this.toString());
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
function guessIndent(code) {
|
|
270
|
+
const lines = code.split("\n");
|
|
271
|
+
const tabbed = lines.filter((line) => /^\t+/.test(line));
|
|
272
|
+
const spaced = lines.filter((line) => /^ {2,}/.test(line));
|
|
273
|
+
if (tabbed.length === 0 && spaced.length === 0) {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
if (tabbed.length >= spaced.length) {
|
|
277
|
+
return " ";
|
|
278
|
+
}
|
|
279
|
+
const min = spaced.reduce((previous, current) => {
|
|
280
|
+
const numSpaces = /^ +/.exec(current)[0].length;
|
|
281
|
+
return Math.min(numSpaces, previous);
|
|
282
|
+
}, Infinity);
|
|
283
|
+
return new Array(min + 1).join(" ");
|
|
284
|
+
}
|
|
285
|
+
function getRelativePath(from, to) {
|
|
286
|
+
const fromParts = from.split(/[/\\]/);
|
|
287
|
+
const toParts = to.split(/[/\\]/);
|
|
288
|
+
fromParts.pop();
|
|
289
|
+
while (fromParts[0] === toParts[0]) {
|
|
290
|
+
fromParts.shift();
|
|
291
|
+
toParts.shift();
|
|
292
|
+
}
|
|
293
|
+
if (fromParts.length) {
|
|
294
|
+
let i = fromParts.length;
|
|
295
|
+
while (i--) fromParts[i] = "..";
|
|
296
|
+
}
|
|
297
|
+
return fromParts.concat(toParts).join("/");
|
|
298
|
+
}
|
|
299
|
+
var toString = Object.prototype.toString;
|
|
300
|
+
function isObject(thing) {
|
|
301
|
+
return toString.call(thing) === "[object Object]";
|
|
302
|
+
}
|
|
303
|
+
function getLocator(source) {
|
|
304
|
+
const originalLines = source.split("\n");
|
|
305
|
+
const lineOffsets = [];
|
|
306
|
+
for (let i = 0, pos = 0; i < originalLines.length; i++) {
|
|
307
|
+
lineOffsets.push(pos);
|
|
308
|
+
pos += originalLines[i].length + 1;
|
|
309
|
+
}
|
|
310
|
+
return function locate(index) {
|
|
311
|
+
let i = 0;
|
|
312
|
+
let j = lineOffsets.length;
|
|
313
|
+
while (i < j) {
|
|
314
|
+
const m = i + j >> 1;
|
|
315
|
+
if (index < lineOffsets[m]) {
|
|
316
|
+
j = m;
|
|
317
|
+
} else {
|
|
318
|
+
i = m + 1;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const line = i - 1;
|
|
322
|
+
const column = index - lineOffsets[line];
|
|
323
|
+
return { line, column };
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
var wordRegex = /\w/;
|
|
327
|
+
var Mappings = class {
|
|
328
|
+
constructor(hires) {
|
|
329
|
+
this.hires = hires;
|
|
330
|
+
this.generatedCodeLine = 0;
|
|
331
|
+
this.generatedCodeColumn = 0;
|
|
332
|
+
this.raw = [];
|
|
333
|
+
this.rawSegments = this.raw[this.generatedCodeLine] = [];
|
|
334
|
+
this.pending = null;
|
|
335
|
+
}
|
|
336
|
+
addEdit(sourceIndex, content, loc, nameIndex) {
|
|
337
|
+
if (content.length) {
|
|
338
|
+
const contentLengthMinusOne = content.length - 1;
|
|
339
|
+
let contentLineEnd = content.indexOf("\n", 0);
|
|
340
|
+
let previousContentLineEnd = -1;
|
|
341
|
+
while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
|
|
342
|
+
const segment2 = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
343
|
+
if (nameIndex >= 0) {
|
|
344
|
+
segment2.push(nameIndex);
|
|
345
|
+
}
|
|
346
|
+
this.rawSegments.push(segment2);
|
|
347
|
+
this.generatedCodeLine += 1;
|
|
348
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
349
|
+
this.generatedCodeColumn = 0;
|
|
350
|
+
previousContentLineEnd = contentLineEnd;
|
|
351
|
+
contentLineEnd = content.indexOf("\n", contentLineEnd + 1);
|
|
352
|
+
}
|
|
353
|
+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
354
|
+
if (nameIndex >= 0) {
|
|
355
|
+
segment.push(nameIndex);
|
|
356
|
+
}
|
|
357
|
+
this.rawSegments.push(segment);
|
|
358
|
+
this.advance(content.slice(previousContentLineEnd + 1));
|
|
359
|
+
} else if (this.pending) {
|
|
360
|
+
this.rawSegments.push(this.pending);
|
|
361
|
+
this.advance(content);
|
|
362
|
+
}
|
|
363
|
+
this.pending = null;
|
|
364
|
+
}
|
|
365
|
+
addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
|
|
366
|
+
let originalCharIndex = chunk.start;
|
|
367
|
+
let first = true;
|
|
368
|
+
let charInHiresBoundary = false;
|
|
369
|
+
while (originalCharIndex < chunk.end) {
|
|
370
|
+
if (original[originalCharIndex] === "\n") {
|
|
371
|
+
loc.line += 1;
|
|
372
|
+
loc.column = 0;
|
|
373
|
+
this.generatedCodeLine += 1;
|
|
374
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
375
|
+
this.generatedCodeColumn = 0;
|
|
376
|
+
first = true;
|
|
377
|
+
charInHiresBoundary = false;
|
|
378
|
+
} else {
|
|
379
|
+
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
|
|
380
|
+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
381
|
+
if (this.hires === "boundary") {
|
|
382
|
+
if (wordRegex.test(original[originalCharIndex])) {
|
|
383
|
+
if (!charInHiresBoundary) {
|
|
384
|
+
this.rawSegments.push(segment);
|
|
385
|
+
charInHiresBoundary = true;
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
this.rawSegments.push(segment);
|
|
389
|
+
charInHiresBoundary = false;
|
|
390
|
+
}
|
|
391
|
+
} else {
|
|
392
|
+
this.rawSegments.push(segment);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
loc.column += 1;
|
|
396
|
+
this.generatedCodeColumn += 1;
|
|
397
|
+
first = false;
|
|
398
|
+
}
|
|
399
|
+
originalCharIndex += 1;
|
|
400
|
+
}
|
|
401
|
+
this.pending = null;
|
|
402
|
+
}
|
|
403
|
+
advance(str) {
|
|
404
|
+
if (!str) return;
|
|
405
|
+
const lines = str.split("\n");
|
|
406
|
+
if (lines.length > 1) {
|
|
407
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
408
|
+
this.generatedCodeLine++;
|
|
409
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
410
|
+
}
|
|
411
|
+
this.generatedCodeColumn = 0;
|
|
412
|
+
}
|
|
413
|
+
this.generatedCodeColumn += lines[lines.length - 1].length;
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
var n = "\n";
|
|
417
|
+
var warned = {
|
|
418
|
+
insertLeft: false,
|
|
419
|
+
insertRight: false,
|
|
420
|
+
storeName: false
|
|
421
|
+
};
|
|
422
|
+
var MagicString = class _MagicString {
|
|
423
|
+
constructor(string, options = {}) {
|
|
424
|
+
const chunk = new Chunk(0, string.length, string);
|
|
425
|
+
Object.defineProperties(this, {
|
|
426
|
+
original: { writable: true, value: string },
|
|
427
|
+
outro: { writable: true, value: "" },
|
|
428
|
+
intro: { writable: true, value: "" },
|
|
429
|
+
firstChunk: { writable: true, value: chunk },
|
|
430
|
+
lastChunk: { writable: true, value: chunk },
|
|
431
|
+
lastSearchedChunk: { writable: true, value: chunk },
|
|
432
|
+
byStart: { writable: true, value: {} },
|
|
433
|
+
byEnd: { writable: true, value: {} },
|
|
434
|
+
filename: { writable: true, value: options.filename },
|
|
435
|
+
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
|
|
436
|
+
sourcemapLocations: { writable: true, value: new BitSet() },
|
|
437
|
+
storedNames: { writable: true, value: {} },
|
|
438
|
+
indentStr: { writable: true, value: void 0 },
|
|
439
|
+
ignoreList: { writable: true, value: options.ignoreList },
|
|
440
|
+
offset: { writable: true, value: options.offset || 0 }
|
|
441
|
+
});
|
|
442
|
+
this.byStart[0] = chunk;
|
|
443
|
+
this.byEnd[string.length] = chunk;
|
|
444
|
+
}
|
|
445
|
+
addSourcemapLocation(char) {
|
|
446
|
+
this.sourcemapLocations.add(char);
|
|
447
|
+
}
|
|
448
|
+
append(content) {
|
|
449
|
+
if (typeof content !== "string") throw new TypeError("outro content must be a string");
|
|
450
|
+
this.outro += content;
|
|
451
|
+
return this;
|
|
452
|
+
}
|
|
453
|
+
appendLeft(index, content) {
|
|
454
|
+
index = index + this.offset;
|
|
455
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
456
|
+
this._split(index);
|
|
457
|
+
const chunk = this.byEnd[index];
|
|
458
|
+
if (chunk) {
|
|
459
|
+
chunk.appendLeft(content);
|
|
460
|
+
} else {
|
|
461
|
+
this.intro += content;
|
|
462
|
+
}
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
appendRight(index, content) {
|
|
466
|
+
index = index + this.offset;
|
|
467
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
468
|
+
this._split(index);
|
|
469
|
+
const chunk = this.byStart[index];
|
|
470
|
+
if (chunk) {
|
|
471
|
+
chunk.appendRight(content);
|
|
472
|
+
} else {
|
|
473
|
+
this.outro += content;
|
|
474
|
+
}
|
|
475
|
+
return this;
|
|
476
|
+
}
|
|
477
|
+
clone() {
|
|
478
|
+
const cloned = new _MagicString(this.original, { filename: this.filename, offset: this.offset });
|
|
479
|
+
let originalChunk = this.firstChunk;
|
|
480
|
+
let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
|
|
481
|
+
while (originalChunk) {
|
|
482
|
+
cloned.byStart[clonedChunk.start] = clonedChunk;
|
|
483
|
+
cloned.byEnd[clonedChunk.end] = clonedChunk;
|
|
484
|
+
const nextOriginalChunk = originalChunk.next;
|
|
485
|
+
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
|
486
|
+
if (nextClonedChunk) {
|
|
487
|
+
clonedChunk.next = nextClonedChunk;
|
|
488
|
+
nextClonedChunk.previous = clonedChunk;
|
|
489
|
+
clonedChunk = nextClonedChunk;
|
|
490
|
+
}
|
|
491
|
+
originalChunk = nextOriginalChunk;
|
|
492
|
+
}
|
|
493
|
+
cloned.lastChunk = clonedChunk;
|
|
494
|
+
if (this.indentExclusionRanges) {
|
|
495
|
+
cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
|
496
|
+
}
|
|
497
|
+
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
|
498
|
+
cloned.intro = this.intro;
|
|
499
|
+
cloned.outro = this.outro;
|
|
500
|
+
return cloned;
|
|
501
|
+
}
|
|
502
|
+
generateDecodedMap(options) {
|
|
503
|
+
options = options || {};
|
|
504
|
+
const sourceIndex = 0;
|
|
505
|
+
const names = Object.keys(this.storedNames);
|
|
506
|
+
const mappings = new Mappings(options.hires);
|
|
507
|
+
const locate = getLocator(this.original);
|
|
508
|
+
if (this.intro) {
|
|
509
|
+
mappings.advance(this.intro);
|
|
510
|
+
}
|
|
511
|
+
this.firstChunk.eachNext((chunk) => {
|
|
512
|
+
const loc = locate(chunk.start);
|
|
513
|
+
if (chunk.intro.length) mappings.advance(chunk.intro);
|
|
514
|
+
if (chunk.edited) {
|
|
515
|
+
mappings.addEdit(
|
|
516
|
+
sourceIndex,
|
|
517
|
+
chunk.content,
|
|
518
|
+
loc,
|
|
519
|
+
chunk.storeName ? names.indexOf(chunk.original) : -1
|
|
520
|
+
);
|
|
521
|
+
} else {
|
|
522
|
+
mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
|
|
523
|
+
}
|
|
524
|
+
if (chunk.outro.length) mappings.advance(chunk.outro);
|
|
525
|
+
});
|
|
526
|
+
if (this.outro) {
|
|
527
|
+
mappings.advance(this.outro);
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
file: options.file ? options.file.split(/[/\\]/).pop() : void 0,
|
|
531
|
+
sources: [
|
|
532
|
+
options.source ? getRelativePath(options.file || "", options.source) : options.file || ""
|
|
533
|
+
],
|
|
534
|
+
sourcesContent: options.includeContent ? [this.original] : void 0,
|
|
535
|
+
names,
|
|
536
|
+
mappings: mappings.raw,
|
|
537
|
+
x_google_ignoreList: this.ignoreList ? [sourceIndex] : void 0
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
generateMap(options) {
|
|
541
|
+
return new SourceMap(this.generateDecodedMap(options));
|
|
542
|
+
}
|
|
543
|
+
_ensureindentStr() {
|
|
544
|
+
if (this.indentStr === void 0) {
|
|
545
|
+
this.indentStr = guessIndent(this.original);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
_getRawIndentString() {
|
|
549
|
+
this._ensureindentStr();
|
|
550
|
+
return this.indentStr;
|
|
551
|
+
}
|
|
552
|
+
getIndentString() {
|
|
553
|
+
this._ensureindentStr();
|
|
554
|
+
return this.indentStr === null ? " " : this.indentStr;
|
|
555
|
+
}
|
|
556
|
+
indent(indentStr, options) {
|
|
557
|
+
const pattern = /^[^\r\n]/gm;
|
|
558
|
+
if (isObject(indentStr)) {
|
|
559
|
+
options = indentStr;
|
|
560
|
+
indentStr = void 0;
|
|
561
|
+
}
|
|
562
|
+
if (indentStr === void 0) {
|
|
563
|
+
this._ensureindentStr();
|
|
564
|
+
indentStr = this.indentStr || " ";
|
|
565
|
+
}
|
|
566
|
+
if (indentStr === "") return this;
|
|
567
|
+
options = options || {};
|
|
568
|
+
const isExcluded = {};
|
|
569
|
+
if (options.exclude) {
|
|
570
|
+
const exclusions = typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude;
|
|
571
|
+
exclusions.forEach((exclusion) => {
|
|
572
|
+
for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
|
|
573
|
+
isExcluded[i] = true;
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
let shouldIndentNextCharacter = options.indentStart !== false;
|
|
578
|
+
const replacer = (match) => {
|
|
579
|
+
if (shouldIndentNextCharacter) return `${indentStr}${match}`;
|
|
580
|
+
shouldIndentNextCharacter = true;
|
|
581
|
+
return match;
|
|
582
|
+
};
|
|
583
|
+
this.intro = this.intro.replace(pattern, replacer);
|
|
584
|
+
let charIndex = 0;
|
|
585
|
+
let chunk = this.firstChunk;
|
|
586
|
+
while (chunk) {
|
|
587
|
+
const end = chunk.end;
|
|
588
|
+
if (chunk.edited) {
|
|
589
|
+
if (!isExcluded[charIndex]) {
|
|
590
|
+
chunk.content = chunk.content.replace(pattern, replacer);
|
|
591
|
+
if (chunk.content.length) {
|
|
592
|
+
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
} else {
|
|
596
|
+
charIndex = chunk.start;
|
|
597
|
+
while (charIndex < end) {
|
|
598
|
+
if (!isExcluded[charIndex]) {
|
|
599
|
+
const char = this.original[charIndex];
|
|
600
|
+
if (char === "\n") {
|
|
601
|
+
shouldIndentNextCharacter = true;
|
|
602
|
+
} else if (char !== "\r" && shouldIndentNextCharacter) {
|
|
603
|
+
shouldIndentNextCharacter = false;
|
|
604
|
+
if (charIndex === chunk.start) {
|
|
605
|
+
chunk.prependRight(indentStr);
|
|
606
|
+
} else {
|
|
607
|
+
this._splitChunk(chunk, charIndex);
|
|
608
|
+
chunk = chunk.next;
|
|
609
|
+
chunk.prependRight(indentStr);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
charIndex += 1;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
charIndex = chunk.end;
|
|
617
|
+
chunk = chunk.next;
|
|
618
|
+
}
|
|
619
|
+
this.outro = this.outro.replace(pattern, replacer);
|
|
620
|
+
return this;
|
|
621
|
+
}
|
|
622
|
+
insert() {
|
|
623
|
+
throw new Error(
|
|
624
|
+
"magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)"
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
insertLeft(index, content) {
|
|
628
|
+
if (!warned.insertLeft) {
|
|
629
|
+
console.warn(
|
|
630
|
+
"magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead"
|
|
631
|
+
);
|
|
632
|
+
warned.insertLeft = true;
|
|
633
|
+
}
|
|
634
|
+
return this.appendLeft(index, content);
|
|
635
|
+
}
|
|
636
|
+
insertRight(index, content) {
|
|
637
|
+
if (!warned.insertRight) {
|
|
638
|
+
console.warn(
|
|
639
|
+
"magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead"
|
|
640
|
+
);
|
|
641
|
+
warned.insertRight = true;
|
|
642
|
+
}
|
|
643
|
+
return this.prependRight(index, content);
|
|
644
|
+
}
|
|
645
|
+
move(start, end, index) {
|
|
646
|
+
start = start + this.offset;
|
|
647
|
+
end = end + this.offset;
|
|
648
|
+
index = index + this.offset;
|
|
649
|
+
if (index >= start && index <= end) throw new Error("Cannot move a selection inside itself");
|
|
650
|
+
this._split(start);
|
|
651
|
+
this._split(end);
|
|
652
|
+
this._split(index);
|
|
653
|
+
const first = this.byStart[start];
|
|
654
|
+
const last = this.byEnd[end];
|
|
655
|
+
const oldLeft = first.previous;
|
|
656
|
+
const oldRight = last.next;
|
|
657
|
+
const newRight = this.byStart[index];
|
|
658
|
+
if (!newRight && last === this.lastChunk) return this;
|
|
659
|
+
const newLeft = newRight ? newRight.previous : this.lastChunk;
|
|
660
|
+
if (oldLeft) oldLeft.next = oldRight;
|
|
661
|
+
if (oldRight) oldRight.previous = oldLeft;
|
|
662
|
+
if (newLeft) newLeft.next = first;
|
|
663
|
+
if (newRight) newRight.previous = last;
|
|
664
|
+
if (!first.previous) this.firstChunk = last.next;
|
|
665
|
+
if (!last.next) {
|
|
666
|
+
this.lastChunk = first.previous;
|
|
667
|
+
this.lastChunk.next = null;
|
|
668
|
+
}
|
|
669
|
+
first.previous = newLeft;
|
|
670
|
+
last.next = newRight || null;
|
|
671
|
+
if (!newLeft) this.firstChunk = first;
|
|
672
|
+
if (!newRight) this.lastChunk = last;
|
|
673
|
+
return this;
|
|
674
|
+
}
|
|
675
|
+
overwrite(start, end, content, options) {
|
|
676
|
+
options = options || {};
|
|
677
|
+
return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
|
|
678
|
+
}
|
|
679
|
+
update(start, end, content, options) {
|
|
680
|
+
start = start + this.offset;
|
|
681
|
+
end = end + this.offset;
|
|
682
|
+
if (typeof content !== "string") throw new TypeError("replacement content must be a string");
|
|
683
|
+
if (this.original.length !== 0) {
|
|
684
|
+
while (start < 0) start += this.original.length;
|
|
685
|
+
while (end < 0) end += this.original.length;
|
|
686
|
+
}
|
|
687
|
+
if (end > this.original.length) throw new Error("end is out of bounds");
|
|
688
|
+
if (start === end)
|
|
689
|
+
throw new Error(
|
|
690
|
+
"Cannot overwrite a zero-length range \u2013 use appendLeft or prependRight instead"
|
|
691
|
+
);
|
|
692
|
+
this._split(start);
|
|
693
|
+
this._split(end);
|
|
694
|
+
if (options === true) {
|
|
695
|
+
if (!warned.storeName) {
|
|
696
|
+
console.warn(
|
|
697
|
+
"The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string"
|
|
698
|
+
);
|
|
699
|
+
warned.storeName = true;
|
|
700
|
+
}
|
|
701
|
+
options = { storeName: true };
|
|
702
|
+
}
|
|
703
|
+
const storeName = options !== void 0 ? options.storeName : false;
|
|
704
|
+
const overwrite = options !== void 0 ? options.overwrite : false;
|
|
705
|
+
if (storeName) {
|
|
706
|
+
const original = this.original.slice(start, end);
|
|
707
|
+
Object.defineProperty(this.storedNames, original, {
|
|
708
|
+
writable: true,
|
|
709
|
+
value: true,
|
|
710
|
+
enumerable: true
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
const first = this.byStart[start];
|
|
714
|
+
const last = this.byEnd[end];
|
|
715
|
+
if (first) {
|
|
716
|
+
let chunk = first;
|
|
717
|
+
while (chunk !== last) {
|
|
718
|
+
if (chunk.next !== this.byStart[chunk.end]) {
|
|
719
|
+
throw new Error("Cannot overwrite across a split point");
|
|
720
|
+
}
|
|
721
|
+
chunk = chunk.next;
|
|
722
|
+
chunk.edit("", false);
|
|
723
|
+
}
|
|
724
|
+
first.edit(content, storeName, !overwrite);
|
|
725
|
+
} else {
|
|
726
|
+
const newChunk = new Chunk(start, end, "").edit(content, storeName);
|
|
727
|
+
last.next = newChunk;
|
|
728
|
+
newChunk.previous = last;
|
|
729
|
+
}
|
|
730
|
+
return this;
|
|
731
|
+
}
|
|
732
|
+
prepend(content) {
|
|
733
|
+
if (typeof content !== "string") throw new TypeError("outro content must be a string");
|
|
734
|
+
this.intro = content + this.intro;
|
|
735
|
+
return this;
|
|
736
|
+
}
|
|
737
|
+
prependLeft(index, content) {
|
|
738
|
+
index = index + this.offset;
|
|
739
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
740
|
+
this._split(index);
|
|
741
|
+
const chunk = this.byEnd[index];
|
|
742
|
+
if (chunk) {
|
|
743
|
+
chunk.prependLeft(content);
|
|
744
|
+
} else {
|
|
745
|
+
this.intro = content + this.intro;
|
|
746
|
+
}
|
|
747
|
+
return this;
|
|
748
|
+
}
|
|
749
|
+
prependRight(index, content) {
|
|
750
|
+
index = index + this.offset;
|
|
751
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
752
|
+
this._split(index);
|
|
753
|
+
const chunk = this.byStart[index];
|
|
754
|
+
if (chunk) {
|
|
755
|
+
chunk.prependRight(content);
|
|
756
|
+
} else {
|
|
757
|
+
this.outro = content + this.outro;
|
|
758
|
+
}
|
|
759
|
+
return this;
|
|
760
|
+
}
|
|
761
|
+
remove(start, end) {
|
|
762
|
+
start = start + this.offset;
|
|
763
|
+
end = end + this.offset;
|
|
764
|
+
if (this.original.length !== 0) {
|
|
765
|
+
while (start < 0) start += this.original.length;
|
|
766
|
+
while (end < 0) end += this.original.length;
|
|
767
|
+
}
|
|
768
|
+
if (start === end) return this;
|
|
769
|
+
if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds");
|
|
770
|
+
if (start > end) throw new Error("end must be greater than start");
|
|
771
|
+
this._split(start);
|
|
772
|
+
this._split(end);
|
|
773
|
+
let chunk = this.byStart[start];
|
|
774
|
+
while (chunk) {
|
|
775
|
+
chunk.intro = "";
|
|
776
|
+
chunk.outro = "";
|
|
777
|
+
chunk.edit("");
|
|
778
|
+
chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
|
779
|
+
}
|
|
780
|
+
return this;
|
|
781
|
+
}
|
|
782
|
+
reset(start, end) {
|
|
783
|
+
start = start + this.offset;
|
|
784
|
+
end = end + this.offset;
|
|
785
|
+
if (this.original.length !== 0) {
|
|
786
|
+
while (start < 0) start += this.original.length;
|
|
787
|
+
while (end < 0) end += this.original.length;
|
|
788
|
+
}
|
|
789
|
+
if (start === end) return this;
|
|
790
|
+
if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds");
|
|
791
|
+
if (start > end) throw new Error("end must be greater than start");
|
|
792
|
+
this._split(start);
|
|
793
|
+
this._split(end);
|
|
794
|
+
let chunk = this.byStart[start];
|
|
795
|
+
while (chunk) {
|
|
796
|
+
chunk.reset();
|
|
797
|
+
chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
|
798
|
+
}
|
|
799
|
+
return this;
|
|
800
|
+
}
|
|
801
|
+
lastChar() {
|
|
802
|
+
if (this.outro.length) return this.outro[this.outro.length - 1];
|
|
803
|
+
let chunk = this.lastChunk;
|
|
804
|
+
do {
|
|
805
|
+
if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
|
|
806
|
+
if (chunk.content.length) return chunk.content[chunk.content.length - 1];
|
|
807
|
+
if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
|
|
808
|
+
} while (chunk = chunk.previous);
|
|
809
|
+
if (this.intro.length) return this.intro[this.intro.length - 1];
|
|
810
|
+
return "";
|
|
811
|
+
}
|
|
812
|
+
lastLine() {
|
|
813
|
+
let lineIndex = this.outro.lastIndexOf(n);
|
|
814
|
+
if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
|
|
815
|
+
let lineStr = this.outro;
|
|
816
|
+
let chunk = this.lastChunk;
|
|
817
|
+
do {
|
|
818
|
+
if (chunk.outro.length > 0) {
|
|
819
|
+
lineIndex = chunk.outro.lastIndexOf(n);
|
|
820
|
+
if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
|
|
821
|
+
lineStr = chunk.outro + lineStr;
|
|
822
|
+
}
|
|
823
|
+
if (chunk.content.length > 0) {
|
|
824
|
+
lineIndex = chunk.content.lastIndexOf(n);
|
|
825
|
+
if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
|
|
826
|
+
lineStr = chunk.content + lineStr;
|
|
827
|
+
}
|
|
828
|
+
if (chunk.intro.length > 0) {
|
|
829
|
+
lineIndex = chunk.intro.lastIndexOf(n);
|
|
830
|
+
if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
|
|
831
|
+
lineStr = chunk.intro + lineStr;
|
|
832
|
+
}
|
|
833
|
+
} while (chunk = chunk.previous);
|
|
834
|
+
lineIndex = this.intro.lastIndexOf(n);
|
|
835
|
+
if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
|
|
836
|
+
return this.intro + lineStr;
|
|
837
|
+
}
|
|
838
|
+
slice(start = 0, end = this.original.length - this.offset) {
|
|
839
|
+
start = start + this.offset;
|
|
840
|
+
end = end + this.offset;
|
|
841
|
+
if (this.original.length !== 0) {
|
|
842
|
+
while (start < 0) start += this.original.length;
|
|
843
|
+
while (end < 0) end += this.original.length;
|
|
844
|
+
}
|
|
845
|
+
let result = "";
|
|
846
|
+
let chunk = this.firstChunk;
|
|
847
|
+
while (chunk && (chunk.start > start || chunk.end <= start)) {
|
|
848
|
+
if (chunk.start < end && chunk.end >= end) {
|
|
849
|
+
return result;
|
|
850
|
+
}
|
|
851
|
+
chunk = chunk.next;
|
|
852
|
+
}
|
|
853
|
+
if (chunk && chunk.edited && chunk.start !== start)
|
|
854
|
+
throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
|
|
855
|
+
const startChunk = chunk;
|
|
856
|
+
while (chunk) {
|
|
857
|
+
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
|
|
858
|
+
result += chunk.intro;
|
|
859
|
+
}
|
|
860
|
+
const containsEnd = chunk.start < end && chunk.end >= end;
|
|
861
|
+
if (containsEnd && chunk.edited && chunk.end !== end)
|
|
862
|
+
throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
|
|
863
|
+
const sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
|
864
|
+
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
|
865
|
+
result += chunk.content.slice(sliceStart, sliceEnd);
|
|
866
|
+
if (chunk.outro && (!containsEnd || chunk.end === end)) {
|
|
867
|
+
result += chunk.outro;
|
|
868
|
+
}
|
|
869
|
+
if (containsEnd) {
|
|
870
|
+
break;
|
|
871
|
+
}
|
|
872
|
+
chunk = chunk.next;
|
|
873
|
+
}
|
|
874
|
+
return result;
|
|
875
|
+
}
|
|
876
|
+
// TODO deprecate this? not really very useful
|
|
877
|
+
snip(start, end) {
|
|
878
|
+
const clone = this.clone();
|
|
879
|
+
clone.remove(0, start);
|
|
880
|
+
clone.remove(end, clone.original.length);
|
|
881
|
+
return clone;
|
|
882
|
+
}
|
|
883
|
+
_split(index) {
|
|
884
|
+
if (this.byStart[index] || this.byEnd[index]) return;
|
|
885
|
+
let chunk = this.lastSearchedChunk;
|
|
886
|
+
let previousChunk = chunk;
|
|
887
|
+
const searchForward = index > chunk.end;
|
|
888
|
+
while (chunk) {
|
|
889
|
+
if (chunk.contains(index)) return this._splitChunk(chunk, index);
|
|
890
|
+
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
|
|
891
|
+
if (chunk === previousChunk) return;
|
|
892
|
+
previousChunk = chunk;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
_splitChunk(chunk, index) {
|
|
896
|
+
if (chunk.edited && chunk.content.length) {
|
|
897
|
+
const loc = getLocator(this.original)(index);
|
|
898
|
+
throw new Error(
|
|
899
|
+
`Cannot split a chunk that has already been edited (${loc.line}:${loc.column} \u2013 "${chunk.original}")`
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
const newChunk = chunk.split(index);
|
|
903
|
+
this.byEnd[index] = chunk;
|
|
904
|
+
this.byStart[index] = newChunk;
|
|
905
|
+
this.byEnd[newChunk.end] = newChunk;
|
|
906
|
+
if (chunk === this.lastChunk) this.lastChunk = newChunk;
|
|
907
|
+
this.lastSearchedChunk = chunk;
|
|
908
|
+
return true;
|
|
909
|
+
}
|
|
910
|
+
toString() {
|
|
911
|
+
let str = this.intro;
|
|
912
|
+
let chunk = this.firstChunk;
|
|
913
|
+
while (chunk) {
|
|
914
|
+
str += chunk.toString();
|
|
915
|
+
chunk = chunk.next;
|
|
916
|
+
}
|
|
917
|
+
return str + this.outro;
|
|
918
|
+
}
|
|
919
|
+
isEmpty() {
|
|
920
|
+
let chunk = this.firstChunk;
|
|
921
|
+
do {
|
|
922
|
+
if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim())
|
|
923
|
+
return false;
|
|
924
|
+
} while (chunk = chunk.next);
|
|
925
|
+
return true;
|
|
926
|
+
}
|
|
927
|
+
length() {
|
|
928
|
+
let chunk = this.firstChunk;
|
|
929
|
+
let length = 0;
|
|
930
|
+
do {
|
|
931
|
+
length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
|
932
|
+
} while (chunk = chunk.next);
|
|
933
|
+
return length;
|
|
934
|
+
}
|
|
935
|
+
trimLines() {
|
|
936
|
+
return this.trim("[\\r\\n]");
|
|
937
|
+
}
|
|
938
|
+
trim(charType) {
|
|
939
|
+
return this.trimStart(charType).trimEnd(charType);
|
|
940
|
+
}
|
|
941
|
+
trimEndAborted(charType) {
|
|
942
|
+
const rx = new RegExp((charType || "\\s") + "+$");
|
|
943
|
+
this.outro = this.outro.replace(rx, "");
|
|
944
|
+
if (this.outro.length) return true;
|
|
945
|
+
let chunk = this.lastChunk;
|
|
946
|
+
do {
|
|
947
|
+
const end = chunk.end;
|
|
948
|
+
const aborted = chunk.trimEnd(rx);
|
|
949
|
+
if (chunk.end !== end) {
|
|
950
|
+
if (this.lastChunk === chunk) {
|
|
951
|
+
this.lastChunk = chunk.next;
|
|
952
|
+
}
|
|
953
|
+
this.byEnd[chunk.end] = chunk;
|
|
954
|
+
this.byStart[chunk.next.start] = chunk.next;
|
|
955
|
+
this.byEnd[chunk.next.end] = chunk.next;
|
|
956
|
+
}
|
|
957
|
+
if (aborted) return true;
|
|
958
|
+
chunk = chunk.previous;
|
|
959
|
+
} while (chunk);
|
|
960
|
+
return false;
|
|
961
|
+
}
|
|
962
|
+
trimEnd(charType) {
|
|
963
|
+
this.trimEndAborted(charType);
|
|
964
|
+
return this;
|
|
965
|
+
}
|
|
966
|
+
trimStartAborted(charType) {
|
|
967
|
+
const rx = new RegExp("^" + (charType || "\\s") + "+");
|
|
968
|
+
this.intro = this.intro.replace(rx, "");
|
|
969
|
+
if (this.intro.length) return true;
|
|
970
|
+
let chunk = this.firstChunk;
|
|
971
|
+
do {
|
|
972
|
+
const end = chunk.end;
|
|
973
|
+
const aborted = chunk.trimStart(rx);
|
|
974
|
+
if (chunk.end !== end) {
|
|
975
|
+
if (chunk === this.lastChunk) this.lastChunk = chunk.next;
|
|
976
|
+
this.byEnd[chunk.end] = chunk;
|
|
977
|
+
this.byStart[chunk.next.start] = chunk.next;
|
|
978
|
+
this.byEnd[chunk.next.end] = chunk.next;
|
|
979
|
+
}
|
|
980
|
+
if (aborted) return true;
|
|
981
|
+
chunk = chunk.next;
|
|
982
|
+
} while (chunk);
|
|
983
|
+
return false;
|
|
984
|
+
}
|
|
985
|
+
trimStart(charType) {
|
|
986
|
+
this.trimStartAborted(charType);
|
|
987
|
+
return this;
|
|
988
|
+
}
|
|
989
|
+
hasChanged() {
|
|
990
|
+
return this.original !== this.toString();
|
|
991
|
+
}
|
|
992
|
+
_replaceRegexp(searchValue, replacement) {
|
|
993
|
+
function getReplacement(match, str) {
|
|
994
|
+
if (typeof replacement === "string") {
|
|
995
|
+
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
|
|
996
|
+
if (i === "$") return "$";
|
|
997
|
+
if (i === "&") return match[0];
|
|
998
|
+
const num = +i;
|
|
999
|
+
if (num < match.length) return match[+i];
|
|
1000
|
+
return `$${i}`;
|
|
1001
|
+
});
|
|
1002
|
+
} else {
|
|
1003
|
+
return replacement(...match, match.index, str, match.groups);
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
function matchAll(re, str) {
|
|
1007
|
+
let match;
|
|
1008
|
+
const matches = [];
|
|
1009
|
+
while (match = re.exec(str)) {
|
|
1010
|
+
matches.push(match);
|
|
1011
|
+
}
|
|
1012
|
+
return matches;
|
|
1013
|
+
}
|
|
1014
|
+
if (searchValue.global) {
|
|
1015
|
+
const matches = matchAll(searchValue, this.original);
|
|
1016
|
+
matches.forEach((match) => {
|
|
1017
|
+
if (match.index != null) {
|
|
1018
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1019
|
+
if (replacement2 !== match[0]) {
|
|
1020
|
+
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
} else {
|
|
1025
|
+
const match = this.original.match(searchValue);
|
|
1026
|
+
if (match && match.index != null) {
|
|
1027
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1028
|
+
if (replacement2 !== match[0]) {
|
|
1029
|
+
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return this;
|
|
1034
|
+
}
|
|
1035
|
+
_replaceString(string, replacement) {
|
|
1036
|
+
const { original } = this;
|
|
1037
|
+
const index = original.indexOf(string);
|
|
1038
|
+
if (index !== -1) {
|
|
1039
|
+
if (typeof replacement === "function") {
|
|
1040
|
+
replacement = replacement(string, index, original);
|
|
1041
|
+
}
|
|
1042
|
+
if (string !== replacement) {
|
|
1043
|
+
this.overwrite(index, index + string.length, replacement);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
return this;
|
|
1047
|
+
}
|
|
1048
|
+
replace(searchValue, replacement) {
|
|
1049
|
+
if (typeof searchValue === "string") {
|
|
1050
|
+
return this._replaceString(searchValue, replacement);
|
|
1051
|
+
}
|
|
1052
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1053
|
+
}
|
|
1054
|
+
_replaceAllString(string, replacement) {
|
|
1055
|
+
const { original } = this;
|
|
1056
|
+
const stringLength = string.length;
|
|
1057
|
+
for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
|
|
1058
|
+
const previous = original.slice(index, index + stringLength);
|
|
1059
|
+
let _replacement = replacement;
|
|
1060
|
+
if (typeof replacement === "function") {
|
|
1061
|
+
_replacement = replacement(previous, index, original);
|
|
1062
|
+
}
|
|
1063
|
+
if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);
|
|
1064
|
+
}
|
|
1065
|
+
return this;
|
|
1066
|
+
}
|
|
1067
|
+
replaceAll(searchValue, replacement) {
|
|
1068
|
+
if (typeof searchValue === "string") {
|
|
1069
|
+
return this._replaceAllString(searchValue, replacement);
|
|
1070
|
+
}
|
|
1071
|
+
if (!searchValue.global) {
|
|
1072
|
+
throw new TypeError(
|
|
1073
|
+
"MagicString.prototype.replaceAll called with a non-global RegExp argument"
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
// src/vite-plugin.ts
|
|
1081
|
+
var SOURCE_LOC_ATTR = "data-source-loc";
|
|
1082
|
+
function injectSourceLocations(code, id, root) {
|
|
1083
|
+
if (!code.includes("<")) return null;
|
|
1084
|
+
const s = new MagicString(code);
|
|
1085
|
+
const relativePath = relative(root, id);
|
|
1086
|
+
let hasChanges = false;
|
|
1087
|
+
const tagRegex = /<([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*?)?)\s*\/?>/g;
|
|
1088
|
+
const skipTags = /* @__PURE__ */ new Set([
|
|
1089
|
+
"script",
|
|
1090
|
+
"style",
|
|
1091
|
+
"template",
|
|
1092
|
+
"slot",
|
|
1093
|
+
"meta",
|
|
1094
|
+
"link",
|
|
1095
|
+
"base",
|
|
1096
|
+
"br",
|
|
1097
|
+
"hr",
|
|
1098
|
+
"img",
|
|
1099
|
+
"input",
|
|
1100
|
+
"area",
|
|
1101
|
+
"embed",
|
|
1102
|
+
"source",
|
|
1103
|
+
"track",
|
|
1104
|
+
"wbr",
|
|
1105
|
+
"html",
|
|
1106
|
+
"head",
|
|
1107
|
+
"title",
|
|
1108
|
+
"!doctype",
|
|
1109
|
+
// Skip SVG internal elements (but not svg itself)
|
|
1110
|
+
"path",
|
|
1111
|
+
"circle",
|
|
1112
|
+
"rect",
|
|
1113
|
+
"line",
|
|
1114
|
+
"polyline",
|
|
1115
|
+
"polygon",
|
|
1116
|
+
"ellipse",
|
|
1117
|
+
"text",
|
|
1118
|
+
"tspan",
|
|
1119
|
+
"g",
|
|
1120
|
+
"defs",
|
|
1121
|
+
"use",
|
|
1122
|
+
"symbol",
|
|
1123
|
+
"clippath",
|
|
1124
|
+
"mask",
|
|
1125
|
+
"pattern",
|
|
1126
|
+
"lineargradient",
|
|
1127
|
+
"radialgradient",
|
|
1128
|
+
"stop",
|
|
1129
|
+
"filter"
|
|
1130
|
+
]);
|
|
1131
|
+
let match;
|
|
1132
|
+
while ((match = tagRegex.exec(code)) !== null) {
|
|
1133
|
+
const [fullMatch, tagName, attributes] = match;
|
|
1134
|
+
const tagNameLower = tagName.toLowerCase();
|
|
1135
|
+
if (skipTags.has(tagNameLower)) continue;
|
|
1136
|
+
if (attributes.includes(SOURCE_LOC_ATTR)) continue;
|
|
1137
|
+
if (tagName[0] === tagName[0].toUpperCase() && tagName[0] !== tagName[0].toLowerCase()) continue;
|
|
1138
|
+
const beforeMatch = code.slice(0, match.index);
|
|
1139
|
+
const lines = beforeMatch.split("\n");
|
|
1140
|
+
const line = lines.length;
|
|
1141
|
+
const column = lines[lines.length - 1].length + 1;
|
|
1142
|
+
const insertPos = match.index + fullMatch.length - (fullMatch.endsWith("/>") ? 2 : 1);
|
|
1143
|
+
const sourceLocAttr = ` ${SOURCE_LOC_ATTR}="${relativePath}:${line}:${column}"`;
|
|
1144
|
+
s.appendLeft(insertPos, sourceLocAttr);
|
|
1145
|
+
hasChanges = true;
|
|
1146
|
+
}
|
|
1147
|
+
if (!hasChanges) return null;
|
|
1148
|
+
return {
|
|
1149
|
+
code: s.toString(),
|
|
1150
|
+
map: s.generateMap({ hires: true })
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
6
1153
|
var AiAnnotatorServer = class {
|
|
7
1154
|
constructor(options = {}) {
|
|
8
1155
|
this.serverProcess = null;
|
|
@@ -12,7 +1159,8 @@ var AiAnnotatorServer = class {
|
|
|
12
1159
|
port,
|
|
13
1160
|
listenAddress,
|
|
14
1161
|
publicAddress: options.publicAddress ?? `http://${listenAddress}:${port}`,
|
|
15
|
-
verbose: options.verbose ?? false
|
|
1162
|
+
verbose: options.verbose ?? false,
|
|
1163
|
+
injectSourceLoc: options.injectSourceLoc ?? true
|
|
16
1164
|
};
|
|
17
1165
|
const currentFileDir = dirname(fileURLToPath(import.meta.url));
|
|
18
1166
|
this.isDevelopment = currentFileDir.endsWith("/src") || currentFileDir.endsWith("\\src");
|
|
@@ -148,22 +1296,39 @@ function injectScriptIntoHtml(html, scriptTag) {
|
|
|
148
1296
|
}
|
|
149
1297
|
function aiAnnotator(options = {}) {
|
|
150
1298
|
let serverManager;
|
|
1299
|
+
let root = process.cwd();
|
|
1300
|
+
const injectSourceLoc = options.injectSourceLoc ?? true;
|
|
151
1301
|
return {
|
|
152
1302
|
name: "vite-plugin-ai-annotator",
|
|
153
1303
|
// Only apply plugin during development (serve command)
|
|
154
1304
|
apply: "serve",
|
|
155
|
-
configResolved() {
|
|
1305
|
+
configResolved(config) {
|
|
156
1306
|
serverManager = new AiAnnotatorServer(options);
|
|
1307
|
+
root = config.root;
|
|
157
1308
|
},
|
|
158
1309
|
async buildStart() {
|
|
159
1310
|
await serverManager.start();
|
|
160
1311
|
},
|
|
1312
|
+
// Transform HTML files to inject source location attributes
|
|
1313
|
+
transform(code, id) {
|
|
1314
|
+
if (!injectSourceLoc) return null;
|
|
1315
|
+
if (!id.endsWith(".html")) return null;
|
|
1316
|
+
if (id.includes("node_modules")) return null;
|
|
1317
|
+
return injectSourceLocations(code, id, root);
|
|
1318
|
+
},
|
|
161
1319
|
// For regular Vite apps (SPA)
|
|
162
|
-
transformIndexHtml(html) {
|
|
1320
|
+
transformIndexHtml(html, ctx) {
|
|
163
1321
|
if (!serverManager || !serverManager.shouldInject()) {
|
|
164
1322
|
return html;
|
|
165
1323
|
}
|
|
166
|
-
|
|
1324
|
+
let result = html;
|
|
1325
|
+
if (injectSourceLoc && ctx.filename) {
|
|
1326
|
+
const transformed = injectSourceLocations(html, ctx.filename, root);
|
|
1327
|
+
if (transformed) {
|
|
1328
|
+
result = transformed.code;
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
return injectScriptIntoHtml(result, serverManager.getInjectScript());
|
|
167
1332
|
},
|
|
168
1333
|
// For SSR frameworks like Nuxt - intercept HTML responses
|
|
169
1334
|
configureServer(server) {
|