@tanstack/devtools-vite 0.6.1 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/ast-utils.d.ts +11 -0
- package/dist/esm/ast-utils.js +44 -0
- package/dist/esm/ast-utils.js.map +1 -0
- package/dist/esm/devtools-packages.d.ts +2 -0
- package/dist/esm/devtools-packages.js +15 -0
- package/dist/esm/devtools-packages.js.map +1 -0
- package/dist/esm/editor.js +1 -1
- package/dist/esm/editor.js.map +1 -1
- package/dist/esm/enhance-logs.d.ts +4 -1
- package/dist/esm/enhance-logs.js +35 -36
- package/dist/esm/enhance-logs.js.map +1 -1
- package/dist/esm/inject-plugin.d.ts +5 -4
- package/dist/esm/inject-plugin.js +114 -103
- package/dist/esm/inject-plugin.js.map +1 -1
- package/dist/esm/inject-source.d.ts +4 -1
- package/dist/esm/inject-source.js +92 -93
- package/dist/esm/inject-source.js.map +1 -1
- package/dist/esm/offset-to-loc.d.ts +14 -0
- package/dist/esm/offset-to-loc.js +29 -0
- package/dist/esm/offset-to-loc.js.map +1 -0
- package/dist/esm/plugin.js +23 -28
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/remove-devtools.d.ts +4 -1
- package/dist/esm/remove-devtools.js +79 -86
- package/dist/esm/remove-devtools.js.map +1 -1
- package/package.json +3 -9
- package/skills/devtools-vite-plugin/SKILL.md +7 -7
- package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
- package/src/ast-utils.ts +54 -0
- package/src/devtools-packages.ts +12 -0
- package/src/editor.ts +1 -1
- package/src/enhance-logs.ts +48 -78
- package/src/inject-plugin.test.ts +200 -147
- package/src/inject-plugin.ts +195 -238
- package/src/inject-source.test.ts +165 -115
- package/src/inject-source.ts +183 -231
- package/src/offset-to-loc.ts +39 -0
- package/src/plugin.ts +26 -28
- package/src/remove-devtools.test.ts +76 -57
- package/src/remove-devtools.ts +160 -168
- package/dist/esm/babel.d.ts +0 -18
- package/dist/esm/babel.js +0 -11
- package/dist/esm/babel.js.map +0 -1
- package/src/babel.ts +0 -18
|
@@ -1,97 +1,90 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { walk } from "./ast-utils.js";
|
|
2
|
+
import { isTanStackDevtoolsImport } from "./devtools-packages.js";
|
|
3
|
+
import MagicString from "magic-string";
|
|
4
|
+
import { parseSync } from "oxc-parser";
|
|
2
5
|
//#region src/remove-devtools.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return "";
|
|
24
|
-
}
|
|
25
|
-
return "";
|
|
26
|
-
}).filter(Boolean);
|
|
27
|
-
finalReferences.push(...referencesToRemove);
|
|
6
|
+
/**
|
|
7
|
+
* Extract component names referenced in the `plugins` prop of a devtools element.
|
|
8
|
+
*/
|
|
9
|
+
function getPluginReferences(opening) {
|
|
10
|
+
const refs = [];
|
|
11
|
+
for (const attr of opening.attributes) {
|
|
12
|
+
if (attr.type !== "JSXAttribute") continue;
|
|
13
|
+
if (attr.name.type !== "JSXIdentifier" || attr.name.name !== "plugins") continue;
|
|
14
|
+
if (!attr.value || attr.value.type !== "JSXExpressionContainer" || attr.value.expression.type !== "ArrayExpression") continue;
|
|
15
|
+
for (const el of attr.value.expression.elements) {
|
|
16
|
+
if (!el || el.type !== "ObjectExpression") continue;
|
|
17
|
+
for (const prop of el.properties) {
|
|
18
|
+
if (prop.type !== "Property" || prop.key.type !== "Identifier" || prop.key.name !== "render") continue;
|
|
19
|
+
const value = prop.value;
|
|
20
|
+
if (value.type === "JSXElement" && value.openingElement.name.type === "JSXIdentifier") refs.push(value.openingElement.name.name);
|
|
21
|
+
else if (value.type === "ArrowFunctionExpression" || value.type === "FunctionExpression") {
|
|
22
|
+
const body = value.body;
|
|
23
|
+
if (body && body.type === "JSXElement" && body.openingElement.name.type === "JSXIdentifier") refs.push(body.openingElement.name.name);
|
|
24
|
+
} else if (value.type === "Identifier") refs.push(value.name);
|
|
25
|
+
else if (value.type === "CallExpression" && value.callee.type === "Identifier") refs.push(value.callee.name);
|
|
28
26
|
}
|
|
29
|
-
});
|
|
30
|
-
} });
|
|
31
|
-
return finalReferences;
|
|
32
|
-
};
|
|
33
|
-
var transform = (ast) => {
|
|
34
|
-
let didTransform = false;
|
|
35
|
-
const devtoolsComponentNames = /* @__PURE__ */ new Set();
|
|
36
|
-
const finalReferences = [];
|
|
37
|
-
const transformations = [];
|
|
38
|
-
trav(ast, {
|
|
39
|
-
ImportDeclaration(path) {
|
|
40
|
-
const importSource = path.node.source.value;
|
|
41
|
-
if (isTanStackDevtoolsImport(importSource)) {
|
|
42
|
-
getImportedNames(path.node).forEach((name) => devtoolsComponentNames.add(name));
|
|
43
|
-
transformations.push(() => {
|
|
44
|
-
path.remove();
|
|
45
|
-
});
|
|
46
|
-
didTransform = true;
|
|
47
|
-
}
|
|
48
|
-
},
|
|
49
|
-
JSXElement(path) {
|
|
50
|
-
const opening = path.node.openingElement;
|
|
51
|
-
if (opening.name.type === "JSXIdentifier" && devtoolsComponentNames.has(opening.name.name)) {
|
|
52
|
-
const refs = getLeftoverImports(path);
|
|
53
|
-
finalReferences.push(...refs);
|
|
54
|
-
transformations.push(() => {
|
|
55
|
-
path.remove();
|
|
56
|
-
});
|
|
57
|
-
didTransform = true;
|
|
58
|
-
}
|
|
59
|
-
if (opening.name.type === "JSXMemberExpression" && opening.name.object.type === "JSXIdentifier" && devtoolsComponentNames.has(opening.name.object.name)) {
|
|
60
|
-
const refs = getLeftoverImports(path);
|
|
61
|
-
finalReferences.push(...refs);
|
|
62
|
-
transformations.push(() => {
|
|
63
|
-
path.remove();
|
|
64
|
-
});
|
|
65
|
-
didTransform = true;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
trav(ast, { ImportDeclaration(path) {
|
|
70
|
-
const imports = path.node.specifiers;
|
|
71
|
-
for (const imported of imports) if (imported.type === "ImportSpecifier") {
|
|
72
|
-
if (finalReferences.includes(imported.local.name)) transformations.push(() => {
|
|
73
|
-
path.node.specifiers = path.node.specifiers.filter((spec) => spec !== imported);
|
|
74
|
-
if (path.node.specifiers.length === 0) path.remove();
|
|
75
|
-
});
|
|
76
27
|
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
};
|
|
28
|
+
}
|
|
29
|
+
return refs;
|
|
30
|
+
}
|
|
81
31
|
function removeDevtools(code, id) {
|
|
82
|
-
const
|
|
32
|
+
const filePath = id.split("?")[0];
|
|
83
33
|
try {
|
|
84
|
-
const
|
|
34
|
+
const result = parseSync(filePath, code, {
|
|
85
35
|
sourceType: "module",
|
|
86
|
-
|
|
36
|
+
lang: "tsx"
|
|
37
|
+
});
|
|
38
|
+
if (result.errors.length > 0) return;
|
|
39
|
+
const s = new MagicString(code);
|
|
40
|
+
const devtoolsNames = /* @__PURE__ */ new Set();
|
|
41
|
+
const pluginRefs = [];
|
|
42
|
+
walk(result.program, (node) => {
|
|
43
|
+
if (node.type === "ImportDeclaration" && isTanStackDevtoolsImport(node.source.value)) {
|
|
44
|
+
for (const spec of node.specifiers) devtoolsNames.add(spec.local.name);
|
|
45
|
+
let end = node.end;
|
|
46
|
+
if (code[end] === "\n") end++;
|
|
47
|
+
s.remove(node.start, end);
|
|
48
|
+
}
|
|
87
49
|
});
|
|
88
|
-
if (
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
50
|
+
if (devtoolsNames.size === 0) return;
|
|
51
|
+
walk(result.program, (node) => {
|
|
52
|
+
if (node.type !== "JSXElement") return;
|
|
53
|
+
const opening = node.openingElement;
|
|
54
|
+
let matches = false;
|
|
55
|
+
if (opening.name.type === "JSXIdentifier" && devtoolsNames.has(opening.name.name)) matches = true;
|
|
56
|
+
else if (opening.name.type === "JSXMemberExpression" && opening.name.object.type === "JSXIdentifier" && devtoolsNames.has(opening.name.object.name)) matches = true;
|
|
57
|
+
if (!matches) return;
|
|
58
|
+
pluginRefs.push(...getPluginReferences(opening));
|
|
59
|
+
let end = node.end;
|
|
60
|
+
if (code[end] === "\n") end++;
|
|
61
|
+
s.remove(node.start, end);
|
|
62
|
+
});
|
|
63
|
+
if (pluginRefs.length > 0) walk(result.program, (node) => {
|
|
64
|
+
if (node.type !== "ImportDeclaration") return;
|
|
65
|
+
if (isTanStackDevtoolsImport(node.source.value)) return;
|
|
66
|
+
const toRemove = node.specifiers.filter((spec) => spec.type === "ImportSpecifier" && pluginRefs.includes(spec.local.name));
|
|
67
|
+
if (toRemove.length === 0) return;
|
|
68
|
+
const remaining = node.specifiers.filter((spec) => !toRemove.includes(spec));
|
|
69
|
+
if (remaining.length === 0) {
|
|
70
|
+
let end = node.end;
|
|
71
|
+
if (code[end] === "\n") end++;
|
|
72
|
+
s.remove(node.start, end);
|
|
73
|
+
} else {
|
|
74
|
+
const specTexts = remaining.map((spec) => code.slice(spec.start, spec.end));
|
|
75
|
+
const sourceText = code.slice(node.source.start, node.source.end);
|
|
76
|
+
s.overwrite(node.start, node.end, `import { ${specTexts.join(", ")} } from ${sourceText}`);
|
|
77
|
+
}
|
|
94
78
|
});
|
|
79
|
+
if (!s.hasChanged()) return;
|
|
80
|
+
return {
|
|
81
|
+
code: s.toString(),
|
|
82
|
+
map: s.generateMap({
|
|
83
|
+
source: filePath,
|
|
84
|
+
file: id,
|
|
85
|
+
includeContent: true
|
|
86
|
+
})
|
|
87
|
+
};
|
|
95
88
|
} catch (e) {
|
|
96
89
|
return;
|
|
97
90
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remove-devtools.js","names":[],"sources":["../../src/remove-devtools.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"remove-devtools.js","names":[],"sources":["../../src/remove-devtools.ts"],"sourcesContent":["import MagicString from 'magic-string'\nimport { parseSync } from 'oxc-parser'\nimport { walk } from './ast-utils'\nimport { isTanStackDevtoolsImport } from './devtools-packages'\nimport type { JSXOpeningElement } from 'oxc-parser'\n\n/**\n * Extract component names referenced in the `plugins` prop of a devtools element.\n */\nfunction getPluginReferences(opening: JSXOpeningElement): Array<string> {\n const refs: Array<string> = []\n\n for (const attr of opening.attributes) {\n if (attr.type !== 'JSXAttribute') continue\n if (attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'plugins')\n continue\n if (\n !attr.value ||\n attr.value.type !== 'JSXExpressionContainer' ||\n attr.value.expression.type !== 'ArrayExpression'\n )\n continue\n\n for (const el of attr.value.expression.elements) {\n if (!el || el.type !== 'ObjectExpression') continue\n\n for (const prop of el.properties) {\n if (\n prop.type !== 'Property' ||\n prop.key.type !== 'Identifier' ||\n prop.key.name !== 'render'\n )\n continue\n\n const value = prop.value\n\n // handle <ReactRouterPanel />\n if (\n value.type === 'JSXElement' &&\n value.openingElement.name.type === 'JSXIdentifier'\n ) {\n refs.push(value.openingElement.name.name)\n }\n // handle () => <ReactRouterPanel />\n else if (\n value.type === 'ArrowFunctionExpression' ||\n value.type === 'FunctionExpression'\n ) {\n const body = value.body\n if (\n body &&\n body.type === 'JSXElement' &&\n body.openingElement.name.type === 'JSXIdentifier'\n ) {\n refs.push(body.openingElement.name.name)\n }\n }\n // handle render: SomeComponent\n else if (value.type === 'Identifier') {\n refs.push(value.name)\n }\n // handle render: someFunction()\n else if (\n value.type === 'CallExpression' &&\n value.callee.type === 'Identifier'\n ) {\n refs.push(value.callee.name)\n }\n }\n }\n }\n\n return refs\n}\n\nexport function removeDevtools(code: string, id: string) {\n const filePath = id.split('?')[0]!\n\n try {\n const result = parseSync(filePath, code, {\n sourceType: 'module',\n lang: 'tsx',\n })\n if (result.errors.length > 0) return\n\n const s = new MagicString(code)\n const devtoolsNames = new Set<string>()\n const pluginRefs: Array<string> = []\n\n // Pass 1: Collect devtools import names and mark for removal\n walk(result.program, (node) => {\n if (\n node.type === 'ImportDeclaration' &&\n isTanStackDevtoolsImport(node.source.value)\n ) {\n for (const spec of node.specifiers) {\n devtoolsNames.add(spec.local.name)\n }\n let end = node.end\n if (code[end] === '\\n') end++\n s.remove(node.start, end)\n }\n })\n\n if (devtoolsNames.size === 0) return\n\n // Pass 2: Find and remove devtools JSX elements, collect plugin references\n walk(result.program, (node) => {\n if (node.type !== 'JSXElement') return\n\n const opening = node.openingElement\n let matches = false\n\n if (\n opening.name.type === 'JSXIdentifier' &&\n devtoolsNames.has(opening.name.name)\n ) {\n matches = true\n } else if (\n opening.name.type === 'JSXMemberExpression' &&\n opening.name.object.type === 'JSXIdentifier' &&\n devtoolsNames.has(opening.name.object.name)\n ) {\n matches = true\n }\n\n if (!matches) return\n\n pluginRefs.push(...getPluginReferences(opening))\n\n let end = node.end\n if (code[end] === '\\n') end++\n s.remove(node.start, end)\n })\n\n // Pass 3: Remove plugin imports that are no longer referenced\n if (pluginRefs.length > 0) {\n walk(result.program, (node) => {\n if (node.type !== 'ImportDeclaration') return\n if (isTanStackDevtoolsImport(node.source.value)) return\n\n const toRemove = node.specifiers.filter(\n (spec) =>\n spec.type === 'ImportSpecifier' &&\n pluginRefs.includes(spec.local.name),\n )\n\n if (toRemove.length === 0) return\n\n const remaining = node.specifiers.filter(\n (spec) => !toRemove.includes(spec),\n )\n\n if (remaining.length === 0) {\n let end = node.end\n if (code[end] === '\\n') end++\n s.remove(node.start, end)\n } else {\n // Rebuild import with remaining specifiers\n const specTexts = remaining.map((spec) =>\n code.slice(spec.start, spec.end),\n )\n const sourceText = code.slice(node.source.start, node.source.end)\n s.overwrite(\n node.start,\n node.end,\n `import { ${specTexts.join(', ')} } from ${sourceText}`,\n )\n }\n })\n }\n\n if (!s.hasChanged()) return\n\n return {\n code: s.toString(),\n map: s.generateMap({\n source: filePath,\n file: id,\n includeContent: true,\n }),\n }\n }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AK,OAAO,IAAI;UACpB;IAEL,MAAM,YAAY,UAAU,KAAK,SAC/B,KAAK,MAAM,KAAK,OAAO,KAAK,IAAI,CACjC;IACD,MAAM,aAAa,KAAK,MAAM,KAAK,OAAO,OAAO,KAAK,OAAO,IAAI;IACjE,EAAE,UACA,KAAK,OACL,KAAK,KACL,YAAY,UAAU,KAAK,KAAK,CAAC,UAAU,aAC5C;;IAEH;EAGJ,IAAI,CAAC,EAAE,YAAY,EAAE;EAErB,OAAO;GACL,MAAM,EAAE,UAAU;GAClB,KAAK,EAAE,YAAY;IACjB,QAAQ;IACR,MAAM;IACN,gBAAgB;IACjB,CAAC;GACH;UACM,GAAG;EACV"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/devtools-vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "TanStack Vite plugin used to enhance the core devtools with additional functionalities",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,21 +46,15 @@
|
|
|
46
46
|
"vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@babel/core": "^7.28.4",
|
|
50
|
-
"@babel/generator": "^7.28.3",
|
|
51
|
-
"@babel/parser": "^7.28.4",
|
|
52
|
-
"@babel/traverse": "^7.28.4",
|
|
53
|
-
"@babel/types": "^7.28.4",
|
|
54
49
|
"chalk": "^5.6.2",
|
|
55
50
|
"launch-editor": "^2.11.1",
|
|
51
|
+
"magic-string": "^0.30.0",
|
|
52
|
+
"oxc-parser": "^0.120.0",
|
|
56
53
|
"picomatch": "^4.0.3",
|
|
57
54
|
"@tanstack/devtools-client": "0.0.6",
|
|
58
55
|
"@tanstack/devtools-event-bus": "0.4.1"
|
|
59
56
|
},
|
|
60
57
|
"devDependencies": {
|
|
61
|
-
"@types/babel__core": "^7.20.5",
|
|
62
|
-
"@types/babel__generator": "^7.27.0",
|
|
63
|
-
"@types/babel__traverse": "^7.28.0",
|
|
64
58
|
"@types/picomatch": "^4.0.2",
|
|
65
59
|
"happy-dom": "^20.0.0"
|
|
66
60
|
},
|
|
@@ -67,13 +67,13 @@ From `packages/devtools-vite/src/index.ts`:
|
|
|
67
67
|
|
|
68
68
|
| Sub-plugin name | What it does | When active |
|
|
69
69
|
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
|
|
70
|
-
| `@tanstack/devtools:inject-source` |
|
|
70
|
+
| `@tanstack/devtools:inject-source` | AST transform adding `data-tsd-source` attrs to JSX | dev mode + `injectSource.enabled` |
|
|
71
71
|
| `@tanstack/devtools:config` | Reserved for future config modifications | serve command only |
|
|
72
72
|
| `@tanstack/devtools:custom-server` | Starts ServerEventBus, registers middleware for open-source/console-pipe endpoints | dev mode |
|
|
73
73
|
| `@tanstack/devtools:remove-devtools-on-build` | Strips devtools imports/JSX from production bundles | build command or production mode + `removeDevtoolsOnBuild` |
|
|
74
74
|
| `@tanstack/devtools:event-client-setup` | Marketplace: listens for install/add-plugin events via devtoolsEventClient | dev mode + serve + not CI |
|
|
75
75
|
| `@tanstack/devtools:console-pipe-transform` | Injects runtime console-pipe code into entry files | dev mode + serve + `consolePiping.enabled` |
|
|
76
|
-
| `@tanstack/devtools:better-console-logs` |
|
|
76
|
+
| `@tanstack/devtools:better-console-logs` | AST transform prepending source location to `console.log`/`console.error` | dev mode + `enhancedLogs.enabled` |
|
|
77
77
|
| `@tanstack/devtools:inject-plugin` | Detects which file imports TanStackDevtools (for marketplace injection) | dev mode + serve |
|
|
78
78
|
| `@tanstack/devtools:connection-injection` | Replaces `__TANSTACK_DEVTOOLS_PORT__`, `__TANSTACK_DEVTOOLS_HOST__`, `__TANSTACK_DEVTOOLS_PROTOCOL__` placeholders | dev mode + serve |
|
|
79
79
|
|
|
@@ -81,7 +81,7 @@ From `packages/devtools-vite/src/index.ts`:
|
|
|
81
81
|
|
|
82
82
|
### Source Injection
|
|
83
83
|
|
|
84
|
-
Adds `data-tsd-source="<relative-path>:<line>:<column>"` attributes to every JSX opening element via
|
|
84
|
+
Adds `data-tsd-source="<relative-path>:<line>:<column>"` attributes to every JSX opening element via oxc-parser + MagicString. This powers the "Go to Source" feature -- hold the inspect hotkey (default: Shift+Alt+Ctrl/Meta), hover over elements, click to open in editor.
|
|
85
85
|
|
|
86
86
|
**Key behaviors:**
|
|
87
87
|
|
|
@@ -137,7 +137,7 @@ devtools({
|
|
|
137
137
|
|
|
138
138
|
### Enhanced Logging
|
|
139
139
|
|
|
140
|
-
|
|
140
|
+
AST transform that prepends source location info to `console.log()` and `console.error()` calls. In the browser, this renders as a clickable "Go to Source" link. On the server, it shows `LOG <path>:<line>:<column>` in chalk colors.
|
|
141
141
|
|
|
142
142
|
The transform inserts a spread of a conditional expression: `...(typeof window === 'undefined' ? serverLogMessage : browserLogMessage)` as the first argument of the console call.
|
|
143
143
|
|
|
@@ -261,7 +261,7 @@ Source injection, console piping, enhanced logging, the server event bus, and th
|
|
|
261
261
|
|
|
262
262
|
### 4. Source injection on spread-props elements (MEDIUM)
|
|
263
263
|
|
|
264
|
-
The
|
|
264
|
+
The AST transform in `inject-source.ts` explicitly skips any JSX element that has a `{...props}` spread where `props` is the component's parameter name. This is intentional -- the spread would overwrite the injected `data-tsd-source` attribute. If source inspection doesn't work for a specific component, check if it spreads its props parameter.
|
|
265
265
|
|
|
266
266
|
```tsx
|
|
267
267
|
// data-tsd-source will NOT be injected on <div> here
|
|
@@ -301,8 +301,8 @@ These are registered on the Vite dev server (not the event bus server):
|
|
|
301
301
|
## Key Source Files
|
|
302
302
|
|
|
303
303
|
- `packages/devtools-vite/src/plugin.ts` -- Main plugin factory with all sub-plugins and config type
|
|
304
|
-
- `packages/devtools-vite/src/inject-source.ts` --
|
|
305
|
-
- `packages/devtools-vite/src/enhance-logs.ts` --
|
|
304
|
+
- `packages/devtools-vite/src/inject-source.ts` -- AST transform for data-tsd-source injection
|
|
305
|
+
- `packages/devtools-vite/src/enhance-logs.ts` -- AST transform for enhanced console logs
|
|
306
306
|
- `packages/devtools-vite/src/remove-devtools.ts` -- Production stripping transform
|
|
307
307
|
- `packages/devtools-vite/src/virtual-console.ts` -- Console pipe runtime code generator
|
|
308
308
|
- `packages/devtools-vite/src/editor.ts` -- Editor config type and launch-editor integration
|
|
@@ -43,7 +43,7 @@ declare function defineDevtoolsConfig(
|
|
|
43
43
|
|
|
44
44
|
## `injectSource`
|
|
45
45
|
|
|
46
|
-
Controls source injection -- the
|
|
46
|
+
Controls source injection -- the AST transform that adds `data-tsd-source` attributes to JSX elements for the "Go to Source" feature.
|
|
47
47
|
|
|
48
48
|
| Field | Type | Default | Description |
|
|
49
49
|
| ------------------- | ------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
@@ -117,7 +117,7 @@ devtools({
|
|
|
117
117
|
|
|
118
118
|
## `enhancedLogs`
|
|
119
119
|
|
|
120
|
-
Controls the
|
|
120
|
+
Controls the AST transform that prepends source location information to `console.log()` and `console.error()` calls.
|
|
121
121
|
|
|
122
122
|
| Field | Type | Default | Description |
|
|
123
123
|
| --------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
package/src/ast-utils.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cache of keys that hold child nodes (objects/arrays) per AST node type.
|
|
5
|
+
* Since oxc-parser produces AST via JSON.parse, every instance of a given
|
|
6
|
+
* node type has the same set of keys, so we only need to discover them once.
|
|
7
|
+
*/
|
|
8
|
+
const childKeysCache = new Map<string, Array<string>>()
|
|
9
|
+
|
|
10
|
+
function getChildKeys(node: Node): Array<string> {
|
|
11
|
+
let keys = childKeysCache.get(node.type)
|
|
12
|
+
if (keys) return keys
|
|
13
|
+
|
|
14
|
+
keys = []
|
|
15
|
+
for (const key in node) {
|
|
16
|
+
if (key === 'type' || key === 'start' || key === 'end') continue
|
|
17
|
+
// typeof null === 'object', so nullable node fields get cached too
|
|
18
|
+
if (typeof (node as any)[key] === 'object') {
|
|
19
|
+
keys.push(key)
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
childKeysCache.set(node.type, keys)
|
|
23
|
+
return keys
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Iterate over the direct child nodes of an AST node.
|
|
28
|
+
* Uses a per-type cache of which keys hold child nodes to avoid
|
|
29
|
+
* allocating Object.entries() arrays on every call.
|
|
30
|
+
*/
|
|
31
|
+
export function forEachChild(node: Node, callback: (child: Node) => void) {
|
|
32
|
+
const keys = getChildKeys(node)
|
|
33
|
+
for (const key of keys) {
|
|
34
|
+
const value = (node as any)[key]
|
|
35
|
+
if (value === null) continue
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
for (const item of value) {
|
|
38
|
+
if (typeof item === 'object' && item !== null && 'type' in item) {
|
|
39
|
+
callback(item)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} else if ('type' in value) {
|
|
43
|
+
callback(value as Node)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
|
|
50
|
+
*/
|
|
51
|
+
export function walk(node: Node, visitor: (node: Node) => void) {
|
|
52
|
+
visitor(node)
|
|
53
|
+
forEachChild(node, (child) => walk(child, visitor))
|
|
54
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const TANSTACK_DEVTOOLS_PACKAGES = [
|
|
2
|
+
'@tanstack/react-devtools',
|
|
3
|
+
'@tanstack/preact-devtools',
|
|
4
|
+
'@tanstack/solid-devtools',
|
|
5
|
+
'@tanstack/vue-devtools',
|
|
6
|
+
'@tanstack/svelte-devtools',
|
|
7
|
+
'@tanstack/angular-devtools',
|
|
8
|
+
'@tanstack/devtools',
|
|
9
|
+
] as const
|
|
10
|
+
|
|
11
|
+
export const isTanStackDevtoolsImport = (source: string): boolean =>
|
|
12
|
+
(TANSTACK_DEVTOOLS_PACKAGES as ReadonlyArray<string>).includes(source)
|
package/src/editor.ts
CHANGED
|
@@ -28,7 +28,7 @@ export const DEFAULT_EDITOR_CONFIG: EditorConfig = {
|
|
|
28
28
|
open: async (path, lineNumber, columnNumber) => {
|
|
29
29
|
const launch = (await import('launch-editor')).default
|
|
30
30
|
launch(
|
|
31
|
-
`${path
|
|
31
|
+
`${path}${lineNumber ? `:${lineNumber}` : ''}${columnNumber ? `:${columnNumber}` : ''}`,
|
|
32
32
|
undefined,
|
|
33
33
|
(filename, err) => {
|
|
34
34
|
console.warn(`Failed to open ${filename} in editor: ${err}`)
|
package/src/enhance-logs.ts
CHANGED
|
@@ -1,100 +1,70 @@
|
|
|
1
1
|
import chalk from 'chalk'
|
|
2
2
|
import { normalizePath } from 'vite'
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
3
|
+
import MagicString from 'magic-string'
|
|
4
|
+
import { parseSync } from 'oxc-parser'
|
|
5
|
+
import { createLocMapper } from './offset-to-loc'
|
|
6
|
+
import { walk } from './ast-utils'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
)
|
|
12
|
-
|
|
8
|
+
function escapeForStringLiteral(str: string): string {
|
|
9
|
+
return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function enhanceConsoleLog(code: string, id: string, port: number) {
|
|
13
|
+
const filePath = id.split('?')[0]!
|
|
14
|
+
const location = filePath.replace(normalizePath(process.cwd()), '')
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const result = parseSync(filePath, code, {
|
|
18
|
+
sourceType: 'module',
|
|
19
|
+
lang: 'tsx',
|
|
20
|
+
})
|
|
21
|
+
if (result.errors.length > 0) return
|
|
22
|
+
|
|
23
|
+
const offsetToLoc = createLocMapper(code)
|
|
24
|
+
const s = new MagicString(code)
|
|
13
25
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
26
|
+
walk(result.program, (node) => {
|
|
27
|
+
if (node.type !== 'CallExpression') return
|
|
28
|
+
|
|
29
|
+
const callee = node.callee
|
|
18
30
|
if (
|
|
19
31
|
callee.type === 'MemberExpression' &&
|
|
32
|
+
!callee.computed &&
|
|
20
33
|
callee.object.type === 'Identifier' &&
|
|
21
34
|
callee.object.name === 'console' &&
|
|
22
35
|
callee.property.type === 'Identifier' &&
|
|
23
36
|
(callee.property.name === 'log' || callee.property.name === 'error')
|
|
24
37
|
) {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
const [lineNumber, column] = [
|
|
30
|
-
location.start.line,
|
|
31
|
-
location.start.column,
|
|
32
|
-
]
|
|
33
|
-
const finalPath = `${filePath}:${lineNumber}:${column + 1}`
|
|
38
|
+
const loc = offsetToLoc(node.start)
|
|
39
|
+
const [lineNumber, column] = [loc.line, loc.column]
|
|
40
|
+
const finalPath = `${location}:${lineNumber}:${column + 1}`
|
|
34
41
|
const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath}`)}\n → `
|
|
35
42
|
|
|
36
|
-
const serverLogMessage =
|
|
37
|
-
|
|
38
|
-
])
|
|
39
|
-
const browserLogMessage = t.arrayExpression([
|
|
40
|
-
// LOG with css formatting specifiers: %c
|
|
41
|
-
t.stringLiteral(
|
|
42
|
-
`%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \n → `,
|
|
43
|
-
),
|
|
44
|
-
// magenta
|
|
45
|
-
t.stringLiteral('color:#A0A'),
|
|
46
|
-
t.stringLiteral('color:#FFF'),
|
|
47
|
-
// blueBright
|
|
48
|
-
t.stringLiteral('color:#55F'),
|
|
49
|
-
t.stringLiteral('color:#FFF'),
|
|
50
|
-
])
|
|
43
|
+
const serverLogMessage = `["${escapeForStringLiteral(logMessage)}"]`
|
|
44
|
+
const browserLogMessage = `["%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \\n \\u2192 ","color:#A0A","color:#FFF","color:#55F","color:#FFF"]`
|
|
51
45
|
|
|
52
|
-
|
|
53
|
-
const checkServerCondition = t.binaryExpression(
|
|
54
|
-
'===',
|
|
55
|
-
t.unaryExpression('typeof', t.identifier('window')),
|
|
56
|
-
t.stringLiteral('undefined'),
|
|
57
|
-
)
|
|
46
|
+
const spreadStr = `...(typeof window === "undefined" ? ${serverLogMessage} : ${browserLogMessage}), `
|
|
58
47
|
|
|
59
|
-
//
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
),
|
|
67
|
-
),
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
didTransform = true
|
|
48
|
+
// Find the opening '(' of the call by scanning forward from callee end
|
|
49
|
+
let parenOffset = callee.end
|
|
50
|
+
while (parenOffset < code.length && code[parenOffset] !== '(') {
|
|
51
|
+
parenOffset++
|
|
52
|
+
}
|
|
53
|
+
// Insert right after '('
|
|
54
|
+
s.appendRight(parenOffset + 1, spreadStr)
|
|
71
55
|
}
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
return didTransform
|
|
76
|
-
}
|
|
56
|
+
})
|
|
77
57
|
|
|
78
|
-
|
|
79
|
-
const [filePath] = id.split('?')
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
|
|
81
|
-
const location = filePath?.replace(normalizePath(process.cwd()), '')!
|
|
58
|
+
if (!s.hasChanged()) return
|
|
82
59
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return
|
|
60
|
+
return {
|
|
61
|
+
code: s.toString(),
|
|
62
|
+
map: s.generateMap({
|
|
63
|
+
source: filePath,
|
|
64
|
+
file: id,
|
|
65
|
+
includeContent: true,
|
|
66
|
+
}),
|
|
91
67
|
}
|
|
92
|
-
return gen(ast, {
|
|
93
|
-
sourceMaps: true,
|
|
94
|
-
retainLines: true,
|
|
95
|
-
filename: id,
|
|
96
|
-
sourceFileName: filePath,
|
|
97
|
-
})
|
|
98
68
|
} catch (e) {
|
|
99
69
|
return
|
|
100
70
|
}
|