@tanstack/router-vite-plugin 1.39.3 → 1.39.4

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.
@@ -1,211 +0,0 @@
1
- // Copied from https://github.com/pcattori/vite-env-only/blob/main/src/dce.ts
2
- // Adapted with some minor changes for the purpose of this project
3
-
4
- import * as t from '@babel/types'
5
- import type { types as BabelTypes } from '@babel/core'
6
- import type { NodePath } from '@babel/traverse'
7
-
8
- type IdentifierPath = NodePath<BabelTypes.Identifier>
9
-
10
- /**
11
- * @param refs - If provided, only these identifiers will be considered for removal.
12
- */
13
- export const eliminateUnreferencedIdentifiers = (
14
- programPath: NodePath<BabelTypes.Program>,
15
- refs?: Set<IdentifierPath>,
16
- ) => {
17
- let referencesRemovedInThisPass: number
18
-
19
- const shouldBeRemoved = (ident: IdentifierPath) => {
20
- if (isIdentifierReferenced(ident)) return false
21
- if (!refs) return true
22
- return refs.has(ident)
23
- }
24
-
25
- const sweepFunction = (
26
- path: NodePath<
27
- | BabelTypes.FunctionDeclaration
28
- | BabelTypes.FunctionExpression
29
- | BabelTypes.ArrowFunctionExpression
30
- >,
31
- ) => {
32
- const identifier = getIdentifier(path)
33
- if (identifier?.node && shouldBeRemoved(identifier)) {
34
- ++referencesRemovedInThisPass
35
-
36
- if (
37
- t.isAssignmentExpression(path.parentPath.node) ||
38
- t.isVariableDeclarator(path.parentPath.node)
39
- ) {
40
- path.parentPath.remove()
41
- } else {
42
- path.remove()
43
- }
44
- }
45
- }
46
-
47
- const sweepImport = (
48
- path: NodePath<
49
- | BabelTypes.ImportSpecifier
50
- | BabelTypes.ImportDefaultSpecifier
51
- | BabelTypes.ImportNamespaceSpecifier
52
- >,
53
- ) => {
54
- const local = path.get('local')
55
- if (shouldBeRemoved(local)) {
56
- ++referencesRemovedInThisPass
57
- path.remove()
58
- if (
59
- (path.parent as BabelTypes.ImportDeclaration).specifiers.length === 0
60
- ) {
61
- path.parentPath.remove()
62
- }
63
- }
64
- }
65
-
66
- const handleObjectPattern = (pattern: NodePath<BabelTypes.ObjectPattern>) => {
67
- const properties = pattern.get('properties')
68
- properties.forEach((property) => {
69
- if (property.node.type === 'ObjectProperty') {
70
- const value = property.get('value') as any
71
- if (t.isIdentifier(value)) {
72
- if (shouldBeRemoved(value as any)) {
73
- property.remove()
74
- }
75
- } else if (t.isObjectPattern(value)) {
76
- handleObjectPattern(value as any)
77
- }
78
- } else if (t.isRestElement(property.node)) {
79
- const argument = property.get('argument')
80
- if (
81
- t.isIdentifier(argument as any) &&
82
- shouldBeRemoved(argument as NodePath<BabelTypes.Identifier>)
83
- ) {
84
- property.remove()
85
- }
86
- }
87
- })
88
- }
89
-
90
- // Traverse again to remove unused references. This happens at least once,
91
- // then repeats until no more references are removed.
92
- do {
93
- referencesRemovedInThisPass = 0
94
-
95
- programPath.scope.crawl()
96
-
97
- programPath.traverse({
98
- VariableDeclarator(path) {
99
- if (path.node.id.type === 'Identifier') {
100
- const local = path.get('id') as NodePath<BabelTypes.Identifier>
101
- if (shouldBeRemoved(local)) {
102
- ++referencesRemovedInThisPass
103
- path.remove()
104
- }
105
- } else if (path.node.id.type === 'ObjectPattern') {
106
- handleObjectPattern(
107
- path.get('id') as NodePath<BabelTypes.ObjectPattern>,
108
- )
109
- } else if (path.node.id.type === 'ArrayPattern') {
110
- const pattern = path.get('id') as NodePath<BabelTypes.ArrayPattern>
111
-
112
- let hasRemoved = false as boolean
113
-
114
- pattern.get('elements').forEach((element, index) => {
115
- // if (!element) return // Skip holes in the pattern
116
-
117
- let identifierPath: NodePath<BabelTypes.Identifier>
118
-
119
- if (t.isIdentifier(element.node)) {
120
- identifierPath = element as NodePath<BabelTypes.Identifier>
121
- } else if (t.isRestElement(element.node)) {
122
- identifierPath = element.get(
123
- 'argument',
124
- ) as NodePath<BabelTypes.Identifier>
125
- } else {
126
- // For now, ignore other types like AssignmentPattern
127
- return
128
- }
129
-
130
- if (shouldBeRemoved(identifierPath)) {
131
- hasRemoved = true
132
- pattern.node.elements[index] = null // Remove the element by setting it to null
133
- }
134
- })
135
-
136
- // If any elements were removed and no elements are left, remove the entire declaration
137
- if (
138
- hasRemoved &&
139
- pattern.node.elements.every((element) => element === null)
140
- ) {
141
- path.remove()
142
- ++referencesRemovedInThisPass
143
- }
144
- }
145
- },
146
- FunctionDeclaration: sweepFunction,
147
- FunctionExpression: sweepFunction,
148
- ArrowFunctionExpression: sweepFunction,
149
- ImportSpecifier: sweepImport,
150
- ImportDefaultSpecifier: sweepImport,
151
- ImportNamespaceSpecifier: sweepImport,
152
- })
153
- } while (referencesRemovedInThisPass)
154
- }
155
-
156
- function getIdentifier(
157
- path: NodePath<
158
- | BabelTypes.FunctionDeclaration
159
- | BabelTypes.FunctionExpression
160
- | BabelTypes.ArrowFunctionExpression
161
- >,
162
- ): NodePath<BabelTypes.Identifier> | null {
163
- const parentPath = path.parentPath
164
- if (parentPath.type === 'VariableDeclarator') {
165
- const variablePath = parentPath as NodePath<BabelTypes.VariableDeclarator>
166
- const name = variablePath.get('id')
167
- return name.node.type === 'Identifier'
168
- ? (name as NodePath<BabelTypes.Identifier>)
169
- : null
170
- }
171
-
172
- if (parentPath.type === 'AssignmentExpression') {
173
- const variablePath = parentPath as NodePath<BabelTypes.AssignmentExpression>
174
- const name = variablePath.get('left')
175
- return name.node.type === 'Identifier'
176
- ? (name as NodePath<BabelTypes.Identifier>)
177
- : null
178
- }
179
-
180
- if (path.node.type === 'ArrowFunctionExpression') {
181
- return null
182
- }
183
-
184
- if (path.node.type === 'FunctionExpression') {
185
- return null
186
- }
187
-
188
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
189
- return path.node.id && path.node.id.type === 'Identifier'
190
- ? (path.get('id') as NodePath<BabelTypes.Identifier>)
191
- : null
192
- }
193
-
194
- function isIdentifierReferenced(
195
- ident: NodePath<BabelTypes.Identifier>,
196
- ): boolean {
197
- const binding = ident.scope.getBinding(ident.node.name)
198
- if (binding?.referenced) {
199
- // Functions can reference themselves, so we need to check if there's a
200
- // binding outside the function scope or not.
201
- if (binding.path.type === 'FunctionDeclaration') {
202
- return !binding.constantViolations
203
- .concat(binding.referencePaths)
204
- // Check that every reference is contained within the function:
205
- .every((ref) => ref.findParent((parent) => parent === binding.path))
206
- }
207
-
208
- return true
209
- }
210
- return false
211
- }