@tanstack/router-plugin 1.168.22 → 1.168.24-pre.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/cjs/core/code-splitter/compilers.cjs +39 -32
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +3 -2
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs +10 -12
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins/framework-plugins.d.cts +3 -4
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs +34 -16
- package/dist/cjs/core/code-splitter/plugins/react-refresh-route-components.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/plugins.d.cts +8 -1
- package/dist/cjs/core/config.cjs.map +1 -1
- package/dist/cjs/core/hmr/handle-route-update.cjs +2 -67
- package/dist/cjs/core/hmr/handle-route-update.cjs.map +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +20 -11
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +1 -2
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -1
- package/dist/esm/core/code-splitter/compilers.d.ts +3 -2
- package/dist/esm/core/code-splitter/compilers.js +39 -32
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/framework-plugins.d.ts +3 -4
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js +10 -12
- package/dist/esm/core/code-splitter/plugins/framework-plugins.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js +34 -16
- package/dist/esm/core/code-splitter/plugins/react-refresh-route-components.js.map +1 -1
- package/dist/esm/core/code-splitter/plugins.d.ts +8 -1
- package/dist/esm/core/config.js.map +1 -1
- package/dist/esm/core/hmr/handle-route-update.js +2 -67
- package/dist/esm/core/hmr/handle-route-update.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +21 -12
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.js +2 -3
- package/dist/esm/core/router-hmr-plugin.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/package.json +6 -6
- package/src/core/code-splitter/compilers.ts +12 -2
- package/src/core/code-splitter/plugins/framework-plugins.ts +11 -15
- package/src/core/code-splitter/plugins/react-refresh-route-components.ts +75 -25
- package/src/core/code-splitter/plugins.ts +12 -1
- package/src/core/config.ts +2 -2
- package/src/core/hmr/handle-route-update.ts +8 -148
- package/src/core/router-code-splitter-plugin.ts +31 -17
- package/src/core/router-hmr-plugin.ts +2 -3
- package/src/index.ts +2 -0
|
@@ -13,29 +13,74 @@ const REACT_REFRESH_ROUTE_COMPONENT_IDENTS = new Set([
|
|
|
13
13
|
'notFoundComponent',
|
|
14
14
|
])
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
type RouteComponentContext = {
|
|
17
17
|
programPath: Parameters<typeof getUniqueProgramIdentifier>[0]
|
|
18
18
|
insertionPath: { insertBefore: (nodes: Array<t.VariableDeclaration>) => void }
|
|
19
19
|
routeOptions: t.ObjectExpression
|
|
20
|
-
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isReactComponentName(name: string) {
|
|
23
|
+
const firstCharacter = name[0]
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
firstCharacter !== undefined &&
|
|
27
|
+
firstCharacter >= 'A' &&
|
|
28
|
+
firstCharacter <= 'Z'
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getRouteComponentKey(prop: t.ObjectProperty) {
|
|
33
|
+
const key = getObjectPropertyKeyName(prop)
|
|
34
|
+
|
|
35
|
+
return key && REACT_REFRESH_ROUTE_COMPONENT_IDENTS.has(key) ? key : undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function prepareRouteComponentsForReactRefresh(ctx: RouteComponentContext) {
|
|
21
39
|
const hoistedDeclarations: Array<t.VariableDeclaration> = []
|
|
40
|
+
let modified = false
|
|
22
41
|
|
|
23
|
-
ctx.routeOptions.properties
|
|
42
|
+
for (const prop of ctx.routeOptions.properties) {
|
|
24
43
|
if (!t.isObjectProperty(prop)) {
|
|
25
|
-
|
|
44
|
+
continue
|
|
26
45
|
}
|
|
27
46
|
|
|
28
|
-
const key =
|
|
47
|
+
const key = getRouteComponentKey(prop)
|
|
29
48
|
|
|
30
|
-
if (!key
|
|
31
|
-
|
|
49
|
+
if (!key) {
|
|
50
|
+
continue
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (t.isIdentifier(prop.value)) {
|
|
54
|
+
if (isReactComponentName(prop.value.name)) {
|
|
55
|
+
continue
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const bindingNode = ctx.programPath.scope.getBinding(prop.value.name)
|
|
59
|
+
?.path.node
|
|
60
|
+
const isLocalComponentBinding =
|
|
61
|
+
t.isFunctionDeclaration(bindingNode) ||
|
|
62
|
+
t.isClassDeclaration(bindingNode) ||
|
|
63
|
+
t.isVariableDeclarator(bindingNode)
|
|
64
|
+
|
|
65
|
+
if (!isLocalComponentBinding) {
|
|
66
|
+
continue
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const componentIdentifier = getUniqueProgramIdentifier(
|
|
70
|
+
ctx.programPath,
|
|
71
|
+
`TSR${key[0]!.toUpperCase()}${key.slice(1)}`,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
ctx.programPath.scope.rename(prop.value.name, componentIdentifier.name)
|
|
75
|
+
modified = true
|
|
76
|
+
continue
|
|
32
77
|
}
|
|
33
78
|
|
|
34
79
|
if (
|
|
35
80
|
!t.isArrowFunctionExpression(prop.value) &&
|
|
36
81
|
!t.isFunctionExpression(prop.value)
|
|
37
82
|
) {
|
|
38
|
-
|
|
83
|
+
continue
|
|
39
84
|
}
|
|
40
85
|
|
|
41
86
|
const hoistedIdentifier = getUniqueProgramIdentifier(
|
|
@@ -50,14 +95,14 @@ function hoistInlineRouteComponents(ctx: {
|
|
|
50
95
|
)
|
|
51
96
|
|
|
52
97
|
prop.value = t.cloneNode(hoistedIdentifier)
|
|
53
|
-
|
|
98
|
+
modified = true
|
|
99
|
+
}
|
|
54
100
|
|
|
55
|
-
if (hoistedDeclarations.length
|
|
56
|
-
|
|
101
|
+
if (hoistedDeclarations.length > 0) {
|
|
102
|
+
ctx.insertionPath.insertBefore(hoistedDeclarations)
|
|
57
103
|
}
|
|
58
104
|
|
|
59
|
-
|
|
60
|
-
return true
|
|
105
|
+
return modified
|
|
61
106
|
}
|
|
62
107
|
|
|
63
108
|
export function createReactRefreshRouteComponentsPlugin(): ReferenceRouteCompilerPlugin {
|
|
@@ -66,27 +111,32 @@ export function createReactRefreshRouteComponentsPlugin(): ReferenceRouteCompile
|
|
|
66
111
|
getStableRouteOptionKeys() {
|
|
67
112
|
return [...REACT_REFRESH_ROUTE_COMPONENT_IDENTS]
|
|
68
113
|
},
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (hoistInlineRouteComponents(ctx)) {
|
|
114
|
+
onAddHmr(ctx) {
|
|
115
|
+
if (prepareRouteComponentsForReactRefresh(ctx)) {
|
|
75
116
|
return { modified: true }
|
|
76
117
|
}
|
|
77
118
|
|
|
78
119
|
return
|
|
79
120
|
},
|
|
80
|
-
|
|
81
|
-
if (
|
|
121
|
+
onVirtualRouteSplitNode(ctx) {
|
|
122
|
+
if (
|
|
123
|
+
ctx.splitNodeMeta.splitStrategy !== 'lazyRouteComponent' ||
|
|
124
|
+
!t.isFunctionDeclaration(ctx.splitNode) ||
|
|
125
|
+
!ctx.splitNode.id ||
|
|
126
|
+
isReactComponentName(ctx.splitNode.id.name)
|
|
127
|
+
) {
|
|
82
128
|
return
|
|
83
129
|
}
|
|
84
130
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
131
|
+
const componentIdentifier = getUniqueProgramIdentifier(
|
|
132
|
+
ctx.programPath,
|
|
133
|
+
ctx.splitNodeMeta.localExporterIdent,
|
|
134
|
+
)
|
|
88
135
|
|
|
89
|
-
|
|
136
|
+
ctx.programPath.scope.rename(
|
|
137
|
+
ctx.splitNode.id.name,
|
|
138
|
+
componentIdentifier.name,
|
|
139
|
+
)
|
|
90
140
|
},
|
|
91
141
|
}
|
|
92
142
|
}
|
|
@@ -40,7 +40,13 @@ export type ReferenceRouteCompilerPluginResult = {
|
|
|
40
40
|
modified?: boolean
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
export type
|
|
43
|
+
export type VirtualRouteSplitNodeCompilerPluginContext = {
|
|
44
|
+
programPath: babel.NodePath<t.Program>
|
|
45
|
+
splitNode: t.Node
|
|
46
|
+
splitNodeMeta: SplitNodeMeta
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type CodeSplitCompilerPlugin = {
|
|
44
50
|
name: string
|
|
45
51
|
getStableRouteOptionKeys?: () => Array<string>
|
|
46
52
|
onRouteOptions?: (
|
|
@@ -55,4 +61,9 @@ export type ReferenceRouteCompilerPlugin = {
|
|
|
55
61
|
onSplitRouteProperty?: (
|
|
56
62
|
ctx: ReferenceRouteSplitPropertyCompilerPluginContext,
|
|
57
63
|
) => void | t.Expression
|
|
64
|
+
onVirtualRouteSplitNode?: (
|
|
65
|
+
ctx: VirtualRouteSplitNodeCompilerPluginContext,
|
|
66
|
+
) => void
|
|
58
67
|
}
|
|
68
|
+
|
|
69
|
+
export type ReferenceRouteCompilerPlugin = CodeSplitCompilerPlugin
|
package/src/core/config.ts
CHANGED
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
RouteIds,
|
|
10
10
|
} from '@tanstack/router-core'
|
|
11
11
|
import type { CodeSplitGroupings } from './constants'
|
|
12
|
-
import type {
|
|
12
|
+
import type { CodeSplitCompilerPlugin } from './code-splitter/plugins'
|
|
13
13
|
|
|
14
14
|
export const splitGroupingsSchema = z
|
|
15
15
|
.array(
|
|
@@ -76,7 +76,7 @@ export type CodeSplittingOptions = {
|
|
|
76
76
|
* Internal compiler plugins used by framework integrations.
|
|
77
77
|
* @internal
|
|
78
78
|
*/
|
|
79
|
-
compilerPlugins?: Array<
|
|
79
|
+
compilerPlugins?: Array<CodeSplitCompilerPlugin>
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
export type HmrStyle = 'vite' | 'webpack'
|
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AnyRoute,
|
|
3
|
-
AnyRouteMatch,
|
|
4
|
-
AnyRouter,
|
|
5
|
-
RouterWritableStore,
|
|
6
|
-
} from '@tanstack/router-core'
|
|
1
|
+
import type { AnyRoute, AnyRouter } from '@tanstack/router-core'
|
|
7
2
|
|
|
8
3
|
type AnyRouteWithPrivateProps = AnyRoute & {
|
|
9
4
|
options: Record<string, unknown>
|
|
10
5
|
parentRoute: AnyRoute
|
|
11
|
-
|
|
12
|
-
_lazyPromise?: Promise<void>
|
|
6
|
+
_lazy?: Promise<void> | true
|
|
13
7
|
update: (options: Record<string, unknown>) => unknown
|
|
14
8
|
_path: string
|
|
15
9
|
_id: string
|
|
@@ -17,37 +11,19 @@ type AnyRouteWithPrivateProps = AnyRoute & {
|
|
|
17
11
|
_to: string
|
|
18
12
|
}
|
|
19
13
|
|
|
20
|
-
type
|
|
14
|
+
type AnyRouterWithPrivateState = AnyRouter & {
|
|
21
15
|
routesById: Record<string, AnyRoute>
|
|
22
16
|
buildRouteTree: () => Parameters<AnyRouter['setRoutes']>[0]
|
|
23
17
|
setRoutes: AnyRouter['setRoutes']
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
string,
|
|
27
|
-
Pick<RouterWritableStore<AnyRouteMatch>, 'get' | 'set'>
|
|
28
|
-
>
|
|
29
|
-
pendingMatchStores: Map<
|
|
30
|
-
string,
|
|
31
|
-
Pick<RouterWritableStore<AnyRouteMatch>, 'get' | 'set'>
|
|
32
|
-
>
|
|
33
|
-
matchStores: Map<
|
|
34
|
-
string,
|
|
35
|
-
Pick<RouterWritableStore<AnyRouteMatch>, 'get' | 'set'>
|
|
36
|
-
>
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
type AnyRouteMatchWithPrivateProps = AnyRouteMatch & {
|
|
41
|
-
__beforeLoadContext?: unknown
|
|
42
|
-
__routeContext?: Record<string, unknown>
|
|
43
|
-
context?: Record<string, unknown>
|
|
18
|
+
_refreshRoute?: () => Promise<void>
|
|
19
|
+
_replaceRouteChunk: (route: AnyRoute, lazyFn: AnyRoute['lazyFn']) => void
|
|
44
20
|
}
|
|
45
21
|
|
|
46
22
|
function handleRouteUpdate(
|
|
47
23
|
routeId: string,
|
|
48
24
|
newRoute: AnyRouteWithPrivateProps,
|
|
49
25
|
) {
|
|
50
|
-
const router = window.__TSR_ROUTER__ as
|
|
26
|
+
const router = window.__TSR_ROUTER__ as AnyRouterWithPrivateState
|
|
51
27
|
const oldRoute = router.routesById[routeId] as
|
|
52
28
|
| AnyRouteWithPrivateProps
|
|
53
29
|
| undefined
|
|
@@ -66,14 +42,6 @@ function handleRouteUpdate(
|
|
|
66
42
|
}
|
|
67
43
|
})
|
|
68
44
|
|
|
69
|
-
const removedKeys = new Set<string>()
|
|
70
|
-
Object.keys(oldRoute.options).forEach((key) => {
|
|
71
|
-
if (!generatedRouteOptionKeys.has(key) && !(key in newRoute.options)) {
|
|
72
|
-
removedKeys.add(key)
|
|
73
|
-
delete oldRoute.options[key]
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
|
|
77
45
|
const oldHasShellComponent = 'shellComponent' in oldRoute.options
|
|
78
46
|
const newHasShellComponent = 'shellComponent' in newRoute.options
|
|
79
47
|
const preserveComponentIdentity =
|
|
@@ -106,60 +74,12 @@ function handleRouteUpdate(
|
|
|
106
74
|
|
|
107
75
|
oldRoute.options = nextOptions
|
|
108
76
|
oldRoute.update(nextOptions)
|
|
109
|
-
oldRoute.
|
|
110
|
-
oldRoute._lazyPromise = undefined
|
|
77
|
+
router._replaceRouteChunk(oldRoute, newRoute.lazyFn)
|
|
111
78
|
|
|
112
79
|
router.setRoutes(router.buildRouteTree())
|
|
113
80
|
syncHotRouteExport(oldRoute)
|
|
114
81
|
router.resolvePathCache.clear()
|
|
115
|
-
|
|
116
|
-
const filter = (m: AnyRouteMatch) => m.routeId === oldRoute.id
|
|
117
|
-
const activeMatch = router.stores.matches.get().find(filter)
|
|
118
|
-
const pendingMatch = router.stores.pendingMatches.get().find(filter)
|
|
119
|
-
const cachedMatches = router.stores.cachedMatches.get().filter(filter)
|
|
120
|
-
|
|
121
|
-
if (activeMatch || pendingMatch || cachedMatches.length > 0) {
|
|
122
|
-
// Clear stale match data for removed route options BEFORE invalidating.
|
|
123
|
-
// Without this, router.invalidate() -> matchRoutes() reuses the existing
|
|
124
|
-
// match from the store (via ...existingMatch spread) and the stale
|
|
125
|
-
// loaderData / __beforeLoadContext survives the reload cycle.
|
|
126
|
-
//
|
|
127
|
-
// We must update the store directly (not via router.updateMatch) because
|
|
128
|
-
// updateMatch wraps in startTransition which may defer the state update,
|
|
129
|
-
// and we need the clear to be visible before invalidate reads the store.
|
|
130
|
-
if (removedKeys.has('loader') || removedKeys.has('beforeLoad')) {
|
|
131
|
-
const matchIds = [
|
|
132
|
-
activeMatch?.id,
|
|
133
|
-
pendingMatch?.id,
|
|
134
|
-
...cachedMatches.map((match) => match.id),
|
|
135
|
-
].filter(Boolean) as Array<string>
|
|
136
|
-
router.batch(() => {
|
|
137
|
-
for (const matchId of matchIds) {
|
|
138
|
-
const store =
|
|
139
|
-
router.stores.pendingMatchStores.get(matchId) ||
|
|
140
|
-
router.stores.matchStores.get(matchId) ||
|
|
141
|
-
router.stores.cachedMatchStores.get(matchId)
|
|
142
|
-
if (store) {
|
|
143
|
-
store.set((prev) => {
|
|
144
|
-
const next: AnyRouteMatchWithPrivateProps = { ...prev }
|
|
145
|
-
|
|
146
|
-
if (removedKeys.has('loader')) {
|
|
147
|
-
next.loaderData = undefined
|
|
148
|
-
}
|
|
149
|
-
if (removedKeys.has('beforeLoad')) {
|
|
150
|
-
next.__beforeLoadContext = undefined
|
|
151
|
-
next.context = rebuildMatchContextWithoutBeforeLoad(next)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return next
|
|
155
|
-
})
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
})
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
router.invalidate({ filter, sync: true })
|
|
162
|
-
}
|
|
82
|
+
void router._refreshRoute?.()
|
|
163
83
|
|
|
164
84
|
function syncHotRouteExport(liveRoute: AnyRouteWithPrivateProps) {
|
|
165
85
|
// routeTree.gen.ts mutates the original module export with generated
|
|
@@ -172,66 +92,6 @@ function handleRouteUpdate(
|
|
|
172
92
|
newRoute._fullPath = liveRoute._fullPath
|
|
173
93
|
newRoute._to = liveRoute._to
|
|
174
94
|
}
|
|
175
|
-
|
|
176
|
-
function getStoreMatch(matchId: string) {
|
|
177
|
-
return (
|
|
178
|
-
router.stores.pendingMatchStores.get(matchId)?.get() ||
|
|
179
|
-
router.stores.matchStores.get(matchId)?.get() ||
|
|
180
|
-
router.stores.cachedMatchStores.get(matchId)?.get()
|
|
181
|
-
)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
function getMatchList(matchId: string) {
|
|
185
|
-
const pendingMatches = router.stores.pendingMatches.get()
|
|
186
|
-
if (pendingMatches.some((match) => match.id === matchId)) {
|
|
187
|
-
return pendingMatches
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const activeMatches = router.stores.matches.get()
|
|
191
|
-
if (activeMatches.some((match) => match.id === matchId)) {
|
|
192
|
-
return activeMatches
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const cachedMatches = router.stores.cachedMatches.get()
|
|
196
|
-
if (cachedMatches.some((match) => match.id === matchId)) {
|
|
197
|
-
return cachedMatches
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return []
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function getParentMatch(match: AnyRouteMatch) {
|
|
204
|
-
const matchList = getMatchList(match.id)
|
|
205
|
-
const matchIndex = matchList.findIndex((item) => item.id === match.id)
|
|
206
|
-
|
|
207
|
-
if (matchIndex <= 0) {
|
|
208
|
-
return undefined
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
const parentMatch = matchList[matchIndex - 1]!
|
|
212
|
-
return getStoreMatch(parentMatch.id) || parentMatch
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
function rebuildMatchContextWithoutBeforeLoad(
|
|
216
|
-
match: AnyRouteMatchWithPrivateProps,
|
|
217
|
-
) {
|
|
218
|
-
const parentMatch = getParentMatch(match)
|
|
219
|
-
const getParentContext = (
|
|
220
|
-
router as unknown as {
|
|
221
|
-
getParentContext?: (
|
|
222
|
-
parentMatch?: AnyRouteMatch,
|
|
223
|
-
) => Record<string, unknown> | undefined
|
|
224
|
-
}
|
|
225
|
-
).getParentContext
|
|
226
|
-
const parentContext = getParentContext
|
|
227
|
-
? getParentContext.call(router, parentMatch)
|
|
228
|
-
: (parentMatch?.context ?? router.options.context)
|
|
229
|
-
|
|
230
|
-
return {
|
|
231
|
-
...(parentContext ?? {}),
|
|
232
|
-
...(match.__routeContext ?? {}),
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
95
|
}
|
|
236
96
|
|
|
237
97
|
const handleRouteUpdateStr = handleRouteUpdate.toString()
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
computeSharedBindings,
|
|
14
14
|
detectCodeSplitGroupingsFromRoute,
|
|
15
15
|
} from './code-splitter/compilers'
|
|
16
|
-
import {
|
|
16
|
+
import { getFrameworkHmrCompilerPlugins } from './code-splitter/plugins/framework-plugins'
|
|
17
17
|
import {
|
|
18
18
|
defaultCodeSplitGroupings,
|
|
19
19
|
splitRouteIdentNodes,
|
|
@@ -24,7 +24,8 @@ import { debug, normalizePath, routeFactoryCallCodeFilter } from './utils'
|
|
|
24
24
|
import { createRouterPluginContext } from './router-plugin-context'
|
|
25
25
|
import type { CodeSplitGroupings, SplitRouteIdentNodes } from './constants'
|
|
26
26
|
import type { GetRoutesByFileMapResultValue } from '@tanstack/router-generator'
|
|
27
|
-
import type {
|
|
27
|
+
import type { CodeSplitCompilerPlugin } from './code-splitter/plugins'
|
|
28
|
+
import type { Config, HmrStyle } from './config'
|
|
28
29
|
import type { RouterPluginContext } from './router-plugin-context'
|
|
29
30
|
import type {
|
|
30
31
|
UnpluginFactory,
|
|
@@ -83,6 +84,12 @@ export function createRouterCodeSplitterPlugin(
|
|
|
83
84
|
): ReturnType<UnpluginFactory<Partial<Config | (() => Config)> | undefined>> {
|
|
84
85
|
let ROOT: string = process.cwd()
|
|
85
86
|
let userConfig: Config
|
|
87
|
+
let addHmr: boolean
|
|
88
|
+
let hmrStyle: HmrStyle
|
|
89
|
+
let compilerPlugins: Array<CodeSplitCompilerPlugin>
|
|
90
|
+
let virtualRouteCompilerPlugins: Array<CodeSplitCompilerPlugin>
|
|
91
|
+
|
|
92
|
+
let isProduction = process.env.NODE_ENV === 'production'
|
|
86
93
|
|
|
87
94
|
function initUserConfig() {
|
|
88
95
|
if (typeof options === 'function') {
|
|
@@ -90,8 +97,22 @@ export function createRouterCodeSplitterPlugin(
|
|
|
90
97
|
} else {
|
|
91
98
|
userConfig = getConfig(options, ROOT)
|
|
92
99
|
}
|
|
100
|
+
|
|
101
|
+
addHmr = (userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction
|
|
102
|
+
hmrStyle = userConfig.plugin?.hmr?.style ?? 'vite'
|
|
103
|
+
compilerPlugins = [
|
|
104
|
+
...(addHmr
|
|
105
|
+
? (getFrameworkHmrCompilerPlugins({
|
|
106
|
+
targetFramework: userConfig.target,
|
|
107
|
+
hmrStyle,
|
|
108
|
+
}) ?? [])
|
|
109
|
+
: []),
|
|
110
|
+
...(userConfig.codeSplittingOptions?.compilerPlugins ?? []),
|
|
111
|
+
]
|
|
112
|
+
virtualRouteCompilerPlugins = compilerPlugins.filter(
|
|
113
|
+
(plugin) => plugin.onVirtualRouteSplitNode,
|
|
114
|
+
)
|
|
93
115
|
}
|
|
94
|
-
const isProduction = process.env.NODE_ENV === 'production'
|
|
95
116
|
// Map from normalized route file path → set of shared binding names.
|
|
96
117
|
// Populated by the reference compiler, consumed by virtual and shared compilers.
|
|
97
118
|
const sharedBindingsMap = new Map<string, Set<string>>()
|
|
@@ -159,10 +180,6 @@ export function createRouterCodeSplitterPlugin(
|
|
|
159
180
|
sharedBindingsMap.delete(id)
|
|
160
181
|
}
|
|
161
182
|
|
|
162
|
-
const addHmr =
|
|
163
|
-
(userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction
|
|
164
|
-
const hmrStyle = userConfig.plugin?.hmr?.style ?? 'vite'
|
|
165
|
-
|
|
166
183
|
const compiledReferenceRoute = compileCodeSplitReferenceRoute({
|
|
167
184
|
code,
|
|
168
185
|
codeSplitGroupings: splitGroupings,
|
|
@@ -176,14 +193,7 @@ export function createRouterCodeSplitterPlugin(
|
|
|
176
193
|
hmrStyle,
|
|
177
194
|
hmrRouteId: generatorNodeInfo.routeId,
|
|
178
195
|
sharedBindings: sharedBindings.size > 0 ? sharedBindings : undefined,
|
|
179
|
-
compilerPlugins
|
|
180
|
-
...(getReferenceRouteCompilerPlugins({
|
|
181
|
-
targetFramework: userConfig.target,
|
|
182
|
-
addHmr,
|
|
183
|
-
hmrStyle,
|
|
184
|
-
}) ?? []),
|
|
185
|
-
...(userConfig.codeSplittingOptions?.compilerPlugins ?? []),
|
|
186
|
-
],
|
|
196
|
+
compilerPlugins,
|
|
187
197
|
})
|
|
188
198
|
|
|
189
199
|
if (compiledReferenceRoute === null) {
|
|
@@ -232,6 +242,7 @@ export function createRouterCodeSplitterPlugin(
|
|
|
232
242
|
filename: id,
|
|
233
243
|
splitTargets: grouping,
|
|
234
244
|
sharedBindings: resolvedSharedBindings,
|
|
245
|
+
compilerPlugins: virtualRouteCompilerPlugins,
|
|
235
246
|
})
|
|
236
247
|
|
|
237
248
|
if (debug) {
|
|
@@ -276,6 +287,7 @@ export function createRouterCodeSplitterPlugin(
|
|
|
276
287
|
|
|
277
288
|
vite: {
|
|
278
289
|
configResolved(config) {
|
|
290
|
+
isProduction = config.command === 'build'
|
|
279
291
|
ROOT = config.root
|
|
280
292
|
initUserConfig()
|
|
281
293
|
|
|
@@ -319,12 +331,14 @@ export function createRouterCodeSplitterPlugin(
|
|
|
319
331
|
},
|
|
320
332
|
},
|
|
321
333
|
|
|
322
|
-
rspack() {
|
|
334
|
+
rspack(compiler) {
|
|
335
|
+
isProduction = compiler.options.mode === 'production'
|
|
323
336
|
ROOT = process.cwd()
|
|
324
337
|
initUserConfig()
|
|
325
338
|
},
|
|
326
339
|
|
|
327
|
-
webpack() {
|
|
340
|
+
webpack(compiler) {
|
|
341
|
+
isProduction = compiler.options.mode === 'production'
|
|
328
342
|
ROOT = process.cwd()
|
|
329
343
|
initUserConfig()
|
|
330
344
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'
|
|
2
2
|
import { compileCodeSplitReferenceRoute } from './code-splitter/compilers'
|
|
3
|
-
import {
|
|
3
|
+
import { getFrameworkHmrCompilerPlugins } from './code-splitter/plugins/framework-plugins'
|
|
4
4
|
import { createRouteHmrStatement } from './hmr'
|
|
5
5
|
import { debug, normalizePath, routeFactoryCallCodeFilter } from './utils'
|
|
6
6
|
import { getConfig } from './config'
|
|
@@ -50,9 +50,8 @@ export function createRouterHmrPlugin(
|
|
|
50
50
|
const hmrStyle = userConfig.plugin?.hmr?.style ?? 'vite'
|
|
51
51
|
|
|
52
52
|
if (userConfig.target === 'react') {
|
|
53
|
-
const compilerPlugins =
|
|
53
|
+
const compilerPlugins = getFrameworkHmrCompilerPlugins({
|
|
54
54
|
targetFramework: 'react',
|
|
55
|
-
addHmr: true,
|
|
56
55
|
hmrStyle,
|
|
57
56
|
})
|
|
58
57
|
const compiled = compileCodeSplitReferenceRoute({
|
package/src/index.ts
CHANGED
|
@@ -13,8 +13,10 @@ export type {
|
|
|
13
13
|
export type { RouterPluginContext } from './core/router-plugin-context'
|
|
14
14
|
export { getObjectPropertyKeyName } from './core/utils'
|
|
15
15
|
export type {
|
|
16
|
+
CodeSplitCompilerPlugin,
|
|
16
17
|
ReferenceRouteCompilerPlugin,
|
|
17
18
|
ReferenceRouteCompilerPluginContext,
|
|
19
|
+
VirtualRouteSplitNodeCompilerPluginContext,
|
|
18
20
|
} from './core/code-splitter/plugins'
|
|
19
21
|
export {
|
|
20
22
|
tsrSplit,
|