@tanstack/devtools-vite 0.7.1 → 0.7.2
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 +1 -1
- package/dist/esm/ast-utils.js +3 -3
- package/dist/esm/ast-utils.js.map +1 -1
- package/dist/esm/remove-devtools.js +3 -2
- package/dist/esm/remove-devtools.js.map +1 -1
- package/package.json +3 -3
- package/src/ast-utils.ts +7 -3
- package/src/remove-devtools.test.ts +27 -0
- package/src/remove-devtools.ts +6 -2
package/dist/esm/ast-utils.d.ts
CHANGED
|
@@ -8,4 +8,4 @@ export declare function forEachChild(node: Node, callback: (child: Node) => void
|
|
|
8
8
|
/**
|
|
9
9
|
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
|
|
10
10
|
*/
|
|
11
|
-
export declare function walk(node: Node, visitor: (node: Node) => void): void;
|
|
11
|
+
export declare function walk(node: Node, visitor: (node: Node, parentNode?: Node) => void, parentNode?: Node): void;
|
package/dist/esm/ast-utils.js
CHANGED
|
@@ -34,9 +34,9 @@ function forEachChild(node, callback) {
|
|
|
34
34
|
/**
|
|
35
35
|
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
|
|
36
36
|
*/
|
|
37
|
-
function walk(node, visitor) {
|
|
38
|
-
visitor(node);
|
|
39
|
-
forEachChild(node, (child) => walk(child, visitor));
|
|
37
|
+
function walk(node, visitor, parentNode) {
|
|
38
|
+
visitor(node, parentNode);
|
|
39
|
+
forEachChild(node, (child) => walk(child, visitor, node));
|
|
40
40
|
}
|
|
41
41
|
//#endregion
|
|
42
42
|
export { forEachChild, walk };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-utils.js","names":[],"sources":["../../src/ast-utils.ts"],"sourcesContent":["import type { Node } from 'oxc-parser'\n\n/**\n * Cache of keys that hold child nodes (objects/arrays) per AST node type.\n * Since oxc-parser produces AST via JSON.parse, every instance of a given\n * node type has the same set of keys, so we only need to discover them once.\n */\nconst childKeysCache = new Map<string, Array<string>>()\n\nfunction getChildKeys(node: Node): Array<string> {\n let keys = childKeysCache.get(node.type)\n if (keys) return keys\n\n keys = []\n for (const key in node) {\n if (key === 'type' || key === 'start' || key === 'end') continue\n // typeof null === 'object', so nullable node fields get cached too\n if (typeof (node as any)[key] === 'object') {\n keys.push(key)\n }\n }\n childKeysCache.set(node.type, keys)\n return keys\n}\n\n/**\n * Iterate over the direct child nodes of an AST node.\n * Uses a per-type cache of which keys hold child nodes to avoid\n * allocating Object.entries() arrays on every call.\n */\nexport function forEachChild(node: Node, callback: (child: Node) => void) {\n const keys = getChildKeys(node)\n for (const key of keys) {\n const value = (node as any)[key]\n if (value === null) continue\n if (Array.isArray(value)) {\n for (const item of value) {\n if (typeof item === 'object' && item !== null && 'type' in item) {\n callback(item)\n }\n }\n } else if ('type' in value) {\n callback(value as Node)\n }\n }\n}\n\n/**\n * Recursively walk AST nodes, calling `visitor` for each node with a `type`.\n */\nexport function walk(node: Node
|
|
1
|
+
{"version":3,"file":"ast-utils.js","names":[],"sources":["../../src/ast-utils.ts"],"sourcesContent":["import type { Node } from 'oxc-parser'\n\n/**\n * Cache of keys that hold child nodes (objects/arrays) per AST node type.\n * Since oxc-parser produces AST via JSON.parse, every instance of a given\n * node type has the same set of keys, so we only need to discover them once.\n */\nconst childKeysCache = new Map<string, Array<string>>()\n\nfunction getChildKeys(node: Node): Array<string> {\n let keys = childKeysCache.get(node.type)\n if (keys) return keys\n\n keys = []\n for (const key in node) {\n if (key === 'type' || key === 'start' || key === 'end') continue\n // typeof null === 'object', so nullable node fields get cached too\n if (typeof (node as any)[key] === 'object') {\n keys.push(key)\n }\n }\n childKeysCache.set(node.type, keys)\n return keys\n}\n\n/**\n * Iterate over the direct child nodes of an AST node.\n * Uses a per-type cache of which keys hold child nodes to avoid\n * allocating Object.entries() arrays on every call.\n */\nexport function forEachChild(node: Node, callback: (child: Node) => void) {\n const keys = getChildKeys(node)\n for (const key of keys) {\n const value = (node as any)[key]\n if (value === null) continue\n if (Array.isArray(value)) {\n for (const item of value) {\n if (typeof item === 'object' && item !== null && 'type' in item) {\n callback(item)\n }\n }\n } else if ('type' in value) {\n callback(value as Node)\n }\n }\n}\n\n/**\n * Recursively walk AST nodes, calling `visitor` for each node with a `type`.\n */\nexport function walk(\n node: Node,\n visitor: (node: Node, parentNode?: Node) => void,\n parentNode?: Node,\n) {\n visitor(node, parentNode)\n forEachChild(node, (child) => walk(child, visitor, node))\n}\n"],"mappings":";;;;;;AAOA,IAAM,iCAAiB,IAAI,KAA4B;AAEvD,SAAS,aAAa,MAA2B;CAC/C,IAAI,OAAO,eAAe,IAAI,KAAK,KAAK;CACxC,IAAI,MAAM,OAAO;CAEjB,OAAO,EAAE;CACT,KAAK,MAAM,OAAO,MAAM;EACtB,IAAI,QAAQ,UAAU,QAAQ,WAAW,QAAQ,OAAO;EAExD,IAAI,OAAQ,KAAa,SAAS,UAChC,KAAK,KAAK,IAAI;;CAGlB,eAAe,IAAI,KAAK,MAAM,KAAK;CACnC,OAAO;;;;;;;AAQT,SAAgB,aAAa,MAAY,UAAiC;CACxE,MAAM,OAAO,aAAa,KAAK;CAC/B,KAAK,MAAM,OAAO,MAAM;EACtB,MAAM,QAAS,KAAa;EAC5B,IAAI,UAAU,MAAM;EACpB,IAAI,MAAM,QAAQ,MAAM;QACjB,MAAM,QAAQ,OACjB,IAAI,OAAO,SAAS,YAAY,SAAS,QAAQ,UAAU,MACzD,SAAS,KAAK;SAGb,IAAI,UAAU,OACnB,SAAS,MAAc;;;;;;AAQ7B,SAAgB,KACd,MACA,SACA,YACA;CACA,QAAQ,MAAM,WAAW;CACzB,aAAa,OAAO,UAAU,KAAK,OAAO,SAAS,KAAK,CAAC"}
|
|
@@ -48,7 +48,7 @@ function removeDevtools(code, id) {
|
|
|
48
48
|
}
|
|
49
49
|
});
|
|
50
50
|
if (devtoolsNames.size === 0) return;
|
|
51
|
-
walk(result.program, (node) => {
|
|
51
|
+
walk(result.program, (node, parentNode) => {
|
|
52
52
|
if (node.type !== "JSXElement") return;
|
|
53
53
|
const opening = node.openingElement;
|
|
54
54
|
let matches = false;
|
|
@@ -58,7 +58,8 @@ function removeDevtools(code, id) {
|
|
|
58
58
|
pluginRefs.push(...getPluginReferences(opening));
|
|
59
59
|
let end = node.end;
|
|
60
60
|
if (code[end] === "\n") end++;
|
|
61
|
-
s.
|
|
61
|
+
if (parentNode?.type === "ParenthesizedExpression") s.overwrite(node.start, end, "null");
|
|
62
|
+
else s.remove(node.start, end);
|
|
62
63
|
});
|
|
63
64
|
if (pluginRefs.length > 0) walk(result.program, (node) => {
|
|
64
65
|
if (node.type !== "ImportDeclaration") return;
|
|
@@ -1 +1 @@
|
|
|
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 } catch (e) {\n return\n }\n}\n"],"mappings":";;;;;;;;AASA,SAAS,oBAAoB,SAA2C;CACtE,MAAM,OAAsB,EAAE;CAE9B,KAAK,MAAM,QAAQ,QAAQ,YAAY;EACrC,IAAI,KAAK,SAAS,gBAAgB;EAClC,IAAI,KAAK,KAAK,SAAS,mBAAmB,KAAK,KAAK,SAAS,WAC3D;EACF,IACE,CAAC,KAAK,SACN,KAAK,MAAM,SAAS,4BACpB,KAAK,MAAM,WAAW,SAAS,mBAE/B;EAEF,KAAK,MAAM,MAAM,KAAK,MAAM,WAAW,UAAU;GAC/C,IAAI,CAAC,MAAM,GAAG,SAAS,oBAAoB;GAE3C,KAAK,MAAM,QAAQ,GAAG,YAAY;IAChC,IACE,KAAK,SAAS,cACd,KAAK,IAAI,SAAS,gBAClB,KAAK,IAAI,SAAS,UAElB;IAEF,MAAM,QAAQ,KAAK;IAGnB,IACE,MAAM,SAAS,gBACf,MAAM,eAAe,KAAK,SAAS,iBAEnC,KAAK,KAAK,MAAM,eAAe,KAAK,KAAK;SAGtC,IACH,MAAM,SAAS,6BACf,MAAM,SAAS,sBACf;KACA,MAAM,OAAO,MAAM;KACnB,IACE,QACA,KAAK,SAAS,gBACd,KAAK,eAAe,KAAK,SAAS,iBAElC,KAAK,KAAK,KAAK,eAAe,KAAK,KAAK;WAIvC,IAAI,MAAM,SAAS,cACtB,KAAK,KAAK,MAAM,KAAK;SAGlB,IACH,MAAM,SAAS,oBACf,MAAM,OAAO,SAAS,cAEtB,KAAK,KAAK,MAAM,OAAO,KAAK;;;;CAMpC,OAAO;;AAGT,SAAgB,eAAe,MAAc,IAAY;CACvD,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC;CAE/B,IAAI;EACF,MAAM,SAAS,UAAU,UAAU,MAAM;GACvC,YAAY;GACZ,MAAM;GACP,CAAC;EACF,IAAI,OAAO,OAAO,SAAS,GAAG;EAE9B,MAAM,IAAI,IAAI,YAAY,KAAK;EAC/B,MAAM,gCAAgB,IAAI,KAAa;EACvC,MAAM,aAA4B,EAAE;EAGpC,KAAK,OAAO,UAAU,SAAS;GAC7B,IACE,KAAK,SAAS,uBACd,yBAAyB,KAAK,OAAO,MAAM,EAC3C;IACA,KAAK,MAAM,QAAQ,KAAK,YACtB,cAAc,IAAI,KAAK,MAAM,KAAK;IAEpC,IAAI,MAAM,KAAK;IACf,IAAI,KAAK,SAAS,MAAM;IACxB,EAAE,OAAO,KAAK,OAAO,IAAI;;IAE3B;EAEF,IAAI,cAAc,SAAS,GAAG;EAG9B,KAAK,OAAO,UAAU,
|
|
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, parentNode) => {\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 if (parentNode?.type === 'ParenthesizedExpression') {\n s.overwrite(node.start, end, 'null')\n } else {\n s.remove(node.start, end)\n }\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 } catch (e) {\n return\n }\n}\n"],"mappings":";;;;;;;;AASA,SAAS,oBAAoB,SAA2C;CACtE,MAAM,OAAsB,EAAE;CAE9B,KAAK,MAAM,QAAQ,QAAQ,YAAY;EACrC,IAAI,KAAK,SAAS,gBAAgB;EAClC,IAAI,KAAK,KAAK,SAAS,mBAAmB,KAAK,KAAK,SAAS,WAC3D;EACF,IACE,CAAC,KAAK,SACN,KAAK,MAAM,SAAS,4BACpB,KAAK,MAAM,WAAW,SAAS,mBAE/B;EAEF,KAAK,MAAM,M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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.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "TanStack Vite plugin used to enhance the core devtools with additional functionalities",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
"magic-string": "^0.30.0",
|
|
52
52
|
"oxc-parser": "^0.120.0",
|
|
53
53
|
"picomatch": "^4.0.3",
|
|
54
|
-
"@tanstack/devtools-client": "0.0.
|
|
55
|
-
"@tanstack/devtools-event-bus": "0.4.
|
|
54
|
+
"@tanstack/devtools-client": "0.0.7",
|
|
55
|
+
"@tanstack/devtools-event-bus": "0.4.2"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@types/picomatch": "^4.0.2",
|
package/src/ast-utils.ts
CHANGED
|
@@ -48,7 +48,11 @@ export function forEachChild(node: Node, callback: (child: Node) => void) {
|
|
|
48
48
|
/**
|
|
49
49
|
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
|
|
50
50
|
*/
|
|
51
|
-
export function walk(
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
export function walk(
|
|
52
|
+
node: Node,
|
|
53
|
+
visitor: (node: Node, parentNode?: Node) => void,
|
|
54
|
+
parentNode?: Node,
|
|
55
|
+
) {
|
|
56
|
+
visitor(node, parentNode)
|
|
57
|
+
forEachChild(node, (child) => walk(child, visitor, node))
|
|
54
58
|
}
|
|
@@ -577,4 +577,31 @@ export default function App() {
|
|
|
577
577
|
expect(output!.code).not.toContain('TanStackDevtools')
|
|
578
578
|
})
|
|
579
579
|
})
|
|
580
|
+
|
|
581
|
+
test('preserves valid syntax when removing parenthesized devtools return', () => {
|
|
582
|
+
const output = removeEmptySpace(
|
|
583
|
+
removeDevtools(
|
|
584
|
+
`
|
|
585
|
+
import { TanStackDevtools } from '@tanstack/react-devtools'
|
|
586
|
+
|
|
587
|
+
export function DevtoolsProvider() {
|
|
588
|
+
return (
|
|
589
|
+
<TanStackDevtools />
|
|
590
|
+
)
|
|
591
|
+
}
|
|
592
|
+
`,
|
|
593
|
+
'test.tsx',
|
|
594
|
+
)!.code,
|
|
595
|
+
)
|
|
596
|
+
|
|
597
|
+
expect(output).toBe(
|
|
598
|
+
removeEmptySpace(`
|
|
599
|
+
export function DevtoolsProvider() {
|
|
600
|
+
return (
|
|
601
|
+
null
|
|
602
|
+
)
|
|
603
|
+
}
|
|
604
|
+
`),
|
|
605
|
+
)
|
|
606
|
+
})
|
|
580
607
|
})
|
package/src/remove-devtools.ts
CHANGED
|
@@ -105,7 +105,7 @@ export function removeDevtools(code: string, id: string) {
|
|
|
105
105
|
if (devtoolsNames.size === 0) return
|
|
106
106
|
|
|
107
107
|
// Pass 2: Find and remove devtools JSX elements, collect plugin references
|
|
108
|
-
walk(result.program, (node) => {
|
|
108
|
+
walk(result.program, (node, parentNode) => {
|
|
109
109
|
if (node.type !== 'JSXElement') return
|
|
110
110
|
|
|
111
111
|
const opening = node.openingElement
|
|
@@ -130,7 +130,11 @@ export function removeDevtools(code: string, id: string) {
|
|
|
130
130
|
|
|
131
131
|
let end = node.end
|
|
132
132
|
if (code[end] === '\n') end++
|
|
133
|
-
|
|
133
|
+
if (parentNode?.type === 'ParenthesizedExpression') {
|
|
134
|
+
s.overwrite(node.start, end, 'null')
|
|
135
|
+
} else {
|
|
136
|
+
s.remove(node.start, end)
|
|
137
|
+
}
|
|
134
138
|
})
|
|
135
139
|
|
|
136
140
|
// Pass 3: Remove plugin imports that are no longer referenced
|