@tanstack/devtools-vite 0.2.14 → 0.3.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.
@@ -144,7 +144,7 @@ const transformJSX = (
144
144
  element.node.attributes.push(
145
145
  t.jsxAttribute(
146
146
  t.jsxIdentifier('data-tsd-source'),
147
- t.stringLiteral(`${file}:${line}:${column}`),
147
+ t.stringLiteral(`${file}:${line}:${column + 1}`),
148
148
  ),
149
149
  )
150
150
 
@@ -232,6 +232,7 @@ export function addSourceToJsx(code: string, id: string) {
232
232
  }
233
233
  return gen(ast, {
234
234
  sourceMaps: true,
235
+ retainLines: true,
235
236
  filename: id,
236
237
  sourceFileName: filePath,
237
238
  })
package/src/plugin.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { normalizePath } from 'vite'
2
- import chalk from 'chalk'
3
1
  import { ServerEventBus } from '@tanstack/devtools-event-bus/server'
4
2
  import { handleDevToolsViteRequest } from './utils'
5
3
  import { DEFAULT_EDITOR_CONFIG, handleOpenSource } from './editor'
4
+ import { removeDevtools } from './remove-devtools'
6
5
  import { addSourceToJsx } from './inject-source'
6
+ import { enhanceConsoleLog } from './enhance-logs'
7
7
  import type { EditorConfig } from './editor'
8
8
  import type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'
9
9
  import type { Plugin } from 'vite'
@@ -27,6 +27,11 @@ export type TanStackDevtoolsViteConfig = {
27
27
  */
28
28
  enabled: boolean
29
29
  }
30
+ /**
31
+ * Whether to remove devtools from the production build.
32
+ * @default true
33
+ */
34
+ removeDevtoolsOnBuild?: boolean
30
35
  /**
31
36
  * Configuration for source injection.
32
37
  */
@@ -46,6 +51,7 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
46
51
  let port = 5173
47
52
  const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true }
48
53
  const injectSourceConfig = args?.injectSource ?? { enabled: true }
54
+ const removeDevtoolsOnBuild = args?.removeDevtoolsOnBuild ?? true
49
55
  const bus = new ServerEventBus(args?.eventBusConfig)
50
56
 
51
57
  return [
@@ -116,6 +122,24 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
116
122
  )
117
123
  },
118
124
  },
125
+ {
126
+ name: '@tanstack/devtools:remove-devtools-on-build',
127
+ apply(_, { command }) {
128
+ return command === 'build' && removeDevtoolsOnBuild
129
+ },
130
+ enforce: 'pre',
131
+ transform(code, id) {
132
+ if (
133
+ id.includes('node_modules') ||
134
+ id.includes('?raw') ||
135
+ id.includes('dist') ||
136
+ id.includes('build')
137
+ )
138
+ return code
139
+
140
+ return removeDevtools(code, id)
141
+ },
142
+ },
119
143
  {
120
144
  name: '@tanstack/devtools:better-console-logs',
121
145
  enforce: 'pre',
@@ -135,38 +159,7 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
135
159
  if (!code.includes('console.')) {
136
160
  return code
137
161
  }
138
- const lines = code.split('\n')
139
- return lines
140
- .map((line, lineNumber) => {
141
- if (
142
- line.trim().startsWith('//') ||
143
- line.trim().startsWith('/**') ||
144
- line.trim().startsWith('*')
145
- ) {
146
- return line
147
- }
148
- // Do not add for arrow functions or return statements
149
- if (
150
- line.replaceAll(' ', '').includes('=>console.') ||
151
- line.includes('return console.')
152
- ) {
153
- return line
154
- }
155
-
156
- const column = line.indexOf('console.')
157
- const location = `${id.replace(normalizePath(process.cwd()), '')}:${lineNumber + 1}:${column + 1}`
158
- 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 → '`
159
- if (line.includes('console.log(')) {
160
- const newLine = `console.log(${logMessage},`
161
- return line.replace('console.log(', newLine)
162
- }
163
- if (line.includes('console.error(')) {
164
- const newLine = `console.error(${logMessage},`
165
- return line.replace('console.error(', newLine)
166
- }
167
- return line
168
- })
169
- .join('\n')
162
+ return enhanceConsoleLog(code, id, port)
170
163
  },
171
164
  },
172
165
  ]
@@ -0,0 +1,230 @@
1
+ import { describe, expect, test } from 'vitest'
2
+ import { removeDevtools } from './remove-devtools'
3
+
4
+ const removeEmptySpace = (str: string) => {
5
+ return str.replace(/\s/g, '').trim()
6
+ }
7
+
8
+ describe('remove-devtools', () => {
9
+ test('it removes devtools if Imported directly', () => {
10
+ const output = removeEmptySpace(
11
+ removeDevtools(
12
+ `
13
+ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
14
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools'
15
+ import {
16
+ Link,
17
+ Outlet,
18
+ RouterProvider,
19
+ createRootRoute,
20
+ createRoute,
21
+ createRouter,
22
+ } from '@tanstack/react-router'
23
+ import { TanStackDevtools } from '@tanstack/react-devtools'
24
+
25
+
26
+
27
+ export default function DevtoolsExample() {
28
+ return (
29
+ <>
30
+ <TanStackDevtools
31
+ eventBusConfig={{
32
+ connectToServerBus: true,
33
+ }}
34
+ plugins={[
35
+ {
36
+ name: 'TanStack Query',
37
+ render: <ReactQueryDevtoolsPanel />,
38
+ },
39
+ {
40
+ name: 'TanStack Router',
41
+ render: <TanStackRouterDevtoolsPanel router={router} />,
42
+ },
43
+ /* {
44
+ name: "The actual app",
45
+ render: <iframe style={{ width: '100%', height: '100%' }} src="http://localhost:3005" />,
46
+ } */
47
+ ]}
48
+ />
49
+ <RouterProvider router={router} />
50
+ </>
51
+ )
52
+ }
53
+
54
+ `,
55
+ 'test.jsx',
56
+ ).code,
57
+ )
58
+ expect(output).toBe(
59
+ removeEmptySpace(`
60
+ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools';
61
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools';
62
+ import {
63
+ Link,
64
+ Outlet,
65
+ RouterProvider,
66
+ createRootRoute,
67
+ createRoute,
68
+ createRouter
69
+ } from '@tanstack/react-router';
70
+
71
+
72
+ export default function DevtoolsExample() {
73
+ return <>
74
+ <RouterProvider router={router} />
75
+ </>;
76
+
77
+ }
78
+
79
+ `),
80
+ )
81
+ })
82
+
83
+ test("it removes devtools if Imported and renamed with 'as' ", () => {
84
+ const output = removeEmptySpace(
85
+ removeDevtools(
86
+ `
87
+ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
88
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools'
89
+ import {
90
+ Link,
91
+ Outlet,
92
+ RouterProvider,
93
+ createRootRoute,
94
+ createRoute,
95
+ createRouter,
96
+ } from '@tanstack/react-router'
97
+ import { TanStackDevtools as Devtools } from '@tanstack/react-devtools'
98
+
99
+
100
+
101
+ export default function DevtoolsExample() {
102
+ return (
103
+ <>
104
+ <Devtools
105
+ eventBusConfig={{
106
+ connectToServerBus: true,
107
+ }}
108
+ plugins={[
109
+ {
110
+ name: 'TanStack Query',
111
+ render: <ReactQueryDevtoolsPanel />,
112
+ },
113
+ {
114
+ name: 'TanStack Router',
115
+ render: <TanStackRouterDevtoolsPanel router={router} />,
116
+ },
117
+ /* {
118
+ name: "The actual app",
119
+ render: <iframe style={{ width: '100%', height: '100%' }} src="http://localhost:3005" />,
120
+ } */
121
+ ]}
122
+ />
123
+ <RouterProvider router={router} />
124
+ </>
125
+ )
126
+ }
127
+
128
+ `,
129
+ 'test.jsx',
130
+ ).code,
131
+ )
132
+ expect(output).toBe(
133
+ removeEmptySpace(`
134
+ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools';
135
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools';
136
+ import {
137
+ Link,
138
+ Outlet,
139
+ RouterProvider,
140
+ createRootRoute,
141
+ createRoute,
142
+ createRouter
143
+ } from '@tanstack/react-router';
144
+
145
+
146
+ export default function DevtoolsExample() {
147
+ return <>
148
+ <RouterProvider router={router} />
149
+ </>;
150
+
151
+ }
152
+
153
+ `),
154
+ )
155
+ })
156
+
157
+ test('it removes devtools if Imported as * then used as a subcomponent ', () => {
158
+ const output = removeEmptySpace(
159
+ removeDevtools(
160
+ `
161
+ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
162
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools'
163
+ import {
164
+ Link,
165
+ Outlet,
166
+ RouterProvider,
167
+ createRootRoute,
168
+ createRoute,
169
+ createRouter,
170
+ } from '@tanstack/react-router'
171
+ import * as Tools from '@tanstack/react-devtools'
172
+
173
+
174
+
175
+ export default function DevtoolsExample() {
176
+ return (
177
+ <>
178
+ <Tools.TanStackDevtools
179
+ eventBusConfig={{
180
+ connectToServerBus: true,
181
+ }}
182
+ plugins={[
183
+ {
184
+ name: 'TanStack Query',
185
+ render: <ReactQueryDevtoolsPanel />,
186
+ },
187
+ {
188
+ name: 'TanStack Router',
189
+ render: <TanStackRouterDevtoolsPanel router={router} />,
190
+ },
191
+ /* {
192
+ name: "The actual app",
193
+ render: <iframe style={{ width: '100%', height: '100%' }} src="http://localhost:3005" />,
194
+ } */
195
+ ]}
196
+ />
197
+ <RouterProvider router={router} />
198
+ </>
199
+ )
200
+ }
201
+
202
+ `,
203
+ 'test.jsx',
204
+ ).code,
205
+ )
206
+ expect(output).toBe(
207
+ removeEmptySpace(`
208
+ import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools';
209
+ import { TanStackRouterDevtoolsPanel } from '@tanstack/react-router-devtools';
210
+ import {
211
+ Link,
212
+ Outlet,
213
+ RouterProvider,
214
+ createRootRoute,
215
+ createRoute,
216
+ createRouter
217
+ } from '@tanstack/react-router';
218
+
219
+
220
+ export default function DevtoolsExample() {
221
+ return <>
222
+ <RouterProvider router={router} />
223
+ </>;
224
+
225
+ }
226
+
227
+ `),
228
+ )
229
+ })
230
+ })
@@ -0,0 +1,74 @@
1
+ import { gen, parse, trav } from './babel'
2
+ import type { t } from './babel'
3
+ import type { types as Babel } from '@babel/core'
4
+ import type { ParseResult } from '@babel/parser'
5
+
6
+ const isTanStackDevtoolsImport = (source: string) =>
7
+ source === '@tanstack/react-devtools' ||
8
+ source === '@tanstack/devtools' ||
9
+ source === '@tanstack/solid-devtools'
10
+
11
+ const getImportedNames = (importDecl: t.ImportDeclaration) => {
12
+ return importDecl.specifiers.map((spec) => spec.local.name)
13
+ }
14
+
15
+ const transform = (ast: ParseResult<Babel.File>) => {
16
+ let didTransform = false
17
+ const devtoolsComponentNames = new Set()
18
+
19
+ trav(ast, {
20
+ ImportDeclaration(path) {
21
+ const importSource = path.node.source.value
22
+ if (isTanStackDevtoolsImport(importSource)) {
23
+ getImportedNames(path.node).forEach((name) =>
24
+ devtoolsComponentNames.add(name),
25
+ )
26
+ path.remove()
27
+ didTransform = true
28
+ }
29
+ },
30
+ JSXElement(path) {
31
+ const opening = path.node.openingElement
32
+ if (
33
+ opening.name.type === 'JSXIdentifier' &&
34
+ devtoolsComponentNames.has(opening.name.name)
35
+ ) {
36
+ path.remove()
37
+ didTransform = true
38
+ }
39
+
40
+ if (
41
+ opening.name.type === 'JSXMemberExpression' &&
42
+ opening.name.object.type === 'JSXIdentifier' &&
43
+ devtoolsComponentNames.has(opening.name.object.name)
44
+ ) {
45
+ path.remove()
46
+ didTransform = true
47
+ }
48
+ },
49
+ })
50
+
51
+ return didTransform
52
+ }
53
+
54
+ export function removeDevtools(code: string, id: string) {
55
+ const [filePath] = id.split('?')
56
+
57
+ try {
58
+ const ast = parse(code, {
59
+ sourceType: 'module',
60
+ plugins: ['jsx', 'typescript'],
61
+ })
62
+ const didTransform = transform(ast)
63
+ if (!didTransform) {
64
+ return { code }
65
+ }
66
+ return gen(ast, {
67
+ sourceMaps: true,
68
+ filename: id,
69
+ sourceFileName: filePath,
70
+ })
71
+ } catch (e) {
72
+ return { code }
73
+ }
74
+ }
package/src/utils.ts CHANGED
@@ -11,6 +11,7 @@ export const handleDevToolsViteRequest = (
11
11
  ) => {
12
12
  if (req.url?.includes('__tsd/open-source')) {
13
13
  const searchParams = new URLSearchParams(req.url.split('?')[1])
14
+
14
15
  const source = searchParams.get('source')
15
16
  if (!source) {
16
17
  return