@tanstack/devtools-vite 0.2.4 → 0.2.5
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/inject-source.js +131 -11
- package/dist/esm/inject-source.js.map +1 -1
- package/dist/esm/plugin.d.ts +10 -0
- package/dist/esm/plugin.js +2 -1
- package/dist/esm/plugin.js.map +1 -1
- package/package.json +3 -2
- package/src/inject-source.ts +186 -14
- package/src/plugin.ts +12 -1
|
@@ -2,21 +2,141 @@ import { normalizePath } from "vite";
|
|
|
2
2
|
import { gen, trav } from "./babel.js";
|
|
3
3
|
import { parse } from "@babel/parser";
|
|
4
4
|
import * as t from "@babel/types";
|
|
5
|
+
const getPropsNameFromFunctionDeclaration = (functionDeclaration) => {
|
|
6
|
+
let propsName = null;
|
|
7
|
+
if (functionDeclaration.type === "FunctionExpression") {
|
|
8
|
+
const firstArgument = functionDeclaration.params[0];
|
|
9
|
+
if (firstArgument && firstArgument.type === "Identifier") {
|
|
10
|
+
propsName = firstArgument.name;
|
|
11
|
+
}
|
|
12
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") {
|
|
13
|
+
firstArgument.properties.forEach((prop) => {
|
|
14
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") {
|
|
15
|
+
propsName = prop.argument.name;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return propsName;
|
|
20
|
+
}
|
|
21
|
+
if (functionDeclaration.type === "ArrowFunctionExpression") {
|
|
22
|
+
const firstArgument = functionDeclaration.params[0];
|
|
23
|
+
if (firstArgument && firstArgument.type === "Identifier") {
|
|
24
|
+
propsName = firstArgument.name;
|
|
25
|
+
}
|
|
26
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") {
|
|
27
|
+
firstArgument.properties.forEach((prop) => {
|
|
28
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") {
|
|
29
|
+
propsName = prop.argument.name;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return propsName;
|
|
34
|
+
}
|
|
35
|
+
if (functionDeclaration.type === "FunctionDeclaration") {
|
|
36
|
+
const firstArgument = functionDeclaration.params[0];
|
|
37
|
+
if (firstArgument && firstArgument.type === "Identifier") {
|
|
38
|
+
propsName = firstArgument.name;
|
|
39
|
+
}
|
|
40
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") {
|
|
41
|
+
firstArgument.properties.forEach((prop) => {
|
|
42
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") {
|
|
43
|
+
propsName = prop.argument.name;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return propsName;
|
|
48
|
+
}
|
|
49
|
+
if (functionDeclaration.init?.type === "ArrowFunctionExpression" || functionDeclaration.init?.type === "FunctionExpression") {
|
|
50
|
+
const firstArgument = functionDeclaration.init.params[0];
|
|
51
|
+
if (firstArgument && firstArgument.type === "Identifier") {
|
|
52
|
+
propsName = firstArgument.name;
|
|
53
|
+
}
|
|
54
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") {
|
|
55
|
+
firstArgument.properties.forEach((prop) => {
|
|
56
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") {
|
|
57
|
+
propsName = prop.argument.name;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return propsName;
|
|
63
|
+
};
|
|
64
|
+
const transformJSX = (element, propsName, file) => {
|
|
65
|
+
const loc = element.node.loc;
|
|
66
|
+
if (!loc) return;
|
|
67
|
+
const line = loc.start.line;
|
|
68
|
+
const column = loc.start.column;
|
|
69
|
+
const hasDataSource = element.node.attributes.some(
|
|
70
|
+
(attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === "data-tsd-source"
|
|
71
|
+
);
|
|
72
|
+
const hasSpread = element.node.attributes.some(
|
|
73
|
+
(attr) => attr.type === "JSXSpreadAttribute" && attr.argument.type === "Identifier" && attr.argument.name === propsName
|
|
74
|
+
);
|
|
75
|
+
if (hasSpread || hasDataSource) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
element.node.attributes.push(
|
|
79
|
+
t.jsxAttribute(
|
|
80
|
+
t.jsxIdentifier("data-tsd-source"),
|
|
81
|
+
t.stringLiteral(`${file}:${line}:${column}`)
|
|
82
|
+
)
|
|
83
|
+
);
|
|
84
|
+
return true;
|
|
85
|
+
};
|
|
5
86
|
const transform = (ast, file) => {
|
|
6
87
|
let didTransform = false;
|
|
7
88
|
trav(ast, {
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const line = loc.start.line;
|
|
12
|
-
const column = loc.start.column;
|
|
13
|
-
path.node.attributes.push(
|
|
14
|
-
t.jsxAttribute(
|
|
15
|
-
t.jsxIdentifier("data-tsd-source"),
|
|
16
|
-
t.stringLiteral(`${file}:${line}:${column}`)
|
|
17
|
-
)
|
|
89
|
+
FunctionDeclaration(functionDeclaration) {
|
|
90
|
+
const propsName = getPropsNameFromFunctionDeclaration(
|
|
91
|
+
functionDeclaration.node
|
|
18
92
|
);
|
|
19
|
-
|
|
93
|
+
functionDeclaration.traverse({
|
|
94
|
+
JSXOpeningElement(element) {
|
|
95
|
+
const transformed = transformJSX(element, propsName, file);
|
|
96
|
+
if (transformed) {
|
|
97
|
+
didTransform = true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
ArrowFunctionExpression(path) {
|
|
103
|
+
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
104
|
+
path.traverse({
|
|
105
|
+
JSXOpeningElement(element) {
|
|
106
|
+
const transformed = transformJSX(element, propsName, file);
|
|
107
|
+
if (transformed) {
|
|
108
|
+
didTransform = true;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
FunctionExpression(path) {
|
|
114
|
+
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
115
|
+
path.traverse({
|
|
116
|
+
JSXOpeningElement(element) {
|
|
117
|
+
const transformed = transformJSX(element, propsName, file);
|
|
118
|
+
if (transformed) {
|
|
119
|
+
didTransform = true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
VariableDeclaration(path) {
|
|
125
|
+
const functionDeclaration = path.node.declarations.find((decl) => {
|
|
126
|
+
return decl.init?.type === "ArrowFunctionExpression" || decl.init?.type === "FunctionExpression";
|
|
127
|
+
});
|
|
128
|
+
if (!functionDeclaration) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration);
|
|
132
|
+
path.traverse({
|
|
133
|
+
JSXOpeningElement(element) {
|
|
134
|
+
const transformed = transformJSX(element, propsName, file);
|
|
135
|
+
if (transformed) {
|
|
136
|
+
didTransform = true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
20
140
|
}
|
|
21
141
|
});
|
|
22
142
|
return didTransform;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"inject-source.js","sources":["../../src/inject-source.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport { gen, parse, t, trav } from './babel'\nimport type { types as Babel } from '@babel/core'\nimport type { ParseResult } from '@babel/parser'\n\nconst transform = (ast: ParseResult<Babel.File>, file: string) => {\n let didTransform = false\n trav(ast, {\n JSXOpeningElement(path) {\n const loc = path.node.loc\n if (!loc) return\n const line = loc.start.line\n const column = loc.start.column\n\n // Inject data-source as a string: \"<file>:<line>:<column>\"\n path.node.attributes.push(\n t.jsxAttribute(\n t.jsxIdentifier('data-tsd-source'),\n t.stringLiteral(`${file}:${line}:${column}`),\n ),\n )\n\n didTransform = true\n },\n })\n\n return didTransform\n}\n\nexport function addSourceToJsx(code: string, id: string) {\n const [filePath] = id.split('?')\n // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n const location = filePath?.replace(normalizePath(process.cwd()), '')!\n\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n })\n const didTransform = transform(ast, location)\n if (!didTransform) {\n return { code }\n }\n return gen(ast, {\n sourceMaps: true,\n filename: id,\n sourceFileName: filePath,\n })\n } catch (e) {\n return { code }\n }\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,YAAY,CAAC,KAA8B,SAAiB;AAChE,MAAI,eAAe;AACnB,OAAK,KAAK;AAAA,IACR,kBAAkB,MAAM;AACtB,YAAM,MAAM,KAAK,KAAK;AACtB,UAAI,CAAC,IAAK;AACV,YAAM,OAAO,IAAI,MAAM;AACvB,YAAM,SAAS,IAAI,MAAM;AAGzB,WAAK,KAAK,WAAW;AAAA,QACnB,EAAE;AAAA,UACA,EAAE,cAAc,iBAAiB;AAAA,UACjC,EAAE,cAAc,GAAG,IAAI,IAAI,IAAI,IAAI,MAAM,EAAE;AAAA,QAAA;AAAA,MAC7C;AAGF,qBAAe;AAAA,IACjB;AAAA,EAAA,CACD;AAED,SAAO;AACT;AAEO,SAAS,eAAe,MAAc,IAAY;AACvD,QAAM,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG;AAE/B,QAAM,WAAW,UAAU,QAAQ,cAAc,QAAQ,IAAA,CAAK,GAAG,EAAE;AAEnE,MAAI;AACF,UAAM,MAAM,MAAM,MAAM;AAAA,MACtB,YAAY;AAAA,MACZ,SAAS,CAAC,OAAO,YAAY;AAAA,IAAA,CAC9B;AACD,UAAM,eAAe,UAAU,KAAK,QAAQ;AAC5C,QAAI,CAAC,cAAc;AACjB,aAAO,EAAE,KAAA;AAAA,IACX;AACA,WAAO,IAAI,KAAK;AAAA,MACd,YAAY;AAAA,MACZ,UAAU;AAAA,MACV,gBAAgB;AAAA,IAAA,CACjB;AAAA,EACH,SAAS,GAAG;AACV,WAAO,EAAE,KAAA;AAAA,EACX;AACF;"}
|
|
1
|
+
{"version":3,"file":"inject-source.js","sources":["../../src/inject-source.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport { gen, parse, t, trav } from './babel'\nimport type { types as Babel, NodePath } from '@babel/core'\nimport type { ParseResult } from '@babel/parser'\n\nconst getPropsNameFromFunctionDeclaration = (\n functionDeclaration:\n | t.VariableDeclarator\n | t.FunctionExpression\n | t.FunctionDeclaration\n | t.ArrowFunctionExpression,\n) => {\n let propsName: string | null = null\n\n if (functionDeclaration.type === 'FunctionExpression') {\n const firstArgument = functionDeclaration.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n return propsName\n }\n if (functionDeclaration.type === 'ArrowFunctionExpression') {\n const firstArgument = functionDeclaration.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n return propsName\n }\n if (functionDeclaration.type === 'FunctionDeclaration') {\n const firstArgument = functionDeclaration.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n return propsName\n }\n // Arrow function case\n if (\n functionDeclaration.init?.type === 'ArrowFunctionExpression' ||\n functionDeclaration.init?.type === 'FunctionExpression'\n ) {\n const firstArgument = functionDeclaration.init.params[0]\n // handles (props) => {}\n if (firstArgument && firstArgument.type === 'Identifier') {\n propsName = firstArgument.name\n }\n // handles ({ ...props }) => {}\n if (firstArgument && firstArgument.type === 'ObjectPattern') {\n firstArgument.properties.forEach((prop) => {\n if (\n prop.type === 'RestElement' &&\n prop.argument.type === 'Identifier'\n ) {\n propsName = prop.argument.name\n }\n })\n }\n }\n return propsName\n}\n\nconst transformJSX = (\n element: NodePath<t.JSXOpeningElement>,\n propsName: string | null,\n file: string,\n) => {\n const loc = element.node.loc\n if (!loc) return\n const line = loc.start.line\n const column = loc.start.column\n\n const hasDataSource = element.node.attributes.some(\n (attr) =>\n attr.type === 'JSXAttribute' &&\n attr.name.type === 'JSXIdentifier' &&\n attr.name.name === 'data-tsd-source',\n )\n // Check if props are spread\n const hasSpread = element.node.attributes.some(\n (attr) =>\n attr.type === 'JSXSpreadAttribute' &&\n attr.argument.type === 'Identifier' &&\n attr.argument.name === propsName,\n )\n\n if (hasSpread || hasDataSource) {\n // Do not inject if props are spread\n return\n }\n\n // Inject data-source as a string: \"<file>:<line>:<column>\"\n element.node.attributes.push(\n t.jsxAttribute(\n t.jsxIdentifier('data-tsd-source'),\n t.stringLiteral(`${file}:${line}:${column}`),\n ),\n )\n\n return true\n}\n\nconst transform = (ast: ParseResult<Babel.File>, file: string) => {\n let didTransform = false\n\n trav(ast, {\n FunctionDeclaration(functionDeclaration) {\n const propsName = getPropsNameFromFunctionDeclaration(\n functionDeclaration.node,\n )\n functionDeclaration.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(element, propsName, file)\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n ArrowFunctionExpression(path) {\n const propsName = getPropsNameFromFunctionDeclaration(path.node)\n path.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(element, propsName, file)\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n FunctionExpression(path) {\n const propsName = getPropsNameFromFunctionDeclaration(path.node)\n path.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(element, propsName, file)\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n VariableDeclaration(path) {\n const functionDeclaration = path.node.declarations.find((decl) => {\n return (\n decl.init?.type === 'ArrowFunctionExpression' ||\n decl.init?.type === 'FunctionExpression'\n )\n })\n if (!functionDeclaration) {\n return\n }\n const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration)\n\n path.traverse({\n JSXOpeningElement(element) {\n const transformed = transformJSX(element, propsName, file)\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n })\n\n return didTransform\n}\n\nexport function addSourceToJsx(code: string, id: string) {\n const [filePath] = id.split('?')\n // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain\n const location = filePath?.replace(normalizePath(process.cwd()), '')!\n\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n })\n const didTransform = transform(ast, location)\n if (!didTransform) {\n return { code }\n }\n return gen(ast, {\n sourceMaps: true,\n filename: id,\n sourceFileName: filePath,\n })\n } catch (e) {\n return { code }\n }\n}\n"],"names":[],"mappings":";;;;AAKA,MAAM,sCAAsC,CAC1C,wBAKG;AACH,MAAI,YAA2B;AAE/B,MAAI,oBAAoB,SAAS,sBAAsB;AACrD,UAAM,gBAAgB,oBAAoB,OAAO,CAAC;AAElD,QAAI,iBAAiB,cAAc,SAAS,cAAc;AACxD,kBAAY,cAAc;AAAA,IAC5B;AAEA,QAAI,iBAAiB,cAAc,SAAS,iBAAiB;AAC3D,oBAAc,WAAW,QAAQ,CAAC,SAAS;AACzC,YACE,KAAK,SAAS,iBACd,KAAK,SAAS,SAAS,cACvB;AACA,sBAAY,KAAK,SAAS;AAAA,QAC5B;AAAA,MACF,CAAC;AAAA,IACH;AACA,WAAO;AAAA,EACT;AACA,MAAI,oBAAoB,SAAS,2BAA2B;AAC1D,UAAM,gBAAgB,oBAAoB,OAAO,CAAC;AAElD,QAAI,iBAAiB,cAAc,SAAS,cAAc;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AAA,IACH;AAAA,IACA,oBAAoB,MAAM;AACxB,YAAM,sBAAsB,KAAK,KAAK,aAAa,KAAK,CAAC,SAAS;AAChE,eACE,KAAK,MAAM,SAAS,6BACpB,KAAK,MAAM,SAAS;AAAA,MAExB,CAAC;AACD,UAAI,CAAC,qBAAqB;AACxB;AAAA,MACF;AACA,YAAM,YAAY,oCAAoC,mBAAmB;AAEzE,WAAK,SAAS;AAAA,QACZ,kBAAkB,SAAS;AACzB,gBAAM,cAAc,aAAa,SAAS,WAAW,IAAI;AACzD,cAAI,aAAa;AACf,2BAAe;AAAA,UACjB;AAAA,QACF;AAAA,MAAA,CACD;AAAA,IACH;AAAA,EAAA,CACD;AAED,SAAO;AACT;AAEO,SAAS,eAAe,MAAc,IAAY;AACvD,QAAM,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG;AAE/B,QAAM,WAAW,UAAU,QAAQ,cAAc,QAAQ,IAAA,CAAK,GAAG,EAAE;AAEnE,MAAI;AACF,UAAM,MAAM,MAAM,MAAM;AAAA,MACtB,YAAY;AAAA,MACZ,SAAS,CAAC,OAAO,YAAY;AAAA,IAAA,CAC9B;AACD,UAAM,eAAe,UAAU,KAAK,QAAQ;AAC5C,QAAI,CAAC,cAAc;AACjB,aAAO,EAAE,KAAA;AAAA,IACX;AACA,WAAO,IAAI,KAAK;AAAA,MACd,YAAY;AAAA,MACZ,UAAU;AAAA,MACV,gBAAgB;AAAA,IAAA,CACjB;AAAA,EACH,SAAS,GAAG;AACV,WAAO,EAAE,KAAA;AAAA,EACX;AACF;"}
|
package/dist/esm/plugin.d.ts
CHANGED
|
@@ -22,6 +22,16 @@ export type TanStackDevtoolsViteConfig = {
|
|
|
22
22
|
*/
|
|
23
23
|
enabled: boolean;
|
|
24
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Configuration for source injection.
|
|
27
|
+
*/
|
|
28
|
+
injectSource: {
|
|
29
|
+
/**
|
|
30
|
+
* Whether to enable source injection via data-tsd-source.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
enabled: boolean;
|
|
34
|
+
};
|
|
25
35
|
};
|
|
26
36
|
export declare const defineDevtoolsConfig: (config: TanStackDevtoolsViteConfig) => TanStackDevtoolsViteConfig;
|
|
27
37
|
export declare const devtools: (args?: TanStackDevtoolsViteConfig) => Array<Plugin>;
|
package/dist/esm/plugin.js
CHANGED
|
@@ -9,13 +9,14 @@ const devtools = (args) => {
|
|
|
9
9
|
let port = 5173;
|
|
10
10
|
const appDir = args?.appDir || "./src";
|
|
11
11
|
const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true };
|
|
12
|
+
const injectSourceConfig = args?.injectSource ?? { enabled: true };
|
|
12
13
|
const bus = new ServerEventBus(args?.eventBusConfig);
|
|
13
14
|
return [
|
|
14
15
|
{
|
|
15
16
|
enforce: "pre",
|
|
16
17
|
name: "@tanstack/devtools:inject-source",
|
|
17
18
|
apply(config) {
|
|
18
|
-
return config.mode === "development";
|
|
19
|
+
return config.mode === "development" && injectSourceConfig.enabled;
|
|
19
20
|
},
|
|
20
21
|
transform(code, id) {
|
|
21
22
|
if (id.includes("node_modules") || id.includes("?raw") || id.includes("dist") || id.includes("build"))
|
package/dist/esm/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport chalk from 'chalk'\nimport { ServerEventBus } from '@tanstack/devtools-event-bus/server'\nimport { handleDevToolsViteRequest } from './utils'\nimport { DEFAULT_EDITOR_CONFIG, handleOpenSource } from './editor'\nimport { addSourceToJsx } from './inject-source'\nimport type { EditorConfig } from './editor'\nimport type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'\nimport type { Plugin } from 'vite'\n\nexport type TanStackDevtoolsViteConfig = {\n /** The directory where the react router app is located. Defaults to the \"./src\" relative to where vite.config is being defined. */\n appDir?: string\n /**\n * Configuration for the editor integration. Defaults to opening in VS code\n */\n editor?: EditorConfig\n /**\n * The configuration options for the server event bus\n */\n eventBusConfig?: ServerEventBusConfig\n /**\n * Configuration for enhanced logging.\n */\n enhancedLogs?: {\n /**\n * Whether to enable enhanced logging.\n * @default true\n */\n enabled: boolean\n }\n}\n\nexport const defineDevtoolsConfig = (config: TanStackDevtoolsViteConfig) =>\n config\n\nexport const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {\n let port = 5173\n const appDir = args?.appDir || './src'\n const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true }\n const bus = new ServerEventBus(args?.eventBusConfig)\n\n return [\n {\n enforce: 'pre',\n name: '@tanstack/devtools:inject-source',\n apply(config) {\n return config.mode === 'development'\n },\n transform(code, id) {\n if (\n id.includes('node_modules') ||\n id.includes('?raw') ||\n id.includes('dist') ||\n id.includes('build')\n )\n return code\n\n return addSourceToJsx(code, id)\n },\n },\n {\n enforce: 'pre',\n name: '@tanstack/devtools:custom-server',\n apply(config) {\n // Custom server is only needed in development for piping events to the client\n return config.mode === 'development'\n },\n configureServer(server) {\n bus.start()\n server.middlewares.use((req, _res, next) => {\n if (req.socket.localPort && req.socket.localPort !== port) {\n port = req.socket.localPort\n }\n next()\n })\n if (server.config.server.port) {\n port = server.config.server.port\n }\n\n server.httpServer?.on('listening', () => {\n port = server.config.server.port\n })\n\n const editor = args?.editor ?? DEFAULT_EDITOR_CONFIG\n const openInEditor = async (\n path: string | undefined,\n lineNum: string | undefined,\n ) => {\n if (!path) {\n return\n }\n await editor.open(path, lineNum)\n }\n server.middlewares.use((req, res, next) =>\n handleDevToolsViteRequest(req, res, next, (parsedData) => {\n const { data, routine } = parsedData\n if (routine === 'open-source') {\n return handleOpenSource({\n data: { type: data.type, data },\n openInEditor,\n appDir,\n })\n }\n return\n }),\n )\n },\n transform(code) {\n if (code.includes('__TSD_PORT__')) {\n code = code.replace('__TSD_PORT__', String(port))\n }\n return code\n },\n },\n {\n name: '@tanstack/devtools:better-console-logs',\n enforce: 'pre',\n apply(config) {\n return config.mode === 'development' && enhancedLogsConfig.enabled\n },\n transform(code, id) {\n // Ignore anything external\n if (\n id.includes('node_modules') ||\n id.includes('?raw') ||\n id.includes('dist') ||\n id.includes('build')\n )\n return code\n\n if (!code.includes('console.')) {\n return code\n }\n const lines = code.split('\\n')\n return lines\n .map((line, lineNumber) => {\n if (\n line.trim().startsWith('//') ||\n line.trim().startsWith('/**') ||\n line.trim().startsWith('*')\n ) {\n return line\n }\n // Do not add for arrow functions or return statements\n if (\n line.replaceAll(' ', '').includes('=>console.') ||\n line.includes('return console.')\n ) {\n return line\n }\n\n const column = line.indexOf('console.')\n const location = `${id.replace(normalizePath(process.cwd()), '')}:${lineNumber + 1}:${column + 1}`\n const logMessage = `'${chalk.magenta('LOG')} ${chalk.blueBright(`${location} - http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(id.replace(normalizePath(process.cwd()), ''))}&line=${lineNumber + 1}&column=${column + 1}`)}\\\\n → '`\n if (line.includes('console.log(')) {\n const newLine = `console.log(${logMessage},`\n return line.replace('console.log(', newLine)\n }\n if (line.includes('console.error(')) {\n const newLine = `console.error(${logMessage},`\n return line.replace('console.error(', newLine)\n }\n return line\n })\n .join('\\n')\n },\n },\n ]\n}\n"],"names":[],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"plugin.js","sources":["../../src/plugin.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport chalk from 'chalk'\nimport { ServerEventBus } from '@tanstack/devtools-event-bus/server'\nimport { handleDevToolsViteRequest } from './utils'\nimport { DEFAULT_EDITOR_CONFIG, handleOpenSource } from './editor'\nimport { addSourceToJsx } from './inject-source'\nimport type { EditorConfig } from './editor'\nimport type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'\nimport type { Plugin } from 'vite'\n\nexport type TanStackDevtoolsViteConfig = {\n /** The directory where the react router app is located. Defaults to the \"./src\" relative to where vite.config is being defined. */\n appDir?: string\n /**\n * Configuration for the editor integration. Defaults to opening in VS code\n */\n editor?: EditorConfig\n /**\n * The configuration options for the server event bus\n */\n eventBusConfig?: ServerEventBusConfig\n /**\n * Configuration for enhanced logging.\n */\n enhancedLogs?: {\n /**\n * Whether to enable enhanced logging.\n * @default true\n */\n enabled: boolean\n }\n /**\n * Configuration for source injection.\n */\n injectSource: {\n /**\n * Whether to enable source injection via data-tsd-source.\n * @default true\n */\n enabled: boolean\n }\n}\n\nexport const defineDevtoolsConfig = (config: TanStackDevtoolsViteConfig) =>\n config\n\nexport const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {\n let port = 5173\n const appDir = args?.appDir || './src'\n const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true }\n const injectSourceConfig = args?.injectSource ?? { enabled: true }\n const bus = new ServerEventBus(args?.eventBusConfig)\n\n return [\n {\n enforce: 'pre',\n name: '@tanstack/devtools:inject-source',\n apply(config) {\n return config.mode === 'development' && injectSourceConfig.enabled\n },\n transform(code, id) {\n if (\n id.includes('node_modules') ||\n id.includes('?raw') ||\n id.includes('dist') ||\n id.includes('build')\n )\n return code\n\n return addSourceToJsx(code, id)\n },\n },\n {\n enforce: 'pre',\n name: '@tanstack/devtools:custom-server',\n apply(config) {\n // Custom server is only needed in development for piping events to the client\n return config.mode === 'development'\n },\n configureServer(server) {\n bus.start()\n server.middlewares.use((req, _res, next) => {\n if (req.socket.localPort && req.socket.localPort !== port) {\n port = req.socket.localPort\n }\n next()\n })\n if (server.config.server.port) {\n port = server.config.server.port\n }\n\n server.httpServer?.on('listening', () => {\n port = server.config.server.port\n })\n\n const editor = args?.editor ?? DEFAULT_EDITOR_CONFIG\n const openInEditor = async (\n path: string | undefined,\n lineNum: string | undefined,\n ) => {\n if (!path) {\n return\n }\n await editor.open(path, lineNum)\n }\n server.middlewares.use((req, res, next) =>\n handleDevToolsViteRequest(req, res, next, (parsedData) => {\n const { data, routine } = parsedData\n if (routine === 'open-source') {\n return handleOpenSource({\n data: { type: data.type, data },\n openInEditor,\n appDir,\n })\n }\n return\n }),\n )\n },\n transform(code) {\n if (code.includes('__TSD_PORT__')) {\n code = code.replace('__TSD_PORT__', String(port))\n }\n return code\n },\n },\n {\n name: '@tanstack/devtools:better-console-logs',\n enforce: 'pre',\n apply(config) {\n return config.mode === 'development' && enhancedLogsConfig.enabled\n },\n transform(code, id) {\n // Ignore anything external\n if (\n id.includes('node_modules') ||\n id.includes('?raw') ||\n id.includes('dist') ||\n id.includes('build')\n )\n return code\n\n if (!code.includes('console.')) {\n return code\n }\n const lines = code.split('\\n')\n return lines\n .map((line, lineNumber) => {\n if (\n line.trim().startsWith('//') ||\n line.trim().startsWith('/**') ||\n line.trim().startsWith('*')\n ) {\n return line\n }\n // Do not add for arrow functions or return statements\n if (\n line.replaceAll(' ', '').includes('=>console.') ||\n line.includes('return console.')\n ) {\n return line\n }\n\n const column = line.indexOf('console.')\n const location = `${id.replace(normalizePath(process.cwd()), '')}:${lineNumber + 1}:${column + 1}`\n const logMessage = `'${chalk.magenta('LOG')} ${chalk.blueBright(`${location} - http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(id.replace(normalizePath(process.cwd()), ''))}&line=${lineNumber + 1}&column=${column + 1}`)}\\\\n → '`\n if (line.includes('console.log(')) {\n const newLine = `console.log(${logMessage},`\n return line.replace('console.log(', newLine)\n }\n if (line.includes('console.error(')) {\n const newLine = `console.error(${logMessage},`\n return line.replace('console.error(', newLine)\n }\n return line\n })\n .join('\\n')\n },\n },\n ]\n}\n"],"names":[],"mappings":";;;;;;AA2CO,MAAM,uBAAuB,CAAC,WACnC;AAEK,MAAM,WAAW,CAAC,SAAqD;AAC5E,MAAI,OAAO;AACX,QAAM,SAAS,MAAM,UAAU;AAC/B,QAAM,qBAAqB,MAAM,gBAAgB,EAAE,SAAS,KAAA;AAC5D,QAAM,qBAAqB,MAAM,gBAAgB,EAAE,SAAS,KAAA;AAC5D,QAAM,MAAM,IAAI,eAAe,MAAM,cAAc;AAEnD,SAAO;AAAA,IACL;AAAA,MACE,SAAS;AAAA,MAC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AAK,SAAS,cAAc,GAAG;AACjC,kBAAM,UAAU,eAAe,UAAU;AACzC,mBAAO,KAAK,QAAQ,gBAAgB,OAAO;AAAA,UAC7C;AACA,cAAI,KAAK,SAAS,gBAAgB,GAAG;AACnC,kBAAM,UAAU,iBAAiB,UAAU;AAC3C,mBAAO,KAAK,QAAQ,kBAAkB,OAAO;AAAA,UAC/C;AACA,iBAAO;AAAA,QACT,CAAC,EACA,KAAK,IAAI;AAAA,MACd;AAAA,IAAA;AAAA,EACF;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/devtools-vite",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "TanStack Vite plugin used to enhance the core devtools with additional functionalities",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,7 +52,8 @@
|
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/babel__core": "^7.20.5",
|
|
54
54
|
"@types/babel__generator": "^7.27.0",
|
|
55
|
-
"@types/babel__traverse": "^7.28.0"
|
|
55
|
+
"@types/babel__traverse": "^7.28.0",
|
|
56
|
+
"happy-dom": "^18.0.1"
|
|
56
57
|
},
|
|
57
58
|
"scripts": {
|
|
58
59
|
"clean": "premove ./build ./dist",
|
package/src/inject-source.ts
CHANGED
|
@@ -1,26 +1,198 @@
|
|
|
1
1
|
import { normalizePath } from 'vite'
|
|
2
2
|
import { gen, parse, t, trav } from './babel'
|
|
3
|
-
import type { types as Babel } from '@babel/core'
|
|
3
|
+
import type { types as Babel, NodePath } from '@babel/core'
|
|
4
4
|
import type { ParseResult } from '@babel/parser'
|
|
5
5
|
|
|
6
|
+
const getPropsNameFromFunctionDeclaration = (
|
|
7
|
+
functionDeclaration:
|
|
8
|
+
| t.VariableDeclarator
|
|
9
|
+
| t.FunctionExpression
|
|
10
|
+
| t.FunctionDeclaration
|
|
11
|
+
| t.ArrowFunctionExpression,
|
|
12
|
+
) => {
|
|
13
|
+
let propsName: string | null = null
|
|
14
|
+
|
|
15
|
+
if (functionDeclaration.type === 'FunctionExpression') {
|
|
16
|
+
const firstArgument = functionDeclaration.params[0]
|
|
17
|
+
// handles (props) => {}
|
|
18
|
+
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
19
|
+
propsName = firstArgument.name
|
|
20
|
+
}
|
|
21
|
+
// handles ({ ...props }) => {}
|
|
22
|
+
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
23
|
+
firstArgument.properties.forEach((prop) => {
|
|
24
|
+
if (
|
|
25
|
+
prop.type === 'RestElement' &&
|
|
26
|
+
prop.argument.type === 'Identifier'
|
|
27
|
+
) {
|
|
28
|
+
propsName = prop.argument.name
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
return propsName
|
|
33
|
+
}
|
|
34
|
+
if (functionDeclaration.type === 'ArrowFunctionExpression') {
|
|
35
|
+
const firstArgument = functionDeclaration.params[0]
|
|
36
|
+
// handles (props) => {}
|
|
37
|
+
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
38
|
+
propsName = firstArgument.name
|
|
39
|
+
}
|
|
40
|
+
// handles ({ ...props }) => {}
|
|
41
|
+
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
42
|
+
firstArgument.properties.forEach((prop) => {
|
|
43
|
+
if (
|
|
44
|
+
prop.type === 'RestElement' &&
|
|
45
|
+
prop.argument.type === 'Identifier'
|
|
46
|
+
) {
|
|
47
|
+
propsName = prop.argument.name
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
return propsName
|
|
52
|
+
}
|
|
53
|
+
if (functionDeclaration.type === 'FunctionDeclaration') {
|
|
54
|
+
const firstArgument = functionDeclaration.params[0]
|
|
55
|
+
// handles (props) => {}
|
|
56
|
+
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
57
|
+
propsName = firstArgument.name
|
|
58
|
+
}
|
|
59
|
+
// handles ({ ...props }) => {}
|
|
60
|
+
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
61
|
+
firstArgument.properties.forEach((prop) => {
|
|
62
|
+
if (
|
|
63
|
+
prop.type === 'RestElement' &&
|
|
64
|
+
prop.argument.type === 'Identifier'
|
|
65
|
+
) {
|
|
66
|
+
propsName = prop.argument.name
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
return propsName
|
|
71
|
+
}
|
|
72
|
+
// Arrow function case
|
|
73
|
+
if (
|
|
74
|
+
functionDeclaration.init?.type === 'ArrowFunctionExpression' ||
|
|
75
|
+
functionDeclaration.init?.type === 'FunctionExpression'
|
|
76
|
+
) {
|
|
77
|
+
const firstArgument = functionDeclaration.init.params[0]
|
|
78
|
+
// handles (props) => {}
|
|
79
|
+
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
80
|
+
propsName = firstArgument.name
|
|
81
|
+
}
|
|
82
|
+
// handles ({ ...props }) => {}
|
|
83
|
+
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
84
|
+
firstArgument.properties.forEach((prop) => {
|
|
85
|
+
if (
|
|
86
|
+
prop.type === 'RestElement' &&
|
|
87
|
+
prop.argument.type === 'Identifier'
|
|
88
|
+
) {
|
|
89
|
+
propsName = prop.argument.name
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return propsName
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const transformJSX = (
|
|
98
|
+
element: NodePath<t.JSXOpeningElement>,
|
|
99
|
+
propsName: string | null,
|
|
100
|
+
file: string,
|
|
101
|
+
) => {
|
|
102
|
+
const loc = element.node.loc
|
|
103
|
+
if (!loc) return
|
|
104
|
+
const line = loc.start.line
|
|
105
|
+
const column = loc.start.column
|
|
106
|
+
|
|
107
|
+
const hasDataSource = element.node.attributes.some(
|
|
108
|
+
(attr) =>
|
|
109
|
+
attr.type === 'JSXAttribute' &&
|
|
110
|
+
attr.name.type === 'JSXIdentifier' &&
|
|
111
|
+
attr.name.name === 'data-tsd-source',
|
|
112
|
+
)
|
|
113
|
+
// Check if props are spread
|
|
114
|
+
const hasSpread = element.node.attributes.some(
|
|
115
|
+
(attr) =>
|
|
116
|
+
attr.type === 'JSXSpreadAttribute' &&
|
|
117
|
+
attr.argument.type === 'Identifier' &&
|
|
118
|
+
attr.argument.name === propsName,
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
if (hasSpread || hasDataSource) {
|
|
122
|
+
// Do not inject if props are spread
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Inject data-source as a string: "<file>:<line>:<column>"
|
|
127
|
+
element.node.attributes.push(
|
|
128
|
+
t.jsxAttribute(
|
|
129
|
+
t.jsxIdentifier('data-tsd-source'),
|
|
130
|
+
t.stringLiteral(`${file}:${line}:${column}`),
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
return true
|
|
135
|
+
}
|
|
136
|
+
|
|
6
137
|
const transform = (ast: ParseResult<Babel.File>, file: string) => {
|
|
7
138
|
let didTransform = false
|
|
139
|
+
|
|
8
140
|
trav(ast, {
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const line = loc.start.line
|
|
13
|
-
const column = loc.start.column
|
|
14
|
-
|
|
15
|
-
// Inject data-source as a string: "<file>:<line>:<column>"
|
|
16
|
-
path.node.attributes.push(
|
|
17
|
-
t.jsxAttribute(
|
|
18
|
-
t.jsxIdentifier('data-tsd-source'),
|
|
19
|
-
t.stringLiteral(`${file}:${line}:${column}`),
|
|
20
|
-
),
|
|
141
|
+
FunctionDeclaration(functionDeclaration) {
|
|
142
|
+
const propsName = getPropsNameFromFunctionDeclaration(
|
|
143
|
+
functionDeclaration.node,
|
|
21
144
|
)
|
|
145
|
+
functionDeclaration.traverse({
|
|
146
|
+
JSXOpeningElement(element) {
|
|
147
|
+
const transformed = transformJSX(element, propsName, file)
|
|
148
|
+
if (transformed) {
|
|
149
|
+
didTransform = true
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
})
|
|
153
|
+
},
|
|
154
|
+
ArrowFunctionExpression(path) {
|
|
155
|
+
const propsName = getPropsNameFromFunctionDeclaration(path.node)
|
|
156
|
+
path.traverse({
|
|
157
|
+
JSXOpeningElement(element) {
|
|
158
|
+
const transformed = transformJSX(element, propsName, file)
|
|
159
|
+
if (transformed) {
|
|
160
|
+
didTransform = true
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
})
|
|
164
|
+
},
|
|
165
|
+
FunctionExpression(path) {
|
|
166
|
+
const propsName = getPropsNameFromFunctionDeclaration(path.node)
|
|
167
|
+
path.traverse({
|
|
168
|
+
JSXOpeningElement(element) {
|
|
169
|
+
const transformed = transformJSX(element, propsName, file)
|
|
170
|
+
if (transformed) {
|
|
171
|
+
didTransform = true
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
})
|
|
175
|
+
},
|
|
176
|
+
VariableDeclaration(path) {
|
|
177
|
+
const functionDeclaration = path.node.declarations.find((decl) => {
|
|
178
|
+
return (
|
|
179
|
+
decl.init?.type === 'ArrowFunctionExpression' ||
|
|
180
|
+
decl.init?.type === 'FunctionExpression'
|
|
181
|
+
)
|
|
182
|
+
})
|
|
183
|
+
if (!functionDeclaration) {
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration)
|
|
22
187
|
|
|
23
|
-
|
|
188
|
+
path.traverse({
|
|
189
|
+
JSXOpeningElement(element) {
|
|
190
|
+
const transformed = transformJSX(element, propsName, file)
|
|
191
|
+
if (transformed) {
|
|
192
|
+
didTransform = true
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
})
|
|
24
196
|
},
|
|
25
197
|
})
|
|
26
198
|
|
package/src/plugin.ts
CHANGED
|
@@ -29,6 +29,16 @@ export type TanStackDevtoolsViteConfig = {
|
|
|
29
29
|
*/
|
|
30
30
|
enabled: boolean
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Configuration for source injection.
|
|
34
|
+
*/
|
|
35
|
+
injectSource: {
|
|
36
|
+
/**
|
|
37
|
+
* Whether to enable source injection via data-tsd-source.
|
|
38
|
+
* @default true
|
|
39
|
+
*/
|
|
40
|
+
enabled: boolean
|
|
41
|
+
}
|
|
32
42
|
}
|
|
33
43
|
|
|
34
44
|
export const defineDevtoolsConfig = (config: TanStackDevtoolsViteConfig) =>
|
|
@@ -38,6 +48,7 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
38
48
|
let port = 5173
|
|
39
49
|
const appDir = args?.appDir || './src'
|
|
40
50
|
const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true }
|
|
51
|
+
const injectSourceConfig = args?.injectSource ?? { enabled: true }
|
|
41
52
|
const bus = new ServerEventBus(args?.eventBusConfig)
|
|
42
53
|
|
|
43
54
|
return [
|
|
@@ -45,7 +56,7 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
45
56
|
enforce: 'pre',
|
|
46
57
|
name: '@tanstack/devtools:inject-source',
|
|
47
58
|
apply(config) {
|
|
48
|
-
return config.mode === 'development'
|
|
59
|
+
return config.mode === 'development' && injectSourceConfig.enabled
|
|
49
60
|
},
|
|
50
61
|
transform(code, id) {
|
|
51
62
|
if (
|