@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.
Files changed (43) hide show
  1. package/dist/cjs/core/code-splitter/compilers.cjs +17 -18
  2. package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
  3. package/dist/cjs/core/code-splitter/compilers.d.cts +0 -1
  4. package/dist/cjs/core/config.d.cts +4 -4
  5. package/dist/cjs/core/route-autoimport-plugin.cjs +51 -45
  6. package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
  7. package/dist/cjs/core/router-code-splitter-plugin.cjs +65 -58
  8. package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
  9. package/dist/cjs/core/router-composed-plugin.cjs +7 -1
  10. package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
  11. package/dist/cjs/core/router-generator-plugin.cjs +18 -19
  12. package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
  13. package/dist/cjs/core/router-hmr-plugin.cjs +26 -24
  14. package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
  15. package/dist/cjs/core/utils.cjs +0 -7
  16. package/dist/cjs/core/utils.cjs.map +1 -1
  17. package/dist/cjs/core/utils.d.cts +0 -1
  18. package/dist/esm/core/code-splitter/compilers.d.ts +0 -1
  19. package/dist/esm/core/code-splitter/compilers.js +17 -18
  20. package/dist/esm/core/code-splitter/compilers.js.map +1 -1
  21. package/dist/esm/core/config.d.ts +4 -4
  22. package/dist/esm/core/route-autoimport-plugin.js +52 -46
  23. package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
  24. package/dist/esm/core/router-code-splitter-plugin.js +67 -60
  25. package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
  26. package/dist/esm/core/router-composed-plugin.js +7 -1
  27. package/dist/esm/core/router-composed-plugin.js.map +1 -1
  28. package/dist/esm/core/router-generator-plugin.js +19 -20
  29. package/dist/esm/core/router-generator-plugin.js.map +1 -1
  30. package/dist/esm/core/router-hmr-plugin.js +27 -25
  31. package/dist/esm/core/router-hmr-plugin.js.map +1 -1
  32. package/dist/esm/core/utils.d.ts +0 -1
  33. package/dist/esm/core/utils.js +1 -8
  34. package/dist/esm/core/utils.js.map +1 -1
  35. package/package.json +3 -3
  36. package/src/core/code-splitter/compilers.ts +25 -24
  37. package/src/core/route-autoimport-plugin.ts +57 -53
  38. package/src/core/router-code-splitter-plugin.ts +75 -79
  39. package/src/core/router-composed-plugin.ts +7 -1
  40. package/src/core/router-generator-plugin.ts +23 -24
  41. package/src/core/router-hmr-plugin.ts +24 -23
  42. package/src/core/utils.ts +0 -15
  43. 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, fileIsInRoutesDirectory } from './utils'
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(externalPlugin: BannedBeforeExternalPlugin, framework: string) {
52
- super(`We detected that the '${externalPlugin.pkg}' was passed before '@tanstack/router-plugin'. Please make sure that '@tanstack/router-plugin' is passed before '${externalPlugin.pkg}' and try again:
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
- TanStackRouter${capitalizeFirst(framework)}(), // Place this before ${externalPlugin.usage}
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: fromCode.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
- name: 'router-code-splitter-plugin',
176
- enforce: 'pre',
177
-
178
- transform(code, id) {
179
- if (!userConfig.autoCodeSplitting) {
180
- return null
181
- }
182
-
183
- const url = pathToFileURL(id)
184
- url.searchParams.delete('v')
185
- id = fileURLToPath(url).replace(/\\/g, '/')
186
-
187
- if (id.includes(tsrSplit)) {
188
- return handleCompilingVirtualFile(code, id)
189
- } else if (
190
- fileIsInRoutesDirectory(id, userConfig.routesDirectory) &&
191
- (code.includes('createRoute(') || code.includes('createFileRoute('))
192
- ) {
193
- for (const externalPlugin of bannedBeforeExternalPlugins) {
194
- if (!externalPlugin.frameworks.includes(framework)) {
195
- continue
196
- }
197
-
198
- if (code.includes(externalPlugin.identifier)) {
199
- throw new FoundPluginInBeforeCode(externalPlugin, framework)
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
- if (
215
- fileIsInRoutesDirectory(id, userConfig.routesDirectory) ||
216
- id.includes(tsrSplit)
217
- ) {
218
- return true
219
- }
220
- return false
221
- },
202
+ return null
203
+ },
204
+ },
222
205
 
223
- vite: {
224
- configResolved(config) {
225
- ROOT = config.root
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
- rspack(_compiler) {
232
- ROOT = process.cwd()
233
- userConfig = getConfig(options, ROOT)
234
- },
218
+ webpack(compiler) {
219
+ ROOT = process.cwd()
220
+ userConfig = getConfig(options, ROOT)
235
221
 
236
- webpack(compiler) {
237
- ROOT = process.cwd()
238
- userConfig = getConfig(options, ROOT)
239
-
240
- if (
241
- userConfig.autoCodeSplitting &&
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, ...routerCodeSplitter, ...routeAutoImport]
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
- try {
39
- await generator.run()
40
- } catch (e) {
41
- console.error(e)
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({ path: filePath, type: event })
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-plugin',
61
+ name: 'tanstack:router-generator',
64
62
  enforce: 'pre',
65
63
  async watchChange(id, { event }) {
66
- if (!routeGenerationDisabled()) {
67
- await handleFile(id, event)
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, fileIsInRoutesDirectory } from './utils'
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-hmr-plugin',
20
+ name: 'tanstack-router:hmr',
21
21
  enforce: 'pre',
22
22
 
23
- transform(code, id) {
24
- if (!code.includes('export const Route = createFileRoute(')) {
25
- return null
26
- }
27
-
28
- if (debug) console.info('Adding HMR handling to route ', id)
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
- const ast = parseAst({ code })
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
- transformInclude(id) {
45
- return fileIsInRoutesDirectory(id, userConfig.routesDirectory)
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
- }
@@ -0,0 +1,7 @@
1
+ /* eslint-disable no-var */
2
+ import type { GetRoutesByFileMapResult } from '@tanstack/router-generator'
3
+
4
+ declare global {
5
+ var TSR_ROUTES_BY_ID_MAP: GetRoutesByFileMapResult | undefined
6
+ }
7
+ export {}