@qwik.dev/mcp 0.0.0-stage → 2.0.0-beta.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +99 -2
- package/dist/browser.js +286 -0
- package/dist/cli.js +366 -0
- package/dist/docs.json +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1686 -0
- package/package.json +53 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,1686 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { mkdir, readFile, unlink, writeFile } from "node:fs/promises";
|
|
5
|
+
import { join, relative } from "node:path";
|
|
6
|
+
|
|
7
|
+
// ../devtools/kit/src/protocol/hooks.ts
|
|
8
|
+
var HOOK_GROUPS = {
|
|
9
|
+
variableDeclaration: [
|
|
10
|
+
"useStore",
|
|
11
|
+
"useSignal",
|
|
12
|
+
"useComputed",
|
|
13
|
+
"useAsyncComputed",
|
|
14
|
+
"useContext",
|
|
15
|
+
"useId",
|
|
16
|
+
"useStyles",
|
|
17
|
+
"useStylesScoped",
|
|
18
|
+
"useConstant",
|
|
19
|
+
"useSerializer",
|
|
20
|
+
"useServerData",
|
|
21
|
+
"useLocation",
|
|
22
|
+
"useNavigate",
|
|
23
|
+
"useContent",
|
|
24
|
+
"useDocumentHead"
|
|
25
|
+
],
|
|
26
|
+
expressionStatement: [
|
|
27
|
+
"useVisibleTask",
|
|
28
|
+
"useTask",
|
|
29
|
+
"useResource",
|
|
30
|
+
"useContextProvider",
|
|
31
|
+
"usePreventNavigate"
|
|
32
|
+
],
|
|
33
|
+
listener: ["useOn", "useOnDocument", "useOnWindow"],
|
|
34
|
+
noReturn: ["useVisibleTask", "useTask"]
|
|
35
|
+
};
|
|
36
|
+
var VARIABLE_DECLARATION_LIST = HOOK_GROUPS.variableDeclaration;
|
|
37
|
+
var EXPRESSION_STATEMENT_LIST = HOOK_GROUPS.expressionStatement;
|
|
38
|
+
var USE_HOOK_LIST = [
|
|
39
|
+
...VARIABLE_DECLARATION_LIST,
|
|
40
|
+
...HOOK_GROUPS.listener,
|
|
41
|
+
...EXPRESSION_STATEMENT_LIST
|
|
42
|
+
];
|
|
43
|
+
var NORETURN_HOOK = HOOK_GROUPS.noReturn;
|
|
44
|
+
var SIGNAL_HOOK_TYPES = [
|
|
45
|
+
"useSignal",
|
|
46
|
+
"useStore",
|
|
47
|
+
"useComputed",
|
|
48
|
+
"useAsyncComputed",
|
|
49
|
+
"useContext"
|
|
50
|
+
];
|
|
51
|
+
var INNER_USE_HOOK = "useCollectHooks";
|
|
52
|
+
var VIRTUAL_QWIK_DEVTOOLS_KEY = "virtual-qwik-devtools.ts";
|
|
53
|
+
|
|
54
|
+
// ../devtools/kit/src/protocol/globals.ts
|
|
55
|
+
var QWIK_DEVTOOLS_GLOBAL = {
|
|
56
|
+
key: "__QWIK_DEVTOOLS__",
|
|
57
|
+
version: 1,
|
|
58
|
+
props: {
|
|
59
|
+
componentState: "componentState",
|
|
60
|
+
hook: "hook",
|
|
61
|
+
pageDataSource: "pageDataSource",
|
|
62
|
+
dataProvider: "dataProvider",
|
|
63
|
+
perf: "perf",
|
|
64
|
+
preloads: "preloads",
|
|
65
|
+
ssrPreloads: "ssrPreloads"
|
|
66
|
+
},
|
|
67
|
+
ssr: {
|
|
68
|
+
perfStore: "__QWIK_SSR_PERF__",
|
|
69
|
+
preloadsProcessStore: "__QWIK_SSR_PRELOADS__",
|
|
70
|
+
perfId: "__QWIK_SSR_PERF_ID__",
|
|
71
|
+
perfIndex: "__QWIK_SSR_PERF_INDEX__",
|
|
72
|
+
perfCount: "__QWIK_SSR_PERF_COUNT__"
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// ../devtools/kit/src/protocol/messages.ts
|
|
77
|
+
var DEVTOOLS_MESSAGES = {
|
|
78
|
+
pageSource: "qwik-devtools",
|
|
79
|
+
viteMessagingEvent: "qwik_tools:vite_messaging_event",
|
|
80
|
+
types: {
|
|
81
|
+
render: "RENDER_EVENT",
|
|
82
|
+
componentTreeUpdate: "COMPONENT_TREE_UPDATE"
|
|
83
|
+
},
|
|
84
|
+
events: {
|
|
85
|
+
preloadsUpdate: "qwik:preloads-update",
|
|
86
|
+
ssrPerf: "qwik:ssr-perf",
|
|
87
|
+
ssrPreloads: "qwik:ssr-preloads"
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// ../devtools/kit/src/protocol/vnode.ts
|
|
92
|
+
var QWIK_VNODE_PROTOCOL = {
|
|
93
|
+
attrs: {
|
|
94
|
+
seq: "q:seq",
|
|
95
|
+
props: "q:props",
|
|
96
|
+
renderFn: "q:renderFn",
|
|
97
|
+
type: "q:type",
|
|
98
|
+
id: "q:id",
|
|
99
|
+
key: "q:key",
|
|
100
|
+
colon: ":"
|
|
101
|
+
},
|
|
102
|
+
qrl: {
|
|
103
|
+
qrl: "$qrl$",
|
|
104
|
+
computed: "$computeQrl$",
|
|
105
|
+
chunk: "$chunk$",
|
|
106
|
+
symbol: "$symbol$",
|
|
107
|
+
captureRef: "$captureRef$",
|
|
108
|
+
untrackedValue: "$untrackedValue$"
|
|
109
|
+
},
|
|
110
|
+
bridgeVirtualModuleId: "virtual:qwik-devtools-bridge"
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// ../devtools/plugin/src/parse/traverse.ts
|
|
114
|
+
import { parseSync } from "oxc-parser";
|
|
115
|
+
|
|
116
|
+
// ../devtools/plugin/src/parse/helpers.ts
|
|
117
|
+
var ALL_HOOK_NAMES = /* @__PURE__ */ new Set([...USE_HOOK_LIST]);
|
|
118
|
+
function isAstNodeLike(value) {
|
|
119
|
+
return Boolean(value) && typeof value === "object" && "type" in value;
|
|
120
|
+
}
|
|
121
|
+
function getVariableIdentifierName(id) {
|
|
122
|
+
if (!isAstNodeLike(id)) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return id.type === "Identifier" ? id.name : null;
|
|
126
|
+
}
|
|
127
|
+
function normalizeHookName(raw) {
|
|
128
|
+
return raw.endsWith("$") ? raw.slice(0, -1) : raw;
|
|
129
|
+
}
|
|
130
|
+
function normalizeQrlHookName(hookName) {
|
|
131
|
+
return hookName.endsWith("Qrl") ? hookName.slice(0, -3) : hookName;
|
|
132
|
+
}
|
|
133
|
+
function isKnownHook(name) {
|
|
134
|
+
return ALL_HOOK_NAMES.has(name);
|
|
135
|
+
}
|
|
136
|
+
function isCustomHook(hookName) {
|
|
137
|
+
const isBuiltIn = USE_HOOK_LIST.some((item) => item.startsWith(hookName));
|
|
138
|
+
return !isBuiltIn && /^use[A-Z_]/.test(hookName);
|
|
139
|
+
}
|
|
140
|
+
function findLineStart(code, index) {
|
|
141
|
+
for (let i = index - 1; i >= 0; i--) {
|
|
142
|
+
const ch = code[i];
|
|
143
|
+
if (ch === "\n" || ch === "\r") {
|
|
144
|
+
return i + 1;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
function readIndent(code, indexFrom) {
|
|
150
|
+
let indent = "";
|
|
151
|
+
let i = indexFrom;
|
|
152
|
+
while (i < code.length) {
|
|
153
|
+
const ch = code[i];
|
|
154
|
+
if (ch === " " || ch === " ") {
|
|
155
|
+
indent += ch;
|
|
156
|
+
i++;
|
|
157
|
+
} else {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return indent;
|
|
162
|
+
}
|
|
163
|
+
function trimStatementSemicolon(segment) {
|
|
164
|
+
return segment.trim().replace(/;?\s*$/, "");
|
|
165
|
+
}
|
|
166
|
+
function buildCollecthookPayload(indent, variableName, hookType, category, hookExpression) {
|
|
167
|
+
const dataValue = hookExpression === "undefined" ? "undefined" : hookExpression;
|
|
168
|
+
return `${indent}collecthook({
|
|
169
|
+
${indent} variableName: '${variableName}',
|
|
170
|
+
${indent} hookType: '${hookType}',
|
|
171
|
+
${indent} category: '${category}',
|
|
172
|
+
${indent} data: ${dataValue}
|
|
173
|
+
${indent}});
|
|
174
|
+
`;
|
|
175
|
+
}
|
|
176
|
+
function hasCollecthookAfterByVariableId(code, fromIndex, variableId, maxLookahead = 600) {
|
|
177
|
+
const lookahead = code.slice(fromIndex, fromIndex + maxLookahead);
|
|
178
|
+
const pattern = new RegExp(`collecthook\\s*\\(\\s*\\{[\\s\\S]{0,300}?data:\\s*${variableId}\\b`);
|
|
179
|
+
return pattern.test(lookahead);
|
|
180
|
+
}
|
|
181
|
+
function hasCollecthookAfterByVariableName(code, fromIndex, variableName, maxLookahead = 600) {
|
|
182
|
+
const lookahead = code.slice(fromIndex, fromIndex + maxLookahead);
|
|
183
|
+
const pattern = new RegExp(
|
|
184
|
+
`collecthook\\s*\\(\\s*\\{[\\s\\S]{0,200}?variableName:\\s*'${variableName}'`
|
|
185
|
+
);
|
|
186
|
+
return pattern.test(lookahead);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ../devtools/plugin/src/parse/traverse.ts
|
|
190
|
+
function parseProgram(code) {
|
|
191
|
+
const parsed = parseSync("file.tsx", code, {
|
|
192
|
+
lang: "tsx",
|
|
193
|
+
sourceType: "module",
|
|
194
|
+
astType: "ts",
|
|
195
|
+
range: true
|
|
196
|
+
});
|
|
197
|
+
return parsed.program;
|
|
198
|
+
}
|
|
199
|
+
function callVisitor(visitor, type, hook, path) {
|
|
200
|
+
if (!visitor) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const specific = visitor[type];
|
|
204
|
+
if (specific) {
|
|
205
|
+
specific(path);
|
|
206
|
+
}
|
|
207
|
+
const handler = visitor[path.node && path.node.type];
|
|
208
|
+
if (!handler) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (typeof handler === "function" && hook === "enter") {
|
|
212
|
+
handler(path);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (typeof handler === "object") {
|
|
216
|
+
const fn = handler[hook];
|
|
217
|
+
if (fn) {
|
|
218
|
+
fn(path);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function traverseProgram(program, visitor, state) {
|
|
223
|
+
let shouldStopAll = false;
|
|
224
|
+
function traverse(node, parent, key, index) {
|
|
225
|
+
if (shouldStopAll) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
if (!isAstNodeLike(node)) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
let shouldSkipChildren = false;
|
|
232
|
+
const path = {
|
|
233
|
+
node,
|
|
234
|
+
parent: isAstNodeLike(parent) ? parent : null,
|
|
235
|
+
key,
|
|
236
|
+
index,
|
|
237
|
+
state,
|
|
238
|
+
stop: () => {
|
|
239
|
+
shouldStopAll = true;
|
|
240
|
+
},
|
|
241
|
+
skip: () => {
|
|
242
|
+
shouldSkipChildren = true;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
callVisitor(visitor, "enter", "enter", path);
|
|
246
|
+
if (!shouldSkipChildren) {
|
|
247
|
+
const record = node;
|
|
248
|
+
for (const k of Object.keys(record)) {
|
|
249
|
+
const value = record[k];
|
|
250
|
+
if (Array.isArray(value)) {
|
|
251
|
+
for (let i = 0; i < value.length; i++) {
|
|
252
|
+
traverse(value[i], node, k, i);
|
|
253
|
+
}
|
|
254
|
+
} else {
|
|
255
|
+
traverse(value, node, k, null);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
callVisitor(visitor, "exit", "exit", path);
|
|
260
|
+
}
|
|
261
|
+
traverse(program, null, null, null);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// ../devtools/plugin/src/parse/componentBodies.ts
|
|
265
|
+
function findAllComponentBodyRangesFromProgram(program) {
|
|
266
|
+
const ranges = [];
|
|
267
|
+
traverseProgram(program, {
|
|
268
|
+
enter: (path) => {
|
|
269
|
+
const range = getComponentBodyRange(path.node, path.parent);
|
|
270
|
+
if (range) {
|
|
271
|
+
ranges.push(range);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
return deduplicateRanges(ranges);
|
|
276
|
+
}
|
|
277
|
+
function getComponentBodyRange(node, parent) {
|
|
278
|
+
if (!isComponentCall(node)) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
const body = getComponentFunctionBody(node);
|
|
282
|
+
if (!body?.range) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
const [bodyStart, bodyEnd] = body.range;
|
|
286
|
+
return {
|
|
287
|
+
insertPos: bodyStart + 1,
|
|
288
|
+
bodyStart,
|
|
289
|
+
bodyEnd,
|
|
290
|
+
exportName: detectExportName(parent)
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
function isComponentCall(node) {
|
|
294
|
+
return node?.type === "CallExpression" && node.callee?.type === "Identifier" && node.callee.name === "component$";
|
|
295
|
+
}
|
|
296
|
+
function getComponentFunctionBody(node) {
|
|
297
|
+
const componentFactory = node.arguments?.[0];
|
|
298
|
+
const isFunctionFactory = componentFactory?.type === "ArrowFunctionExpression" || componentFactory?.type === "FunctionExpression";
|
|
299
|
+
if (!isFunctionFactory) {
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
return componentFactory.body?.type === "BlockStatement" ? componentFactory.body : null;
|
|
303
|
+
}
|
|
304
|
+
function detectExportName(parent) {
|
|
305
|
+
if (!parent) {
|
|
306
|
+
return void 0;
|
|
307
|
+
}
|
|
308
|
+
if (parent.type === "ExportDefaultDeclaration") {
|
|
309
|
+
return "";
|
|
310
|
+
}
|
|
311
|
+
if (parent.type === "VariableDeclarator") {
|
|
312
|
+
const identifier = parent.id;
|
|
313
|
+
if (identifier?.type === "Identifier" && typeof identifier.name === "string") {
|
|
314
|
+
return identifier.name;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return void 0;
|
|
318
|
+
}
|
|
319
|
+
function deduplicateRanges(ranges) {
|
|
320
|
+
const seen = /* @__PURE__ */ new Set();
|
|
321
|
+
return ranges.filter((range) => {
|
|
322
|
+
if (seen.has(range.insertPos)) {
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
seen.add(range.insertPos);
|
|
326
|
+
return true;
|
|
327
|
+
}).sort((left, right) => left.insertPos - right.insertPos);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// ../devtools/plugin/src/parse/sourceEdits.ts
|
|
331
|
+
function applySourceEdits(code, edits) {
|
|
332
|
+
if (edits.length === 0) {
|
|
333
|
+
return code;
|
|
334
|
+
}
|
|
335
|
+
const orderedEdits = [...edits].sort((left, right) => getEditStart(right) - getEditStart(left));
|
|
336
|
+
let result = code;
|
|
337
|
+
for (const edit of orderedEdits) {
|
|
338
|
+
if (edit.kind === "insert") {
|
|
339
|
+
result = result.slice(0, edit.pos) + edit.text + result.slice(edit.pos);
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
result = result.slice(0, edit.start) + edit.text + result.slice(edit.end);
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
function getEditStart(edit) {
|
|
347
|
+
return edit.kind === "insert" ? edit.pos : edit.start;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ../devtools/plugin/src/parse/initInjector.ts
|
|
351
|
+
function injectInitHooks(code, bodies, options) {
|
|
352
|
+
const tasks = [];
|
|
353
|
+
for (const body of bodies) {
|
|
354
|
+
const task = createInitTask(code, body, options);
|
|
355
|
+
if (task) {
|
|
356
|
+
tasks.push(task);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return applySourceEdits(code, tasks);
|
|
360
|
+
}
|
|
361
|
+
function createInitTask(code, body, options) {
|
|
362
|
+
const { insertPos, exportName } = body;
|
|
363
|
+
if (hasExistingCollecthook(code, insertPos)) {
|
|
364
|
+
return null;
|
|
365
|
+
}
|
|
366
|
+
const { insertIndex, prefixNewline } = calculateInsertPosition(code, insertPos);
|
|
367
|
+
const indent = readIndent(code, insertIndex);
|
|
368
|
+
const componentArg = buildComponentArg(options?.path, exportName);
|
|
369
|
+
const initLine = `${prefixNewline}${indent}const collecthook = ${INNER_USE_HOOK}(${componentArg})
|
|
370
|
+
`;
|
|
371
|
+
return { kind: "insert", pos: insertIndex, text: initLine };
|
|
372
|
+
}
|
|
373
|
+
function hasExistingCollecthook(code, insertPos) {
|
|
374
|
+
const lookahead = code.slice(insertPos, insertPos + 200);
|
|
375
|
+
return /const\s+collecthook\s*=\s*useCollectHooks\s*\(/.test(lookahead);
|
|
376
|
+
}
|
|
377
|
+
function calculateInsertPosition(code, insertPos) {
|
|
378
|
+
if (code[insertPos] === "\r" && code[insertPos + 1] === "\n") {
|
|
379
|
+
return { insertIndex: insertPos + 2, prefixNewline: "" };
|
|
380
|
+
}
|
|
381
|
+
if (code[insertPos] === "\n") {
|
|
382
|
+
return { insertIndex: insertPos + 1, prefixNewline: "" };
|
|
383
|
+
}
|
|
384
|
+
return { insertIndex: insertPos, prefixNewline: "\n" };
|
|
385
|
+
}
|
|
386
|
+
function buildComponentArg(path, exportName) {
|
|
387
|
+
const rawArg = String(path ?? "");
|
|
388
|
+
const baseArg = rawArg.split("?")[0].split("#")[0];
|
|
389
|
+
const suffix = buildComponentSuffix(baseArg, exportName);
|
|
390
|
+
return JSON.stringify(`${baseArg}${suffix}`);
|
|
391
|
+
}
|
|
392
|
+
function buildComponentSuffix(baseArg, exportName) {
|
|
393
|
+
if (exportName === "") {
|
|
394
|
+
return "_default";
|
|
395
|
+
}
|
|
396
|
+
if (exportName && typeof exportName === "string") {
|
|
397
|
+
return `_${exportName}`;
|
|
398
|
+
}
|
|
399
|
+
if (baseArg.endsWith("index.tsx")) {
|
|
400
|
+
const parts = baseArg.split("/");
|
|
401
|
+
const parent = parts.length >= 2 ? parts[parts.length - 2] : "index";
|
|
402
|
+
return `_${parent.replace(/-/g, "_")}`;
|
|
403
|
+
}
|
|
404
|
+
const file = baseArg.split("/").pop() || "";
|
|
405
|
+
const name = file.replace(/\.[^.]+$/, "");
|
|
406
|
+
return name ? `_${name.replace(/-/g, "_")}` : "";
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// ../devtools/plugin/src/parse/hookTracker.ts
|
|
410
|
+
function injectHookTrackers(code) {
|
|
411
|
+
const program = parseProgram(code);
|
|
412
|
+
const tasks = [];
|
|
413
|
+
let customHookIndex = 0;
|
|
414
|
+
traverseProgram(program, {
|
|
415
|
+
enter: (path) => {
|
|
416
|
+
const node = path.node;
|
|
417
|
+
if (!node) {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
if (node.type === "VariableDeclarator") {
|
|
421
|
+
const task = processVariableDeclarator(code, node, path);
|
|
422
|
+
if (task) {
|
|
423
|
+
tasks.push(task);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (node.type === "ExpressionStatement") {
|
|
427
|
+
const result = processExpressionStatement(code, node, customHookIndex);
|
|
428
|
+
if (result) {
|
|
429
|
+
tasks.push(result.task);
|
|
430
|
+
customHookIndex = result.newIndex;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
return applySourceEdits(code, tasks);
|
|
436
|
+
}
|
|
437
|
+
function processVariableDeclarator(code, node, path) {
|
|
438
|
+
const hookInfo = extractHookInfo(node);
|
|
439
|
+
if (!hookInfo) {
|
|
440
|
+
return null;
|
|
441
|
+
}
|
|
442
|
+
const { hookName, normalizedName, variableId } = hookInfo;
|
|
443
|
+
if (hookName === INNER_USE_HOOK) {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
const range = getParentRange(path.parent);
|
|
447
|
+
if (!range) {
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
const { declEnd, indent } = getPositionInfo(code, range);
|
|
451
|
+
if (isCustomHook(normalizedName)) {
|
|
452
|
+
if (hasCollecthookAfterByVariableId(code, declEnd, variableId)) {
|
|
453
|
+
return null;
|
|
454
|
+
}
|
|
455
|
+
const payload2 = buildCollecthookPayload(
|
|
456
|
+
indent,
|
|
457
|
+
variableId,
|
|
458
|
+
"customhook",
|
|
459
|
+
"VariableDeclarator",
|
|
460
|
+
variableId
|
|
461
|
+
);
|
|
462
|
+
return { kind: "insert", pos: declEnd, text: "\n" + payload2 };
|
|
463
|
+
}
|
|
464
|
+
if (!isKnownHook(normalizedName)) {
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
if (hasCollecthookAfterByVariableId(code, declEnd, variableId)) {
|
|
468
|
+
return null;
|
|
469
|
+
}
|
|
470
|
+
const payload = buildCollecthookPayload(
|
|
471
|
+
indent,
|
|
472
|
+
variableId,
|
|
473
|
+
normalizedName,
|
|
474
|
+
"VariableDeclarator",
|
|
475
|
+
variableId
|
|
476
|
+
);
|
|
477
|
+
return { kind: "insert", pos: declEnd, text: "\n" + payload };
|
|
478
|
+
}
|
|
479
|
+
function processExpressionStatement(code, node, currentIndex) {
|
|
480
|
+
const hookInfo = extractExpressionHookInfo(node);
|
|
481
|
+
if (!hookInfo) {
|
|
482
|
+
return null;
|
|
483
|
+
}
|
|
484
|
+
const { hookName, normalizedName } = hookInfo;
|
|
485
|
+
if (hookName === INNER_USE_HOOK) {
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
const stmtRange = node.range;
|
|
489
|
+
if (!stmtRange) {
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
const [stmtStart, stmtEnd] = stmtRange;
|
|
493
|
+
const lineStart = findLineStart(code, stmtStart);
|
|
494
|
+
const indent = readIndent(code, lineStart);
|
|
495
|
+
if (isKnownHook(normalizedName)) {
|
|
496
|
+
if (hasCollecthookAfterByVariableName(code, stmtEnd, normalizedName)) {
|
|
497
|
+
return null;
|
|
498
|
+
}
|
|
499
|
+
const payload = buildCollecthookPayload(
|
|
500
|
+
indent,
|
|
501
|
+
normalizedName,
|
|
502
|
+
normalizedName,
|
|
503
|
+
"expressionStatement",
|
|
504
|
+
"undefined"
|
|
505
|
+
);
|
|
506
|
+
return {
|
|
507
|
+
task: { kind: "insert", pos: stmtEnd, text: "\n" + payload },
|
|
508
|
+
newIndex: currentIndex
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
if (isCustomHook(normalizedName)) {
|
|
512
|
+
return convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentIndex);
|
|
513
|
+
}
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
function convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentIndex) {
|
|
517
|
+
const callSource = code.slice(stmtStart, stmtEnd);
|
|
518
|
+
const variableName = `_customhook_${currentIndex}`;
|
|
519
|
+
const declLine = `${indent}let ${variableName} = ${trimStatementSemicolon(callSource)};
|
|
520
|
+
`;
|
|
521
|
+
const payload = buildCollecthookPayload(
|
|
522
|
+
indent,
|
|
523
|
+
variableName,
|
|
524
|
+
"customhook",
|
|
525
|
+
"VariableDeclarator",
|
|
526
|
+
variableName
|
|
527
|
+
);
|
|
528
|
+
return {
|
|
529
|
+
task: {
|
|
530
|
+
kind: "replace",
|
|
531
|
+
start: stmtStart,
|
|
532
|
+
end: stmtEnd,
|
|
533
|
+
text: declLine + payload
|
|
534
|
+
},
|
|
535
|
+
newIndex: currentIndex + 1
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
function extractHookInfo(node) {
|
|
539
|
+
const variableId = getVariableIdentifierName(node.id);
|
|
540
|
+
if (!variableId) {
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
const hookCall = extractHookCall(node.init);
|
|
544
|
+
if (!hookCall) {
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
return { ...hookCall, variableId };
|
|
548
|
+
}
|
|
549
|
+
function extractExpressionHookInfo(node) {
|
|
550
|
+
return extractHookCall(node.expression);
|
|
551
|
+
}
|
|
552
|
+
function extractHookCall(node) {
|
|
553
|
+
if (!isAstNodeLike(node) || node.type !== "CallExpression") {
|
|
554
|
+
return null;
|
|
555
|
+
}
|
|
556
|
+
const callee = node.callee;
|
|
557
|
+
if (!isAstNodeLike(callee) || callee.type !== "Identifier") {
|
|
558
|
+
return null;
|
|
559
|
+
}
|
|
560
|
+
const hookName = normalizeHookName(callee.name);
|
|
561
|
+
return {
|
|
562
|
+
hookName,
|
|
563
|
+
normalizedName: normalizeQrlHookName(hookName)
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
function getParentRange(parent) {
|
|
567
|
+
const range = parent?.range;
|
|
568
|
+
if (!range) {
|
|
569
|
+
return null;
|
|
570
|
+
}
|
|
571
|
+
return [range[0], range[1]];
|
|
572
|
+
}
|
|
573
|
+
function getPositionInfo(code, range) {
|
|
574
|
+
const [declStart, declEnd] = range;
|
|
575
|
+
const lineStart = findLineStart(code, declStart);
|
|
576
|
+
const indent = readIndent(code, lineStart);
|
|
577
|
+
return { declStart, declEnd, indent };
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// ../devtools/plugin/src/parse/parse.ts
|
|
581
|
+
function parseQwikCode(code, options) {
|
|
582
|
+
const program = parseProgram(code);
|
|
583
|
+
const componentBodies = findAllComponentBodyRangesFromProgram(program);
|
|
584
|
+
if (componentBodies.length === 0) {
|
|
585
|
+
return code;
|
|
586
|
+
}
|
|
587
|
+
let result = injectInitHooks(code, componentBodies, options);
|
|
588
|
+
result = injectHookTrackers(result);
|
|
589
|
+
return result;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// ../devtools/plugin/src/transforms/source-utils.ts
|
|
593
|
+
function injectNamedImportIfMissing(code, key, name) {
|
|
594
|
+
if (code.includes(key)) {
|
|
595
|
+
return code;
|
|
596
|
+
}
|
|
597
|
+
return `import { ${name} } from '${key}';
|
|
598
|
+
${code}`;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// ../devtools/plugin/src/transforms/component-transform.ts
|
|
602
|
+
function transformComponentFile(code, id) {
|
|
603
|
+
const codeWithCollectorImport = injectNamedImportIfMissing(
|
|
604
|
+
code,
|
|
605
|
+
VIRTUAL_QWIK_DEVTOOLS_KEY,
|
|
606
|
+
INNER_USE_HOOK
|
|
607
|
+
);
|
|
608
|
+
return parseQwikCode(codeWithCollectorImport, { path: id });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// ../devtools/plugin/src/virtualmodules/useCollectHooks.ts
|
|
612
|
+
var globalKey = JSON.stringify(QWIK_DEVTOOLS_GLOBAL.key);
|
|
613
|
+
var globalVersion = JSON.stringify(QWIK_DEVTOOLS_GLOBAL.version);
|
|
614
|
+
var componentStateKey = JSON.stringify(QWIK_DEVTOOLS_GLOBAL.props.componentState);
|
|
615
|
+
var useCollectHooks = `import { $, useSignal, useVisibleTask$ } from "@qwik.dev/core"
|
|
616
|
+
import { _getContextContainer, _getContextHostElement } from "@qwik.dev/core/internal"
|
|
617
|
+
|
|
618
|
+
function initComponentState() {
|
|
619
|
+
return {
|
|
620
|
+
hooks: [],
|
|
621
|
+
stats: {}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
function getOrCreateState(src) {
|
|
626
|
+
const root = window[${globalKey}] || (window[${globalKey}] = { version: ${globalVersion} })
|
|
627
|
+
root.version = root.version || ${globalVersion}
|
|
628
|
+
if (!root[${componentStateKey}]) {
|
|
629
|
+
root[${componentStateKey}] = {}
|
|
630
|
+
}
|
|
631
|
+
if (!root[${componentStateKey}][src]) {
|
|
632
|
+
root[${componentStateKey}][src] = initComponentState()
|
|
633
|
+
}
|
|
634
|
+
return root[${componentStateKey}][src]
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Hook to collect component hooks
|
|
639
|
+
*/
|
|
640
|
+
export const useCollectHooks = (src) => {
|
|
641
|
+
const container = _getContextContainer()
|
|
642
|
+
const host = _getContextHostElement()
|
|
643
|
+
const symbol = container?.getHostProp(host, "q:renderFn")?.getSymbol?.()
|
|
644
|
+
const hooksList = useSignal(new Set())
|
|
645
|
+
useVisibleTask$(({ track }) => {
|
|
646
|
+
const newHooks = track(() => hooksList.value)
|
|
647
|
+
const state = getOrCreateState(src)
|
|
648
|
+
state.hooks = [...newHooks]
|
|
649
|
+
state.symbol = symbol
|
|
650
|
+
window[${globalKey}]?.hook?.refreshVNodeTree?.()
|
|
651
|
+
}, { strategy: 'document-ready' })
|
|
652
|
+
|
|
653
|
+
return $((args) => {
|
|
654
|
+
if (hooksList.value.has(args)) {
|
|
655
|
+
return
|
|
656
|
+
}
|
|
657
|
+
hooksList.value.add(args)
|
|
658
|
+
if (typeof window !== 'undefined') {
|
|
659
|
+
getOrCreateState(src).hooks = [...hooksList.value]
|
|
660
|
+
window[${globalKey}]?.hook?.refreshVNodeTree?.()
|
|
661
|
+
}
|
|
662
|
+
})
|
|
663
|
+
}
|
|
664
|
+
`;
|
|
665
|
+
var useCollectHooks_default = useCollectHooks;
|
|
666
|
+
|
|
667
|
+
// ../devtools/plugin/src/runtime/create-runtime-module.ts
|
|
668
|
+
function createRuntimeModule(parts) {
|
|
669
|
+
return parts.filter((part) => typeof part === "string" && part.length > 0).map((part) => part.trim()).join("\n\n") + "\n";
|
|
670
|
+
}
|
|
671
|
+
function createRuntimeInstallerSource(installer) {
|
|
672
|
+
return installer.toString();
|
|
673
|
+
}
|
|
674
|
+
function createRuntimeCall(name, args) {
|
|
675
|
+
return `${name}(${args.map(serializeRuntimeValue).join(", ")});`;
|
|
676
|
+
}
|
|
677
|
+
function runtimeExpression(source) {
|
|
678
|
+
return { __runtimeExpression: source };
|
|
679
|
+
}
|
|
680
|
+
function createRuntimeVar(name, value) {
|
|
681
|
+
return `var ${name} = ${JSON.stringify(value)};`;
|
|
682
|
+
}
|
|
683
|
+
function serializeRuntimeValue(value) {
|
|
684
|
+
if (isRuntimeExpression(value)) {
|
|
685
|
+
return value.__runtimeExpression;
|
|
686
|
+
}
|
|
687
|
+
if (Array.isArray(value)) {
|
|
688
|
+
return `[${value.map(serializeRuntimeValue).join(", ")}]`;
|
|
689
|
+
}
|
|
690
|
+
if (value && typeof value === "object") {
|
|
691
|
+
return `{ ${Object.entries(value).map(([key, item]) => `${JSON.stringify(key)}: ${serializeRuntimeValue(item)}`).join(", ")} }`;
|
|
692
|
+
}
|
|
693
|
+
return JSON.stringify(value);
|
|
694
|
+
}
|
|
695
|
+
function isRuntimeExpression(value) {
|
|
696
|
+
return Boolean(value) && typeof value === "object" && typeof value.__runtimeExpression === "string";
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
// ../devtools/plugin/src/runtime/installers.ts
|
|
700
|
+
function __qwik_derive_component_name__(path) {
|
|
701
|
+
const lastSeg = path.split("/").pop() || path;
|
|
702
|
+
const sourceSuffix = lastSeg.match(/\.[jt]sx?_(.+)$/);
|
|
703
|
+
if (sourceSuffix) {
|
|
704
|
+
return sourceSuffix[1];
|
|
705
|
+
}
|
|
706
|
+
const underIdx = lastSeg.lastIndexOf("_");
|
|
707
|
+
return underIdx > 0 ? lastSeg.substring(underIdx + 1) : lastSeg;
|
|
708
|
+
}
|
|
709
|
+
function __qwik_find_component_key__(state, componentName, qrlChunk) {
|
|
710
|
+
const keys = Object.keys(state);
|
|
711
|
+
if (qrlChunk) {
|
|
712
|
+
const byChunk = keys.find((key) => key.endsWith(qrlChunk));
|
|
713
|
+
if (byChunk) {
|
|
714
|
+
return byChunk;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
const lowerName = componentName.toLowerCase();
|
|
718
|
+
for (const key of keys) {
|
|
719
|
+
if (__qwik_derive_component_name__(key).toLowerCase() === lowerName) {
|
|
720
|
+
return key;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return null;
|
|
724
|
+
}
|
|
725
|
+
function __qwik_install_hook_runtime__(options) {
|
|
726
|
+
const renderListeners = [];
|
|
727
|
+
const signalTypes = {};
|
|
728
|
+
for (let i = 0; i < options.signalHookTypes.length; i++) {
|
|
729
|
+
signalTypes[options.signalHookTypes[i]] = true;
|
|
730
|
+
}
|
|
731
|
+
const safeSerialize = (val) => {
|
|
732
|
+
if (val === null || val === void 0) {
|
|
733
|
+
return val;
|
|
734
|
+
}
|
|
735
|
+
const t = typeof val;
|
|
736
|
+
if (t === "string" || t === "number" || t === "boolean") {
|
|
737
|
+
return val;
|
|
738
|
+
}
|
|
739
|
+
if (t === "function") {
|
|
740
|
+
return "[Function]";
|
|
741
|
+
}
|
|
742
|
+
try {
|
|
743
|
+
return JSON.parse(JSON.stringify(val));
|
|
744
|
+
} catch (_) {
|
|
745
|
+
return "[" + t + "]";
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
const serializeDeep = (val, depth) => {
|
|
749
|
+
if (depth > 6) {
|
|
750
|
+
return "[depth limit]";
|
|
751
|
+
}
|
|
752
|
+
if (val === null) {
|
|
753
|
+
return null;
|
|
754
|
+
}
|
|
755
|
+
if (val === void 0) {
|
|
756
|
+
return void 0;
|
|
757
|
+
}
|
|
758
|
+
const t = typeof val;
|
|
759
|
+
if (t === "string" || t === "number" || t === "boolean") {
|
|
760
|
+
return val;
|
|
761
|
+
}
|
|
762
|
+
if (t === "function") {
|
|
763
|
+
const n = val.name || "anonymous";
|
|
764
|
+
return { __type: "function", __name: n };
|
|
765
|
+
}
|
|
766
|
+
try {
|
|
767
|
+
if (val && t === "object" && "$untrackedValue$" in val) {
|
|
768
|
+
return serializeDeep(val.$untrackedValue$, depth + 1);
|
|
769
|
+
}
|
|
770
|
+
if (Array.isArray(val)) {
|
|
771
|
+
return val.map((item) => serializeDeep(item, depth + 1));
|
|
772
|
+
}
|
|
773
|
+
if (t === "object") {
|
|
774
|
+
const className = val.constructor ? val.constructor.name : "Object";
|
|
775
|
+
const result = {};
|
|
776
|
+
if (className !== "Object") {
|
|
777
|
+
result.__className = className;
|
|
778
|
+
try {
|
|
779
|
+
if (typeof val.toString === "function" && val.toString !== Object.prototype.toString) {
|
|
780
|
+
result.__display = val.toString();
|
|
781
|
+
}
|
|
782
|
+
} catch (_) {
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
const keys = Object.keys(val);
|
|
786
|
+
for (let i = 0; i < keys.length; i++) {
|
|
787
|
+
const key = keys[i];
|
|
788
|
+
if (key.startsWith("$") && key.endsWith("$")) {
|
|
789
|
+
continue;
|
|
790
|
+
}
|
|
791
|
+
try {
|
|
792
|
+
result[key] = serializeDeep(val[key], depth + 1);
|
|
793
|
+
} catch (_) {
|
|
794
|
+
result[key] = "[unreadable]";
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return result;
|
|
798
|
+
}
|
|
799
|
+
} catch (_) {
|
|
800
|
+
}
|
|
801
|
+
return String(val);
|
|
802
|
+
};
|
|
803
|
+
const readValue = (ref) => {
|
|
804
|
+
try {
|
|
805
|
+
if (ref && typeof ref === "object" && "value" in ref) {
|
|
806
|
+
return safeSerialize(ref.value);
|
|
807
|
+
}
|
|
808
|
+
if (ref && typeof ref === "object") {
|
|
809
|
+
return safeSerialize(ref);
|
|
810
|
+
}
|
|
811
|
+
return void 0;
|
|
812
|
+
} catch (_) {
|
|
813
|
+
return "[error]";
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
const getOrCreateRoot = () => {
|
|
817
|
+
if (typeof window === "undefined") {
|
|
818
|
+
return void 0;
|
|
819
|
+
}
|
|
820
|
+
const win = window;
|
|
821
|
+
const root2 = win[options.devtoolsGlobalKey] || (win[options.devtoolsGlobalKey] = {
|
|
822
|
+
version: options.globalVersion
|
|
823
|
+
});
|
|
824
|
+
root2.version = root2.version || options.globalVersion;
|
|
825
|
+
root2[options.componentStateKey] = root2[options.componentStateKey] || {};
|
|
826
|
+
return root2;
|
|
827
|
+
};
|
|
828
|
+
const getState = () => {
|
|
829
|
+
return getOrCreateRoot()?.[options.componentStateKey];
|
|
830
|
+
};
|
|
831
|
+
const methods = {
|
|
832
|
+
_emitRender(info) {
|
|
833
|
+
for (let i = 0; i < renderListeners.length; i++) {
|
|
834
|
+
try {
|
|
835
|
+
renderListeners[i](info);
|
|
836
|
+
} catch (_) {
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
},
|
|
840
|
+
getSignalValue(signal) {
|
|
841
|
+
if (signal && typeof signal === "object" && "value" in signal) {
|
|
842
|
+
return signal.value;
|
|
843
|
+
}
|
|
844
|
+
return void 0;
|
|
845
|
+
},
|
|
846
|
+
getSignalsSnapshot() {
|
|
847
|
+
const state = getState();
|
|
848
|
+
if (!state) {
|
|
849
|
+
return {};
|
|
850
|
+
}
|
|
851
|
+
const snapshot = {};
|
|
852
|
+
for (const path of Object.keys(state)) {
|
|
853
|
+
const hooks = state[path].hooks || [];
|
|
854
|
+
const signals = [];
|
|
855
|
+
for (const h of hooks) {
|
|
856
|
+
if (signalTypes[h.hookType] && h.data != null) {
|
|
857
|
+
signals.push({
|
|
858
|
+
name: h.variableName || "",
|
|
859
|
+
hookType: h.hookType,
|
|
860
|
+
value: readValue(h.data)
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
if (signals.length > 0) {
|
|
865
|
+
snapshot[path] = signals;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
return snapshot;
|
|
869
|
+
},
|
|
870
|
+
getComponentTreeSnapshot() {
|
|
871
|
+
const state = getState();
|
|
872
|
+
if (!state) {
|
|
873
|
+
return [];
|
|
874
|
+
}
|
|
875
|
+
return Object.keys(state).map((path) => {
|
|
876
|
+
const comp = state[path];
|
|
877
|
+
const hooks = comp.hooks || [];
|
|
878
|
+
const name = __qwik_derive_component_name__(path);
|
|
879
|
+
const signals = [];
|
|
880
|
+
const hookEntries = [];
|
|
881
|
+
for (const h of hooks) {
|
|
882
|
+
hookEntries.push({
|
|
883
|
+
variableName: h.variableName || "",
|
|
884
|
+
hookType: h.hookType || "",
|
|
885
|
+
category: h.category || ""
|
|
886
|
+
});
|
|
887
|
+
if (signalTypes[h.hookType] && h.data != null) {
|
|
888
|
+
signals.push({
|
|
889
|
+
name: h.variableName || "",
|
|
890
|
+
hookType: h.hookType,
|
|
891
|
+
value: readValue(h.data)
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
return {
|
|
896
|
+
path,
|
|
897
|
+
name,
|
|
898
|
+
signals,
|
|
899
|
+
hooks: hookEntries,
|
|
900
|
+
...comp.symbol ? { symbol: comp.symbol } : {}
|
|
901
|
+
};
|
|
902
|
+
});
|
|
903
|
+
},
|
|
904
|
+
onRender(callback) {
|
|
905
|
+
renderListeners.push(callback);
|
|
906
|
+
return () => {
|
|
907
|
+
const idx = renderListeners.indexOf(callback);
|
|
908
|
+
if (idx >= 0) {
|
|
909
|
+
renderListeners.splice(idx, 1);
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
},
|
|
913
|
+
getComponentDetail(componentName, qrlChunk) {
|
|
914
|
+
const state = getState();
|
|
915
|
+
if (!state) {
|
|
916
|
+
return null;
|
|
917
|
+
}
|
|
918
|
+
const matchingKey = __qwik_find_component_key__(state, componentName, qrlChunk ?? null);
|
|
919
|
+
if (!matchingKey) {
|
|
920
|
+
return null;
|
|
921
|
+
}
|
|
922
|
+
const comp = state[matchingKey];
|
|
923
|
+
if (!comp || !comp.hooks) {
|
|
924
|
+
return null;
|
|
925
|
+
}
|
|
926
|
+
return comp.hooks.filter((h) => h.data != null).map((h) => ({
|
|
927
|
+
hookType: h.hookType || "unknown",
|
|
928
|
+
variableName: h.variableName || h.hookType || "unknown",
|
|
929
|
+
data: serializeDeep(h.data, 0)
|
|
930
|
+
}));
|
|
931
|
+
},
|
|
932
|
+
setSignalValue(componentName, qrlChunk, variableName, newValue) {
|
|
933
|
+
const state = getState();
|
|
934
|
+
if (!state) {
|
|
935
|
+
return false;
|
|
936
|
+
}
|
|
937
|
+
const matchingKey = __qwik_find_component_key__(state, componentName, qrlChunk ?? null);
|
|
938
|
+
if (!matchingKey) {
|
|
939
|
+
return false;
|
|
940
|
+
}
|
|
941
|
+
const comp = state[matchingKey];
|
|
942
|
+
if (!comp || !comp.hooks) {
|
|
943
|
+
return false;
|
|
944
|
+
}
|
|
945
|
+
for (const h of comp.hooks) {
|
|
946
|
+
if (h.variableName === variableName && h.data != null) {
|
|
947
|
+
try {
|
|
948
|
+
if (typeof h.data === "object" && "value" in h.data) {
|
|
949
|
+
h.data.value = newValue;
|
|
950
|
+
return true;
|
|
951
|
+
}
|
|
952
|
+
} catch (_) {
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
return false;
|
|
957
|
+
},
|
|
958
|
+
onSignalUpdate(_callback) {
|
|
959
|
+
return () => {
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
};
|
|
963
|
+
const root = getOrCreateRoot();
|
|
964
|
+
if (!root || root[options.hookKey]) {
|
|
965
|
+
return;
|
|
966
|
+
}
|
|
967
|
+
root[options.hookKey] = {
|
|
968
|
+
version: 1,
|
|
969
|
+
...methods
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
function __qwik_install_vnode_runtime__(options, internals) {
|
|
973
|
+
const QRENDERFN = options.qRenderFn;
|
|
974
|
+
const QPROPS = options.qProps;
|
|
975
|
+
const QTYPE = options.qType;
|
|
976
|
+
const QID = options.qId;
|
|
977
|
+
const QKEY = options.qKey;
|
|
978
|
+
const QCOLON = options.qColon;
|
|
979
|
+
const CHUNK_KEY = options.chunkKey;
|
|
980
|
+
const SYMBOL_KEY = options.symbolKey;
|
|
981
|
+
const UNTRACKED_VALUE_KEY = options.untrackedValueKey;
|
|
982
|
+
let idx = 0;
|
|
983
|
+
let vnodeMap = {};
|
|
984
|
+
function serializeProps(val, depth) {
|
|
985
|
+
if (depth > 4) {
|
|
986
|
+
return "[depth]";
|
|
987
|
+
}
|
|
988
|
+
if (val === null || val === void 0) {
|
|
989
|
+
return val;
|
|
990
|
+
}
|
|
991
|
+
const t = typeof val;
|
|
992
|
+
if (t === "string" || t === "number" || t === "boolean") {
|
|
993
|
+
return val;
|
|
994
|
+
}
|
|
995
|
+
if (t === "function") {
|
|
996
|
+
return "[Function]";
|
|
997
|
+
}
|
|
998
|
+
try {
|
|
999
|
+
if (Array.isArray(val)) {
|
|
1000
|
+
return val.map((item) => serializeProps(item, depth + 1));
|
|
1001
|
+
}
|
|
1002
|
+
if (t === "object") {
|
|
1003
|
+
if (CHUNK_KEY in val || SYMBOL_KEY in val) {
|
|
1004
|
+
return "[QRL]";
|
|
1005
|
+
}
|
|
1006
|
+
if (UNTRACKED_VALUE_KEY in val) {
|
|
1007
|
+
return serializeProps(val[UNTRACKED_VALUE_KEY], depth + 1);
|
|
1008
|
+
}
|
|
1009
|
+
const result = {};
|
|
1010
|
+
const keys = Object.keys(val);
|
|
1011
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1012
|
+
const key = keys[i];
|
|
1013
|
+
if (key.startsWith("$") && key.endsWith("$")) {
|
|
1014
|
+
continue;
|
|
1015
|
+
}
|
|
1016
|
+
try {
|
|
1017
|
+
result[key] = serializeProps(val[key], depth + 1);
|
|
1018
|
+
} catch (_) {
|
|
1019
|
+
result[key] = "[error]";
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
return result;
|
|
1023
|
+
}
|
|
1024
|
+
} catch (_) {
|
|
1025
|
+
}
|
|
1026
|
+
return String(val);
|
|
1027
|
+
}
|
|
1028
|
+
function normalizeName(str) {
|
|
1029
|
+
return str.split("_component")[0];
|
|
1030
|
+
}
|
|
1031
|
+
function buildTree(container, vnode) {
|
|
1032
|
+
if (!vnode) {
|
|
1033
|
+
return [];
|
|
1034
|
+
}
|
|
1035
|
+
const result = [];
|
|
1036
|
+
let current = vnode;
|
|
1037
|
+
while (current) {
|
|
1038
|
+
const isVirtual = internals._vnode_isVirtualVNode(current);
|
|
1039
|
+
const renderFn = isVirtual ? container.getHostProp(current, QRENDERFN) : null;
|
|
1040
|
+
const isComponent = isVirtual && typeof renderFn === "function";
|
|
1041
|
+
if (isComponent) {
|
|
1042
|
+
let component;
|
|
1043
|
+
let name = "Component";
|
|
1044
|
+
let qId = "";
|
|
1045
|
+
let colonId = "";
|
|
1046
|
+
try {
|
|
1047
|
+
const symbol = renderFn.getSymbol?.() || renderFn[SYMBOL_KEY] || "";
|
|
1048
|
+
component = componentNames.get(symbol);
|
|
1049
|
+
name = component?.name || normalizeName(symbol) || "Component";
|
|
1050
|
+
const keys = internals._vnode_getAttrKeys(container, current);
|
|
1051
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1052
|
+
if (keys[i] === QTYPE) {
|
|
1053
|
+
continue;
|
|
1054
|
+
}
|
|
1055
|
+
if (keys[i] === QID) {
|
|
1056
|
+
qId = String(container.getHostProp(current, QID) || "");
|
|
1057
|
+
}
|
|
1058
|
+
if (keys[i] === QCOLON) {
|
|
1059
|
+
colonId = String(container.getHostProp(current, QCOLON) || "");
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
} catch (_) {
|
|
1063
|
+
}
|
|
1064
|
+
let qrlChunk = "";
|
|
1065
|
+
let qrlPath = "";
|
|
1066
|
+
try {
|
|
1067
|
+
const chunk = renderFn[CHUNK_KEY] || "";
|
|
1068
|
+
const splitPoint = "_component";
|
|
1069
|
+
const chunkIdx = chunk.indexOf(splitPoint);
|
|
1070
|
+
qrlChunk = component?.path || (chunkIdx > 0 ? chunk.substring(0, chunkIdx) : chunk);
|
|
1071
|
+
qrlPath = component?.file || (renderFn.dev && renderFn.dev.file ? renderFn.dev.file : qrlChunk);
|
|
1072
|
+
} catch (_) {
|
|
1073
|
+
}
|
|
1074
|
+
let children = [];
|
|
1075
|
+
const firstChild = internals._vnode_getFirstChild(current);
|
|
1076
|
+
if (firstChild) {
|
|
1077
|
+
children = buildTree(container, firstChild);
|
|
1078
|
+
}
|
|
1079
|
+
const nodeProps = qId ? { [QID]: qId } : {};
|
|
1080
|
+
if (colonId) {
|
|
1081
|
+
nodeProps.__colonId = colonId;
|
|
1082
|
+
}
|
|
1083
|
+
if (qrlChunk) {
|
|
1084
|
+
nodeProps.__qrlChunk = qrlChunk;
|
|
1085
|
+
}
|
|
1086
|
+
if (qrlPath) {
|
|
1087
|
+
nodeProps.__qrlPath = qrlPath;
|
|
1088
|
+
}
|
|
1089
|
+
const nodeId = qId ? "q-" + qId : "vnode-" + idx++;
|
|
1090
|
+
vnodeMap[nodeId] = { vnode: current, container };
|
|
1091
|
+
result.push({
|
|
1092
|
+
name,
|
|
1093
|
+
id: nodeId,
|
|
1094
|
+
label: name,
|
|
1095
|
+
...component?.file ? { source: { file: component.file } } : {},
|
|
1096
|
+
props: nodeProps,
|
|
1097
|
+
children: children.length > 0 ? children : void 0
|
|
1098
|
+
});
|
|
1099
|
+
} else if (internals._vnode_isMaterialized(current) || isVirtual && !isComponent) {
|
|
1100
|
+
const fc = internals._vnode_getFirstChild(current);
|
|
1101
|
+
if (fc) {
|
|
1102
|
+
const nested = buildTree(container, fc);
|
|
1103
|
+
for (let j = 0; j < nested.length; j++) {
|
|
1104
|
+
result.push(nested[j]);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
current = current.nextSibling || null;
|
|
1109
|
+
}
|
|
1110
|
+
return result;
|
|
1111
|
+
}
|
|
1112
|
+
function filterDevtools(nodes) {
|
|
1113
|
+
const result = [];
|
|
1114
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1115
|
+
let n = nodes[i];
|
|
1116
|
+
if (n.name?.toLowerCase() === "qwikdevtools" || n.name?.toLowerCase() === "devtoolscontainer") {
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
if (n.children) {
|
|
1120
|
+
n = {
|
|
1121
|
+
name: n.name,
|
|
1122
|
+
id: n.id,
|
|
1123
|
+
label: n.label,
|
|
1124
|
+
props: n.props,
|
|
1125
|
+
...n.source ? { source: n.source } : {},
|
|
1126
|
+
children: filterDevtools(n.children)
|
|
1127
|
+
};
|
|
1128
|
+
if (n.children?.length === 0) {
|
|
1129
|
+
delete n.children;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
result.push(n);
|
|
1133
|
+
}
|
|
1134
|
+
return result;
|
|
1135
|
+
}
|
|
1136
|
+
const componentNames = /* @__PURE__ */ new Map();
|
|
1137
|
+
function getTree() {
|
|
1138
|
+
try {
|
|
1139
|
+
idx = 0;
|
|
1140
|
+
vnodeMap = {};
|
|
1141
|
+
componentNames.clear();
|
|
1142
|
+
const hook = window[options.devtoolsGlobalKey]?.[options.hookKey];
|
|
1143
|
+
for (const component of hook?.getComponentTreeSnapshot?.() ?? []) {
|
|
1144
|
+
if (component.symbol) {
|
|
1145
|
+
componentNames.set(component.symbol, {
|
|
1146
|
+
name: component.name,
|
|
1147
|
+
path: component.path,
|
|
1148
|
+
file: component.path.match(/^(.*\.[jt]sx?)_/)?.[1]
|
|
1149
|
+
});
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
const elements = document.querySelectorAll("[q\\:container]");
|
|
1153
|
+
if (!elements.length) {
|
|
1154
|
+
return null;
|
|
1155
|
+
}
|
|
1156
|
+
const tree = [];
|
|
1157
|
+
for (const element of elements) {
|
|
1158
|
+
const container = internals._getDomContainer(element);
|
|
1159
|
+
if (!container?.rootVNode) {
|
|
1160
|
+
return null;
|
|
1161
|
+
}
|
|
1162
|
+
tree.push(...buildTree(container, container.rootVNode));
|
|
1163
|
+
}
|
|
1164
|
+
return filterDevtools(tree);
|
|
1165
|
+
} catch (_) {
|
|
1166
|
+
return null;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function setupBridge() {
|
|
1170
|
+
if (typeof window === "undefined") {
|
|
1171
|
+
return;
|
|
1172
|
+
}
|
|
1173
|
+
const hook = window[options.devtoolsGlobalKey]?.[options.hookKey];
|
|
1174
|
+
if (!hook || hook.getVNodeTree) {
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
hook.getVNodeTree = getTree;
|
|
1178
|
+
hook.resolveElementToComponent = function(el) {
|
|
1179
|
+
if (!el) {
|
|
1180
|
+
return null;
|
|
1181
|
+
}
|
|
1182
|
+
let cur = el;
|
|
1183
|
+
while (cur) {
|
|
1184
|
+
const inspector = cur.getAttribute ? cur.getAttribute("data-qwik-inspector") : null;
|
|
1185
|
+
if (inspector) {
|
|
1186
|
+
const parts = inspector.split("/");
|
|
1187
|
+
const fileName = (parts[parts.length - 1] || "").split(":")[0];
|
|
1188
|
+
const compName = fileName.replace(/[.](tsx|ts|jsx|js)$/, "");
|
|
1189
|
+
if (compName) {
|
|
1190
|
+
for (const id in vnodeMap) {
|
|
1191
|
+
const entry = vnodeMap[id];
|
|
1192
|
+
try {
|
|
1193
|
+
const renderFn = entry.container.getHostProp(entry.vnode, QRENDERFN);
|
|
1194
|
+
if (typeof renderFn === "function") {
|
|
1195
|
+
const sym = renderFn.getSymbol ? renderFn.getSymbol() : renderFn[SYMBOL_KEY] || "";
|
|
1196
|
+
const nodeName = normalizeName(sym);
|
|
1197
|
+
if (nodeName.toLowerCase() === compName.toLowerCase()) {
|
|
1198
|
+
return id;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
} catch (_) {
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
cur = cur.parentElement;
|
|
1207
|
+
}
|
|
1208
|
+
return null;
|
|
1209
|
+
};
|
|
1210
|
+
function findDomElement(vnode) {
|
|
1211
|
+
if (!vnode) {
|
|
1212
|
+
return null;
|
|
1213
|
+
}
|
|
1214
|
+
if (!internals._vnode_isVirtualVNode(vnode) || vnode.node) {
|
|
1215
|
+
return vnode.node || null;
|
|
1216
|
+
}
|
|
1217
|
+
let child = internals._vnode_getFirstChild(vnode);
|
|
1218
|
+
while (child) {
|
|
1219
|
+
const el = findDomElement(child);
|
|
1220
|
+
if (el) {
|
|
1221
|
+
return el;
|
|
1222
|
+
}
|
|
1223
|
+
child = child.nextSibling || null;
|
|
1224
|
+
}
|
|
1225
|
+
return null;
|
|
1226
|
+
}
|
|
1227
|
+
hook.getElementRect = function(nodeId) {
|
|
1228
|
+
const entry = vnodeMap[nodeId];
|
|
1229
|
+
if (!entry) {
|
|
1230
|
+
return null;
|
|
1231
|
+
}
|
|
1232
|
+
try {
|
|
1233
|
+
const el = findDomElement(entry.vnode);
|
|
1234
|
+
if (!el) {
|
|
1235
|
+
return null;
|
|
1236
|
+
}
|
|
1237
|
+
const r = el.getBoundingClientRect();
|
|
1238
|
+
return { top: r.top, left: r.left, width: r.width, height: r.height };
|
|
1239
|
+
} catch (_) {
|
|
1240
|
+
return null;
|
|
1241
|
+
}
|
|
1242
|
+
};
|
|
1243
|
+
hook.highlightNode = function(nodeId, name) {
|
|
1244
|
+
const entry = vnodeMap[nodeId];
|
|
1245
|
+
if (!entry) {
|
|
1246
|
+
return false;
|
|
1247
|
+
}
|
|
1248
|
+
try {
|
|
1249
|
+
const el = findDomElement(entry.vnode);
|
|
1250
|
+
if (!el) {
|
|
1251
|
+
return false;
|
|
1252
|
+
}
|
|
1253
|
+
let ov = document.getElementById("__qwik_dt_hover_ov");
|
|
1254
|
+
if (!ov) {
|
|
1255
|
+
ov = document.createElement("div");
|
|
1256
|
+
ov.id = "__qwik_dt_hover_ov";
|
|
1257
|
+
ov.style.cssText = "position:fixed;pointer-events:none;border:2px solid #8b5cf6;background:rgba(139,92,246,0.08);z-index:2147483646;border-radius:4px;transition:all 0.15s ease";
|
|
1258
|
+
const lbl = document.createElement("div");
|
|
1259
|
+
lbl.id = "__qwik_dt_hover_lbl";
|
|
1260
|
+
lbl.style.cssText = "position:absolute;top:-20px;left:-2px;background:#8b5cf6;color:#fff;font-size:10px;padding:1px 6px;border-radius:3px 3px 0 0;white-space:nowrap;font-family:system-ui,sans-serif";
|
|
1261
|
+
ov.appendChild(lbl);
|
|
1262
|
+
document.body.appendChild(ov);
|
|
1263
|
+
}
|
|
1264
|
+
const r = el.getBoundingClientRect();
|
|
1265
|
+
ov.style.display = "block";
|
|
1266
|
+
ov.style.top = r.top + "px";
|
|
1267
|
+
ov.style.left = r.left + "px";
|
|
1268
|
+
ov.style.width = r.width + "px";
|
|
1269
|
+
ov.style.height = r.height + "px";
|
|
1270
|
+
const lbl2 = document.getElementById("__qwik_dt_hover_lbl");
|
|
1271
|
+
if (lbl2) {
|
|
1272
|
+
lbl2.textContent = "<" + (name || "Component") + " />";
|
|
1273
|
+
}
|
|
1274
|
+
return true;
|
|
1275
|
+
} catch (_) {
|
|
1276
|
+
return false;
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
hook.unhighlightNode = function() {
|
|
1280
|
+
const ov = document.getElementById("__qwik_dt_hover_ov");
|
|
1281
|
+
if (ov) {
|
|
1282
|
+
ov.style.display = "none";
|
|
1283
|
+
}
|
|
1284
|
+
};
|
|
1285
|
+
hook.getNodeProps = function(nodeId) {
|
|
1286
|
+
const entry = vnodeMap[nodeId];
|
|
1287
|
+
if (!entry) {
|
|
1288
|
+
return null;
|
|
1289
|
+
}
|
|
1290
|
+
try {
|
|
1291
|
+
const props = entry.container.getHostProp(entry.vnode, QPROPS);
|
|
1292
|
+
if (!props) {
|
|
1293
|
+
return null;
|
|
1294
|
+
}
|
|
1295
|
+
const result = {};
|
|
1296
|
+
const keys = Object.keys(props);
|
|
1297
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1298
|
+
const key = keys[i];
|
|
1299
|
+
if (key.startsWith("on:") || key.startsWith("on$:")) {
|
|
1300
|
+
continue;
|
|
1301
|
+
}
|
|
1302
|
+
try {
|
|
1303
|
+
result[key] = serializeProps(props[key], 0);
|
|
1304
|
+
} catch (_) {
|
|
1305
|
+
result[key] = "[error]";
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
return result;
|
|
1309
|
+
} catch (_) {
|
|
1310
|
+
return null;
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
let debounceTimer = null;
|
|
1314
|
+
const DEBOUNCE_MS = 100;
|
|
1315
|
+
function pushTree() {
|
|
1316
|
+
const tree = getTree();
|
|
1317
|
+
if (!tree) {
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
window.postMessage(
|
|
1321
|
+
{
|
|
1322
|
+
source: options.pageMessageSource,
|
|
1323
|
+
type: options.componentTreeUpdateType,
|
|
1324
|
+
tree
|
|
1325
|
+
},
|
|
1326
|
+
"*"
|
|
1327
|
+
);
|
|
1328
|
+
}
|
|
1329
|
+
hook.refreshVNodeTree = function() {
|
|
1330
|
+
if (debounceTimer) {
|
|
1331
|
+
clearTimeout(debounceTimer);
|
|
1332
|
+
}
|
|
1333
|
+
debounceTimer = setTimeout(pushTree, DEBOUNCE_MS);
|
|
1334
|
+
};
|
|
1335
|
+
const observer = new MutationObserver(hook.refreshVNodeTree);
|
|
1336
|
+
observer.observe(document.documentElement, {
|
|
1337
|
+
childList: true,
|
|
1338
|
+
subtree: true,
|
|
1339
|
+
characterData: true,
|
|
1340
|
+
attributes: true,
|
|
1341
|
+
attributeFilter: [QID, QKEY, QCOLON]
|
|
1342
|
+
});
|
|
1343
|
+
pushTree();
|
|
1344
|
+
}
|
|
1345
|
+
if (typeof window !== "undefined") {
|
|
1346
|
+
if (document.readyState === "loading") {
|
|
1347
|
+
document.addEventListener("DOMContentLoaded", setupBridge);
|
|
1348
|
+
} else {
|
|
1349
|
+
setupBridge();
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
// ../devtools/plugin/src/runtime/create-hook-runtime.ts
|
|
1355
|
+
var HOOK_RUNTIME_UTILS = [
|
|
1356
|
+
createRuntimeInstallerSource(__qwik_derive_component_name__),
|
|
1357
|
+
createRuntimeInstallerSource(__qwik_find_component_key__)
|
|
1358
|
+
];
|
|
1359
|
+
var HOOK_RUNTIME_OPTIONS = {
|
|
1360
|
+
componentStateKey: QWIK_DEVTOOLS_GLOBAL.props.componentState,
|
|
1361
|
+
devtoolsGlobalKey: QWIK_DEVTOOLS_GLOBAL.key,
|
|
1362
|
+
globalVersion: QWIK_DEVTOOLS_GLOBAL.version,
|
|
1363
|
+
hookKey: QWIK_DEVTOOLS_GLOBAL.props.hook,
|
|
1364
|
+
signalHookTypes: SIGNAL_HOOK_TYPES
|
|
1365
|
+
};
|
|
1366
|
+
function createHookRuntime() {
|
|
1367
|
+
return createRuntimeModule([
|
|
1368
|
+
"// [qwik-devtools-hook] runtime (injected by @devtools/plugin)",
|
|
1369
|
+
...HOOK_RUNTIME_UTILS,
|
|
1370
|
+
createRuntimeInstallerSource(__qwik_install_hook_runtime__),
|
|
1371
|
+
createRuntimeCall("__qwik_install_hook_runtime__", [HOOK_RUNTIME_OPTIONS])
|
|
1372
|
+
]);
|
|
1373
|
+
}
|
|
1374
|
+
var EXTENSION_HOOK_BANNER = [
|
|
1375
|
+
"/**",
|
|
1376
|
+
" * Devtools hook runtime - injected by the browser extension into the main world.",
|
|
1377
|
+
" * Sets up window.__QWIK_DEVTOOLS__.hook with signal tracking, component snapshots,",
|
|
1378
|
+
" * and state editing. Skips if the Vite plugin already installed the hook.",
|
|
1379
|
+
" *",
|
|
1380
|
+
" * GENERATED FILE - DO NOT EDIT BY HAND.",
|
|
1381
|
+
" * Source of truth: packages/devtools/plugin/src/runtime/installers.ts",
|
|
1382
|
+
" * (__qwik_install_hook_runtime__)",
|
|
1383
|
+
" * Regenerated automatically by the browser-extension build and dev scripts.",
|
|
1384
|
+
" */"
|
|
1385
|
+
].join("\n");
|
|
1386
|
+
|
|
1387
|
+
// ../devtools/plugin/src/runtime/create-vnode-runtime.ts
|
|
1388
|
+
function createVNodeRuntime() {
|
|
1389
|
+
return createRuntimeModule([
|
|
1390
|
+
`
|
|
1391
|
+
import {
|
|
1392
|
+
_getDomContainer,
|
|
1393
|
+
_vnode_getFirstChild,
|
|
1394
|
+
_vnode_isVirtualVNode,
|
|
1395
|
+
_vnode_isMaterialized,
|
|
1396
|
+
_vnode_getAttrKeys,
|
|
1397
|
+
} from '@qwik.dev/core/internal';
|
|
1398
|
+
`,
|
|
1399
|
+
createRuntimeVar("QRENDERFN", QWIK_VNODE_PROTOCOL.attrs.renderFn),
|
|
1400
|
+
createRuntimeVar("QPROPS", QWIK_VNODE_PROTOCOL.attrs.props),
|
|
1401
|
+
createRuntimeVar("QTYPE", QWIK_VNODE_PROTOCOL.attrs.type),
|
|
1402
|
+
createRuntimeVar("QID", QWIK_VNODE_PROTOCOL.attrs.id),
|
|
1403
|
+
createRuntimeVar("QKEY", QWIK_VNODE_PROTOCOL.attrs.key),
|
|
1404
|
+
createRuntimeVar("QCOLON", QWIK_VNODE_PROTOCOL.attrs.colon),
|
|
1405
|
+
createRuntimeVar("CHUNK_KEY", QWIK_VNODE_PROTOCOL.qrl.chunk),
|
|
1406
|
+
createRuntimeVar("SYMBOL_KEY", QWIK_VNODE_PROTOCOL.qrl.symbol),
|
|
1407
|
+
createRuntimeVar("UNTRACKED_VALUE_KEY", QWIK_VNODE_PROTOCOL.qrl.untrackedValue),
|
|
1408
|
+
`const __qwik_vnode_internal__ = {
|
|
1409
|
+
_getDomContainer,
|
|
1410
|
+
_vnode_getFirstChild,
|
|
1411
|
+
_vnode_isVirtualVNode,
|
|
1412
|
+
_vnode_isMaterialized,
|
|
1413
|
+
_vnode_getAttrKeys,
|
|
1414
|
+
};`,
|
|
1415
|
+
createRuntimeInstallerSource(__qwik_install_vnode_runtime__),
|
|
1416
|
+
createRuntimeCall("__qwik_install_vnode_runtime__", [
|
|
1417
|
+
{
|
|
1418
|
+
chunkKey: runtimeExpression("CHUNK_KEY"),
|
|
1419
|
+
componentTreeUpdateType: DEVTOOLS_MESSAGES.types.componentTreeUpdate,
|
|
1420
|
+
devtoolsGlobalKey: QWIK_DEVTOOLS_GLOBAL.key,
|
|
1421
|
+
hookKey: QWIK_DEVTOOLS_GLOBAL.props.hook,
|
|
1422
|
+
pageMessageSource: DEVTOOLS_MESSAGES.pageSource,
|
|
1423
|
+
qColon: runtimeExpression("QCOLON"),
|
|
1424
|
+
qId: runtimeExpression("QID"),
|
|
1425
|
+
qKey: runtimeExpression("QKEY"),
|
|
1426
|
+
qProps: runtimeExpression("QPROPS"),
|
|
1427
|
+
qRenderFn: runtimeExpression("QRENDERFN"),
|
|
1428
|
+
qType: runtimeExpression("QTYPE"),
|
|
1429
|
+
symbolKey: runtimeExpression("SYMBOL_KEY"),
|
|
1430
|
+
untrackedValueKey: runtimeExpression("UNTRACKED_VALUE_KEY")
|
|
1431
|
+
},
|
|
1432
|
+
runtimeExpression("__qwik_vnode_internal__")
|
|
1433
|
+
])
|
|
1434
|
+
]);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
// src/protocol.ts
|
|
1438
|
+
import { z } from "zod";
|
|
1439
|
+
var pageInput = z.object({ url: z.string().url().optional() });
|
|
1440
|
+
var inspectInput = pageInput.extend({
|
|
1441
|
+
selector: z.string().min(1).optional(),
|
|
1442
|
+
includeHtml: z.boolean().default(false),
|
|
1443
|
+
includeSignalValues: z.boolean().default(false),
|
|
1444
|
+
includeSerializedState: z.boolean().default(false),
|
|
1445
|
+
includeSerializedVNodeTree: z.boolean().default(false),
|
|
1446
|
+
offset: z.number().int().nonnegative().safe().default(0)
|
|
1447
|
+
});
|
|
1448
|
+
|
|
1449
|
+
// src/access.ts
|
|
1450
|
+
function isLoopback(address) {
|
|
1451
|
+
return address === "::1" || address === "127.0.0.1" || address === "::ffff:127.0.0.1";
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
// src/index.ts
|
|
1455
|
+
var bridgeId = "virtual:qwik-mcp";
|
|
1456
|
+
var endpoint = "/__qwik_mcp";
|
|
1457
|
+
function qwikMcp() {
|
|
1458
|
+
let server;
|
|
1459
|
+
const token = randomBytes(32).toString("hex");
|
|
1460
|
+
const pages = /* @__PURE__ */ new Map();
|
|
1461
|
+
const localSockets = /* @__PURE__ */ new WeakSet();
|
|
1462
|
+
const pending = /* @__PURE__ */ new Map();
|
|
1463
|
+
let discoveryFile;
|
|
1464
|
+
let devUrl = "";
|
|
1465
|
+
let endpointUrl = "";
|
|
1466
|
+
const trackedClients = /* @__PURE__ */ new WeakSet();
|
|
1467
|
+
const version = (name) => {
|
|
1468
|
+
const require2 = createRequire(join(server.config.root, "package.json"));
|
|
1469
|
+
try {
|
|
1470
|
+
return require2(`${name}/package.json`).version;
|
|
1471
|
+
} catch {
|
|
1472
|
+
throw new Error(`Cannot resolve ${name} in ${server.config.root}.`);
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1475
|
+
const router = () => server.config.plugins.find((plugin) => plugin.name === "vite-plugin-qwik-router")?.api;
|
|
1476
|
+
const call = async (name, args) => {
|
|
1477
|
+
if (name === "get_project_info") {
|
|
1478
|
+
return {
|
|
1479
|
+
root: server.config.root,
|
|
1480
|
+
qwikVersion: version("@qwik.dev/core"),
|
|
1481
|
+
...router() ? { routerVersion: version("@qwik.dev/router") } : {},
|
|
1482
|
+
devUrl
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
if (name === "list_routes") {
|
|
1486
|
+
return {
|
|
1487
|
+
routerInstalled: !!router(),
|
|
1488
|
+
routes: router()?.getRoutes().map((route) => ({
|
|
1489
|
+
pathname: route.pathname,
|
|
1490
|
+
file: relative(server.config.root, route.filePath),
|
|
1491
|
+
params: route.paramNames,
|
|
1492
|
+
layouts: route.layouts.map((layout) => relative(server.config.root, layout.filePath))
|
|
1493
|
+
})) ?? []
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
if (name !== "inspect_page" && name !== "get_dev_errors") {
|
|
1497
|
+
throw new Error("Unknown MCP tool.");
|
|
1498
|
+
}
|
|
1499
|
+
args = inspectInput.parse(args);
|
|
1500
|
+
const matches = [...pages.values()].filter((page) => !args.url || page.url === args.url);
|
|
1501
|
+
if (!matches.length) {
|
|
1502
|
+
throw new Error("No connected page. Open the requested Qwik URL in a local browser.");
|
|
1503
|
+
}
|
|
1504
|
+
if (matches.length !== 1) {
|
|
1505
|
+
throw new Error(
|
|
1506
|
+
`Multiple pages are connected. Supply a unique url or close duplicate tabs: ${matches.map((page) => page.url).join(", ")}`
|
|
1507
|
+
);
|
|
1508
|
+
}
|
|
1509
|
+
const client = matches[0].client;
|
|
1510
|
+
const id = randomUUID();
|
|
1511
|
+
return new Promise((resolve, reject) => {
|
|
1512
|
+
const timeout = setTimeout(() => {
|
|
1513
|
+
pending.delete(id);
|
|
1514
|
+
reject(new Error("Page did not respond. Reload the page and retry."));
|
|
1515
|
+
}, 5e3);
|
|
1516
|
+
pending.set(id, {
|
|
1517
|
+
client,
|
|
1518
|
+
resolve: (result) => {
|
|
1519
|
+
clearTimeout(timeout);
|
|
1520
|
+
resolve(result);
|
|
1521
|
+
},
|
|
1522
|
+
reject: (error) => {
|
|
1523
|
+
clearTimeout(timeout);
|
|
1524
|
+
reject(error);
|
|
1525
|
+
}
|
|
1526
|
+
});
|
|
1527
|
+
client.send({ type: "custom", event: "qwik:mcp:request", data: { id, name, args } });
|
|
1528
|
+
});
|
|
1529
|
+
};
|
|
1530
|
+
return {
|
|
1531
|
+
name: "qwik-mcp",
|
|
1532
|
+
apply: "serve",
|
|
1533
|
+
enforce: "pre",
|
|
1534
|
+
resolveId(id) {
|
|
1535
|
+
const normalized = id.replace(/^\/?@id\//, "").replace(/^\//, "").split("?")[0];
|
|
1536
|
+
if (normalized === bridgeId || normalized === VIRTUAL_QWIK_DEVTOOLS_KEY) {
|
|
1537
|
+
return `/${normalized}`;
|
|
1538
|
+
}
|
|
1539
|
+
},
|
|
1540
|
+
async load(id) {
|
|
1541
|
+
if (id === `/${VIRTUAL_QWIK_DEVTOOLS_KEY}`) {
|
|
1542
|
+
return useCollectHooks_default;
|
|
1543
|
+
}
|
|
1544
|
+
if (id === `/${bridgeId}`) {
|
|
1545
|
+
return createHookRuntime() + createVNodeRuntime() + await readFile(new URL("../dist/browser.js", import.meta.url), "utf8");
|
|
1546
|
+
}
|
|
1547
|
+
},
|
|
1548
|
+
transform(code, id) {
|
|
1549
|
+
if (/\.[jt]sx(?:\?|$)/.test(id) && !id.includes("/node_modules/") && code.includes("component$")) {
|
|
1550
|
+
return { code: transformComponentFile(code, id), map: null };
|
|
1551
|
+
}
|
|
1552
|
+
},
|
|
1553
|
+
transformIndexHtml() {
|
|
1554
|
+
return [
|
|
1555
|
+
{
|
|
1556
|
+
tag: "script",
|
|
1557
|
+
attrs: { type: "module", src: `${server.config.base}@id/${bridgeId}` },
|
|
1558
|
+
injectTo: "head"
|
|
1559
|
+
}
|
|
1560
|
+
];
|
|
1561
|
+
},
|
|
1562
|
+
configureServer(vite) {
|
|
1563
|
+
server = vite;
|
|
1564
|
+
if (!server.httpServer || server.config.server.https || server.config.server.hmr === false) {
|
|
1565
|
+
throw new Error("qwikMcp requires a standalone HTTP Vite server with HMR enabled.");
|
|
1566
|
+
}
|
|
1567
|
+
const directory = join(server.config.root, "node_modules/.cache/qwik-mcp");
|
|
1568
|
+
discoveryFile = join(directory, `${process.pid}-${randomUUID()}.json`);
|
|
1569
|
+
server.httpServer.once("listening", async () => {
|
|
1570
|
+
const address = server.httpServer.address();
|
|
1571
|
+
if (!address || typeof address === "string") {
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1574
|
+
const host = address.address === "::1" ? "[::1]" : "127.0.0.1";
|
|
1575
|
+
endpointUrl = `http://${host}:${address.port}${endpoint}`;
|
|
1576
|
+
devUrl = `http://${host}:${address.port}${server.config.base}`;
|
|
1577
|
+
try {
|
|
1578
|
+
await mkdir(directory, { recursive: true, mode: 448 });
|
|
1579
|
+
await writeFile(discoveryFile, JSON.stringify({ url: endpointUrl, token }), {
|
|
1580
|
+
mode: 384,
|
|
1581
|
+
flag: "wx"
|
|
1582
|
+
});
|
|
1583
|
+
} catch (error) {
|
|
1584
|
+
server.config.logger.error(`Cannot write Qwik MCP discovery file: ${error}`);
|
|
1585
|
+
}
|
|
1586
|
+
});
|
|
1587
|
+
server.httpServer.once("close", () => {
|
|
1588
|
+
void unlink(discoveryFile).catch(() => {
|
|
1589
|
+
});
|
|
1590
|
+
});
|
|
1591
|
+
server.ws.on("connection", (socket, request) => {
|
|
1592
|
+
if (isLoopback(request.socket.remoteAddress)) {
|
|
1593
|
+
localSockets.add(socket);
|
|
1594
|
+
}
|
|
1595
|
+
});
|
|
1596
|
+
server.ws.on("qwik:mcp:page", (page, client) => {
|
|
1597
|
+
if (!localSockets.has(client.socket) || typeof page?.pageId !== "string" || typeof page.url !== "string") {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
try {
|
|
1601
|
+
if (!["http:", "https:"].includes(new URL(page.url).protocol)) {
|
|
1602
|
+
return;
|
|
1603
|
+
}
|
|
1604
|
+
} catch {
|
|
1605
|
+
return;
|
|
1606
|
+
}
|
|
1607
|
+
if (pages.has(page.pageId) && pages.get(page.pageId).client !== client) {
|
|
1608
|
+
return;
|
|
1609
|
+
}
|
|
1610
|
+
pages.set(page.pageId, { url: page.url, client });
|
|
1611
|
+
if (trackedClients.has(client)) {
|
|
1612
|
+
return;
|
|
1613
|
+
}
|
|
1614
|
+
trackedClients.add(client);
|
|
1615
|
+
client.socket.once("close", () => {
|
|
1616
|
+
for (const [id, page2] of pages) {
|
|
1617
|
+
if (page2.client === client) {
|
|
1618
|
+
pages.delete(id);
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
for (const [id, request] of pending) {
|
|
1622
|
+
if (request.client === client) {
|
|
1623
|
+
pending.delete(id);
|
|
1624
|
+
request.reject(new Error("Page disconnected."));
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
});
|
|
1628
|
+
});
|
|
1629
|
+
server.ws.on("qwik:mcp:leave", (page, client) => {
|
|
1630
|
+
if (page?.pageId && pages.get(page.pageId)?.client === client) {
|
|
1631
|
+
pages.delete(page.pageId);
|
|
1632
|
+
}
|
|
1633
|
+
});
|
|
1634
|
+
server.ws.on(
|
|
1635
|
+
"qwik:mcp:response",
|
|
1636
|
+
(response, client) => {
|
|
1637
|
+
const request = pending.get(response?.id);
|
|
1638
|
+
if (!request || request.client !== client) {
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
pending.delete(response.id);
|
|
1642
|
+
if (response.error) {
|
|
1643
|
+
request.reject(new Error(response.error));
|
|
1644
|
+
} else {
|
|
1645
|
+
request.resolve(response.result);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
);
|
|
1649
|
+
server.middlewares.use(async (req, res, next) => {
|
|
1650
|
+
if (req.url?.split("?")[0] !== endpoint) {
|
|
1651
|
+
next();
|
|
1652
|
+
return;
|
|
1653
|
+
}
|
|
1654
|
+
if (!isLoopback(req.socket.remoteAddress) || req.headers.authorization !== `Bearer ${token}` || req.headers.origin || req.headers.host !== new URL(endpointUrl).host) {
|
|
1655
|
+
res.writeHead(403).end();
|
|
1656
|
+
return;
|
|
1657
|
+
}
|
|
1658
|
+
if (req.method !== "POST") {
|
|
1659
|
+
res.writeHead(405).end();
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
res.setHeader("content-type", "application/json");
|
|
1663
|
+
res.setHeader("cache-control", "no-store");
|
|
1664
|
+
try {
|
|
1665
|
+
let body = Buffer.alloc(0);
|
|
1666
|
+
for await (const chunk of req) {
|
|
1667
|
+
body = Buffer.concat([body, chunk]);
|
|
1668
|
+
if (body.length > 8192) {
|
|
1669
|
+
throw new Error("MCP request is too large.");
|
|
1670
|
+
}
|
|
1671
|
+
}
|
|
1672
|
+
const { name, args } = JSON.parse(body.toString("utf8"));
|
|
1673
|
+
const result = await call(name, args ?? {});
|
|
1674
|
+
res.end(JSON.stringify({ result }));
|
|
1675
|
+
} catch (error) {
|
|
1676
|
+
res.end(
|
|
1677
|
+
JSON.stringify({ error: error instanceof Error ? error.message : String(error) })
|
|
1678
|
+
);
|
|
1679
|
+
}
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
export {
|
|
1685
|
+
qwikMcp
|
|
1686
|
+
};
|