@tanstack/router-plugin 1.99.14 → 1.101.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 +187 -34
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +14 -2
- package/dist/cjs/core/code-splitter/path-ids.cjs +37 -0
- package/dist/cjs/core/code-splitter/path-ids.cjs.map +1 -0
- package/dist/cjs/core/code-splitter/path-ids.d.cts +2 -0
- package/dist/cjs/core/config.cjs +33 -1
- package/dist/cjs/core/config.cjs.map +1 -1
- package/dist/cjs/core/config.d.cts +24 -0
- package/dist/cjs/core/constants.cjs +17 -2
- package/dist/cjs/core/constants.cjs.map +1 -1
- package/dist/cjs/core/constants.d.cts +5 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +69 -16
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/esbuild.d.cts +3 -0
- package/dist/cjs/rspack.d.cts +3 -0
- package/dist/cjs/vite.d.cts +3 -0
- package/dist/cjs/webpack.d.cts +3 -0
- package/dist/esm/core/code-splitter/compilers.d.ts +14 -2
- package/dist/esm/core/code-splitter/compilers.js +189 -36
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/path-ids.d.ts +2 -0
- package/dist/esm/core/code-splitter/path-ids.js +37 -0
- package/dist/esm/core/code-splitter/path-ids.js.map +1 -0
- package/dist/esm/core/config.d.ts +24 -0
- package/dist/esm/core/config.js +34 -2
- package/dist/esm/core/config.js.map +1 -1
- package/dist/esm/core/constants.d.ts +5 -1
- package/dist/esm/core/constants.js +17 -2
- package/dist/esm/core/constants.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +72 -19
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/esbuild.d.ts +3 -0
- package/dist/esm/rspack.d.ts +3 -0
- package/dist/esm/vite.d.ts +3 -0
- package/dist/esm/webpack.d.ts +3 -0
- package/package.json +3 -3
- package/src/core/code-splitter/compilers.ts +230 -46
- package/src/core/code-splitter/path-ids.ts +39 -0
- package/src/core/config.ts +63 -0
- package/src/core/constants.ts +18 -1
- package/src/core/router-code-splitter-plugin.ts +104 -20
|
@@ -1,16 +1,31 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* It is important to familiarize yourself with how the code-splitting works in this plugin.
|
|
3
|
+
* https://github.com/TanStack/router/pull/3355
|
|
4
|
+
*/
|
|
2
5
|
|
|
6
|
+
import { isAbsolute, join, normalize } from 'node:path'
|
|
3
7
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
4
8
|
import { logDiff } from '@tanstack/router-utils'
|
|
5
|
-
import { getConfig } from './config'
|
|
9
|
+
import { getConfig, splitGroupingsSchema } from './config'
|
|
6
10
|
import {
|
|
7
11
|
compileCodeSplitReferenceRoute,
|
|
8
12
|
compileCodeSplitVirtualRoute,
|
|
13
|
+
detectCodeSplitGroupingsFromRoute,
|
|
9
14
|
} from './code-splitter/compilers'
|
|
10
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
defaultCodeSplitGroupings,
|
|
17
|
+
splitRouteIdentNodes,
|
|
18
|
+
tsrSplit,
|
|
19
|
+
} from './constants'
|
|
20
|
+
import { decodeIdentifier } from './code-splitter/path-ids'
|
|
21
|
+
import type { CodeSplitGroupings, SplitRouteIdentNodes } from './constants'
|
|
11
22
|
|
|
12
23
|
import type { Config } from './config'
|
|
13
|
-
import type {
|
|
24
|
+
import type {
|
|
25
|
+
UnpluginContextMeta,
|
|
26
|
+
UnpluginFactory,
|
|
27
|
+
TransformResult as UnpluginTransformResult,
|
|
28
|
+
} from 'unplugin'
|
|
14
29
|
|
|
15
30
|
const debug =
|
|
16
31
|
process.env.TSR_VITE_DEBUG &&
|
|
@@ -71,31 +86,63 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
71
86
|
|
|
72
87
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
73
88
|
|
|
74
|
-
const
|
|
75
|
-
|
|
89
|
+
const getGlobalCodeSplitGroupings = () => {
|
|
90
|
+
return (
|
|
91
|
+
userConfig.codeSplittingOptions?.defaultBehavior ||
|
|
92
|
+
defaultCodeSplitGroupings
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
const getShouldSplitFn = () => {
|
|
96
|
+
return userConfig.codeSplittingOptions?.splitBehavior
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const handleCompilingReferenceFile = (
|
|
100
|
+
code: string,
|
|
101
|
+
id: string,
|
|
102
|
+
): UnpluginTransformResult => {
|
|
103
|
+
if (debug) console.info('Compiling Route: ', id)
|
|
76
104
|
|
|
77
|
-
const
|
|
105
|
+
const fromCode = detectCodeSplitGroupingsFromRoute({
|
|
78
106
|
code,
|
|
79
107
|
root: ROOT,
|
|
80
108
|
filename: id,
|
|
81
109
|
})
|
|
82
110
|
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
|
|
111
|
+
if (fromCode.groupings) {
|
|
112
|
+
const res = splitGroupingsSchema.safeParse(fromCode.groupings)
|
|
113
|
+
if (!res.success) {
|
|
114
|
+
const message = res.error.errors.map((e) => e.message).join('. ')
|
|
115
|
+
throw new Error(
|
|
116
|
+
`The groupings for the route "${id}" are invalid.\n${message}`,
|
|
117
|
+
)
|
|
118
|
+
}
|
|
86
119
|
}
|
|
87
120
|
|
|
88
|
-
|
|
89
|
-
}
|
|
121
|
+
const userShouldSplitFn = getShouldSplitFn()
|
|
90
122
|
|
|
91
|
-
|
|
92
|
-
|
|
123
|
+
const pluginSplitBehavior = userShouldSplitFn?.({
|
|
124
|
+
routeId: fromCode.routeId,
|
|
125
|
+
}) as CodeSplitGroupings | undefined
|
|
126
|
+
|
|
127
|
+
if (pluginSplitBehavior) {
|
|
128
|
+
const res = splitGroupingsSchema.safeParse(pluginSplitBehavior)
|
|
129
|
+
if (!res.success) {
|
|
130
|
+
const message = res.error.errors.map((e) => e.message).join('. ')
|
|
131
|
+
throw new Error(
|
|
132
|
+
`The groupings returned when using \`splitBehavior\` for the route "${id}" are invalid.\n${message}`,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const splitGroupings: CodeSplitGroupings =
|
|
138
|
+
fromCode.groupings || pluginSplitBehavior || getGlobalCodeSplitGroupings()
|
|
93
139
|
|
|
94
140
|
const compiledReferenceRoute = compileCodeSplitReferenceRoute({
|
|
95
141
|
code,
|
|
96
142
|
root: ROOT,
|
|
97
143
|
filename: id,
|
|
98
|
-
isProduction,
|
|
144
|
+
runtimeEnv: isProduction ? 'prod' : 'dev',
|
|
145
|
+
codeSplitGroupings: splitGroupings,
|
|
99
146
|
})
|
|
100
147
|
|
|
101
148
|
if (debug) {
|
|
@@ -106,6 +153,43 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
106
153
|
return compiledReferenceRoute
|
|
107
154
|
}
|
|
108
155
|
|
|
156
|
+
const handleCompilingVirtualFile = (
|
|
157
|
+
code: string,
|
|
158
|
+
id: string,
|
|
159
|
+
): UnpluginTransformResult => {
|
|
160
|
+
if (debug) console.info('Splitting Route: ', id)
|
|
161
|
+
|
|
162
|
+
const [_, ...pathnameParts] = id.split('?')
|
|
163
|
+
|
|
164
|
+
const searchParams = new URLSearchParams(pathnameParts.join('?'))
|
|
165
|
+
const splitValue = searchParams.get(tsrSplit)
|
|
166
|
+
|
|
167
|
+
if (!splitValue) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`The split value for the virtual route "${id}" was not found.`,
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const rawGrouping = decodeIdentifier(splitValue)
|
|
174
|
+
const grouping = [...new Set(rawGrouping)].filter((p) =>
|
|
175
|
+
splitRouteIdentNodes.includes(p as any),
|
|
176
|
+
) as Array<SplitRouteIdentNodes>
|
|
177
|
+
|
|
178
|
+
const result = compileCodeSplitVirtualRoute({
|
|
179
|
+
code,
|
|
180
|
+
root: ROOT,
|
|
181
|
+
filename: id,
|
|
182
|
+
splitTargets: grouping,
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
if (debug) {
|
|
186
|
+
logDiff(code, result.code)
|
|
187
|
+
console.log('Output:\n', result.code + '\n\n')
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return result
|
|
191
|
+
}
|
|
192
|
+
|
|
109
193
|
return {
|
|
110
194
|
name: 'router-code-splitter-plugin',
|
|
111
195
|
enforce: 'pre',
|
|
@@ -119,8 +203,8 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
119
203
|
url.searchParams.delete('v')
|
|
120
204
|
id = fileURLToPath(url).replace(/\\/g, '/')
|
|
121
205
|
|
|
122
|
-
if (id.includes(
|
|
123
|
-
return
|
|
206
|
+
if (id.includes(tsrSplit)) {
|
|
207
|
+
return handleCompilingVirtualFile(code, id)
|
|
124
208
|
} else if (
|
|
125
209
|
fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&
|
|
126
210
|
(code.includes('createRoute(') || code.includes('createFileRoute('))
|
|
@@ -135,7 +219,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
135
219
|
}
|
|
136
220
|
}
|
|
137
221
|
|
|
138
|
-
return
|
|
222
|
+
return handleCompilingReferenceFile(code, id)
|
|
139
223
|
}
|
|
140
224
|
|
|
141
225
|
return null
|
|
@@ -148,7 +232,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
148
232
|
|
|
149
233
|
if (
|
|
150
234
|
fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||
|
|
151
|
-
id.includes(
|
|
235
|
+
id.includes(tsrSplit)
|
|
152
236
|
) {
|
|
153
237
|
return true
|
|
154
238
|
}
|
|
@@ -163,7 +247,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
163
247
|
},
|
|
164
248
|
},
|
|
165
249
|
|
|
166
|
-
rspack(
|
|
250
|
+
rspack(_compiler) {
|
|
167
251
|
ROOT = process.cwd()
|
|
168
252
|
userConfig = getConfig(options, ROOT)
|
|
169
253
|
},
|