@tanstack/devtools-vite 0.2.13 → 0.3.0
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.test.d.ts +1 -0
- package/dist/esm/plugin.d.ts +5 -0
- package/dist/esm/plugin.js +13 -12
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/remove-devtools.d.ts +3 -0
- package/dist/esm/remove-devtools.js +58 -0
- package/dist/esm/remove-devtools.js.map +1 -0
- package/dist/esm/remove-devtools.test.d.ts +1 -0
- package/package.json +2 -2
- package/src/inject-source.test.ts +1017 -0
- package/src/plugin.ts +24 -12
- package/src/remove-devtools.test.ts +230 -0
- package/src/remove-devtools.ts +74 -0
package/src/plugin.ts
CHANGED
|
@@ -3,6 +3,7 @@ import chalk from 'chalk'
|
|
|
3
3
|
import { ServerEventBus } from '@tanstack/devtools-event-bus/server'
|
|
4
4
|
import { handleDevToolsViteRequest } from './utils'
|
|
5
5
|
import { DEFAULT_EDITOR_CONFIG, handleOpenSource } from './editor'
|
|
6
|
+
import { removeDevtools } from './remove-devtools'
|
|
6
7
|
import { addSourceToJsx } from './inject-source'
|
|
7
8
|
import type { EditorConfig } from './editor'
|
|
8
9
|
import type { ServerEventBusConfig } from '@tanstack/devtools-event-bus/server'
|
|
@@ -27,6 +28,11 @@ export type TanStackDevtoolsViteConfig = {
|
|
|
27
28
|
*/
|
|
28
29
|
enabled: boolean
|
|
29
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Whether to remove devtools from the production build.
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
removeDevtoolsOnBuild?: boolean
|
|
30
36
|
/**
|
|
31
37
|
* Configuration for source injection.
|
|
32
38
|
*/
|
|
@@ -44,9 +50,9 @@ export const defineDevtoolsConfig = (config: TanStackDevtoolsViteConfig) =>
|
|
|
44
50
|
|
|
45
51
|
export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
46
52
|
let port = 5173
|
|
47
|
-
let host = 'http'
|
|
48
53
|
const enhancedLogsConfig = args?.enhancedLogs ?? { enabled: true }
|
|
49
54
|
const injectSourceConfig = args?.injectSource ?? { enabled: true }
|
|
55
|
+
const removeDevtoolsOnBuild = args?.removeDevtoolsOnBuild ?? true
|
|
50
56
|
const bus = new ServerEventBus(args?.eventBusConfig)
|
|
51
57
|
|
|
52
58
|
return [
|
|
@@ -71,9 +77,6 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
71
77
|
{
|
|
72
78
|
enforce: 'pre',
|
|
73
79
|
name: '@tanstack/devtools:custom-server',
|
|
74
|
-
configResolved(config) {
|
|
75
|
-
host = config.server.https?.cert ? 'https' : 'http'
|
|
76
|
-
},
|
|
77
80
|
apply(config) {
|
|
78
81
|
// Custom server is only needed in development for piping events to the client
|
|
79
82
|
return config.mode === 'development'
|
|
@@ -119,14 +122,23 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
119
122
|
}),
|
|
120
123
|
)
|
|
121
124
|
},
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: '@tanstack/devtools:remove-devtools-on-build',
|
|
128
|
+
apply(_, { command }) {
|
|
129
|
+
return command === 'build' && removeDevtoolsOnBuild
|
|
130
|
+
},
|
|
131
|
+
enforce: 'pre',
|
|
132
|
+
transform(code, id) {
|
|
133
|
+
if (
|
|
134
|
+
id.includes('node_modules') ||
|
|
135
|
+
id.includes('?raw') ||
|
|
136
|
+
id.includes('dist') ||
|
|
137
|
+
id.includes('build')
|
|
138
|
+
)
|
|
139
|
+
return code
|
|
140
|
+
|
|
141
|
+
return removeDevtools(code, id)
|
|
130
142
|
},
|
|
131
143
|
},
|
|
132
144
|
{
|
|
@@ -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
|
+
}
|