@tanstack/router-plugin 1.121.6 → 1.121.9
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 +17 -18
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +0 -1
- package/dist/cjs/core/config.d.cts +4 -4
- package/dist/cjs/core/route-autoimport-plugin.cjs +51 -45
- package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +65 -58
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-composed-plugin.cjs +7 -1
- package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-generator-plugin.cjs +18 -19
- package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +26 -24
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/core/utils.cjs +0 -7
- package/dist/cjs/core/utils.cjs.map +1 -1
- package/dist/cjs/core/utils.d.cts +0 -1
- package/dist/esm/core/code-splitter/compilers.d.ts +0 -1
- package/dist/esm/core/code-splitter/compilers.js +17 -18
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/config.d.ts +4 -4
- package/dist/esm/core/route-autoimport-plugin.js +52 -46
- package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +67 -60
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-composed-plugin.js +7 -1
- package/dist/esm/core/router-composed-plugin.js.map +1 -1
- package/dist/esm/core/router-generator-plugin.js +19 -20
- package/dist/esm/core/router-generator-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.js +27 -25
- package/dist/esm/core/router-hmr-plugin.js.map +1 -1
- package/dist/esm/core/utils.d.ts +0 -1
- package/dist/esm/core/utils.js +1 -8
- package/dist/esm/core/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/core/code-splitter/compilers.ts +25 -24
- package/src/core/route-autoimport-plugin.ts +57 -53
- package/src/core/router-code-splitter-plugin.ts +75 -79
- package/src/core/router-composed-plugin.ts +7 -1
- package/src/core/router-generator-plugin.ts +23 -24
- package/src/core/router-hmr-plugin.ts +24 -23
- package/src/core/utils.ts +0 -15
- package/src/global.d.ts +7 -0
|
@@ -17,9 +17,9 @@ import {
|
|
|
17
17
|
tsrSplit,
|
|
18
18
|
} from './constants'
|
|
19
19
|
import { decodeIdentifier } from './code-splitter/path-ids'
|
|
20
|
-
import { debug
|
|
20
|
+
import { debug } from './utils'
|
|
21
21
|
import type { CodeSplitGroupings, SplitRouteIdentNodes } from './constants'
|
|
22
|
-
|
|
22
|
+
import type { GetRoutesByFileMapResultValue } from '@tanstack/router-generator'
|
|
23
23
|
import type { Config } from './config'
|
|
24
24
|
import type {
|
|
25
25
|
UnpluginContextMeta,
|
|
@@ -27,10 +27,6 @@ import type {
|
|
|
27
27
|
TransformResult as UnpluginTransformResult,
|
|
28
28
|
} from 'unplugin'
|
|
29
29
|
|
|
30
|
-
function capitalizeFirst(str: string): string {
|
|
31
|
-
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
30
|
type BannedBeforeExternalPlugin = {
|
|
35
31
|
identifier: string
|
|
36
32
|
pkg: string
|
|
@@ -48,11 +44,14 @@ const bannedBeforeExternalPlugins: Array<BannedBeforeExternalPlugin> = [
|
|
|
48
44
|
]
|
|
49
45
|
|
|
50
46
|
class FoundPluginInBeforeCode extends Error {
|
|
51
|
-
constructor(
|
|
52
|
-
|
|
47
|
+
constructor(
|
|
48
|
+
externalPlugin: BannedBeforeExternalPlugin,
|
|
49
|
+
pluginFramework: string,
|
|
50
|
+
) {
|
|
51
|
+
super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin/${pluginFramework}'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again:
|
|
53
52
|
e.g.
|
|
54
53
|
plugins: [
|
|
55
|
-
|
|
54
|
+
tanstackRouter(), // Place this before ${externalPlugin.usage}
|
|
56
55
|
${externalPlugin.usage},
|
|
57
56
|
]
|
|
58
57
|
`)
|
|
@@ -82,6 +81,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
82
81
|
const handleCompilingReferenceFile = (
|
|
83
82
|
code: string,
|
|
84
83
|
id: string,
|
|
84
|
+
generatorNodeInfo: GetRoutesByFileMapResultValue,
|
|
85
85
|
): UnpluginTransformResult => {
|
|
86
86
|
if (debug) console.info('Compiling Route: ', id)
|
|
87
87
|
|
|
@@ -102,7 +102,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
102
102
|
const userShouldSplitFn = getShouldSplitFn()
|
|
103
103
|
|
|
104
104
|
const pluginSplitBehavior = userShouldSplitFn?.({
|
|
105
|
-
routeId:
|
|
105
|
+
routeId: generatorNodeInfo.routePath,
|
|
106
106
|
}) as CodeSplitGroupings | undefined
|
|
107
107
|
|
|
108
108
|
if (pluginSplitBehavior) {
|
|
@@ -171,83 +171,79 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
171
171
|
return result
|
|
172
172
|
}
|
|
173
173
|
|
|
174
|
-
return
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
174
|
+
return [
|
|
175
|
+
{
|
|
176
|
+
name: 'tanstack-router:code-splitter:compile-reference-file',
|
|
177
|
+
enforce: 'pre',
|
|
178
|
+
|
|
179
|
+
transform: {
|
|
180
|
+
filter: {
|
|
181
|
+
id: {
|
|
182
|
+
exclude: tsrSplit,
|
|
183
|
+
},
|
|
184
|
+
code: 'createFileRoute(',
|
|
185
|
+
},
|
|
186
|
+
handler(code, id) {
|
|
187
|
+
const generatorFileInfo = globalThis.TSR_ROUTES_BY_ID_MAP?.get(id)
|
|
188
|
+
if (generatorFileInfo && code.includes('createFileRoute(')) {
|
|
189
|
+
for (const externalPlugin of bannedBeforeExternalPlugins) {
|
|
190
|
+
if (!externalPlugin.frameworks.includes(framework)) {
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (code.includes(externalPlugin.identifier)) {
|
|
195
|
+
throw new FoundPluginInBeforeCode(externalPlugin, framework)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return handleCompilingReferenceFile(code, id, generatorFileInfo)
|
|
200
200
|
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return handleCompilingReferenceFile(code, id)
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return null
|
|
207
|
-
},
|
|
208
|
-
|
|
209
|
-
transformInclude(id) {
|
|
210
|
-
if (!userConfig.autoCodeSplitting) {
|
|
211
|
-
return undefined
|
|
212
|
-
}
|
|
213
201
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
) {
|
|
218
|
-
return true
|
|
219
|
-
}
|
|
220
|
-
return false
|
|
221
|
-
},
|
|
202
|
+
return null
|
|
203
|
+
},
|
|
204
|
+
},
|
|
222
205
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
206
|
+
vite: {
|
|
207
|
+
configResolved(config) {
|
|
208
|
+
ROOT = config.root
|
|
209
|
+
userConfig = getConfig(options, ROOT)
|
|
210
|
+
},
|
|
211
|
+
},
|
|
226
212
|
|
|
213
|
+
rspack() {
|
|
214
|
+
ROOT = process.cwd()
|
|
227
215
|
userConfig = getConfig(options, ROOT)
|
|
228
216
|
},
|
|
229
|
-
},
|
|
230
217
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
},
|
|
218
|
+
webpack(compiler) {
|
|
219
|
+
ROOT = process.cwd()
|
|
220
|
+
userConfig = getConfig(options, ROOT)
|
|
235
221
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
compiler.options.mode === 'production'
|
|
243
|
-
) {
|
|
244
|
-
compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
245
|
-
console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')
|
|
246
|
-
setTimeout(() => {
|
|
247
|
-
process.exit(0)
|
|
222
|
+
if (compiler.options.mode === 'production') {
|
|
223
|
+
compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
224
|
+
console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')
|
|
225
|
+
setTimeout(() => {
|
|
226
|
+
process.exit(0)
|
|
227
|
+
})
|
|
248
228
|
})
|
|
249
|
-
}
|
|
250
|
-
}
|
|
229
|
+
}
|
|
230
|
+
},
|
|
251
231
|
},
|
|
252
|
-
|
|
232
|
+
{
|
|
233
|
+
name: 'tanstack-router:code-splitter:compile-virtual-file',
|
|
234
|
+
enforce: 'pre',
|
|
235
|
+
|
|
236
|
+
transform: {
|
|
237
|
+
filter: {
|
|
238
|
+
id: /tsr-split/,
|
|
239
|
+
},
|
|
240
|
+
handler(code, id) {
|
|
241
|
+
const url = pathToFileURL(id)
|
|
242
|
+
url.searchParams.delete('v')
|
|
243
|
+
id = fileURLToPath(url).replace(/\\/g, '/')
|
|
244
|
+
return handleCompilingVirtualFile(code, id)
|
|
245
|
+
},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
]
|
|
253
249
|
}
|
|
@@ -20,7 +20,13 @@ export const unpluginRouterComposedFactory: UnpluginFactory<
|
|
|
20
20
|
const routerCodeSplitter = getPlugin(unpluginRouterCodeSplitterFactory)
|
|
21
21
|
const routeAutoImport = getPlugin(unpluginRouteAutoImportFactory)
|
|
22
22
|
|
|
23
|
-
const result = [...routerGenerator
|
|
23
|
+
const result = [...routerGenerator]
|
|
24
|
+
if (options.autoCodeSplitting) {
|
|
25
|
+
result.push(...routerCodeSplitter)
|
|
26
|
+
}
|
|
27
|
+
if (options.verboseFileRoutes === false) {
|
|
28
|
+
result.push(...routeAutoImport)
|
|
29
|
+
}
|
|
24
30
|
|
|
25
31
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
26
32
|
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { isAbsolute, join, normalize } from 'node:path'
|
|
2
2
|
import { Generator, resolveConfigPath } from '@tanstack/router-generator'
|
|
3
|
-
|
|
4
3
|
import { getConfig } from './config'
|
|
4
|
+
|
|
5
|
+
import type { GeneratorEvent } from '@tanstack/router-generator'
|
|
5
6
|
import type { FSWatcher } from 'chokidar'
|
|
6
7
|
import type { UnpluginFactory } from 'unplugin'
|
|
7
8
|
import type { Config } from './config'
|
|
@@ -31,41 +32,39 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
31
32
|
})
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
const generate = async (
|
|
35
|
+
const generate = async (opts?: {
|
|
36
|
+
file: string
|
|
37
|
+
event: 'create' | 'update' | 'delete'
|
|
38
|
+
}) => {
|
|
35
39
|
if (routeGenerationDisabled()) {
|
|
36
40
|
return
|
|
37
41
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
let generatorEvent: GeneratorEvent | undefined = undefined
|
|
43
|
+
if (opts) {
|
|
44
|
+
const filePath = normalize(opts.file)
|
|
45
|
+
if (filePath === resolveConfigPath({ configDirectory: ROOT })) {
|
|
46
|
+
initConfigAndGenerator()
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
generatorEvent = { path: filePath, type: opts.event }
|
|
42
50
|
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const handleFile = async (
|
|
46
|
-
file: string,
|
|
47
|
-
event: 'create' | 'update' | 'delete',
|
|
48
|
-
) => {
|
|
49
|
-
const filePath = normalize(file)
|
|
50
51
|
|
|
51
|
-
if (filePath === resolveConfigPath({ configDirectory: ROOT })) {
|
|
52
|
-
initConfigAndGenerator()
|
|
53
|
-
return
|
|
54
|
-
}
|
|
55
52
|
try {
|
|
56
|
-
await generator.run(
|
|
53
|
+
await generator.run(generatorEvent)
|
|
54
|
+
globalThis.TSR_ROUTES_BY_ID_MAP = generator.getRoutesByFileMap()
|
|
57
55
|
} catch (e) {
|
|
58
56
|
console.error(e)
|
|
59
57
|
}
|
|
60
58
|
}
|
|
61
59
|
|
|
62
60
|
return {
|
|
63
|
-
name: 'router-generator
|
|
61
|
+
name: 'tanstack:router-generator',
|
|
64
62
|
enforce: 'pre',
|
|
65
63
|
async watchChange(id, { event }) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
await generate({
|
|
65
|
+
file: id,
|
|
66
|
+
event,
|
|
67
|
+
})
|
|
69
68
|
},
|
|
70
69
|
async buildStart() {
|
|
71
70
|
await generate()
|
|
@@ -89,7 +88,7 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
89
88
|
|
|
90
89
|
let handle: FSWatcher | null = null
|
|
91
90
|
|
|
92
|
-
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate)
|
|
91
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate())
|
|
93
92
|
|
|
94
93
|
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
95
94
|
if (handle) {
|
|
@@ -117,7 +116,7 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
117
116
|
|
|
118
117
|
let handle: FSWatcher | null = null
|
|
119
118
|
|
|
120
|
-
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate)
|
|
119
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate())
|
|
121
120
|
|
|
122
121
|
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
123
122
|
if (handle) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'
|
|
2
2
|
import { getConfig } from './config'
|
|
3
3
|
import { routeHmrStatement } from './route-hmr-statement'
|
|
4
|
-
import { debug
|
|
4
|
+
import { debug } from './utils'
|
|
5
5
|
import type { Config } from './config'
|
|
6
6
|
import type { UnpluginFactory } from 'unplugin'
|
|
7
7
|
|
|
@@ -17,32 +17,33 @@ export const unpluginRouterHmrFactory: UnpluginFactory<
|
|
|
17
17
|
let userConfig = options as Config
|
|
18
18
|
|
|
19
19
|
return {
|
|
20
|
-
name: 'router
|
|
20
|
+
name: 'tanstack-router:hmr',
|
|
21
21
|
enforce: 'pre',
|
|
22
22
|
|
|
23
|
-
transform
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
transform: {
|
|
24
|
+
filter: {
|
|
25
|
+
code: 'createFileRoute(',
|
|
26
|
+
},
|
|
27
|
+
handler(code, id) {
|
|
28
|
+
if (!globalThis.TSR_ROUTES_BY_ID_MAP?.has(id)) {
|
|
29
|
+
return null
|
|
30
|
+
}
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
ast.program.body.push(routeHmrStatement)
|
|
32
|
-
const result = generateFromAst(ast, {
|
|
33
|
-
sourceMaps: true,
|
|
34
|
-
filename: id,
|
|
35
|
-
sourceFileName: id,
|
|
36
|
-
})
|
|
37
|
-
if (debug) {
|
|
38
|
-
logDiff(code, result.code)
|
|
39
|
-
console.log('Output:\n', result.code + '\n\n')
|
|
40
|
-
}
|
|
41
|
-
return result
|
|
42
|
-
},
|
|
32
|
+
if (debug) console.info('Adding HMR handling to route ', id)
|
|
43
33
|
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
const ast = parseAst({ code })
|
|
35
|
+
ast.program.body.push(routeHmrStatement)
|
|
36
|
+
const result = generateFromAst(ast, {
|
|
37
|
+
sourceMaps: true,
|
|
38
|
+
filename: id,
|
|
39
|
+
sourceFileName: id,
|
|
40
|
+
})
|
|
41
|
+
if (debug) {
|
|
42
|
+
logDiff(code, result.code)
|
|
43
|
+
console.log('Output:\n', result.code + '\n\n')
|
|
44
|
+
}
|
|
45
|
+
return result
|
|
46
|
+
},
|
|
46
47
|
},
|
|
47
48
|
|
|
48
49
|
vite: {
|
package/src/core/utils.ts
CHANGED
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
import { isAbsolute, join, normalize } from 'node:path'
|
|
2
|
-
|
|
3
1
|
export const debug =
|
|
4
2
|
process.env.TSR_VITE_DEBUG &&
|
|
5
3
|
['true', 'router-plugin'].includes(process.env.TSR_VITE_DEBUG)
|
|
6
|
-
|
|
7
|
-
export function fileIsInRoutesDirectory(
|
|
8
|
-
filePath: string,
|
|
9
|
-
routesDirectory: string,
|
|
10
|
-
): boolean {
|
|
11
|
-
const routesDirectoryPath = isAbsolute(routesDirectory)
|
|
12
|
-
? routesDirectory
|
|
13
|
-
: join(process.cwd(), routesDirectory)
|
|
14
|
-
|
|
15
|
-
const path = normalize(filePath)
|
|
16
|
-
|
|
17
|
-
return path.startsWith(routesDirectoryPath)
|
|
18
|
-
}
|