@qwik.dev/devtools 2.0.0-rc.2 → 2.0.0-rc.3

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.
@@ -564,6 +564,7 @@ declare const QWIK_ATTR: {
564
564
  readonly BASE: "q:base";
565
565
  readonly CONTAINER: "q:container";
566
566
  readonly ID: "q:id";
567
+ readonly INSPECTOR: "data-qwik-inspector";
567
568
  readonly KEY: "q:key";
568
569
  readonly MANIFEST_HASH: "q:manifest-hash";
569
570
  readonly RENDER: "q:render";
@@ -319,6 +319,7 @@ const QWIK_ATTR = {
319
319
  BASE: "q:base",
320
320
  CONTAINER: "q:container",
321
321
  ID: "q:id",
322
+ INSPECTOR: "data-qwik-inspector",
322
323
  KEY: "q:key",
323
324
  MANIFEST_HASH: "q:manifest-hash",
324
325
  RENDER: "q:render",
@@ -1175,39 +1175,13 @@ function isKnownHook(name) {
1175
1175
  function isCustomHook(hookName) {
1176
1176
  return !USE_HOOK_LIST.some((item) => item.startsWith(hookName)) && /^use[A-Z_]/.test(hookName);
1177
1177
  }
1178
- /** Finds the start position of the line containing the given index */
1179
- function findLineStart$1(code, index) {
1180
- for (let i = index - 1; i >= 0; i--) {
1181
- const ch = code[i];
1182
- if (ch === "\n" || ch === "\r") return i + 1;
1183
- }
1184
- return 0;
1185
- }
1186
- /** Reads the indentation (spaces/tabs) starting from a given position */
1187
- function readIndent(code, indexFrom) {
1188
- let indent = "";
1189
- let i = indexFrom;
1190
- while (i < code.length) {
1191
- const ch = code[i];
1192
- if (ch === " " || ch === " ") {
1193
- indent += ch;
1194
- i++;
1195
- } else break;
1196
- }
1197
- return indent;
1198
- }
1199
1178
  /** Removes trailing semicolon and whitespace from a code segment */
1200
1179
  function trimStatementSemicolon(segment) {
1201
1180
  return segment.trim().replace(/;?\s*$/, "");
1202
1181
  }
1203
- /** Builds the collecthook() call code with proper indentation */
1204
- function buildCollecthookPayload(indent, variableName, hookType, category, hookExpression) {
1205
- return `${indent}collecthook({
1206
- ${indent} variableName: '${variableName}',
1207
- ${indent} hookType: '${hookType}',
1208
- ${indent} category: '${category}',
1209
- ${indent} data: ${hookExpression === "undefined" ? "undefined" : hookExpression}
1210
- ${indent}});\n`;
1182
+ /** Builds a single-line collecthook() call so injected tracking never adds source lines */
1183
+ function buildCollecthookPayload(variableName, hookType, category, hookExpression) {
1184
+ return `collecthook({ variableName: '${variableName}', hookType: '${hookType}', category: '${category}', data: ${hookExpression} });`;
1211
1185
  }
1212
1186
  /** Checks if a collecthook call already exists after the given position (by variable ID) */
1213
1187
  function hasCollecthookAfterByVariableId(code, fromIndex, variableId, maxLookahead = 600) {
@@ -1358,31 +1332,16 @@ function injectInitHooks(code, bodies, options) {
1358
1332
  function createInitTask(code, body, options) {
1359
1333
  const { insertPos, exportName } = body;
1360
1334
  if (hasExistingCollecthook(code, insertPos)) return null;
1361
- const { insertIndex, prefixNewline } = calculateInsertPosition(code, insertPos);
1362
1335
  return {
1363
1336
  kind: "insert",
1364
- pos: insertIndex,
1365
- text: `${prefixNewline}${readIndent(code, insertIndex)}const collecthook = ${INNER_USE_HOOK}(${buildComponentArg(options?.path, exportName)})\n`
1337
+ pos: insertPos,
1338
+ text: ` const collecthook = ${INNER_USE_HOOK}(${buildComponentArg(options?.path, exportName)});`
1366
1339
  };
1367
1340
  }
1368
1341
  function hasExistingCollecthook(code, insertPos) {
1369
1342
  const lookahead = code.slice(insertPos, insertPos + 200);
1370
1343
  return /const\s+collecthook\s*=\s*useCollectHooks\s*\(/.test(lookahead);
1371
1344
  }
1372
- function calculateInsertPosition(code, insertPos) {
1373
- if (code[insertPos] === "\r" && code[insertPos + 1] === "\n") return {
1374
- insertIndex: insertPos + 2,
1375
- prefixNewline: ""
1376
- };
1377
- if (code[insertPos] === "\n") return {
1378
- insertIndex: insertPos + 1,
1379
- prefixNewline: ""
1380
- };
1381
- return {
1382
- insertIndex: insertPos,
1383
- prefixNewline: "\n"
1384
- };
1385
- }
1386
1345
  /** Builds the component argument string for collecthook initialization */
1387
1346
  function buildComponentArg(path, exportName) {
1388
1347
  const baseArg = String(path ?? "").split("?")[0].split("#")[0];
@@ -1435,22 +1394,14 @@ function processVariableDeclarator(code, node, path) {
1435
1394
  if (hookName === INNER_USE_HOOK) return null;
1436
1395
  const range = getParentRange(path.parent);
1437
1396
  if (!range) return null;
1438
- const { declEnd, indent } = getPositionInfo(code, range);
1397
+ const [, declEnd] = range;
1439
1398
  if (isCustomHook(normalizedName)) {
1440
1399
  if (hasCollecthookAfterByVariableId(code, declEnd, variableId)) return null;
1441
- return {
1442
- kind: "insert",
1443
- pos: declEnd,
1444
- text: "\n" + buildCollecthookPayload(indent, variableId, "customhook", "VariableDeclarator", variableId)
1445
- };
1400
+ return insertAfterStatement(code, declEnd, buildCollecthookPayload(variableId, "customhook", "VariableDeclarator", variableId));
1446
1401
  }
1447
1402
  if (!isKnownHook(normalizedName)) return null;
1448
1403
  if (hasCollecthookAfterByVariableId(code, declEnd, variableId)) return null;
1449
- return {
1450
- kind: "insert",
1451
- pos: declEnd,
1452
- text: "\n" + buildCollecthookPayload(indent, variableId, normalizedName, "VariableDeclarator", variableId)
1453
- };
1404
+ return insertAfterStatement(code, declEnd, buildCollecthookPayload(variableId, normalizedName, "VariableDeclarator", variableId));
1454
1405
  }
1455
1406
  /** Processes: useTask$(() => {}), useCustomHook() */
1456
1407
  function processExpressionStatement(code, node, currentIndex) {
@@ -1461,31 +1412,27 @@ function processExpressionStatement(code, node, currentIndex) {
1461
1412
  const stmtRange = node.range;
1462
1413
  if (!stmtRange) return null;
1463
1414
  const [stmtStart, stmtEnd] = stmtRange;
1464
- const indent = readIndent(code, findLineStart$1(code, stmtStart));
1465
1415
  if (isKnownHook(normalizedName)) {
1466
1416
  if (hasCollecthookAfterByVariableName(code, stmtEnd, normalizedName)) return null;
1467
1417
  return {
1468
- task: {
1469
- kind: "insert",
1470
- pos: stmtEnd,
1471
- text: "\n" + buildCollecthookPayload(indent, normalizedName, normalizedName, "expressionStatement", "undefined")
1472
- },
1418
+ task: insertAfterStatement(code, stmtEnd, buildCollecthookPayload(normalizedName, normalizedName, "expressionStatement", "undefined")),
1473
1419
  newIndex: currentIndex
1474
1420
  };
1475
1421
  }
1476
- if (isCustomHook(normalizedName)) return convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentIndex);
1422
+ if (isCustomHook(normalizedName)) return convertToVariableDeclaration(code, stmtStart, stmtEnd, currentIndex);
1477
1423
  return null;
1478
1424
  }
1479
1425
  /** Converts a custom hook expression to a variable declaration with tracking */
1480
- function convertToVariableDeclaration(code, stmtStart, stmtEnd, indent, currentIndex) {
1426
+ function convertToVariableDeclaration(code, stmtStart, stmtEnd, currentIndex) {
1481
1427
  const callSource = code.slice(stmtStart, stmtEnd);
1482
1428
  const variableName = `_customhook_${currentIndex}`;
1429
+ const payload = buildCollecthookPayload(variableName, "customhook", "VariableDeclarator", variableName);
1483
1430
  return {
1484
1431
  task: {
1485
1432
  kind: "replace",
1486
1433
  start: stmtStart,
1487
1434
  end: stmtEnd,
1488
- text: `${indent}let ${variableName} = ${trimStatementSemicolon(callSource)};\n` + buildCollecthookPayload(indent, variableName, "customhook", "VariableDeclarator", variableName)
1435
+ text: `let ${variableName} = ${trimStatementSemicolon(callSource)}; ${payload}`
1489
1436
  },
1490
1437
  newIndex: currentIndex + 1
1491
1438
  };
@@ -1513,19 +1460,19 @@ function extractHookCall(node) {
1513
1460
  normalizedName: normalizeQrlHookName(hookName)
1514
1461
  };
1515
1462
  }
1463
+ /** Inserts on the statement's own line, adding the `;` it may lack because of ASI */
1464
+ function insertAfterStatement(code, statementEnd, statement) {
1465
+ return {
1466
+ kind: "insert",
1467
+ pos: statementEnd,
1468
+ text: (code[statementEnd - 1] === ";" ? " " : "; ") + statement
1469
+ };
1470
+ }
1516
1471
  function getParentRange(parent) {
1517
1472
  const range = parent?.range;
1518
1473
  if (!range) return null;
1519
1474
  return [range[0], range[1]];
1520
1475
  }
1521
- function getPositionInfo(code, range) {
1522
- const [declStart, declEnd] = range;
1523
- return {
1524
- declStart,
1525
- declEnd,
1526
- indent: readIndent(code, findLineStart$1(code, declStart))
1527
- };
1528
- }
1529
1476
  //#endregion
1530
1477
  //#region src/parse/parse.ts
1531
1478
  /** Qwik Code Parser Transforms Qwik component code by injecting devtools hooks */
@@ -1869,9 +1816,13 @@ function assertUniqueVirtualModuleIds(modules) {
1869
1816
  }
1870
1817
  //#endregion
1871
1818
  //#region src/transforms/source-utils.ts
1819
+ /** Prepends on line 1 so the optimizer still sees every source line where it was */
1820
+ function prependImportIfMissing(code, importStatement) {
1821
+ return code.includes(importStatement) ? code : `${importStatement}${code}`;
1822
+ }
1872
1823
  function injectNamedImportIfMissing(code, key, name) {
1873
1824
  if (code.includes(key)) return code;
1874
- return `import { ${name} } from '${key}';\n${code}`;
1825
+ return prependImportIfMissing(code, `import { ${name} } from '${key}';`);
1875
1826
  }
1876
1827
  //#endregion
1877
1828
  //#region src/transforms/component-transform.ts
@@ -1896,47 +1847,30 @@ const DEVTOOLS_IMPORT = `import { QwikDevtools } from '@qwik.dev/devtools/ui';`;
1896
1847
  const STYLES_IMPORT = `import '@qwik.dev/devtools/ui/styles.css';`;
1897
1848
  function transformRootFile(code, opts = {}) {
1898
1849
  const excludePathnames = normalizeExcludePathnames(opts.overlay?.excludePathnames);
1899
- const bodyInsertion = findBodyInsertion(code);
1900
- if (bodyInsertion !== null) {
1850
+ const bodyClosingStart = findBodyClosingStart(code);
1851
+ if (bodyClosingStart !== null) {
1901
1852
  const devtoolsElement = excludePathnames.length > 0 ? `<QwikDevtools excludePathnames={${JSON.stringify(excludePathnames)}} />` : "<QwikDevtools />";
1902
1853
  code = applySourceEdits(code, [{
1903
1854
  kind: "insert",
1904
- pos: bodyInsertion.pos,
1905
- text: `${bodyInsertion.prefix}${devtoolsElement}${bodyInsertion.suffix}`
1855
+ pos: bodyClosingStart,
1856
+ text: devtoolsElement
1906
1857
  }]);
1907
1858
  }
1908
- if (!code.includes(DEVTOOLS_IMPORT)) code = `${DEVTOOLS_IMPORT}\n${code}`;
1909
- if (!code.includes(STYLES_IMPORT)) code = `${STYLES_IMPORT}\n${code}`;
1910
- return code;
1859
+ return prependImportIfMissing(prependImportIfMissing(code, DEVTOOLS_IMPORT), STYLES_IMPORT);
1911
1860
  }
1912
- function findBodyInsertion(code) {
1913
- const edits = [];
1861
+ function findBodyClosingStart(code) {
1862
+ let closingStart = null;
1914
1863
  try {
1915
1864
  traverseProgram(parseProgram(code), { JSXElement(path) {
1916
1865
  const node = path.node;
1917
1866
  if (getJsxName(node.openingElement?.name) !== "body" || !node.closingElement) return;
1918
- const closingStart = node.closingElement.start;
1919
- const lineStart = findLineStart(code, closingStart);
1920
- const closingIndent = readIndent(code, lineStart);
1921
- const hasOnlyIndentBeforeClosing = code.slice(lineStart, closingStart).trim() === "";
1922
- edits.push({
1923
- pos: hasOnlyIndentBeforeClosing ? lineStart : closingStart,
1924
- prefix: hasOnlyIndentBeforeClosing ? `${closingIndent} ` : `\n${closingIndent} `,
1925
- suffix: hasOnlyIndentBeforeClosing ? "\n" : ""
1926
- });
1867
+ closingStart = node.closingElement.start;
1927
1868
  path.stop();
1928
1869
  } });
1929
1870
  } catch (_) {
1930
1871
  return null;
1931
1872
  }
1932
- return edits[0] ?? null;
1933
- }
1934
- function findLineStart(code, index) {
1935
- for (let i = index - 1; i >= 0; i--) {
1936
- const ch = code[i];
1937
- if (ch === "\n" || ch === "\r") return i + 1;
1938
- }
1939
- return 0;
1873
+ return closingStart;
1940
1874
  }
1941
1875
  function getJsxName(name) {
1942
1876
  if (!name || typeof name !== "object") return;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qwik.dev/devtools",
3
3
  "description": "Qwik devtools package",
4
- "version": "2.0.0-rc.2",
4
+ "version": "2.0.0-rc.3",
5
5
  "bugs": "https://github.com/QwikDev/devtools/issues",
6
6
  "dependencies": {
7
7
  "birpc": "^4.0.0",
@@ -47,8 +47,8 @@
47
47
  "vite": "8.2.1",
48
48
  "vite-tsconfig-paths": "6.1.1",
49
49
  "vitest": "5.0.0",
50
- "@qwik.dev/core": "2.0.0-rc.2",
51
- "eslint-plugin-qwik": "2.0.0-rc.2"
50
+ "@qwik.dev/core": "2.0.0-rc.3",
51
+ "eslint-plugin-qwik": "2.0.0-rc.3"
52
52
  },
53
53
  "exports": {
54
54
  ".": {
@@ -88,7 +88,7 @@
88
88
  "@tailwindcss/vite": "^4.2.1",
89
89
  "tailwindcss": "^4.2.1",
90
90
  "vite": ">=8 <9",
91
- "@qwik.dev/core": "2.0.0-rc.2"
91
+ "@qwik.dev/core": "2.0.0-rc.3"
92
92
  },
93
93
  "private": false,
94
94
  "publishConfig": {