vite-plugin-ai-annotator 1.14.2 → 1.14.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -4
- package/dist/annotator/inspection.d.ts +1 -3
- package/dist/annotator-toolbar.d.ts +1 -1
- package/dist/annotator-toolbar.js +144 -196
- package/dist/nuxt-module.d.ts +5 -0
- package/dist/nuxt-module.js +1528 -0
- package/dist/nuxt-module.js.map +7 -0
- package/dist/rpc/define.d.ts +2 -6
- package/package.json +18 -4
|
@@ -0,0 +1,1528 @@
|
|
|
1
|
+
// src/nuxt-module.ts
|
|
2
|
+
import { defineNuxtModule, addVitePlugin, addWebpackPlugin } from "@nuxt/kit";
|
|
3
|
+
|
|
4
|
+
// src/vite-plugin.ts
|
|
5
|
+
import { spawn } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { dirname as dirname2, join as join2, relative } from "node:path";
|
|
8
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
9
|
+
|
|
10
|
+
// node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.mjs
|
|
11
|
+
var comma = ",".charCodeAt(0);
|
|
12
|
+
var semicolon = ";".charCodeAt(0);
|
|
13
|
+
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
14
|
+
var intToChar = new Uint8Array(64);
|
|
15
|
+
var charToInt = new Uint8Array(128);
|
|
16
|
+
for (let i = 0; i < chars.length; i++) {
|
|
17
|
+
const c = chars.charCodeAt(i);
|
|
18
|
+
intToChar[i] = c;
|
|
19
|
+
charToInt[c] = i;
|
|
20
|
+
}
|
|
21
|
+
function encodeInteger(builder, num, relative2) {
|
|
22
|
+
let delta = num - relative2;
|
|
23
|
+
delta = delta < 0 ? -delta << 1 | 1 : delta << 1;
|
|
24
|
+
do {
|
|
25
|
+
let clamped = delta & 31;
|
|
26
|
+
delta >>>= 5;
|
|
27
|
+
if (delta > 0) clamped |= 32;
|
|
28
|
+
builder.write(intToChar[clamped]);
|
|
29
|
+
} while (delta > 0);
|
|
30
|
+
return num;
|
|
31
|
+
}
|
|
32
|
+
var bufLength = 1024 * 16;
|
|
33
|
+
var td = typeof TextDecoder !== "undefined" ? /* @__PURE__ */ new TextDecoder() : typeof Buffer !== "undefined" ? {
|
|
34
|
+
decode(buf) {
|
|
35
|
+
const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
36
|
+
return out.toString();
|
|
37
|
+
}
|
|
38
|
+
} : {
|
|
39
|
+
decode(buf) {
|
|
40
|
+
let out = "";
|
|
41
|
+
for (let i = 0; i < buf.length; i++) {
|
|
42
|
+
out += String.fromCharCode(buf[i]);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var StringWriter = class {
|
|
48
|
+
constructor() {
|
|
49
|
+
this.pos = 0;
|
|
50
|
+
this.out = "";
|
|
51
|
+
this.buffer = new Uint8Array(bufLength);
|
|
52
|
+
}
|
|
53
|
+
write(v) {
|
|
54
|
+
const { buffer } = this;
|
|
55
|
+
buffer[this.pos++] = v;
|
|
56
|
+
if (this.pos === bufLength) {
|
|
57
|
+
this.out += td.decode(buffer);
|
|
58
|
+
this.pos = 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
flush() {
|
|
62
|
+
const { buffer, out, pos } = this;
|
|
63
|
+
return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
function encode(decoded) {
|
|
67
|
+
const writer = new StringWriter();
|
|
68
|
+
let sourcesIndex = 0;
|
|
69
|
+
let sourceLine = 0;
|
|
70
|
+
let sourceColumn = 0;
|
|
71
|
+
let namesIndex = 0;
|
|
72
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
73
|
+
const line = decoded[i];
|
|
74
|
+
if (i > 0) writer.write(semicolon);
|
|
75
|
+
if (line.length === 0) continue;
|
|
76
|
+
let genColumn = 0;
|
|
77
|
+
for (let j = 0; j < line.length; j++) {
|
|
78
|
+
const segment = line[j];
|
|
79
|
+
if (j > 0) writer.write(comma);
|
|
80
|
+
genColumn = encodeInteger(writer, segment[0], genColumn);
|
|
81
|
+
if (segment.length === 1) continue;
|
|
82
|
+
sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
|
|
83
|
+
sourceLine = encodeInteger(writer, segment[2], sourceLine);
|
|
84
|
+
sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
|
|
85
|
+
if (segment.length === 4) continue;
|
|
86
|
+
namesIndex = encodeInteger(writer, segment[4], namesIndex);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return writer.flush();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// node_modules/magic-string/dist/magic-string.es.mjs
|
|
93
|
+
var BitSet = class _BitSet {
|
|
94
|
+
constructor(arg) {
|
|
95
|
+
this.bits = arg instanceof _BitSet ? arg.bits.slice() : [];
|
|
96
|
+
}
|
|
97
|
+
add(n2) {
|
|
98
|
+
this.bits[n2 >> 5] |= 1 << (n2 & 31);
|
|
99
|
+
}
|
|
100
|
+
has(n2) {
|
|
101
|
+
return !!(this.bits[n2 >> 5] & 1 << (n2 & 31));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var Chunk = class _Chunk {
|
|
105
|
+
constructor(start, end, content) {
|
|
106
|
+
this.start = start;
|
|
107
|
+
this.end = end;
|
|
108
|
+
this.original = content;
|
|
109
|
+
this.intro = "";
|
|
110
|
+
this.outro = "";
|
|
111
|
+
this.content = content;
|
|
112
|
+
this.storeName = false;
|
|
113
|
+
this.edited = false;
|
|
114
|
+
{
|
|
115
|
+
this.previous = null;
|
|
116
|
+
this.next = null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
appendLeft(content) {
|
|
120
|
+
this.outro += content;
|
|
121
|
+
}
|
|
122
|
+
appendRight(content) {
|
|
123
|
+
this.intro = this.intro + content;
|
|
124
|
+
}
|
|
125
|
+
clone() {
|
|
126
|
+
const chunk = new _Chunk(this.start, this.end, this.original);
|
|
127
|
+
chunk.intro = this.intro;
|
|
128
|
+
chunk.outro = this.outro;
|
|
129
|
+
chunk.content = this.content;
|
|
130
|
+
chunk.storeName = this.storeName;
|
|
131
|
+
chunk.edited = this.edited;
|
|
132
|
+
return chunk;
|
|
133
|
+
}
|
|
134
|
+
contains(index) {
|
|
135
|
+
return this.start < index && index < this.end;
|
|
136
|
+
}
|
|
137
|
+
eachNext(fn) {
|
|
138
|
+
let chunk = this;
|
|
139
|
+
while (chunk) {
|
|
140
|
+
fn(chunk);
|
|
141
|
+
chunk = chunk.next;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
eachPrevious(fn) {
|
|
145
|
+
let chunk = this;
|
|
146
|
+
while (chunk) {
|
|
147
|
+
fn(chunk);
|
|
148
|
+
chunk = chunk.previous;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
edit(content, storeName, contentOnly) {
|
|
152
|
+
this.content = content;
|
|
153
|
+
if (!contentOnly) {
|
|
154
|
+
this.intro = "";
|
|
155
|
+
this.outro = "";
|
|
156
|
+
}
|
|
157
|
+
this.storeName = storeName;
|
|
158
|
+
this.edited = true;
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
prependLeft(content) {
|
|
162
|
+
this.outro = content + this.outro;
|
|
163
|
+
}
|
|
164
|
+
prependRight(content) {
|
|
165
|
+
this.intro = content + this.intro;
|
|
166
|
+
}
|
|
167
|
+
reset() {
|
|
168
|
+
this.intro = "";
|
|
169
|
+
this.outro = "";
|
|
170
|
+
if (this.edited) {
|
|
171
|
+
this.content = this.original;
|
|
172
|
+
this.storeName = false;
|
|
173
|
+
this.edited = false;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
split(index) {
|
|
177
|
+
const sliceIndex = index - this.start;
|
|
178
|
+
const originalBefore = this.original.slice(0, sliceIndex);
|
|
179
|
+
const originalAfter = this.original.slice(sliceIndex);
|
|
180
|
+
this.original = originalBefore;
|
|
181
|
+
const newChunk = new _Chunk(index, this.end, originalAfter);
|
|
182
|
+
newChunk.outro = this.outro;
|
|
183
|
+
this.outro = "";
|
|
184
|
+
this.end = index;
|
|
185
|
+
if (this.edited) {
|
|
186
|
+
newChunk.edit("", false);
|
|
187
|
+
this.content = "";
|
|
188
|
+
} else {
|
|
189
|
+
this.content = originalBefore;
|
|
190
|
+
}
|
|
191
|
+
newChunk.next = this.next;
|
|
192
|
+
if (newChunk.next) newChunk.next.previous = newChunk;
|
|
193
|
+
newChunk.previous = this;
|
|
194
|
+
this.next = newChunk;
|
|
195
|
+
return newChunk;
|
|
196
|
+
}
|
|
197
|
+
toString() {
|
|
198
|
+
return this.intro + this.content + this.outro;
|
|
199
|
+
}
|
|
200
|
+
trimEnd(rx) {
|
|
201
|
+
this.outro = this.outro.replace(rx, "");
|
|
202
|
+
if (this.outro.length) return true;
|
|
203
|
+
const trimmed = this.content.replace(rx, "");
|
|
204
|
+
if (trimmed.length) {
|
|
205
|
+
if (trimmed !== this.content) {
|
|
206
|
+
this.split(this.start + trimmed.length).edit("", void 0, true);
|
|
207
|
+
if (this.edited) {
|
|
208
|
+
this.edit(trimmed, this.storeName, true);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return true;
|
|
212
|
+
} else {
|
|
213
|
+
this.edit("", void 0, true);
|
|
214
|
+
this.intro = this.intro.replace(rx, "");
|
|
215
|
+
if (this.intro.length) return true;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
trimStart(rx) {
|
|
219
|
+
this.intro = this.intro.replace(rx, "");
|
|
220
|
+
if (this.intro.length) return true;
|
|
221
|
+
const trimmed = this.content.replace(rx, "");
|
|
222
|
+
if (trimmed.length) {
|
|
223
|
+
if (trimmed !== this.content) {
|
|
224
|
+
const newChunk = this.split(this.end - trimmed.length);
|
|
225
|
+
if (this.edited) {
|
|
226
|
+
newChunk.edit(trimmed, this.storeName, true);
|
|
227
|
+
}
|
|
228
|
+
this.edit("", void 0, true);
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
} else {
|
|
232
|
+
this.edit("", void 0, true);
|
|
233
|
+
this.outro = this.outro.replace(rx, "");
|
|
234
|
+
if (this.outro.length) return true;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
function getBtoa() {
|
|
239
|
+
if (typeof globalThis !== "undefined" && typeof globalThis.btoa === "function") {
|
|
240
|
+
return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
|
|
241
|
+
} else if (typeof Buffer === "function") {
|
|
242
|
+
return (str) => Buffer.from(str, "utf-8").toString("base64");
|
|
243
|
+
} else {
|
|
244
|
+
return () => {
|
|
245
|
+
throw new Error("Unsupported environment: `window.btoa` or `Buffer` should be supported.");
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
var btoa = /* @__PURE__ */ getBtoa();
|
|
250
|
+
var SourceMap = class {
|
|
251
|
+
constructor(properties) {
|
|
252
|
+
this.version = 3;
|
|
253
|
+
this.file = properties.file;
|
|
254
|
+
this.sources = properties.sources;
|
|
255
|
+
this.sourcesContent = properties.sourcesContent;
|
|
256
|
+
this.names = properties.names;
|
|
257
|
+
this.mappings = encode(properties.mappings);
|
|
258
|
+
if (typeof properties.x_google_ignoreList !== "undefined") {
|
|
259
|
+
this.x_google_ignoreList = properties.x_google_ignoreList;
|
|
260
|
+
}
|
|
261
|
+
if (typeof properties.debugId !== "undefined") {
|
|
262
|
+
this.debugId = properties.debugId;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
toString() {
|
|
266
|
+
return JSON.stringify(this);
|
|
267
|
+
}
|
|
268
|
+
toUrl() {
|
|
269
|
+
return "data:application/json;charset=utf-8;base64," + btoa(this.toString());
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
function guessIndent(code) {
|
|
273
|
+
const lines = code.split("\n");
|
|
274
|
+
const tabbed = lines.filter((line) => /^\t+/.test(line));
|
|
275
|
+
const spaced = lines.filter((line) => /^ {2,}/.test(line));
|
|
276
|
+
if (tabbed.length === 0 && spaced.length === 0) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
if (tabbed.length >= spaced.length) {
|
|
280
|
+
return " ";
|
|
281
|
+
}
|
|
282
|
+
const min = spaced.reduce((previous, current) => {
|
|
283
|
+
const numSpaces = /^ +/.exec(current)[0].length;
|
|
284
|
+
return Math.min(numSpaces, previous);
|
|
285
|
+
}, Infinity);
|
|
286
|
+
return new Array(min + 1).join(" ");
|
|
287
|
+
}
|
|
288
|
+
function getRelativePath(from, to) {
|
|
289
|
+
const fromParts = from.split(/[/\\]/);
|
|
290
|
+
const toParts = to.split(/[/\\]/);
|
|
291
|
+
fromParts.pop();
|
|
292
|
+
while (fromParts[0] === toParts[0]) {
|
|
293
|
+
fromParts.shift();
|
|
294
|
+
toParts.shift();
|
|
295
|
+
}
|
|
296
|
+
if (fromParts.length) {
|
|
297
|
+
let i = fromParts.length;
|
|
298
|
+
while (i--) fromParts[i] = "..";
|
|
299
|
+
}
|
|
300
|
+
return fromParts.concat(toParts).join("/");
|
|
301
|
+
}
|
|
302
|
+
var toString = Object.prototype.toString;
|
|
303
|
+
function isObject(thing) {
|
|
304
|
+
return toString.call(thing) === "[object Object]";
|
|
305
|
+
}
|
|
306
|
+
function getLocator(source) {
|
|
307
|
+
const originalLines = source.split("\n");
|
|
308
|
+
const lineOffsets = [];
|
|
309
|
+
for (let i = 0, pos = 0; i < originalLines.length; i++) {
|
|
310
|
+
lineOffsets.push(pos);
|
|
311
|
+
pos += originalLines[i].length + 1;
|
|
312
|
+
}
|
|
313
|
+
return function locate(index) {
|
|
314
|
+
let i = 0;
|
|
315
|
+
let j = lineOffsets.length;
|
|
316
|
+
while (i < j) {
|
|
317
|
+
const m = i + j >> 1;
|
|
318
|
+
if (index < lineOffsets[m]) {
|
|
319
|
+
j = m;
|
|
320
|
+
} else {
|
|
321
|
+
i = m + 1;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const line = i - 1;
|
|
325
|
+
const column = index - lineOffsets[line];
|
|
326
|
+
return { line, column };
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
var wordRegex = /\w/;
|
|
330
|
+
var Mappings = class {
|
|
331
|
+
constructor(hires) {
|
|
332
|
+
this.hires = hires;
|
|
333
|
+
this.generatedCodeLine = 0;
|
|
334
|
+
this.generatedCodeColumn = 0;
|
|
335
|
+
this.raw = [];
|
|
336
|
+
this.rawSegments = this.raw[this.generatedCodeLine] = [];
|
|
337
|
+
this.pending = null;
|
|
338
|
+
}
|
|
339
|
+
addEdit(sourceIndex, content, loc, nameIndex) {
|
|
340
|
+
if (content.length) {
|
|
341
|
+
const contentLengthMinusOne = content.length - 1;
|
|
342
|
+
let contentLineEnd = content.indexOf("\n", 0);
|
|
343
|
+
let previousContentLineEnd = -1;
|
|
344
|
+
while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
|
|
345
|
+
const segment2 = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
346
|
+
if (nameIndex >= 0) {
|
|
347
|
+
segment2.push(nameIndex);
|
|
348
|
+
}
|
|
349
|
+
this.rawSegments.push(segment2);
|
|
350
|
+
this.generatedCodeLine += 1;
|
|
351
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
352
|
+
this.generatedCodeColumn = 0;
|
|
353
|
+
previousContentLineEnd = contentLineEnd;
|
|
354
|
+
contentLineEnd = content.indexOf("\n", contentLineEnd + 1);
|
|
355
|
+
}
|
|
356
|
+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
357
|
+
if (nameIndex >= 0) {
|
|
358
|
+
segment.push(nameIndex);
|
|
359
|
+
}
|
|
360
|
+
this.rawSegments.push(segment);
|
|
361
|
+
this.advance(content.slice(previousContentLineEnd + 1));
|
|
362
|
+
} else if (this.pending) {
|
|
363
|
+
this.rawSegments.push(this.pending);
|
|
364
|
+
this.advance(content);
|
|
365
|
+
}
|
|
366
|
+
this.pending = null;
|
|
367
|
+
}
|
|
368
|
+
addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
|
|
369
|
+
let originalCharIndex = chunk.start;
|
|
370
|
+
let first = true;
|
|
371
|
+
let charInHiresBoundary = false;
|
|
372
|
+
while (originalCharIndex < chunk.end) {
|
|
373
|
+
if (original[originalCharIndex] === "\n") {
|
|
374
|
+
loc.line += 1;
|
|
375
|
+
loc.column = 0;
|
|
376
|
+
this.generatedCodeLine += 1;
|
|
377
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
378
|
+
this.generatedCodeColumn = 0;
|
|
379
|
+
first = true;
|
|
380
|
+
charInHiresBoundary = false;
|
|
381
|
+
} else {
|
|
382
|
+
if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
|
|
383
|
+
const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
|
|
384
|
+
if (this.hires === "boundary") {
|
|
385
|
+
if (wordRegex.test(original[originalCharIndex])) {
|
|
386
|
+
if (!charInHiresBoundary) {
|
|
387
|
+
this.rawSegments.push(segment);
|
|
388
|
+
charInHiresBoundary = true;
|
|
389
|
+
}
|
|
390
|
+
} else {
|
|
391
|
+
this.rawSegments.push(segment);
|
|
392
|
+
charInHiresBoundary = false;
|
|
393
|
+
}
|
|
394
|
+
} else {
|
|
395
|
+
this.rawSegments.push(segment);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
loc.column += 1;
|
|
399
|
+
this.generatedCodeColumn += 1;
|
|
400
|
+
first = false;
|
|
401
|
+
}
|
|
402
|
+
originalCharIndex += 1;
|
|
403
|
+
}
|
|
404
|
+
this.pending = null;
|
|
405
|
+
}
|
|
406
|
+
advance(str) {
|
|
407
|
+
if (!str) return;
|
|
408
|
+
const lines = str.split("\n");
|
|
409
|
+
if (lines.length > 1) {
|
|
410
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
411
|
+
this.generatedCodeLine++;
|
|
412
|
+
this.raw[this.generatedCodeLine] = this.rawSegments = [];
|
|
413
|
+
}
|
|
414
|
+
this.generatedCodeColumn = 0;
|
|
415
|
+
}
|
|
416
|
+
this.generatedCodeColumn += lines[lines.length - 1].length;
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
var n = "\n";
|
|
420
|
+
var warned = {
|
|
421
|
+
insertLeft: false,
|
|
422
|
+
insertRight: false,
|
|
423
|
+
storeName: false
|
|
424
|
+
};
|
|
425
|
+
var MagicString = class _MagicString {
|
|
426
|
+
constructor(string, options = {}) {
|
|
427
|
+
const chunk = new Chunk(0, string.length, string);
|
|
428
|
+
Object.defineProperties(this, {
|
|
429
|
+
original: { writable: true, value: string },
|
|
430
|
+
outro: { writable: true, value: "" },
|
|
431
|
+
intro: { writable: true, value: "" },
|
|
432
|
+
firstChunk: { writable: true, value: chunk },
|
|
433
|
+
lastChunk: { writable: true, value: chunk },
|
|
434
|
+
lastSearchedChunk: { writable: true, value: chunk },
|
|
435
|
+
byStart: { writable: true, value: {} },
|
|
436
|
+
byEnd: { writable: true, value: {} },
|
|
437
|
+
filename: { writable: true, value: options.filename },
|
|
438
|
+
indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
|
|
439
|
+
sourcemapLocations: { writable: true, value: new BitSet() },
|
|
440
|
+
storedNames: { writable: true, value: {} },
|
|
441
|
+
indentStr: { writable: true, value: void 0 },
|
|
442
|
+
ignoreList: { writable: true, value: options.ignoreList },
|
|
443
|
+
offset: { writable: true, value: options.offset || 0 }
|
|
444
|
+
});
|
|
445
|
+
this.byStart[0] = chunk;
|
|
446
|
+
this.byEnd[string.length] = chunk;
|
|
447
|
+
}
|
|
448
|
+
addSourcemapLocation(char) {
|
|
449
|
+
this.sourcemapLocations.add(char);
|
|
450
|
+
}
|
|
451
|
+
append(content) {
|
|
452
|
+
if (typeof content !== "string") throw new TypeError("outro content must be a string");
|
|
453
|
+
this.outro += content;
|
|
454
|
+
return this;
|
|
455
|
+
}
|
|
456
|
+
appendLeft(index, content) {
|
|
457
|
+
index = index + this.offset;
|
|
458
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
459
|
+
this._split(index);
|
|
460
|
+
const chunk = this.byEnd[index];
|
|
461
|
+
if (chunk) {
|
|
462
|
+
chunk.appendLeft(content);
|
|
463
|
+
} else {
|
|
464
|
+
this.intro += content;
|
|
465
|
+
}
|
|
466
|
+
return this;
|
|
467
|
+
}
|
|
468
|
+
appendRight(index, content) {
|
|
469
|
+
index = index + this.offset;
|
|
470
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
471
|
+
this._split(index);
|
|
472
|
+
const chunk = this.byStart[index];
|
|
473
|
+
if (chunk) {
|
|
474
|
+
chunk.appendRight(content);
|
|
475
|
+
} else {
|
|
476
|
+
this.outro += content;
|
|
477
|
+
}
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
480
|
+
clone() {
|
|
481
|
+
const cloned = new _MagicString(this.original, { filename: this.filename, offset: this.offset });
|
|
482
|
+
let originalChunk = this.firstChunk;
|
|
483
|
+
let clonedChunk = cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone();
|
|
484
|
+
while (originalChunk) {
|
|
485
|
+
cloned.byStart[clonedChunk.start] = clonedChunk;
|
|
486
|
+
cloned.byEnd[clonedChunk.end] = clonedChunk;
|
|
487
|
+
const nextOriginalChunk = originalChunk.next;
|
|
488
|
+
const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
|
|
489
|
+
if (nextClonedChunk) {
|
|
490
|
+
clonedChunk.next = nextClonedChunk;
|
|
491
|
+
nextClonedChunk.previous = clonedChunk;
|
|
492
|
+
clonedChunk = nextClonedChunk;
|
|
493
|
+
}
|
|
494
|
+
originalChunk = nextOriginalChunk;
|
|
495
|
+
}
|
|
496
|
+
cloned.lastChunk = clonedChunk;
|
|
497
|
+
if (this.indentExclusionRanges) {
|
|
498
|
+
cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
|
|
499
|
+
}
|
|
500
|
+
cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
|
|
501
|
+
cloned.intro = this.intro;
|
|
502
|
+
cloned.outro = this.outro;
|
|
503
|
+
return cloned;
|
|
504
|
+
}
|
|
505
|
+
generateDecodedMap(options) {
|
|
506
|
+
options = options || {};
|
|
507
|
+
const sourceIndex = 0;
|
|
508
|
+
const names = Object.keys(this.storedNames);
|
|
509
|
+
const mappings = new Mappings(options.hires);
|
|
510
|
+
const locate = getLocator(this.original);
|
|
511
|
+
if (this.intro) {
|
|
512
|
+
mappings.advance(this.intro);
|
|
513
|
+
}
|
|
514
|
+
this.firstChunk.eachNext((chunk) => {
|
|
515
|
+
const loc = locate(chunk.start);
|
|
516
|
+
if (chunk.intro.length) mappings.advance(chunk.intro);
|
|
517
|
+
if (chunk.edited) {
|
|
518
|
+
mappings.addEdit(
|
|
519
|
+
sourceIndex,
|
|
520
|
+
chunk.content,
|
|
521
|
+
loc,
|
|
522
|
+
chunk.storeName ? names.indexOf(chunk.original) : -1
|
|
523
|
+
);
|
|
524
|
+
} else {
|
|
525
|
+
mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
|
|
526
|
+
}
|
|
527
|
+
if (chunk.outro.length) mappings.advance(chunk.outro);
|
|
528
|
+
});
|
|
529
|
+
if (this.outro) {
|
|
530
|
+
mappings.advance(this.outro);
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
file: options.file ? options.file.split(/[/\\]/).pop() : void 0,
|
|
534
|
+
sources: [
|
|
535
|
+
options.source ? getRelativePath(options.file || "", options.source) : options.file || ""
|
|
536
|
+
],
|
|
537
|
+
sourcesContent: options.includeContent ? [this.original] : void 0,
|
|
538
|
+
names,
|
|
539
|
+
mappings: mappings.raw,
|
|
540
|
+
x_google_ignoreList: this.ignoreList ? [sourceIndex] : void 0
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
generateMap(options) {
|
|
544
|
+
return new SourceMap(this.generateDecodedMap(options));
|
|
545
|
+
}
|
|
546
|
+
_ensureindentStr() {
|
|
547
|
+
if (this.indentStr === void 0) {
|
|
548
|
+
this.indentStr = guessIndent(this.original);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
_getRawIndentString() {
|
|
552
|
+
this._ensureindentStr();
|
|
553
|
+
return this.indentStr;
|
|
554
|
+
}
|
|
555
|
+
getIndentString() {
|
|
556
|
+
this._ensureindentStr();
|
|
557
|
+
return this.indentStr === null ? " " : this.indentStr;
|
|
558
|
+
}
|
|
559
|
+
indent(indentStr, options) {
|
|
560
|
+
const pattern = /^[^\r\n]/gm;
|
|
561
|
+
if (isObject(indentStr)) {
|
|
562
|
+
options = indentStr;
|
|
563
|
+
indentStr = void 0;
|
|
564
|
+
}
|
|
565
|
+
if (indentStr === void 0) {
|
|
566
|
+
this._ensureindentStr();
|
|
567
|
+
indentStr = this.indentStr || " ";
|
|
568
|
+
}
|
|
569
|
+
if (indentStr === "") return this;
|
|
570
|
+
options = options || {};
|
|
571
|
+
const isExcluded = {};
|
|
572
|
+
if (options.exclude) {
|
|
573
|
+
const exclusions = typeof options.exclude[0] === "number" ? [options.exclude] : options.exclude;
|
|
574
|
+
exclusions.forEach((exclusion) => {
|
|
575
|
+
for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
|
|
576
|
+
isExcluded[i] = true;
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
let shouldIndentNextCharacter = options.indentStart !== false;
|
|
581
|
+
const replacer = (match) => {
|
|
582
|
+
if (shouldIndentNextCharacter) return `${indentStr}${match}`;
|
|
583
|
+
shouldIndentNextCharacter = true;
|
|
584
|
+
return match;
|
|
585
|
+
};
|
|
586
|
+
this.intro = this.intro.replace(pattern, replacer);
|
|
587
|
+
let charIndex = 0;
|
|
588
|
+
let chunk = this.firstChunk;
|
|
589
|
+
while (chunk) {
|
|
590
|
+
const end = chunk.end;
|
|
591
|
+
if (chunk.edited) {
|
|
592
|
+
if (!isExcluded[charIndex]) {
|
|
593
|
+
chunk.content = chunk.content.replace(pattern, replacer);
|
|
594
|
+
if (chunk.content.length) {
|
|
595
|
+
shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === "\n";
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
} else {
|
|
599
|
+
charIndex = chunk.start;
|
|
600
|
+
while (charIndex < end) {
|
|
601
|
+
if (!isExcluded[charIndex]) {
|
|
602
|
+
const char = this.original[charIndex];
|
|
603
|
+
if (char === "\n") {
|
|
604
|
+
shouldIndentNextCharacter = true;
|
|
605
|
+
} else if (char !== "\r" && shouldIndentNextCharacter) {
|
|
606
|
+
shouldIndentNextCharacter = false;
|
|
607
|
+
if (charIndex === chunk.start) {
|
|
608
|
+
chunk.prependRight(indentStr);
|
|
609
|
+
} else {
|
|
610
|
+
this._splitChunk(chunk, charIndex);
|
|
611
|
+
chunk = chunk.next;
|
|
612
|
+
chunk.prependRight(indentStr);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
charIndex += 1;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
charIndex = chunk.end;
|
|
620
|
+
chunk = chunk.next;
|
|
621
|
+
}
|
|
622
|
+
this.outro = this.outro.replace(pattern, replacer);
|
|
623
|
+
return this;
|
|
624
|
+
}
|
|
625
|
+
insert() {
|
|
626
|
+
throw new Error(
|
|
627
|
+
"magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)"
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
insertLeft(index, content) {
|
|
631
|
+
if (!warned.insertLeft) {
|
|
632
|
+
console.warn(
|
|
633
|
+
"magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead"
|
|
634
|
+
);
|
|
635
|
+
warned.insertLeft = true;
|
|
636
|
+
}
|
|
637
|
+
return this.appendLeft(index, content);
|
|
638
|
+
}
|
|
639
|
+
insertRight(index, content) {
|
|
640
|
+
if (!warned.insertRight) {
|
|
641
|
+
console.warn(
|
|
642
|
+
"magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead"
|
|
643
|
+
);
|
|
644
|
+
warned.insertRight = true;
|
|
645
|
+
}
|
|
646
|
+
return this.prependRight(index, content);
|
|
647
|
+
}
|
|
648
|
+
move(start, end, index) {
|
|
649
|
+
start = start + this.offset;
|
|
650
|
+
end = end + this.offset;
|
|
651
|
+
index = index + this.offset;
|
|
652
|
+
if (index >= start && index <= end) throw new Error("Cannot move a selection inside itself");
|
|
653
|
+
this._split(start);
|
|
654
|
+
this._split(end);
|
|
655
|
+
this._split(index);
|
|
656
|
+
const first = this.byStart[start];
|
|
657
|
+
const last = this.byEnd[end];
|
|
658
|
+
const oldLeft = first.previous;
|
|
659
|
+
const oldRight = last.next;
|
|
660
|
+
const newRight = this.byStart[index];
|
|
661
|
+
if (!newRight && last === this.lastChunk) return this;
|
|
662
|
+
const newLeft = newRight ? newRight.previous : this.lastChunk;
|
|
663
|
+
if (oldLeft) oldLeft.next = oldRight;
|
|
664
|
+
if (oldRight) oldRight.previous = oldLeft;
|
|
665
|
+
if (newLeft) newLeft.next = first;
|
|
666
|
+
if (newRight) newRight.previous = last;
|
|
667
|
+
if (!first.previous) this.firstChunk = last.next;
|
|
668
|
+
if (!last.next) {
|
|
669
|
+
this.lastChunk = first.previous;
|
|
670
|
+
this.lastChunk.next = null;
|
|
671
|
+
}
|
|
672
|
+
first.previous = newLeft;
|
|
673
|
+
last.next = newRight || null;
|
|
674
|
+
if (!newLeft) this.firstChunk = first;
|
|
675
|
+
if (!newRight) this.lastChunk = last;
|
|
676
|
+
return this;
|
|
677
|
+
}
|
|
678
|
+
overwrite(start, end, content, options) {
|
|
679
|
+
options = options || {};
|
|
680
|
+
return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
|
|
681
|
+
}
|
|
682
|
+
update(start, end, content, options) {
|
|
683
|
+
start = start + this.offset;
|
|
684
|
+
end = end + this.offset;
|
|
685
|
+
if (typeof content !== "string") throw new TypeError("replacement content must be a string");
|
|
686
|
+
if (this.original.length !== 0) {
|
|
687
|
+
while (start < 0) start += this.original.length;
|
|
688
|
+
while (end < 0) end += this.original.length;
|
|
689
|
+
}
|
|
690
|
+
if (end > this.original.length) throw new Error("end is out of bounds");
|
|
691
|
+
if (start === end)
|
|
692
|
+
throw new Error(
|
|
693
|
+
"Cannot overwrite a zero-length range \u2013 use appendLeft or prependRight instead"
|
|
694
|
+
);
|
|
695
|
+
this._split(start);
|
|
696
|
+
this._split(end);
|
|
697
|
+
if (options === true) {
|
|
698
|
+
if (!warned.storeName) {
|
|
699
|
+
console.warn(
|
|
700
|
+
"The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string"
|
|
701
|
+
);
|
|
702
|
+
warned.storeName = true;
|
|
703
|
+
}
|
|
704
|
+
options = { storeName: true };
|
|
705
|
+
}
|
|
706
|
+
const storeName = options !== void 0 ? options.storeName : false;
|
|
707
|
+
const overwrite = options !== void 0 ? options.overwrite : false;
|
|
708
|
+
if (storeName) {
|
|
709
|
+
const original = this.original.slice(start, end);
|
|
710
|
+
Object.defineProperty(this.storedNames, original, {
|
|
711
|
+
writable: true,
|
|
712
|
+
value: true,
|
|
713
|
+
enumerable: true
|
|
714
|
+
});
|
|
715
|
+
}
|
|
716
|
+
const first = this.byStart[start];
|
|
717
|
+
const last = this.byEnd[end];
|
|
718
|
+
if (first) {
|
|
719
|
+
let chunk = first;
|
|
720
|
+
while (chunk !== last) {
|
|
721
|
+
if (chunk.next !== this.byStart[chunk.end]) {
|
|
722
|
+
throw new Error("Cannot overwrite across a split point");
|
|
723
|
+
}
|
|
724
|
+
chunk = chunk.next;
|
|
725
|
+
chunk.edit("", false);
|
|
726
|
+
}
|
|
727
|
+
first.edit(content, storeName, !overwrite);
|
|
728
|
+
} else {
|
|
729
|
+
const newChunk = new Chunk(start, end, "").edit(content, storeName);
|
|
730
|
+
last.next = newChunk;
|
|
731
|
+
newChunk.previous = last;
|
|
732
|
+
}
|
|
733
|
+
return this;
|
|
734
|
+
}
|
|
735
|
+
prepend(content) {
|
|
736
|
+
if (typeof content !== "string") throw new TypeError("outro content must be a string");
|
|
737
|
+
this.intro = content + this.intro;
|
|
738
|
+
return this;
|
|
739
|
+
}
|
|
740
|
+
prependLeft(index, content) {
|
|
741
|
+
index = index + this.offset;
|
|
742
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
743
|
+
this._split(index);
|
|
744
|
+
const chunk = this.byEnd[index];
|
|
745
|
+
if (chunk) {
|
|
746
|
+
chunk.prependLeft(content);
|
|
747
|
+
} else {
|
|
748
|
+
this.intro = content + this.intro;
|
|
749
|
+
}
|
|
750
|
+
return this;
|
|
751
|
+
}
|
|
752
|
+
prependRight(index, content) {
|
|
753
|
+
index = index + this.offset;
|
|
754
|
+
if (typeof content !== "string") throw new TypeError("inserted content must be a string");
|
|
755
|
+
this._split(index);
|
|
756
|
+
const chunk = this.byStart[index];
|
|
757
|
+
if (chunk) {
|
|
758
|
+
chunk.prependRight(content);
|
|
759
|
+
} else {
|
|
760
|
+
this.outro = content + this.outro;
|
|
761
|
+
}
|
|
762
|
+
return this;
|
|
763
|
+
}
|
|
764
|
+
remove(start, end) {
|
|
765
|
+
start = start + this.offset;
|
|
766
|
+
end = end + this.offset;
|
|
767
|
+
if (this.original.length !== 0) {
|
|
768
|
+
while (start < 0) start += this.original.length;
|
|
769
|
+
while (end < 0) end += this.original.length;
|
|
770
|
+
}
|
|
771
|
+
if (start === end) return this;
|
|
772
|
+
if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds");
|
|
773
|
+
if (start > end) throw new Error("end must be greater than start");
|
|
774
|
+
this._split(start);
|
|
775
|
+
this._split(end);
|
|
776
|
+
let chunk = this.byStart[start];
|
|
777
|
+
while (chunk) {
|
|
778
|
+
chunk.intro = "";
|
|
779
|
+
chunk.outro = "";
|
|
780
|
+
chunk.edit("");
|
|
781
|
+
chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
|
782
|
+
}
|
|
783
|
+
return this;
|
|
784
|
+
}
|
|
785
|
+
reset(start, end) {
|
|
786
|
+
start = start + this.offset;
|
|
787
|
+
end = end + this.offset;
|
|
788
|
+
if (this.original.length !== 0) {
|
|
789
|
+
while (start < 0) start += this.original.length;
|
|
790
|
+
while (end < 0) end += this.original.length;
|
|
791
|
+
}
|
|
792
|
+
if (start === end) return this;
|
|
793
|
+
if (start < 0 || end > this.original.length) throw new Error("Character is out of bounds");
|
|
794
|
+
if (start > end) throw new Error("end must be greater than start");
|
|
795
|
+
this._split(start);
|
|
796
|
+
this._split(end);
|
|
797
|
+
let chunk = this.byStart[start];
|
|
798
|
+
while (chunk) {
|
|
799
|
+
chunk.reset();
|
|
800
|
+
chunk = end > chunk.end ? this.byStart[chunk.end] : null;
|
|
801
|
+
}
|
|
802
|
+
return this;
|
|
803
|
+
}
|
|
804
|
+
lastChar() {
|
|
805
|
+
if (this.outro.length) return this.outro[this.outro.length - 1];
|
|
806
|
+
let chunk = this.lastChunk;
|
|
807
|
+
do {
|
|
808
|
+
if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
|
|
809
|
+
if (chunk.content.length) return chunk.content[chunk.content.length - 1];
|
|
810
|
+
if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
|
|
811
|
+
} while (chunk = chunk.previous);
|
|
812
|
+
if (this.intro.length) return this.intro[this.intro.length - 1];
|
|
813
|
+
return "";
|
|
814
|
+
}
|
|
815
|
+
lastLine() {
|
|
816
|
+
let lineIndex = this.outro.lastIndexOf(n);
|
|
817
|
+
if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
|
|
818
|
+
let lineStr = this.outro;
|
|
819
|
+
let chunk = this.lastChunk;
|
|
820
|
+
do {
|
|
821
|
+
if (chunk.outro.length > 0) {
|
|
822
|
+
lineIndex = chunk.outro.lastIndexOf(n);
|
|
823
|
+
if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
|
|
824
|
+
lineStr = chunk.outro + lineStr;
|
|
825
|
+
}
|
|
826
|
+
if (chunk.content.length > 0) {
|
|
827
|
+
lineIndex = chunk.content.lastIndexOf(n);
|
|
828
|
+
if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
|
|
829
|
+
lineStr = chunk.content + lineStr;
|
|
830
|
+
}
|
|
831
|
+
if (chunk.intro.length > 0) {
|
|
832
|
+
lineIndex = chunk.intro.lastIndexOf(n);
|
|
833
|
+
if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
|
|
834
|
+
lineStr = chunk.intro + lineStr;
|
|
835
|
+
}
|
|
836
|
+
} while (chunk = chunk.previous);
|
|
837
|
+
lineIndex = this.intro.lastIndexOf(n);
|
|
838
|
+
if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
|
|
839
|
+
return this.intro + lineStr;
|
|
840
|
+
}
|
|
841
|
+
slice(start = 0, end = this.original.length - this.offset) {
|
|
842
|
+
start = start + this.offset;
|
|
843
|
+
end = end + this.offset;
|
|
844
|
+
if (this.original.length !== 0) {
|
|
845
|
+
while (start < 0) start += this.original.length;
|
|
846
|
+
while (end < 0) end += this.original.length;
|
|
847
|
+
}
|
|
848
|
+
let result = "";
|
|
849
|
+
let chunk = this.firstChunk;
|
|
850
|
+
while (chunk && (chunk.start > start || chunk.end <= start)) {
|
|
851
|
+
if (chunk.start < end && chunk.end >= end) {
|
|
852
|
+
return result;
|
|
853
|
+
}
|
|
854
|
+
chunk = chunk.next;
|
|
855
|
+
}
|
|
856
|
+
if (chunk && chunk.edited && chunk.start !== start)
|
|
857
|
+
throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
|
|
858
|
+
const startChunk = chunk;
|
|
859
|
+
while (chunk) {
|
|
860
|
+
if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
|
|
861
|
+
result += chunk.intro;
|
|
862
|
+
}
|
|
863
|
+
const containsEnd = chunk.start < end && chunk.end >= end;
|
|
864
|
+
if (containsEnd && chunk.edited && chunk.end !== end)
|
|
865
|
+
throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
|
|
866
|
+
const sliceStart = startChunk === chunk ? start - chunk.start : 0;
|
|
867
|
+
const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
|
|
868
|
+
result += chunk.content.slice(sliceStart, sliceEnd);
|
|
869
|
+
if (chunk.outro && (!containsEnd || chunk.end === end)) {
|
|
870
|
+
result += chunk.outro;
|
|
871
|
+
}
|
|
872
|
+
if (containsEnd) {
|
|
873
|
+
break;
|
|
874
|
+
}
|
|
875
|
+
chunk = chunk.next;
|
|
876
|
+
}
|
|
877
|
+
return result;
|
|
878
|
+
}
|
|
879
|
+
// TODO deprecate this? not really very useful
|
|
880
|
+
snip(start, end) {
|
|
881
|
+
const clone = this.clone();
|
|
882
|
+
clone.remove(0, start);
|
|
883
|
+
clone.remove(end, clone.original.length);
|
|
884
|
+
return clone;
|
|
885
|
+
}
|
|
886
|
+
_split(index) {
|
|
887
|
+
if (this.byStart[index] || this.byEnd[index]) return;
|
|
888
|
+
let chunk = this.lastSearchedChunk;
|
|
889
|
+
let previousChunk = chunk;
|
|
890
|
+
const searchForward = index > chunk.end;
|
|
891
|
+
while (chunk) {
|
|
892
|
+
if (chunk.contains(index)) return this._splitChunk(chunk, index);
|
|
893
|
+
chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
|
|
894
|
+
if (chunk === previousChunk) return;
|
|
895
|
+
previousChunk = chunk;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
_splitChunk(chunk, index) {
|
|
899
|
+
if (chunk.edited && chunk.content.length) {
|
|
900
|
+
const loc = getLocator(this.original)(index);
|
|
901
|
+
throw new Error(
|
|
902
|
+
`Cannot split a chunk that has already been edited (${loc.line}:${loc.column} \u2013 "${chunk.original}")`
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
const newChunk = chunk.split(index);
|
|
906
|
+
this.byEnd[index] = chunk;
|
|
907
|
+
this.byStart[index] = newChunk;
|
|
908
|
+
this.byEnd[newChunk.end] = newChunk;
|
|
909
|
+
if (chunk === this.lastChunk) this.lastChunk = newChunk;
|
|
910
|
+
this.lastSearchedChunk = chunk;
|
|
911
|
+
return true;
|
|
912
|
+
}
|
|
913
|
+
toString() {
|
|
914
|
+
let str = this.intro;
|
|
915
|
+
let chunk = this.firstChunk;
|
|
916
|
+
while (chunk) {
|
|
917
|
+
str += chunk.toString();
|
|
918
|
+
chunk = chunk.next;
|
|
919
|
+
}
|
|
920
|
+
return str + this.outro;
|
|
921
|
+
}
|
|
922
|
+
isEmpty() {
|
|
923
|
+
let chunk = this.firstChunk;
|
|
924
|
+
do {
|
|
925
|
+
if (chunk.intro.length && chunk.intro.trim() || chunk.content.length && chunk.content.trim() || chunk.outro.length && chunk.outro.trim())
|
|
926
|
+
return false;
|
|
927
|
+
} while (chunk = chunk.next);
|
|
928
|
+
return true;
|
|
929
|
+
}
|
|
930
|
+
length() {
|
|
931
|
+
let chunk = this.firstChunk;
|
|
932
|
+
let length = 0;
|
|
933
|
+
do {
|
|
934
|
+
length += chunk.intro.length + chunk.content.length + chunk.outro.length;
|
|
935
|
+
} while (chunk = chunk.next);
|
|
936
|
+
return length;
|
|
937
|
+
}
|
|
938
|
+
trimLines() {
|
|
939
|
+
return this.trim("[\\r\\n]");
|
|
940
|
+
}
|
|
941
|
+
trim(charType) {
|
|
942
|
+
return this.trimStart(charType).trimEnd(charType);
|
|
943
|
+
}
|
|
944
|
+
trimEndAborted(charType) {
|
|
945
|
+
const rx = new RegExp((charType || "\\s") + "+$");
|
|
946
|
+
this.outro = this.outro.replace(rx, "");
|
|
947
|
+
if (this.outro.length) return true;
|
|
948
|
+
let chunk = this.lastChunk;
|
|
949
|
+
do {
|
|
950
|
+
const end = chunk.end;
|
|
951
|
+
const aborted = chunk.trimEnd(rx);
|
|
952
|
+
if (chunk.end !== end) {
|
|
953
|
+
if (this.lastChunk === chunk) {
|
|
954
|
+
this.lastChunk = chunk.next;
|
|
955
|
+
}
|
|
956
|
+
this.byEnd[chunk.end] = chunk;
|
|
957
|
+
this.byStart[chunk.next.start] = chunk.next;
|
|
958
|
+
this.byEnd[chunk.next.end] = chunk.next;
|
|
959
|
+
}
|
|
960
|
+
if (aborted) return true;
|
|
961
|
+
chunk = chunk.previous;
|
|
962
|
+
} while (chunk);
|
|
963
|
+
return false;
|
|
964
|
+
}
|
|
965
|
+
trimEnd(charType) {
|
|
966
|
+
this.trimEndAborted(charType);
|
|
967
|
+
return this;
|
|
968
|
+
}
|
|
969
|
+
trimStartAborted(charType) {
|
|
970
|
+
const rx = new RegExp("^" + (charType || "\\s") + "+");
|
|
971
|
+
this.intro = this.intro.replace(rx, "");
|
|
972
|
+
if (this.intro.length) return true;
|
|
973
|
+
let chunk = this.firstChunk;
|
|
974
|
+
do {
|
|
975
|
+
const end = chunk.end;
|
|
976
|
+
const aborted = chunk.trimStart(rx);
|
|
977
|
+
if (chunk.end !== end) {
|
|
978
|
+
if (chunk === this.lastChunk) this.lastChunk = chunk.next;
|
|
979
|
+
this.byEnd[chunk.end] = chunk;
|
|
980
|
+
this.byStart[chunk.next.start] = chunk.next;
|
|
981
|
+
this.byEnd[chunk.next.end] = chunk.next;
|
|
982
|
+
}
|
|
983
|
+
if (aborted) return true;
|
|
984
|
+
chunk = chunk.next;
|
|
985
|
+
} while (chunk);
|
|
986
|
+
return false;
|
|
987
|
+
}
|
|
988
|
+
trimStart(charType) {
|
|
989
|
+
this.trimStartAborted(charType);
|
|
990
|
+
return this;
|
|
991
|
+
}
|
|
992
|
+
hasChanged() {
|
|
993
|
+
return this.original !== this.toString();
|
|
994
|
+
}
|
|
995
|
+
_replaceRegexp(searchValue, replacement) {
|
|
996
|
+
function getReplacement(match, str) {
|
|
997
|
+
if (typeof replacement === "string") {
|
|
998
|
+
return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
|
|
999
|
+
if (i === "$") return "$";
|
|
1000
|
+
if (i === "&") return match[0];
|
|
1001
|
+
const num = +i;
|
|
1002
|
+
if (num < match.length) return match[+i];
|
|
1003
|
+
return `$${i}`;
|
|
1004
|
+
});
|
|
1005
|
+
} else {
|
|
1006
|
+
return replacement(...match, match.index, str, match.groups);
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
function matchAll(re, str) {
|
|
1010
|
+
let match;
|
|
1011
|
+
const matches = [];
|
|
1012
|
+
while (match = re.exec(str)) {
|
|
1013
|
+
matches.push(match);
|
|
1014
|
+
}
|
|
1015
|
+
return matches;
|
|
1016
|
+
}
|
|
1017
|
+
if (searchValue.global) {
|
|
1018
|
+
const matches = matchAll(searchValue, this.original);
|
|
1019
|
+
matches.forEach((match) => {
|
|
1020
|
+
if (match.index != null) {
|
|
1021
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1022
|
+
if (replacement2 !== match[0]) {
|
|
1023
|
+
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
});
|
|
1027
|
+
} else {
|
|
1028
|
+
const match = this.original.match(searchValue);
|
|
1029
|
+
if (match && match.index != null) {
|
|
1030
|
+
const replacement2 = getReplacement(match, this.original);
|
|
1031
|
+
if (replacement2 !== match[0]) {
|
|
1032
|
+
this.overwrite(match.index, match.index + match[0].length, replacement2);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
return this;
|
|
1037
|
+
}
|
|
1038
|
+
_replaceString(string, replacement) {
|
|
1039
|
+
const { original } = this;
|
|
1040
|
+
const index = original.indexOf(string);
|
|
1041
|
+
if (index !== -1) {
|
|
1042
|
+
if (typeof replacement === "function") {
|
|
1043
|
+
replacement = replacement(string, index, original);
|
|
1044
|
+
}
|
|
1045
|
+
if (string !== replacement) {
|
|
1046
|
+
this.overwrite(index, index + string.length, replacement);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return this;
|
|
1050
|
+
}
|
|
1051
|
+
replace(searchValue, replacement) {
|
|
1052
|
+
if (typeof searchValue === "string") {
|
|
1053
|
+
return this._replaceString(searchValue, replacement);
|
|
1054
|
+
}
|
|
1055
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1056
|
+
}
|
|
1057
|
+
_replaceAllString(string, replacement) {
|
|
1058
|
+
const { original } = this;
|
|
1059
|
+
const stringLength = string.length;
|
|
1060
|
+
for (let index = original.indexOf(string); index !== -1; index = original.indexOf(string, index + stringLength)) {
|
|
1061
|
+
const previous = original.slice(index, index + stringLength);
|
|
1062
|
+
let _replacement = replacement;
|
|
1063
|
+
if (typeof replacement === "function") {
|
|
1064
|
+
_replacement = replacement(previous, index, original);
|
|
1065
|
+
}
|
|
1066
|
+
if (previous !== _replacement) this.overwrite(index, index + stringLength, _replacement);
|
|
1067
|
+
}
|
|
1068
|
+
return this;
|
|
1069
|
+
}
|
|
1070
|
+
replaceAll(searchValue, replacement) {
|
|
1071
|
+
if (typeof searchValue === "string") {
|
|
1072
|
+
return this._replaceAllString(searchValue, replacement);
|
|
1073
|
+
}
|
|
1074
|
+
if (!searchValue.global) {
|
|
1075
|
+
throw new TypeError(
|
|
1076
|
+
"MagicString.prototype.replaceAll called with a non-global RegExp argument"
|
|
1077
|
+
);
|
|
1078
|
+
}
|
|
1079
|
+
return this._replaceRegexp(searchValue, replacement);
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
|
|
1083
|
+
// src/auto-setup-mcp.ts
|
|
1084
|
+
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
1085
|
+
import { join, dirname } from "node:path";
|
|
1086
|
+
var MCP_SERVER_NAME = "ai-annotator";
|
|
1087
|
+
function detectPackageManager(projectRoot) {
|
|
1088
|
+
if (existsSync(join(projectRoot, "bun.lockb")) || existsSync(join(projectRoot, "bun.lock"))) {
|
|
1089
|
+
return "bunx";
|
|
1090
|
+
}
|
|
1091
|
+
if (existsSync(join(projectRoot, "pnpm-lock.yaml"))) {
|
|
1092
|
+
return "pnpm dlx";
|
|
1093
|
+
}
|
|
1094
|
+
return "npx";
|
|
1095
|
+
}
|
|
1096
|
+
function buildMcpServerConfig(packageManager, serverUrl) {
|
|
1097
|
+
const baseArgs = ["vite-plugin-ai-annotator", "mcp", "-s", serverUrl];
|
|
1098
|
+
if (packageManager === "pnpm dlx") {
|
|
1099
|
+
return {
|
|
1100
|
+
command: "pnpm",
|
|
1101
|
+
args: ["dlx", ...baseArgs]
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
return {
|
|
1105
|
+
command: packageManager,
|
|
1106
|
+
args: baseArgs
|
|
1107
|
+
};
|
|
1108
|
+
}
|
|
1109
|
+
function readJsonFile(filePath) {
|
|
1110
|
+
try {
|
|
1111
|
+
const content = readFileSync(filePath, "utf-8");
|
|
1112
|
+
return JSON.parse(content);
|
|
1113
|
+
} catch {
|
|
1114
|
+
return null;
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
function writeJsonFile(filePath, data) {
|
|
1118
|
+
const dir = dirname(filePath);
|
|
1119
|
+
if (!existsSync(dir)) {
|
|
1120
|
+
mkdirSync(dir, { recursive: true });
|
|
1121
|
+
}
|
|
1122
|
+
writeFileSync(filePath, JSON.stringify(data, null, 2) + "\n");
|
|
1123
|
+
}
|
|
1124
|
+
function setupConfigFile(filePath, serverConfig, verbose) {
|
|
1125
|
+
const existingConfig = readJsonFile(filePath);
|
|
1126
|
+
if (existingConfig) {
|
|
1127
|
+
const existing = existingConfig.mcpServers?.[MCP_SERVER_NAME];
|
|
1128
|
+
if (existing && existing.command === serverConfig.command && JSON.stringify(existing.args) === JSON.stringify(serverConfig.args)) {
|
|
1129
|
+
if (verbose) {
|
|
1130
|
+
console.log(`[ai-annotator] ${filePath} already configured`);
|
|
1131
|
+
}
|
|
1132
|
+
return false;
|
|
1133
|
+
}
|
|
1134
|
+
existingConfig.mcpServers = existingConfig.mcpServers || {};
|
|
1135
|
+
existingConfig.mcpServers[MCP_SERVER_NAME] = serverConfig;
|
|
1136
|
+
writeJsonFile(filePath, existingConfig);
|
|
1137
|
+
if (verbose) {
|
|
1138
|
+
console.log(`[ai-annotator] Updated ${filePath}`);
|
|
1139
|
+
}
|
|
1140
|
+
return true;
|
|
1141
|
+
}
|
|
1142
|
+
const newConfig = {
|
|
1143
|
+
mcpServers: {
|
|
1144
|
+
[MCP_SERVER_NAME]: serverConfig
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
writeJsonFile(filePath, newConfig);
|
|
1148
|
+
if (verbose) {
|
|
1149
|
+
console.log(`[ai-annotator] Created ${filePath}`);
|
|
1150
|
+
}
|
|
1151
|
+
return true;
|
|
1152
|
+
}
|
|
1153
|
+
function detectExistingConfigs(projectRoot) {
|
|
1154
|
+
const potentialConfigs = [
|
|
1155
|
+
join(projectRoot, ".mcp.json"),
|
|
1156
|
+
join(projectRoot, ".cursor", "mcp.json"),
|
|
1157
|
+
join(projectRoot, ".vscode", "mcp.json")
|
|
1158
|
+
];
|
|
1159
|
+
return potentialConfigs.filter(existsSync);
|
|
1160
|
+
}
|
|
1161
|
+
function autoSetupMcp(options) {
|
|
1162
|
+
const { projectRoot, serverUrl, verbose = false } = options;
|
|
1163
|
+
const result = {
|
|
1164
|
+
updated: [],
|
|
1165
|
+
alreadyConfigured: []
|
|
1166
|
+
};
|
|
1167
|
+
const packageManager = detectPackageManager(projectRoot);
|
|
1168
|
+
const serverConfig = buildMcpServerConfig(packageManager, serverUrl);
|
|
1169
|
+
if (verbose) {
|
|
1170
|
+
console.log(`[ai-annotator] Detected package manager: ${packageManager}`);
|
|
1171
|
+
}
|
|
1172
|
+
const existingConfigs = detectExistingConfigs(projectRoot);
|
|
1173
|
+
if (existingConfigs.length === 0) {
|
|
1174
|
+
const defaultConfigPath = join(projectRoot, ".mcp.json");
|
|
1175
|
+
const wasUpdated = setupConfigFile(defaultConfigPath, serverConfig, verbose);
|
|
1176
|
+
if (wasUpdated) {
|
|
1177
|
+
result.updated.push(defaultConfigPath);
|
|
1178
|
+
}
|
|
1179
|
+
return result;
|
|
1180
|
+
}
|
|
1181
|
+
if (verbose) {
|
|
1182
|
+
console.log(`[ai-annotator] Found ${existingConfigs.length} MCP config file(s)`);
|
|
1183
|
+
}
|
|
1184
|
+
for (const configFile of existingConfigs) {
|
|
1185
|
+
const wasUpdated = setupConfigFile(configFile, serverConfig, verbose);
|
|
1186
|
+
if (wasUpdated) {
|
|
1187
|
+
result.updated.push(configFile);
|
|
1188
|
+
} else {
|
|
1189
|
+
result.alreadyConfigured.push(configFile);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
return result;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
// src/vite-plugin.ts
|
|
1196
|
+
var SOURCE_LOC_ATTR = "data-source-loc";
|
|
1197
|
+
function injectSourceLocations(code, id, root) {
|
|
1198
|
+
if (!code.includes("<")) return null;
|
|
1199
|
+
const s = new MagicString(code);
|
|
1200
|
+
const relativePath = relative(root, id);
|
|
1201
|
+
let hasChanges = false;
|
|
1202
|
+
const tagRegex = /<([a-zA-Z][a-zA-Z0-9-]*)((?:\s+[^>]*?)?)\s*\/?>/g;
|
|
1203
|
+
const skipTags = /* @__PURE__ */ new Set([
|
|
1204
|
+
"script",
|
|
1205
|
+
"style",
|
|
1206
|
+
"template",
|
|
1207
|
+
"slot",
|
|
1208
|
+
"meta",
|
|
1209
|
+
"link",
|
|
1210
|
+
"base",
|
|
1211
|
+
"br",
|
|
1212
|
+
"hr",
|
|
1213
|
+
"img",
|
|
1214
|
+
"input",
|
|
1215
|
+
"area",
|
|
1216
|
+
"embed",
|
|
1217
|
+
"source",
|
|
1218
|
+
"track",
|
|
1219
|
+
"wbr",
|
|
1220
|
+
"html",
|
|
1221
|
+
"head",
|
|
1222
|
+
"title",
|
|
1223
|
+
"!doctype",
|
|
1224
|
+
// Skip SVG internal elements (but not svg itself)
|
|
1225
|
+
"path",
|
|
1226
|
+
"circle",
|
|
1227
|
+
"rect",
|
|
1228
|
+
"line",
|
|
1229
|
+
"polyline",
|
|
1230
|
+
"polygon",
|
|
1231
|
+
"ellipse",
|
|
1232
|
+
"text",
|
|
1233
|
+
"tspan",
|
|
1234
|
+
"g",
|
|
1235
|
+
"defs",
|
|
1236
|
+
"use",
|
|
1237
|
+
"symbol",
|
|
1238
|
+
"clippath",
|
|
1239
|
+
"mask",
|
|
1240
|
+
"pattern",
|
|
1241
|
+
"lineargradient",
|
|
1242
|
+
"radialgradient",
|
|
1243
|
+
"stop",
|
|
1244
|
+
"filter"
|
|
1245
|
+
]);
|
|
1246
|
+
let match;
|
|
1247
|
+
while ((match = tagRegex.exec(code)) !== null) {
|
|
1248
|
+
const [fullMatch, tagName, attributes] = match;
|
|
1249
|
+
const tagNameLower = tagName.toLowerCase();
|
|
1250
|
+
if (skipTags.has(tagNameLower)) continue;
|
|
1251
|
+
if (attributes.includes(SOURCE_LOC_ATTR)) continue;
|
|
1252
|
+
if (tagName[0] === tagName[0].toUpperCase() && tagName[0] !== tagName[0].toLowerCase()) continue;
|
|
1253
|
+
const beforeMatch = code.slice(0, match.index);
|
|
1254
|
+
const lines = beforeMatch.split("\n");
|
|
1255
|
+
const line = lines.length;
|
|
1256
|
+
const column = lines[lines.length - 1].length + 1;
|
|
1257
|
+
const insertPos = match.index + fullMatch.length - (fullMatch.endsWith("/>") ? 2 : 1);
|
|
1258
|
+
const sourceLocAttr = ` ${SOURCE_LOC_ATTR}="${relativePath}:${line}:${column}"`;
|
|
1259
|
+
s.appendLeft(insertPos, sourceLocAttr);
|
|
1260
|
+
hasChanges = true;
|
|
1261
|
+
}
|
|
1262
|
+
if (!hasChanges) return null;
|
|
1263
|
+
return {
|
|
1264
|
+
code: s.toString(),
|
|
1265
|
+
map: s.generateMap({ hires: true })
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
var AiAnnotatorServer = class {
|
|
1269
|
+
constructor(options = {}) {
|
|
1270
|
+
this.serverProcess = null;
|
|
1271
|
+
const port = options.port ?? 7318;
|
|
1272
|
+
const listenAddress = options.listenAddress ?? "127.0.0.1";
|
|
1273
|
+
this.options = {
|
|
1274
|
+
port,
|
|
1275
|
+
listenAddress,
|
|
1276
|
+
publicAddress: options.publicAddress ?? `http://${listenAddress}:${port}`,
|
|
1277
|
+
verbose: options.verbose ?? false,
|
|
1278
|
+
injectSourceLoc: options.injectSourceLoc ?? true,
|
|
1279
|
+
autoSetupMcp: options.autoSetupMcp ?? false
|
|
1280
|
+
};
|
|
1281
|
+
const currentFileDir = dirname2(fileURLToPath(import.meta.url));
|
|
1282
|
+
this.isRunningFromSource = currentFileDir.endsWith("/src") || currentFileDir.endsWith("\\src");
|
|
1283
|
+
this.packageDir = dirname2(currentFileDir);
|
|
1284
|
+
this.log(`Package directory: ${this.packageDir}`);
|
|
1285
|
+
this.log(`Running from ${this.isRunningFromSource ? "source" : "installed package"}`);
|
|
1286
|
+
}
|
|
1287
|
+
async start() {
|
|
1288
|
+
if (this.serverProcess) {
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
const isRunning = await this.isServerRunning();
|
|
1292
|
+
if (isRunning) {
|
|
1293
|
+
this.log(`Server already running on port ${this.options.port}, skipping spawn`);
|
|
1294
|
+
return;
|
|
1295
|
+
}
|
|
1296
|
+
let serverFile;
|
|
1297
|
+
let cmd;
|
|
1298
|
+
let args;
|
|
1299
|
+
if (this.isRunningFromSource) {
|
|
1300
|
+
serverFile = join2(this.packageDir, "src", "index.ts");
|
|
1301
|
+
cmd = "bun";
|
|
1302
|
+
args = [serverFile];
|
|
1303
|
+
} else {
|
|
1304
|
+
serverFile = join2(this.packageDir, "dist", "index.cjs");
|
|
1305
|
+
if (!existsSync2(serverFile)) {
|
|
1306
|
+
const fallbackFile = join2(this.packageDir, "src", "index.ts");
|
|
1307
|
+
if (existsSync2(fallbackFile)) {
|
|
1308
|
+
this.log("dist/index.cjs not found, falling back to src/index.ts");
|
|
1309
|
+
serverFile = fallbackFile;
|
|
1310
|
+
cmd = "bun";
|
|
1311
|
+
args = [serverFile];
|
|
1312
|
+
} else {
|
|
1313
|
+
throw new Error(`Annotator server file not found at ${serverFile} or ${fallbackFile}`);
|
|
1314
|
+
}
|
|
1315
|
+
} else {
|
|
1316
|
+
cmd = "node";
|
|
1317
|
+
args = [serverFile];
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
args.push("--port", String(this.options.port));
|
|
1321
|
+
args.push("--listen", this.options.listenAddress);
|
|
1322
|
+
args.push("--public-address", this.options.publicAddress);
|
|
1323
|
+
if (this.options.verbose) {
|
|
1324
|
+
args.push("--verbose");
|
|
1325
|
+
}
|
|
1326
|
+
if (this.options.autoSetupMcp) {
|
|
1327
|
+
args.push("--skip-mcp-instructions");
|
|
1328
|
+
}
|
|
1329
|
+
this.log(`Starting annotator server: ${cmd} ${args.join(" ")}`);
|
|
1330
|
+
this.log(`Working directory: ${this.packageDir}`);
|
|
1331
|
+
this.serverProcess = spawn(cmd, args, {
|
|
1332
|
+
cwd: this.packageDir,
|
|
1333
|
+
env: process.env,
|
|
1334
|
+
stdio: this.options.verbose ? "inherit" : "pipe"
|
|
1335
|
+
});
|
|
1336
|
+
if (!this.options.verbose && this.serverProcess.stdout) {
|
|
1337
|
+
this.serverProcess.stdout.on("data", (_data) => {
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
if (!this.options.verbose && this.serverProcess.stderr) {
|
|
1341
|
+
this.serverProcess.stderr.on("data", (data) => {
|
|
1342
|
+
console.error(`[ai-annotator-server] Error: ${data}`);
|
|
1343
|
+
});
|
|
1344
|
+
}
|
|
1345
|
+
this.serverProcess.on("error", (error) => {
|
|
1346
|
+
console.error("[ai-annotator-server] Failed to start:", error);
|
|
1347
|
+
});
|
|
1348
|
+
this.serverProcess.on("exit", (code) => {
|
|
1349
|
+
if (code !== 0 && code !== null) {
|
|
1350
|
+
console.error(`[ai-annotator-server] Process exited with code ${code}`);
|
|
1351
|
+
}
|
|
1352
|
+
this.serverProcess = null;
|
|
1353
|
+
});
|
|
1354
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
1355
|
+
}
|
|
1356
|
+
async stop() {
|
|
1357
|
+
if (this.serverProcess) {
|
|
1358
|
+
this.log("Stopping annotator server...");
|
|
1359
|
+
this.serverProcess.kill("SIGTERM");
|
|
1360
|
+
await new Promise((resolve) => {
|
|
1361
|
+
if (!this.serverProcess) {
|
|
1362
|
+
resolve();
|
|
1363
|
+
return;
|
|
1364
|
+
}
|
|
1365
|
+
const timeout = setTimeout(() => {
|
|
1366
|
+
if (this.serverProcess) {
|
|
1367
|
+
this.log("Force killing annotator server...");
|
|
1368
|
+
this.serverProcess.kill("SIGKILL");
|
|
1369
|
+
}
|
|
1370
|
+
resolve();
|
|
1371
|
+
}, 5e3);
|
|
1372
|
+
this.serverProcess.on("exit", () => {
|
|
1373
|
+
clearTimeout(timeout);
|
|
1374
|
+
resolve();
|
|
1375
|
+
});
|
|
1376
|
+
});
|
|
1377
|
+
this.serverProcess = null;
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
async isServerRunning() {
|
|
1381
|
+
try {
|
|
1382
|
+
const response = await fetch(`http://${this.options.listenAddress}:${this.options.port}/health`, {
|
|
1383
|
+
signal: AbortSignal.timeout(1e3)
|
|
1384
|
+
});
|
|
1385
|
+
return response.ok;
|
|
1386
|
+
} catch {
|
|
1387
|
+
return false;
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
log(message) {
|
|
1391
|
+
if (this.options.verbose) {
|
|
1392
|
+
console.log(`[ai-annotator] ${message}`);
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
getInjectScript() {
|
|
1396
|
+
return `<script src="${this.options.publicAddress}/annotator-toolbar.js" type="module" async></script>`;
|
|
1397
|
+
}
|
|
1398
|
+
};
|
|
1399
|
+
function injectScriptIntoHtml(html, scriptTag) {
|
|
1400
|
+
if (html.includes("</body>")) {
|
|
1401
|
+
return html.replace("</body>", `${scriptTag}
|
|
1402
|
+
</body>`);
|
|
1403
|
+
} else if (html.includes("</html>")) {
|
|
1404
|
+
return html.replace("</html>", `${scriptTag}
|
|
1405
|
+
</html>`);
|
|
1406
|
+
}
|
|
1407
|
+
return html + scriptTag;
|
|
1408
|
+
}
|
|
1409
|
+
function aiAnnotator(options = {}) {
|
|
1410
|
+
let serverManager;
|
|
1411
|
+
let root = process.cwd();
|
|
1412
|
+
const injectSourceLoc = options.injectSourceLoc ?? true;
|
|
1413
|
+
return {
|
|
1414
|
+
name: "vite-plugin-ai-annotator",
|
|
1415
|
+
// Only apply plugin during development (serve command)
|
|
1416
|
+
apply: "serve",
|
|
1417
|
+
configResolved(config) {
|
|
1418
|
+
serverManager = new AiAnnotatorServer(options);
|
|
1419
|
+
root = config.root;
|
|
1420
|
+
if (options.autoSetupMcp) {
|
|
1421
|
+
const serverUrl = `http://${options.listenAddress ?? "127.0.0.1"}:${options.port ?? 7318}`;
|
|
1422
|
+
const result = autoSetupMcp({
|
|
1423
|
+
projectRoot: root,
|
|
1424
|
+
serverUrl,
|
|
1425
|
+
verbose: options.verbose
|
|
1426
|
+
});
|
|
1427
|
+
if (result.updated.length > 0) {
|
|
1428
|
+
console.log(`[ai-annotator] \u2705 MCP config updated: ${result.updated.map((f) => f.replace(root + "/", "")).join(", ")}`);
|
|
1429
|
+
} else if (result.alreadyConfigured.length > 0) {
|
|
1430
|
+
console.log(`[ai-annotator] \u2705 MCP config already up-to-date`);
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
},
|
|
1434
|
+
async buildStart() {
|
|
1435
|
+
await serverManager.start();
|
|
1436
|
+
},
|
|
1437
|
+
// Transform HTML files to inject source location attributes
|
|
1438
|
+
transform(code, id) {
|
|
1439
|
+
if (!injectSourceLoc) return null;
|
|
1440
|
+
if (!id.endsWith(".html")) return null;
|
|
1441
|
+
if (id.includes("node_modules")) return null;
|
|
1442
|
+
return injectSourceLocations(code, id, root);
|
|
1443
|
+
},
|
|
1444
|
+
// For regular Vite apps (SPA)
|
|
1445
|
+
transformIndexHtml(html, ctx) {
|
|
1446
|
+
if (!serverManager) {
|
|
1447
|
+
return html;
|
|
1448
|
+
}
|
|
1449
|
+
let result = html;
|
|
1450
|
+
if (injectSourceLoc && ctx.filename) {
|
|
1451
|
+
const transformed = injectSourceLocations(html, ctx.filename, root);
|
|
1452
|
+
if (transformed) {
|
|
1453
|
+
result = transformed.code;
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1456
|
+
return injectScriptIntoHtml(result, serverManager.getInjectScript());
|
|
1457
|
+
},
|
|
1458
|
+
// For SSR frameworks like Nuxt - intercept HTML responses
|
|
1459
|
+
configureServer(server) {
|
|
1460
|
+
server.middlewares.use((_req, res, next) => {
|
|
1461
|
+
if (!serverManager) {
|
|
1462
|
+
return next();
|
|
1463
|
+
}
|
|
1464
|
+
const originalWrite = res.write.bind(res);
|
|
1465
|
+
const originalEnd = res.end.bind(res);
|
|
1466
|
+
let chunks = [];
|
|
1467
|
+
let isHtml = false;
|
|
1468
|
+
res.write = function(chunk, ...args) {
|
|
1469
|
+
const contentType = res.getHeader("content-type");
|
|
1470
|
+
if (typeof contentType === "string" && contentType.includes("text/html")) {
|
|
1471
|
+
isHtml = true;
|
|
1472
|
+
}
|
|
1473
|
+
if (isHtml && chunk) {
|
|
1474
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
1475
|
+
return true;
|
|
1476
|
+
}
|
|
1477
|
+
return originalWrite(chunk, ...args);
|
|
1478
|
+
};
|
|
1479
|
+
res.end = function(chunk, ...args) {
|
|
1480
|
+
if (chunk) {
|
|
1481
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
1482
|
+
}
|
|
1483
|
+
if (isHtml && chunks.length > 0) {
|
|
1484
|
+
const html = Buffer.concat(chunks).toString("utf-8");
|
|
1485
|
+
const injectedHtml = injectScriptIntoHtml(html, serverManager.getInjectScript());
|
|
1486
|
+
res.setHeader("content-length", Buffer.byteLength(injectedHtml));
|
|
1487
|
+
return originalEnd(injectedHtml, ...args);
|
|
1488
|
+
}
|
|
1489
|
+
return originalEnd(chunk, ...args);
|
|
1490
|
+
};
|
|
1491
|
+
next();
|
|
1492
|
+
});
|
|
1493
|
+
},
|
|
1494
|
+
async closeBundle() {
|
|
1495
|
+
await serverManager.stop();
|
|
1496
|
+
}
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
// src/nuxt-module.ts
|
|
1501
|
+
var nuxt_module_default = defineNuxtModule({
|
|
1502
|
+
meta: {
|
|
1503
|
+
name: "vite-plugin-ai-annotator",
|
|
1504
|
+
configKey: "aiAnnotator",
|
|
1505
|
+
compatibility: {
|
|
1506
|
+
nuxt: "^3.0.0"
|
|
1507
|
+
}
|
|
1508
|
+
},
|
|
1509
|
+
defaults: {
|
|
1510
|
+
port: 7318,
|
|
1511
|
+
listenAddress: "127.0.0.1",
|
|
1512
|
+
verbose: false,
|
|
1513
|
+
injectSourceLoc: true,
|
|
1514
|
+
autoSetupMcp: false
|
|
1515
|
+
},
|
|
1516
|
+
setup(options, nuxt) {
|
|
1517
|
+
if (!nuxt.options.dev) {
|
|
1518
|
+
return;
|
|
1519
|
+
}
|
|
1520
|
+
addVitePlugin(aiAnnotator(options));
|
|
1521
|
+
addWebpackPlugin(aiAnnotator(options));
|
|
1522
|
+
console.log("[ai-annotator] Nuxt module initialized");
|
|
1523
|
+
}
|
|
1524
|
+
});
|
|
1525
|
+
export {
|
|
1526
|
+
nuxt_module_default as default
|
|
1527
|
+
};
|
|
1528
|
+
//# sourceMappingURL=nuxt-module.js.map
|