@tanstack/devtools-vite 0.6.1 → 0.7.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/ast-utils.d.ts +11 -0
- package/dist/esm/ast-utils.js +44 -0
- package/dist/esm/ast-utils.js.map +1 -0
- package/dist/esm/devtools-packages.d.ts +2 -0
- package/dist/esm/devtools-packages.js +15 -0
- package/dist/esm/devtools-packages.js.map +1 -0
- package/dist/esm/editor.js +1 -1
- package/dist/esm/editor.js.map +1 -1
- package/dist/esm/enhance-logs.d.ts +4 -1
- package/dist/esm/enhance-logs.js +35 -36
- package/dist/esm/enhance-logs.js.map +1 -1
- package/dist/esm/inject-plugin.d.ts +5 -4
- package/dist/esm/inject-plugin.js +114 -103
- package/dist/esm/inject-plugin.js.map +1 -1
- package/dist/esm/inject-source.d.ts +4 -1
- package/dist/esm/inject-source.js +92 -93
- package/dist/esm/inject-source.js.map +1 -1
- package/dist/esm/offset-to-loc.d.ts +14 -0
- package/dist/esm/offset-to-loc.js +29 -0
- package/dist/esm/offset-to-loc.js.map +1 -0
- package/dist/esm/plugin.js +23 -28
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/remove-devtools.d.ts +4 -1
- package/dist/esm/remove-devtools.js +79 -86
- package/dist/esm/remove-devtools.js.map +1 -1
- package/package.json +3 -9
- package/skills/devtools-vite-plugin/SKILL.md +7 -7
- package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
- package/src/ast-utils.ts +54 -0
- package/src/devtools-packages.ts +12 -0
- package/src/editor.ts +1 -1
- package/src/enhance-logs.ts +48 -78
- package/src/inject-plugin.test.ts +200 -147
- package/src/inject-plugin.ts +195 -238
- package/src/inject-source.test.ts +165 -115
- package/src/inject-source.ts +183 -231
- package/src/offset-to-loc.ts +39 -0
- package/src/plugin.ts +26 -28
- package/src/remove-devtools.test.ts +76 -57
- package/src/remove-devtools.ts +160 -168
- package/dist/esm/babel.d.ts +0 -18
- package/dist/esm/babel.js +0 -11
- package/dist/esm/babel.js.map +0 -1
- package/src/babel.ts +0 -18
|
@@ -57,23 +57,22 @@ export default function DevtoolsExample() {
|
|
|
57
57
|
)
|
|
58
58
|
expect(output).toBe(
|
|
59
59
|
removeEmptySpace(`
|
|
60
|
-
|
|
60
|
+
import {
|
|
61
61
|
Link,
|
|
62
62
|
Outlet,
|
|
63
63
|
RouterProvider,
|
|
64
64
|
createRootRoute,
|
|
65
65
|
createRoute,
|
|
66
|
-
createRouter
|
|
67
|
-
} from '@tanstack/react-router'
|
|
66
|
+
createRouter,
|
|
67
|
+
} from '@tanstack/react-router'
|
|
68
68
|
|
|
69
|
-
|
|
70
69
|
export default function DevtoolsExample() {
|
|
71
|
-
return
|
|
70
|
+
return (
|
|
71
|
+
<>
|
|
72
72
|
<RouterProvider router={router} />
|
|
73
|
-
</>
|
|
74
|
-
|
|
73
|
+
</>
|
|
74
|
+
)
|
|
75
75
|
}
|
|
76
|
-
|
|
77
76
|
`),
|
|
78
77
|
)
|
|
79
78
|
})
|
|
@@ -93,9 +92,9 @@ import {
|
|
|
93
92
|
createRouter,
|
|
94
93
|
} from '@tanstack/react-router'
|
|
95
94
|
import { TanStackDevtools as Devtools } from '@tanstack/react-devtools'
|
|
96
|
-
|
|
97
95
|
|
|
98
|
-
|
|
96
|
+
|
|
97
|
+
|
|
99
98
|
export default function DevtoolsExample() {
|
|
100
99
|
return (
|
|
101
100
|
<>
|
|
@@ -129,23 +128,22 @@ export default function DevtoolsExample() {
|
|
|
129
128
|
)
|
|
130
129
|
expect(output).toBe(
|
|
131
130
|
removeEmptySpace(`
|
|
132
|
-
|
|
131
|
+
import {
|
|
133
132
|
Link,
|
|
134
133
|
Outlet,
|
|
135
134
|
RouterProvider,
|
|
136
135
|
createRootRoute,
|
|
137
136
|
createRoute,
|
|
138
|
-
createRouter
|
|
139
|
-
} from '@tanstack/react-router'
|
|
137
|
+
createRouter,
|
|
138
|
+
} from '@tanstack/react-router'
|
|
140
139
|
|
|
141
|
-
|
|
142
140
|
export default function DevtoolsExample() {
|
|
143
|
-
return (
|
|
141
|
+
return (
|
|
142
|
+
<>
|
|
144
143
|
<RouterProvider router={router} />
|
|
145
|
-
</>
|
|
146
|
-
|
|
144
|
+
</>
|
|
145
|
+
)
|
|
147
146
|
}
|
|
148
|
-
|
|
149
147
|
`),
|
|
150
148
|
)
|
|
151
149
|
})
|
|
@@ -165,9 +163,9 @@ import {
|
|
|
165
163
|
createRouter,
|
|
166
164
|
} from '@tanstack/react-router'
|
|
167
165
|
import * as Tools from '@tanstack/react-devtools'
|
|
168
|
-
|
|
169
166
|
|
|
170
|
-
|
|
167
|
+
|
|
168
|
+
|
|
171
169
|
export default function DevtoolsExample() {
|
|
172
170
|
return (
|
|
173
171
|
<>
|
|
@@ -201,23 +199,22 @@ export default function DevtoolsExample() {
|
|
|
201
199
|
)
|
|
202
200
|
expect(output).toBe(
|
|
203
201
|
removeEmptySpace(`
|
|
204
|
-
|
|
202
|
+
import {
|
|
205
203
|
Link,
|
|
206
204
|
Outlet,
|
|
207
205
|
RouterProvider,
|
|
208
206
|
createRootRoute,
|
|
209
207
|
createRoute,
|
|
210
|
-
createRouter
|
|
211
|
-
} from '@tanstack/react-router'
|
|
208
|
+
createRouter,
|
|
209
|
+
} from '@tanstack/react-router'
|
|
212
210
|
|
|
213
|
-
|
|
214
211
|
export default function DevtoolsExample() {
|
|
215
|
-
return
|
|
212
|
+
return (
|
|
213
|
+
<>
|
|
216
214
|
<RouterProvider router={router} />
|
|
217
|
-
</>
|
|
218
|
-
|
|
215
|
+
</>
|
|
216
|
+
)
|
|
219
217
|
}
|
|
220
|
-
|
|
221
218
|
`),
|
|
222
219
|
)
|
|
223
220
|
})
|
|
@@ -272,24 +269,22 @@ export default function DevtoolsExample() {
|
|
|
272
269
|
)
|
|
273
270
|
|
|
274
271
|
expect(output).toBe(
|
|
275
|
-
removeEmptySpace(`
|
|
272
|
+
removeEmptySpace(`
|
|
276
273
|
import {
|
|
277
274
|
Link,
|
|
278
275
|
Outlet,
|
|
279
276
|
RouterProvider,
|
|
280
277
|
createRootRoute,
|
|
281
278
|
createRoute,
|
|
282
|
-
createRouter
|
|
283
|
-
} from '@tanstack/react-router'
|
|
284
|
-
|
|
279
|
+
createRouter,
|
|
280
|
+
} from '@tanstack/react-router'
|
|
285
281
|
|
|
286
|
-
|
|
287
282
|
export default function DevtoolsExample() {
|
|
288
283
|
return (
|
|
289
|
-
<>
|
|
284
|
+
<>
|
|
290
285
|
<RouterProvider router={router} />
|
|
291
286
|
</>
|
|
292
|
-
)
|
|
287
|
+
)
|
|
293
288
|
}
|
|
294
289
|
`),
|
|
295
290
|
)
|
|
@@ -329,15 +324,15 @@ export default function DevtoolsExample() {
|
|
|
329
324
|
)
|
|
330
325
|
|
|
331
326
|
expect(output).toBe(
|
|
332
|
-
removeEmptySpace(`
|
|
333
|
-
import { test } from '@tanstack/react-query-devtools'
|
|
334
|
-
|
|
327
|
+
removeEmptySpace(`
|
|
328
|
+
import { test } from '@tanstack/react-query-devtools'
|
|
329
|
+
|
|
335
330
|
export default function DevtoolsExample() {
|
|
336
331
|
return (
|
|
337
|
-
<>
|
|
332
|
+
<>
|
|
338
333
|
<RouterProvider router={router} />
|
|
339
334
|
</>
|
|
340
|
-
)
|
|
335
|
+
)
|
|
341
336
|
}
|
|
342
337
|
`),
|
|
343
338
|
)
|
|
@@ -376,15 +371,15 @@ export default function DevtoolsExample() {
|
|
|
376
371
|
)
|
|
377
372
|
|
|
378
373
|
expect(output).toBe(
|
|
379
|
-
removeEmptySpace(`
|
|
380
|
-
import * as Stuff from '@tanstack/react-query-devtools'
|
|
381
|
-
|
|
374
|
+
removeEmptySpace(`
|
|
375
|
+
import * as Stuff from '@tanstack/react-query-devtools'
|
|
376
|
+
|
|
382
377
|
export default function DevtoolsExample() {
|
|
383
378
|
return (
|
|
384
|
-
<>
|
|
379
|
+
<>
|
|
385
380
|
<RouterProvider router={router} />
|
|
386
381
|
</>
|
|
387
|
-
)
|
|
382
|
+
)
|
|
388
383
|
}
|
|
389
384
|
`),
|
|
390
385
|
)
|
|
@@ -420,13 +415,13 @@ export default function DevtoolsExample() {
|
|
|
420
415
|
)
|
|
421
416
|
|
|
422
417
|
expect(output).toBe(
|
|
423
|
-
removeEmptySpace(`
|
|
418
|
+
removeEmptySpace(`
|
|
424
419
|
export default function DevtoolsExample() {
|
|
425
420
|
return (
|
|
426
|
-
<>
|
|
421
|
+
<>
|
|
427
422
|
<RouterProvider router={router} />
|
|
428
423
|
</>
|
|
429
|
-
)
|
|
424
|
+
)
|
|
430
425
|
}
|
|
431
426
|
`),
|
|
432
427
|
)
|
|
@@ -462,13 +457,13 @@ export default function DevtoolsExample() {
|
|
|
462
457
|
)
|
|
463
458
|
|
|
464
459
|
expect(output).toBe(
|
|
465
|
-
removeEmptySpace(`
|
|
460
|
+
removeEmptySpace(`
|
|
466
461
|
export default function DevtoolsExample() {
|
|
467
462
|
return (
|
|
468
|
-
<>
|
|
463
|
+
<>
|
|
469
464
|
<RouterProvider router={router} />
|
|
470
465
|
</>
|
|
471
|
-
)
|
|
466
|
+
)
|
|
472
467
|
}
|
|
473
468
|
`),
|
|
474
469
|
)
|
|
@@ -504,13 +499,13 @@ export default function DevtoolsExample() {
|
|
|
504
499
|
)
|
|
505
500
|
|
|
506
501
|
expect(output).toBe(
|
|
507
|
-
removeEmptySpace(`
|
|
502
|
+
removeEmptySpace(`
|
|
508
503
|
export default function DevtoolsExample() {
|
|
509
504
|
return (
|
|
510
|
-
<>
|
|
505
|
+
<>
|
|
511
506
|
<RouterProvider router={router} />
|
|
512
507
|
</>
|
|
513
|
-
)
|
|
508
|
+
)
|
|
514
509
|
}
|
|
515
510
|
`),
|
|
516
511
|
)
|
|
@@ -546,16 +541,40 @@ export default function DevtoolsExample() {
|
|
|
546
541
|
)
|
|
547
542
|
|
|
548
543
|
expect(output).toBe(
|
|
549
|
-
removeEmptySpace(`
|
|
544
|
+
removeEmptySpace(`
|
|
550
545
|
export default function DevtoolsExample() {
|
|
551
546
|
return (
|
|
552
|
-
<>
|
|
547
|
+
<>
|
|
553
548
|
<RouterProvider router={router} />
|
|
554
549
|
</>
|
|
555
|
-
)
|
|
550
|
+
)
|
|
556
551
|
}
|
|
557
552
|
`),
|
|
558
553
|
)
|
|
559
554
|
})
|
|
560
555
|
})
|
|
556
|
+
|
|
557
|
+
describe('framework devtools package coverage', () => {
|
|
558
|
+
test.each([
|
|
559
|
+
['@tanstack/preact-devtools'],
|
|
560
|
+
['@tanstack/vue-devtools'],
|
|
561
|
+
['@tanstack/svelte-devtools'],
|
|
562
|
+
['@tanstack/angular-devtools'],
|
|
563
|
+
])('strips imports + JSX from %s', (pkg) => {
|
|
564
|
+
const output = removeDevtools(
|
|
565
|
+
`
|
|
566
|
+
import { TanStackDevtools } from '${pkg}'
|
|
567
|
+
|
|
568
|
+
export default function App() {
|
|
569
|
+
return <TanStackDevtools />
|
|
570
|
+
}
|
|
571
|
+
`,
|
|
572
|
+
'test.jsx',
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
expect(output).toBeDefined()
|
|
576
|
+
expect(output!.code).not.toContain(pkg)
|
|
577
|
+
expect(output!.code).not.toContain('TanStackDevtools')
|
|
578
|
+
})
|
|
579
|
+
})
|
|
561
580
|
})
|
package/src/remove-devtools.ts
CHANGED
|
@@ -1,193 +1,185 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import MagicString from 'magic-string'
|
|
2
|
+
import { parseSync } from 'oxc-parser'
|
|
3
|
+
import { walk } from './ast-utils'
|
|
4
|
+
import { isTanStackDevtoolsImport } from './devtools-packages'
|
|
5
|
+
import type { JSXOpeningElement } from 'oxc-parser'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Extract component names referenced in the `plugins` prop of a devtools element.
|
|
9
|
+
*/
|
|
10
|
+
function getPluginReferences(opening: JSXOpeningElement): Array<string> {
|
|
11
|
+
const refs: Array<string> = []
|
|
12
|
+
|
|
13
|
+
for (const attr of opening.attributes) {
|
|
14
|
+
if (attr.type !== 'JSXAttribute') continue
|
|
15
|
+
if (attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'plugins')
|
|
16
|
+
continue
|
|
17
|
+
if (
|
|
18
|
+
!attr.value ||
|
|
19
|
+
attr.value.type !== 'JSXExpressionContainer' ||
|
|
20
|
+
attr.value.expression.type !== 'ArrayExpression'
|
|
21
|
+
)
|
|
22
|
+
continue
|
|
23
|
+
|
|
24
|
+
for (const el of attr.value.expression.elements) {
|
|
25
|
+
if (!el || el.type !== 'ObjectExpression') continue
|
|
26
|
+
|
|
27
|
+
for (const prop of el.properties) {
|
|
28
|
+
if (
|
|
29
|
+
prop.type !== 'Property' ||
|
|
30
|
+
prop.key.type !== 'Identifier' ||
|
|
31
|
+
prop.key.name !== 'render'
|
|
32
|
+
)
|
|
33
|
+
continue
|
|
14
34
|
|
|
15
|
-
const
|
|
16
|
-
const finalReferences: Array<string> = []
|
|
17
|
-
node.traverse({
|
|
18
|
-
JSXAttribute(path) {
|
|
19
|
-
const node = path.node
|
|
20
|
-
const propName =
|
|
21
|
-
typeof node.name.name === 'string'
|
|
22
|
-
? node.name.name
|
|
23
|
-
: node.name.name.name
|
|
35
|
+
const value = prop.value
|
|
24
36
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
// handle <ReactRouterPanel />
|
|
45
|
-
if (
|
|
46
|
-
value.type === 'JSXElement' &&
|
|
47
|
-
value.openingElement.name.type === 'JSXIdentifier'
|
|
48
|
-
) {
|
|
49
|
-
const elementName = value.openingElement.name.name
|
|
50
|
-
return elementName
|
|
51
|
-
}
|
|
52
|
-
// handle () => <ReactRouterPanel /> or function() { return <ReactRouterPanel /> }
|
|
53
|
-
if (
|
|
54
|
-
value.type === 'ArrowFunctionExpression' ||
|
|
55
|
-
value.type === 'FunctionExpression'
|
|
56
|
-
) {
|
|
57
|
-
const body = value.body
|
|
58
|
-
if (
|
|
59
|
-
body.type === 'JSXElement' &&
|
|
60
|
-
body.openingElement.name.type === 'JSXIdentifier'
|
|
61
|
-
) {
|
|
62
|
-
const elementName = body.openingElement.name.name
|
|
63
|
-
return elementName
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
// handle render: SomeComponent
|
|
67
|
-
if (value.type === 'Identifier') {
|
|
68
|
-
const elementName = value.name
|
|
69
|
-
return elementName
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// handle render: someFunction()
|
|
73
|
-
if (
|
|
74
|
-
value.type === 'CallExpression' &&
|
|
75
|
-
value.callee.type === 'Identifier'
|
|
76
|
-
) {
|
|
77
|
-
const elementName = value.callee.name
|
|
78
|
-
return elementName
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return ''
|
|
82
|
-
}
|
|
83
|
-
return ''
|
|
84
|
-
})
|
|
85
|
-
.filter(Boolean)
|
|
86
|
-
finalReferences.push(...referencesToRemove)
|
|
37
|
+
// handle <ReactRouterPanel />
|
|
38
|
+
if (
|
|
39
|
+
value.type === 'JSXElement' &&
|
|
40
|
+
value.openingElement.name.type === 'JSXIdentifier'
|
|
41
|
+
) {
|
|
42
|
+
refs.push(value.openingElement.name.name)
|
|
43
|
+
}
|
|
44
|
+
// handle () => <ReactRouterPanel />
|
|
45
|
+
else if (
|
|
46
|
+
value.type === 'ArrowFunctionExpression' ||
|
|
47
|
+
value.type === 'FunctionExpression'
|
|
48
|
+
) {
|
|
49
|
+
const body = value.body
|
|
50
|
+
if (
|
|
51
|
+
body &&
|
|
52
|
+
body.type === 'JSXElement' &&
|
|
53
|
+
body.openingElement.name.type === 'JSXIdentifier'
|
|
54
|
+
) {
|
|
55
|
+
refs.push(body.openingElement.name.name)
|
|
87
56
|
}
|
|
88
|
-
}
|
|
57
|
+
}
|
|
58
|
+
// handle render: SomeComponent
|
|
59
|
+
else if (value.type === 'Identifier') {
|
|
60
|
+
refs.push(value.name)
|
|
61
|
+
}
|
|
62
|
+
// handle render: someFunction()
|
|
63
|
+
else if (
|
|
64
|
+
value.type === 'CallExpression' &&
|
|
65
|
+
value.callee.type === 'Identifier'
|
|
66
|
+
) {
|
|
67
|
+
refs.push(value.callee.name)
|
|
68
|
+
}
|
|
89
69
|
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return finalReferences
|
|
93
|
-
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
94
72
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
const devtoolsComponentNames = new Set()
|
|
98
|
-
const finalReferences: Array<string> = []
|
|
73
|
+
return refs
|
|
74
|
+
}
|
|
99
75
|
|
|
100
|
-
|
|
76
|
+
export function removeDevtools(code: string, id: string) {
|
|
77
|
+
const filePath = id.split('?')[0]!
|
|
101
78
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
)
|
|
79
|
+
try {
|
|
80
|
+
const result = parseSync(filePath, code, {
|
|
81
|
+
sourceType: 'module',
|
|
82
|
+
lang: 'tsx',
|
|
83
|
+
})
|
|
84
|
+
if (result.errors.length > 0) return
|
|
109
85
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
86
|
+
const s = new MagicString(code)
|
|
87
|
+
const devtoolsNames = new Set<string>()
|
|
88
|
+
const pluginRefs: Array<string> = []
|
|
113
89
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
},
|
|
117
|
-
JSXElement(path) {
|
|
118
|
-
const opening = path.node.openingElement
|
|
90
|
+
// Pass 1: Collect devtools import names and mark for removal
|
|
91
|
+
walk(result.program, (node) => {
|
|
119
92
|
if (
|
|
120
|
-
|
|
121
|
-
|
|
93
|
+
node.type === 'ImportDeclaration' &&
|
|
94
|
+
isTanStackDevtoolsImport(node.source.value)
|
|
122
95
|
) {
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
didTransform = true
|
|
96
|
+
for (const spec of node.specifiers) {
|
|
97
|
+
devtoolsNames.add(spec.local.name)
|
|
98
|
+
}
|
|
99
|
+
let end = node.end
|
|
100
|
+
if (code[end] === '\n') end++
|
|
101
|
+
s.remove(node.start, end)
|
|
130
102
|
}
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
if (devtoolsNames.size === 0) return
|
|
106
|
+
|
|
107
|
+
// Pass 2: Find and remove devtools JSX elements, collect plugin references
|
|
108
|
+
walk(result.program, (node) => {
|
|
109
|
+
if (node.type !== 'JSXElement') return
|
|
110
|
+
|
|
111
|
+
const opening = node.openingElement
|
|
112
|
+
let matches = false
|
|
113
|
+
|
|
131
114
|
if (
|
|
115
|
+
opening.name.type === 'JSXIdentifier' &&
|
|
116
|
+
devtoolsNames.has(opening.name.name)
|
|
117
|
+
) {
|
|
118
|
+
matches = true
|
|
119
|
+
} else if (
|
|
132
120
|
opening.name.type === 'JSXMemberExpression' &&
|
|
133
121
|
opening.name.object.type === 'JSXIdentifier' &&
|
|
134
|
-
|
|
122
|
+
devtoolsNames.has(opening.name.object.name)
|
|
135
123
|
) {
|
|
136
|
-
|
|
137
|
-
finalReferences.push(...refs)
|
|
138
|
-
transformations.push(() => {
|
|
139
|
-
path.remove()
|
|
140
|
-
})
|
|
141
|
-
didTransform = true
|
|
142
|
-
}
|
|
143
|
-
},
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
trav(ast, {
|
|
147
|
-
ImportDeclaration(path) {
|
|
148
|
-
const imports = path.node.specifiers
|
|
149
|
-
for (const imported of imports) {
|
|
150
|
-
if (imported.type === 'ImportSpecifier') {
|
|
151
|
-
if (finalReferences.includes(imported.local.name)) {
|
|
152
|
-
transformations.push(() => {
|
|
153
|
-
// remove the specifier
|
|
154
|
-
path.node.specifiers = path.node.specifiers.filter(
|
|
155
|
-
(spec) => spec !== imported,
|
|
156
|
-
)
|
|
157
|
-
// remove whole import if nothing is left
|
|
158
|
-
if (path.node.specifiers.length === 0) {
|
|
159
|
-
path.remove()
|
|
160
|
-
}
|
|
161
|
-
})
|
|
162
|
-
}
|
|
163
|
-
}
|
|
124
|
+
matches = true
|
|
164
125
|
}
|
|
165
|
-
},
|
|
166
|
-
})
|
|
167
|
-
|
|
168
|
-
transformations.forEach((fn) => fn())
|
|
169
126
|
|
|
170
|
-
|
|
171
|
-
}
|
|
127
|
+
if (!matches) return
|
|
172
128
|
|
|
173
|
-
|
|
174
|
-
const [filePath] = id.split('?')
|
|
129
|
+
pluginRefs.push(...getPluginReferences(opening))
|
|
175
130
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
plugins: ['jsx', 'typescript'],
|
|
131
|
+
let end = node.end
|
|
132
|
+
if (code[end] === '\n') end++
|
|
133
|
+
s.remove(node.start, end)
|
|
180
134
|
})
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
135
|
+
|
|
136
|
+
// Pass 3: Remove plugin imports that are no longer referenced
|
|
137
|
+
if (pluginRefs.length > 0) {
|
|
138
|
+
walk(result.program, (node) => {
|
|
139
|
+
if (node.type !== 'ImportDeclaration') return
|
|
140
|
+
if (isTanStackDevtoolsImport(node.source.value)) return
|
|
141
|
+
|
|
142
|
+
const toRemove = node.specifiers.filter(
|
|
143
|
+
(spec) =>
|
|
144
|
+
spec.type === 'ImportSpecifier' &&
|
|
145
|
+
pluginRefs.includes(spec.local.name),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
if (toRemove.length === 0) return
|
|
149
|
+
|
|
150
|
+
const remaining = node.specifiers.filter(
|
|
151
|
+
(spec) => !toRemove.includes(spec),
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
if (remaining.length === 0) {
|
|
155
|
+
let end = node.end
|
|
156
|
+
if (code[end] === '\n') end++
|
|
157
|
+
s.remove(node.start, end)
|
|
158
|
+
} else {
|
|
159
|
+
// Rebuild import with remaining specifiers
|
|
160
|
+
const specTexts = remaining.map((spec) =>
|
|
161
|
+
code.slice(spec.start, spec.end),
|
|
162
|
+
)
|
|
163
|
+
const sourceText = code.slice(node.source.start, node.source.end)
|
|
164
|
+
s.overwrite(
|
|
165
|
+
node.start,
|
|
166
|
+
node.end,
|
|
167
|
+
`import { ${specTexts.join(', ')} } from ${sourceText}`,
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (!s.hasChanged()) return
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
code: s.toString(),
|
|
177
|
+
map: s.generateMap({
|
|
178
|
+
source: filePath,
|
|
179
|
+
file: id,
|
|
180
|
+
includeContent: true,
|
|
181
|
+
}),
|
|
184
182
|
}
|
|
185
|
-
return gen(ast, {
|
|
186
|
-
sourceMaps: true,
|
|
187
|
-
retainLines: true,
|
|
188
|
-
filename: id,
|
|
189
|
-
sourceFileName: filePath,
|
|
190
|
-
})
|
|
191
183
|
} catch (e) {
|
|
192
184
|
return
|
|
193
185
|
}
|
package/dist/esm/babel.d.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { parse } from '@babel/parser';
|
|
2
|
-
import { default as generate } from '@babel/generator';
|
|
3
|
-
import * as t from '@babel/types';
|
|
4
|
-
export { parse, t };
|
|
5
|
-
export declare const trav: {
|
|
6
|
-
<S>(parent: t.Node, opts: import('@babel/traverse').TraverseOptions<S>, scope: import('@babel/traverse').Scope | undefined, state: S, parentPath?: import('@babel/traverse').NodePath): void;
|
|
7
|
-
(parent: t.Node, opts?: import('@babel/traverse').TraverseOptions, scope?: import('@babel/traverse').Scope, state?: any, parentPath?: import('@babel/traverse').NodePath): void;
|
|
8
|
-
visitors: typeof import('@babel/traverse').visitors;
|
|
9
|
-
verify: typeof import("@babel/traverse").visitors.verify;
|
|
10
|
-
explode: typeof import("@babel/traverse").visitors.explode;
|
|
11
|
-
cheap: (node: t.Node, enter: (node: t.Node) => void) => void;
|
|
12
|
-
node: (node: t.Node, opts: import('@babel/traverse').TraverseOptions, scope?: import('@babel/traverse').Scope, state?: any, path?: import('@babel/traverse').NodePath, skipKeys?: Record<string, boolean>) => void;
|
|
13
|
-
clearNode: (node: t.Node, opts?: t.RemovePropertiesOptions) => void;
|
|
14
|
-
removeProperties: (tree: t.Node, opts?: t.RemovePropertiesOptions) => t.Node;
|
|
15
|
-
hasType: (tree: t.Node, type: t.Node["type"], denylistTypes?: string[]) => boolean;
|
|
16
|
-
cache: typeof import('@babel/traverse').cache;
|
|
17
|
-
};
|
|
18
|
-
export declare const gen: typeof generate;
|
package/dist/esm/babel.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { parse } from "@babel/parser";
|
|
2
|
-
import * as t from "@babel/types";
|
|
3
|
-
import generate from "@babel/generator";
|
|
4
|
-
import traverse from "@babel/traverse";
|
|
5
|
-
//#region src/babel.ts
|
|
6
|
-
var trav = typeof traverse.default !== "undefined" ? traverse.default : traverse;
|
|
7
|
-
var gen = typeof generate.default !== "undefined" ? generate.default : generate;
|
|
8
|
-
//#endregion
|
|
9
|
-
export { gen, parse, t, trav };
|
|
10
|
-
|
|
11
|
-
//# sourceMappingURL=babel.js.map
|
package/dist/esm/babel.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"babel.js","names":[],"sources":["../../src/babel.ts"],"sourcesContent":["import { parse } from '@babel/parser'\nimport * as t from '@babel/types'\nimport generate from '@babel/generator'\nimport traverse from '@babel/traverse'\n\nexport { parse, t }\n\nexport const trav =\n typeof (traverse as any).default !== 'undefined'\n ? // eslint-disable-next-line @typescript-eslint/consistent-type-imports\n ((traverse as any).default as typeof import('@babel/traverse').default)\n : traverse\n\nexport const gen =\n typeof (generate as any).default !== 'undefined'\n ? // eslint-disable-next-line @typescript-eslint/consistent-type-imports\n ((generate as any).default as typeof import('@babel/generator').default)\n : generate\n"],"mappings":";;;;;AAOA,IAAa,OACX,OAAQ,SAAiB,YAAY,cAE/B,SAAiB,UACnB;AAEN,IAAa,MACX,OAAQ,SAAiB,YAAY,cAE/B,SAAiB,UACnB"}
|
package/src/babel.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { parse } from '@babel/parser'
|
|
2
|
-
import * as t from '@babel/types'
|
|
3
|
-
import generate from '@babel/generator'
|
|
4
|
-
import traverse from '@babel/traverse'
|
|
5
|
-
|
|
6
|
-
export { parse, t }
|
|
7
|
-
|
|
8
|
-
export const trav =
|
|
9
|
-
typeof (traverse as any).default !== 'undefined'
|
|
10
|
-
? // eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
|
11
|
-
((traverse as any).default as typeof import('@babel/traverse').default)
|
|
12
|
-
: traverse
|
|
13
|
-
|
|
14
|
-
export const gen =
|
|
15
|
-
typeof (generate as any).default !== 'undefined'
|
|
16
|
-
? // eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
|
17
|
-
((generate as any).default as typeof import('@babel/generator').default)
|
|
18
|
-
: generate
|