@tanstack/devtools-vite 0.5.5 → 0.6.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/babel.js +7 -15
- package/dist/esm/babel.js.map +1 -1
- package/dist/esm/editor.js +18 -27
- package/dist/esm/editor.js.map +1 -1
- package/dist/esm/enhance-logs.js +48 -80
- package/dist/esm/enhance-logs.js.map +1 -1
- package/dist/esm/index.js +1 -5
- package/dist/esm/inject-plugin.js +160 -220
- package/dist/esm/inject-plugin.js.map +1 -1
- package/dist/esm/inject-source.js +108 -201
- package/dist/esm/inject-source.js.map +1 -1
- package/dist/esm/matcher.js +11 -16
- package/dist/esm/matcher.js.map +1 -1
- package/dist/esm/package-manager.js +90 -131
- package/dist/esm/package-manager.js.map +1 -1
- package/dist/esm/plugin.js +334 -448
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/remove-devtools.js +97 -131
- package/dist/esm/remove-devtools.js.map +1 -1
- package/dist/esm/utils.js +149 -162
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/virtual-console.js +170 -14
- package/dist/esm/virtual-console.js.map +1 -1
- package/package.json +3 -3
- package/src/plugin.ts +9 -1
- package/src/virtual-console.test.ts +172 -1
- package/src/virtual-console.ts +146 -7
- package/dist/esm/index.js.map +0 -1
|
@@ -1,208 +1,115 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { gen, trav } from "./babel.js";
|
|
1
|
+
import { gen, parse, t, trav } from "./babel.js";
|
|
3
2
|
import { matcher } from "./matcher.js";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
if (firstArgument && firstArgument.type === "ObjectPattern") {
|
|
42
|
-
firstArgument.properties.forEach((prop) => {
|
|
43
|
-
if (prop.type === "RestElement" && prop.argument.type === "Identifier") {
|
|
44
|
-
propsName = prop.argument.name;
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return propsName;
|
|
49
|
-
}
|
|
50
|
-
if (functionDeclaration.init?.type === "ArrowFunctionExpression" || functionDeclaration.init?.type === "FunctionExpression") {
|
|
51
|
-
const firstArgument = functionDeclaration.init.params[0];
|
|
52
|
-
if (firstArgument && firstArgument.type === "Identifier") {
|
|
53
|
-
propsName = firstArgument.name;
|
|
54
|
-
}
|
|
55
|
-
if (firstArgument && firstArgument.type === "ObjectPattern") {
|
|
56
|
-
firstArgument.properties.forEach((prop) => {
|
|
57
|
-
if (prop.type === "RestElement" && prop.argument.type === "Identifier") {
|
|
58
|
-
propsName = prop.argument.name;
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return propsName;
|
|
3
|
+
import { normalizePath } from "vite";
|
|
4
|
+
//#region src/inject-source.ts
|
|
5
|
+
var getPropsNameFromFunctionDeclaration = (functionDeclaration) => {
|
|
6
|
+
let propsName = null;
|
|
7
|
+
if (functionDeclaration.type === "FunctionExpression") {
|
|
8
|
+
const firstArgument = functionDeclaration.params[0];
|
|
9
|
+
if (firstArgument && firstArgument.type === "Identifier") propsName = firstArgument.name;
|
|
10
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") firstArgument.properties.forEach((prop) => {
|
|
11
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") propsName = prop.argument.name;
|
|
12
|
+
});
|
|
13
|
+
return propsName;
|
|
14
|
+
}
|
|
15
|
+
if (functionDeclaration.type === "ArrowFunctionExpression") {
|
|
16
|
+
const firstArgument = functionDeclaration.params[0];
|
|
17
|
+
if (firstArgument && firstArgument.type === "Identifier") propsName = firstArgument.name;
|
|
18
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") firstArgument.properties.forEach((prop) => {
|
|
19
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") propsName = prop.argument.name;
|
|
20
|
+
});
|
|
21
|
+
return propsName;
|
|
22
|
+
}
|
|
23
|
+
if (functionDeclaration.type === "FunctionDeclaration") {
|
|
24
|
+
const firstArgument = functionDeclaration.params[0];
|
|
25
|
+
if (firstArgument && firstArgument.type === "Identifier") propsName = firstArgument.name;
|
|
26
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") firstArgument.properties.forEach((prop) => {
|
|
27
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") propsName = prop.argument.name;
|
|
28
|
+
});
|
|
29
|
+
return propsName;
|
|
30
|
+
}
|
|
31
|
+
if (functionDeclaration.init?.type === "ArrowFunctionExpression" || functionDeclaration.init?.type === "FunctionExpression") {
|
|
32
|
+
const firstArgument = functionDeclaration.init.params[0];
|
|
33
|
+
if (firstArgument && firstArgument.type === "Identifier") propsName = firstArgument.name;
|
|
34
|
+
if (firstArgument && firstArgument.type === "ObjectPattern") firstArgument.properties.forEach((prop) => {
|
|
35
|
+
if (prop.type === "RestElement" && prop.argument.type === "Identifier") propsName = prop.argument.name;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return propsName;
|
|
64
39
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (element.type === "JSXMemberExpression") {
|
|
70
|
-
return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`;
|
|
71
|
-
}
|
|
72
|
-
return `${element.namespace.name}:${element.name.name}`;
|
|
40
|
+
var getNameOfElement = (element) => {
|
|
41
|
+
if (element.type === "JSXIdentifier") return element.name;
|
|
42
|
+
if (element.type === "JSXMemberExpression") return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`;
|
|
43
|
+
return `${element.namespace.name}:${element.name.name}`;
|
|
73
44
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
);
|
|
87
|
-
const hasSpread = element.node.attributes.some(
|
|
88
|
-
(attr) => attr.type === "JSXSpreadAttribute" && attr.argument.type === "Identifier" && attr.argument.name === propsName
|
|
89
|
-
);
|
|
90
|
-
if (hasSpread || hasDataSource) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
element.node.attributes.push(
|
|
94
|
-
t.jsxAttribute(
|
|
95
|
-
t.jsxIdentifier("data-tsd-source"),
|
|
96
|
-
t.stringLiteral(`${file}:${line}:${column + 1}`)
|
|
97
|
-
)
|
|
98
|
-
);
|
|
99
|
-
return true;
|
|
45
|
+
var transformJSX = (element, propsName, file, ignorePatterns) => {
|
|
46
|
+
const loc = element.node.loc;
|
|
47
|
+
if (!loc) return;
|
|
48
|
+
const line = loc.start.line;
|
|
49
|
+
const column = loc.start.column;
|
|
50
|
+
const nameOfElement = getNameOfElement(element.node.name);
|
|
51
|
+
const isIgnored = matcher(ignorePatterns, nameOfElement);
|
|
52
|
+
if (nameOfElement === "Fragment" || nameOfElement === "React.Fragment" || isIgnored) return;
|
|
53
|
+
const hasDataSource = element.node.attributes.some((attr) => attr.type === "JSXAttribute" && attr.name.type === "JSXIdentifier" && attr.name.name === "data-tsd-source");
|
|
54
|
+
if (element.node.attributes.some((attr) => attr.type === "JSXSpreadAttribute" && attr.argument.type === "Identifier" && attr.argument.name === propsName) || hasDataSource) return;
|
|
55
|
+
element.node.attributes.push(t.jsxAttribute(t.jsxIdentifier("data-tsd-source"), t.stringLiteral(`${file}:${line}:${column + 1}`)));
|
|
56
|
+
return true;
|
|
100
57
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
},
|
|
138
|
-
FunctionExpression(path) {
|
|
139
|
-
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
140
|
-
path.traverse({
|
|
141
|
-
JSXOpeningElement(element) {
|
|
142
|
-
const transformed = transformJSX(
|
|
143
|
-
element,
|
|
144
|
-
propsName,
|
|
145
|
-
file,
|
|
146
|
-
ignorePatterns
|
|
147
|
-
);
|
|
148
|
-
if (transformed) {
|
|
149
|
-
didTransform = true;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
},
|
|
154
|
-
VariableDeclaration(path) {
|
|
155
|
-
const functionDeclaration = path.node.declarations.find((decl) => {
|
|
156
|
-
return decl.init?.type === "ArrowFunctionExpression" || decl.init?.type === "FunctionExpression";
|
|
157
|
-
});
|
|
158
|
-
if (!functionDeclaration) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration);
|
|
162
|
-
path.traverse({
|
|
163
|
-
JSXOpeningElement(element) {
|
|
164
|
-
const transformed = transformJSX(
|
|
165
|
-
element,
|
|
166
|
-
propsName,
|
|
167
|
-
file,
|
|
168
|
-
ignorePatterns
|
|
169
|
-
);
|
|
170
|
-
if (transformed) {
|
|
171
|
-
didTransform = true;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
return didTransform;
|
|
58
|
+
var transform = (ast, file, ignorePatterns) => {
|
|
59
|
+
let didTransform = false;
|
|
60
|
+
trav(ast, {
|
|
61
|
+
FunctionDeclaration(functionDeclaration) {
|
|
62
|
+
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration.node);
|
|
63
|
+
functionDeclaration.traverse({ JSXOpeningElement(element) {
|
|
64
|
+
if (transformJSX(element, propsName, file, ignorePatterns)) didTransform = true;
|
|
65
|
+
} });
|
|
66
|
+
},
|
|
67
|
+
ArrowFunctionExpression(path) {
|
|
68
|
+
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
69
|
+
path.traverse({ JSXOpeningElement(element) {
|
|
70
|
+
if (transformJSX(element, propsName, file, ignorePatterns)) didTransform = true;
|
|
71
|
+
} });
|
|
72
|
+
},
|
|
73
|
+
FunctionExpression(path) {
|
|
74
|
+
const propsName = getPropsNameFromFunctionDeclaration(path.node);
|
|
75
|
+
path.traverse({ JSXOpeningElement(element) {
|
|
76
|
+
if (transformJSX(element, propsName, file, ignorePatterns)) didTransform = true;
|
|
77
|
+
} });
|
|
78
|
+
},
|
|
79
|
+
VariableDeclaration(path) {
|
|
80
|
+
const functionDeclaration = path.node.declarations.find((decl) => {
|
|
81
|
+
return decl.init?.type === "ArrowFunctionExpression" || decl.init?.type === "FunctionExpression";
|
|
82
|
+
});
|
|
83
|
+
if (!functionDeclaration) return;
|
|
84
|
+
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration);
|
|
85
|
+
path.traverse({ JSXOpeningElement(element) {
|
|
86
|
+
if (transformJSX(element, propsName, file, ignorePatterns)) didTransform = true;
|
|
87
|
+
} });
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return didTransform;
|
|
178
91
|
};
|
|
179
92
|
function addSourceToJsx(code, id, ignore = {}) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
filename: id,
|
|
199
|
-
sourceFileName: filePath
|
|
200
|
-
});
|
|
201
|
-
} catch (e) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
93
|
+
const [filePath] = id.split("?");
|
|
94
|
+
const location = filePath?.replace(normalizePath(process.cwd()), "");
|
|
95
|
+
if (matcher(ignore.files || [], location)) return;
|
|
96
|
+
try {
|
|
97
|
+
const ast = parse(code, {
|
|
98
|
+
sourceType: "module",
|
|
99
|
+
plugins: ["jsx", "typescript"]
|
|
100
|
+
});
|
|
101
|
+
if (!transform(ast, location, ignore.components || [])) return;
|
|
102
|
+
return gen(ast, {
|
|
103
|
+
sourceMaps: true,
|
|
104
|
+
retainLines: true,
|
|
105
|
+
filename: id,
|
|
106
|
+
sourceFileName: filePath
|
|
107
|
+
});
|
|
108
|
+
} catch (e) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
204
111
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
//# sourceMappingURL=inject-source.js.map
|
|
112
|
+
//#endregion
|
|
113
|
+
export { addSourceToJsx };
|
|
114
|
+
|
|
115
|
+
//# sourceMappingURL=inject-source.js.map
|
|
@@ -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 { matcher } from './matcher'\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 getNameOfElement = (\n element: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName,\n): string => {\n if (element.type === 'JSXIdentifier') {\n return element.name\n }\n if (element.type === 'JSXMemberExpression') {\n return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`\n }\n\n return `${element.namespace.name}:${element.name.name}`\n}\n\nconst transformJSX = (\n element: NodePath<t.JSXOpeningElement>,\n propsName: string | null,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n) => {\n const loc = element.node.loc\n if (!loc) return\n const line = loc.start.line\n const column = loc.start.column\n const nameOfElement = getNameOfElement(element.node.name)\n const isIgnored = matcher(ignorePatterns, nameOfElement)\n if (\n nameOfElement === 'Fragment' ||\n nameOfElement === 'React.Fragment' ||\n isIgnored\n ) {\n return\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 + 1}`),\n ),\n )\n\n return true\n}\n\nconst transform = (\n ast: ParseResult<Babel.File>,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n) => {\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n })\n\n return didTransform\n}\n\nexport function addSourceToJsx(\n code: string,\n id: string,\n ignore: {\n files?: Array<string | RegExp>\n components?: Array<string | RegExp>\n } = {},\n) {\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 const fileIgnored = matcher(ignore.files || [], location)\n if (fileIgnored) {\n return\n }\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n })\n const didTransform = transform(ast, location, ignore.components || [])\n if (!didTransform) {\n return\n }\n return gen(ast, {\n sourceMaps: true,\n retainLines: true,\n filename: id,\n sourceFileName: filePath,\n })\n } catch (e) {\n return\n }\n}\n"],"names":[],"mappings":";;;;;AAMA,MAAM,sCAAsC,CAC1C,wBAKG;AACH,MAAI,YAA2B;AAE/B,MAAI,oBAAoB,SAAS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mBAAmB;AAEzE,WAAK,SAAS;AAAA,QACZ,kBAAkB,SAAS;AACzB,gBAAM,cAAc;AAAA,YAClB;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA;AAEF,cAAI,aAAa;AACf,2BAAe;AAAA,UACjB;AAAA,QACF;AAAA,MAAA,CACD;AAAA,IACH;AAAA,EAAA,CACD;AAED,SAAO;AACT;AAEO,SAAS,eACd,MACA,IACA,SAGI,CAAA,GACJ;AACA,QAAM,CAAC,QAAQ,IAAI,GAAG,MAAM,GAAG;AAE/B,QAAM,WAAW,UAAU,QAAQ,cAAc,QAAQ,IAAA,CAAK,GAAG,EAAE;AAEnE,QAAM,cAAc,QAAQ,OAAO,SAAS,CAAA,GAAI,QAAQ;AACxD,MAAI,aAAa;AACf;AAAA,EACF;AACA,MAAI;AACF,UAAM,MAAM,MAAM,MAAM;AAAA,MACtB,YAAY;AAAA,MACZ,SAAS,CAAC,OAAO,YAAY;AAAA,IAAA,CAC9B;AACD,UAAM,eAAe,UAAU,KAAK,UAAU,OAAO,cAAc,EAAE;AACrE,QAAI,CAAC,cAAc;AACjB;AAAA,IACF;AACA,WAAO,IAAI,KAAK;AAAA,MACd,YAAY;AAAA,MACZ,aAAa;AAAA,MACb,UAAU;AAAA,MACV,gBAAgB;AAAA,IAAA,CACjB;AAAA,EACH,SAAS,GAAG;AACV;AAAA,EACF;AACF;"}
|
|
1
|
+
{"version":3,"file":"inject-source.js","names":[],"sources":["../../src/inject-source.ts"],"sourcesContent":["import { normalizePath } from 'vite'\nimport { gen, parse, t, trav } from './babel'\nimport { matcher } from './matcher'\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 getNameOfElement = (\n element: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName,\n): string => {\n if (element.type === 'JSXIdentifier') {\n return element.name\n }\n if (element.type === 'JSXMemberExpression') {\n return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`\n }\n\n return `${element.namespace.name}:${element.name.name}`\n}\n\nconst transformJSX = (\n element: NodePath<t.JSXOpeningElement>,\n propsName: string | null,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n) => {\n const loc = element.node.loc\n if (!loc) return\n const line = loc.start.line\n const column = loc.start.column\n const nameOfElement = getNameOfElement(element.node.name)\n const isIgnored = matcher(ignorePatterns, nameOfElement)\n if (\n nameOfElement === 'Fragment' ||\n nameOfElement === 'React.Fragment' ||\n isIgnored\n ) {\n return\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 + 1}`),\n ),\n )\n\n return true\n}\n\nconst transform = (\n ast: ParseResult<Babel.File>,\n file: string,\n ignorePatterns: Array<string | RegExp>,\n) => {\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\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(\n element,\n propsName,\n file,\n ignorePatterns,\n )\n if (transformed) {\n didTransform = true\n }\n },\n })\n },\n })\n\n return didTransform\n}\n\nexport function addSourceToJsx(\n code: string,\n id: string,\n ignore: {\n files?: Array<string | RegExp>\n components?: Array<string | RegExp>\n } = {},\n) {\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 const fileIgnored = matcher(ignore.files || [], location)\n if (fileIgnored) {\n return\n }\n try {\n const ast = parse(code, {\n sourceType: 'module',\n plugins: ['jsx', 'typescript'],\n })\n const didTransform = transform(ast, location, ignore.components || [])\n if (!didTransform) {\n return\n }\n return gen(ast, {\n sourceMaps: true,\n retainLines: true,\n filename: id,\n sourceFileName: filePath,\n })\n } catch (e) {\n return\n }\n}\n"],"mappings":";;;;AAMA,IAAM,uCACJ,wBAKG;CACH,IAAI,YAA2B;CAE/B,IAAI,oBAAoB,SAAS,sBAAsB;EACrD,MAAM,gBAAgB,oBAAoB,OAAO;EAEjD,IAAI,iBAAiB,cAAc,SAAS,cAC1C,YAAY,cAAc;EAG5B,IAAI,iBAAiB,cAAc,SAAS,iBAC1C,cAAc,WAAW,SAAS,SAAS;GACzC,IACE,KAAK,SAA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AAsB,KAAK,KAAK,aAAa,MAAM,SAAS;IAChE,OACE,KAAK,MAAM,SAAS,6BACpB,KAAK,MAAM,SAAS;KAEtB;GACF,IAAI,CAAC,qBACH;GAEF,MAAM,YAAY,oCAAoC,oBAAoB;GAE1E,KAAK,SAAS,EACZ,kBAAkB,SAAS;IAOzB,IANoB,aAClB,SACA,WACA,MACA,eAEE,EACF,eAAe;MAGpB,CAAC;;EAEL,CAAC;CAEF,OAAO;;AAGT,SAAgB,eACd,MACA,IACA,SAGI,EAAE,EACN;CACA,MAAM,CAAC,YAAY,GAAG,MAAM,IAAI;CAEhC,MAAM,WAAW,UAAU,QAAQ,cAAc,QAAQ,KAAK,CAAC,EAAE,GAAG;CAGpE,IADoB,QAAQ,OAAO,SAAS,EAAE,EAAE,SAC5C,EACF;CAEF,IAAI;EACF,MAAM,MAAM,MAAM,MAAM;GACtB,YAAY;GACZ,SAAS,CAAC,OAAO,aAAa;GAC/B,CAAC;EAEF,IAAI,CADiB,UAAU,KAAK,UAAU,OAAO,cAAc,EAAE,CAChE,EACH;EAEF,OAAO,IAAI,KAAK;GACd,YAAY;GACZ,aAAa;GACb,UAAU;GACV,gBAAgB;GACjB,CAAC;UACK,GAAG;EACV"}
|
package/dist/esm/matcher.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
import picomatch from "picomatch";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} else {
|
|
10
|
-
return (s) => pattern.test(s);
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
return matchers.some((isMatch) => isMatch(str));
|
|
2
|
+
//#region src/matcher.ts
|
|
3
|
+
var matcher = (patterns, str) => {
|
|
4
|
+
if (patterns.length === 0) return false;
|
|
5
|
+
return patterns.map((pattern) => {
|
|
6
|
+
if (typeof pattern === "string") return picomatch(pattern);
|
|
7
|
+
else return (s) => pattern.test(s);
|
|
8
|
+
}).some((isMatch) => isMatch(str));
|
|
14
9
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//# sourceMappingURL=matcher.js.map
|
|
10
|
+
//#endregion
|
|
11
|
+
export { matcher };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=matcher.js.map
|
package/dist/esm/matcher.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matcher.js","sources":["../../src/matcher.ts"],"sourcesContent":["import picomatch from 'picomatch'\n\nexport const matcher = (\n patterns: Array<string | RegExp>,\n str: string,\n): boolean => {\n if (patterns.length === 0) {\n return false\n }\n const matchers = patterns.map((pattern) => {\n if (typeof pattern === 'string') {\n return picomatch(pattern)\n } else {\n return (s: string) => pattern.test(s)\n }\n })\n\n return matchers.some((isMatch) => isMatch(str))\n}\n"],"
|
|
1
|
+
{"version":3,"file":"matcher.js","names":[],"sources":["../../src/matcher.ts"],"sourcesContent":["import picomatch from 'picomatch'\n\nexport const matcher = (\n patterns: Array<string | RegExp>,\n str: string,\n): boolean => {\n if (patterns.length === 0) {\n return false\n }\n const matchers = patterns.map((pattern) => {\n if (typeof pattern === 'string') {\n return picomatch(pattern)\n } else {\n return (s: string) => pattern.test(s)\n }\n })\n\n return matchers.some((isMatch) => isMatch(str))\n}\n"],"mappings":";;AAEA,IAAa,WACX,UACA,QACY;CACZ,IAAI,SAAS,WAAW,GACtB,OAAO;CAUT,OARiB,SAAS,KAAK,YAAY;EACzC,IAAI,OAAO,YAAY,UACrB,OAAO,UAAU,QAAQ;OAEzB,QAAQ,MAAc,QAAQ,KAAK,EAAE;GAIlC,CAAS,MAAM,YAAY,QAAQ,IAAI,CAAC"}
|
|
@@ -1,139 +1,98 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { join } from "node:path";
|
|
1
|
+
import { readPackageJson, tryParseJson } from "./utils.js";
|
|
2
|
+
import { injectPluginIntoFile } from "./inject-plugin.js";
|
|
4
3
|
import { devtoolsEventClient } from "@tanstack/devtools-client";
|
|
5
4
|
import chalk from "chalk";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
const addPluginToDevtools = (devtoolsFileId, packageName, pluginName, pluginImport) => {
|
|
22
|
-
if (!devtoolsFileId) {
|
|
23
|
-
const error = "Devtools file not found";
|
|
24
|
-
console.log(
|
|
25
|
-
chalk.yellowBright(
|
|
26
|
-
`[@tanstack/devtools-vite] Could not add plugin. ${error}.`
|
|
27
|
-
)
|
|
28
|
-
);
|
|
29
|
-
return { success: false, error };
|
|
30
|
-
}
|
|
31
|
-
const result = injectPluginIntoFile(devtoolsFileId, {
|
|
32
|
-
packageName,
|
|
33
|
-
pluginName,
|
|
34
|
-
pluginImport
|
|
35
|
-
});
|
|
36
|
-
if (result.success) {
|
|
37
|
-
console.log(
|
|
38
|
-
chalk.greenBright(
|
|
39
|
-
`[@tanstack/devtools-vite] Successfully added ${packageName} to devtools!`
|
|
40
|
-
)
|
|
41
|
-
);
|
|
42
|
-
} else {
|
|
43
|
-
console.log(
|
|
44
|
-
chalk.yellowBright(
|
|
45
|
-
`[@tanstack/devtools-vite] Could not add plugin: ${result.error}`
|
|
46
|
-
)
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return result;
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { exec } from "node:child_process";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
//#region src/package-manager.ts
|
|
9
|
+
/**
|
|
10
|
+
* Gets the outdated command for the detected package manager
|
|
11
|
+
*/
|
|
12
|
+
var getOutdatedCommand = (packageManager) => {
|
|
13
|
+
switch (packageManager) {
|
|
14
|
+
case "yarn": return "yarn outdated --json";
|
|
15
|
+
case "pnpm": return "pnpm outdated --format json";
|
|
16
|
+
case "bun": return "bun outdated --json";
|
|
17
|
+
default: return "npm outdated --json";
|
|
18
|
+
}
|
|
50
19
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Adds a plugin to the devtools configuration file
|
|
22
|
+
*/
|
|
23
|
+
var addPluginToDevtools = (devtoolsFileId, packageName, pluginName, pluginImport) => {
|
|
24
|
+
if (!devtoolsFileId) {
|
|
25
|
+
const error = "Devtools file not found";
|
|
26
|
+
console.log(chalk.yellowBright(`[@tanstack/devtools-vite] Could not add plugin. ${error}.`));
|
|
27
|
+
return {
|
|
28
|
+
success: false,
|
|
29
|
+
error
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const result = injectPluginIntoFile(devtoolsFileId, {
|
|
33
|
+
packageName,
|
|
34
|
+
pluginName,
|
|
35
|
+
pluginImport
|
|
36
|
+
});
|
|
37
|
+
if (result.success) console.log(chalk.greenBright(`[@tanstack/devtools-vite] Successfully added ${packageName} to devtools!`));
|
|
38
|
+
else console.log(chalk.yellowBright(`[@tanstack/devtools-vite] Could not add plugin: ${result.error}`));
|
|
39
|
+
return result;
|
|
63
40
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (installError) {
|
|
75
|
-
console.error(
|
|
76
|
-
chalk.redBright(
|
|
77
|
-
`[@tanstack/devtools-vite] Failed to install ${packageName}:`
|
|
78
|
-
),
|
|
79
|
-
installError.message
|
|
80
|
-
);
|
|
81
|
-
resolve({
|
|
82
|
-
success: false,
|
|
83
|
-
error: installError.message
|
|
84
|
-
});
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
console.log(
|
|
88
|
-
chalk.greenBright(
|
|
89
|
-
`[@tanstack/devtools-vite] Successfully installed ${packageName}`
|
|
90
|
-
)
|
|
91
|
-
);
|
|
92
|
-
const updatedPackageJson = await readPackageJson();
|
|
93
|
-
devtoolsEventClient.emit("package-json-updated", {
|
|
94
|
-
packageJson: updatedPackageJson
|
|
95
|
-
});
|
|
96
|
-
resolve({ success: true });
|
|
97
|
-
});
|
|
98
|
-
});
|
|
41
|
+
/**
|
|
42
|
+
* Gets the install command for the detected package manager
|
|
43
|
+
*/
|
|
44
|
+
var getInstallCommand = (packageManager, packageName) => {
|
|
45
|
+
switch (packageManager) {
|
|
46
|
+
case "yarn": return `yarn add -D ${packageName}`;
|
|
47
|
+
case "pnpm": return `pnpm add -D ${packageName}`;
|
|
48
|
+
case "bun": return `bun add -D ${packageName}`;
|
|
49
|
+
default: return `npm install -D ${packageName}`;
|
|
50
|
+
}
|
|
99
51
|
};
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
52
|
+
var installPackage = async (packageName) => {
|
|
53
|
+
return new Promise((resolve) => {
|
|
54
|
+
const installCommand = getInstallCommand(detectPackageManager(), packageName);
|
|
55
|
+
console.log(chalk.blueBright(`[@tanstack/devtools-vite] Installing ${packageName}...`));
|
|
56
|
+
exec(installCommand, async (installError) => {
|
|
57
|
+
if (installError) {
|
|
58
|
+
console.error(chalk.redBright(`[@tanstack/devtools-vite] Failed to install ${packageName}:`), installError.message);
|
|
59
|
+
resolve({
|
|
60
|
+
success: false,
|
|
61
|
+
error: installError.message
|
|
62
|
+
});
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
console.log(chalk.greenBright(`[@tanstack/devtools-vite] Successfully installed ${packageName}`));
|
|
66
|
+
const updatedPackageJson = await readPackageJson();
|
|
67
|
+
devtoolsEventClient.emit("package-json-updated", { packageJson: updatedPackageJson });
|
|
68
|
+
resolve({ success: true });
|
|
69
|
+
});
|
|
70
|
+
});
|
|
115
71
|
};
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
devtoolsEventClient.emit("outdated-deps-read", {
|
|
127
|
-
outdatedDeps: newOutdatedDeps
|
|
128
|
-
});
|
|
129
|
-
resolve(newOutdatedDeps);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
});
|
|
72
|
+
/**
|
|
73
|
+
* Detects the package manager used in the project by checking for lock files
|
|
74
|
+
*/
|
|
75
|
+
var detectPackageManager = () => {
|
|
76
|
+
const cwd = process.cwd();
|
|
77
|
+
if (existsSync(join(cwd, "bun.lockb")) || existsSync(join(cwd, "bun.lock"))) return "bun";
|
|
78
|
+
if (existsSync(join(cwd, "pnpm-lock.yaml"))) return "pnpm";
|
|
79
|
+
if (existsSync(join(cwd, "yarn.lock"))) return "yarn";
|
|
80
|
+
if (existsSync(join(cwd, "package-lock.json"))) return "npm";
|
|
81
|
+
return "pnpm";
|
|
133
82
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
83
|
+
var emitOutdatedDeps = async () => {
|
|
84
|
+
return await new Promise((resolve) => {
|
|
85
|
+
exec(getOutdatedCommand(detectPackageManager()), (_, stdout) => {
|
|
86
|
+
if (stdout) {
|
|
87
|
+
const newOutdatedDeps = tryParseJson(stdout);
|
|
88
|
+
if (!newOutdatedDeps) return;
|
|
89
|
+
devtoolsEventClient.emit("outdated-deps-read", { outdatedDeps: newOutdatedDeps });
|
|
90
|
+
resolve(newOutdatedDeps);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
138
94
|
};
|
|
139
|
-
//#
|
|
95
|
+
//#endregion
|
|
96
|
+
export { addPluginToDevtools, emitOutdatedDeps, installPackage };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=package-manager.js.map
|