@tanstack/router-plugin 1.39.2

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 (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/dist/cjs/ast.cjs +51 -0
  4. package/dist/cjs/ast.cjs.map +1 -0
  5. package/dist/cjs/ast.d.cts +22 -0
  6. package/dist/cjs/code-splitter.cjs +157 -0
  7. package/dist/cjs/code-splitter.cjs.map +1 -0
  8. package/dist/cjs/code-splitter.d.cts +22 -0
  9. package/dist/cjs/compilers.cjs +327 -0
  10. package/dist/cjs/compilers.cjs.map +1 -0
  11. package/dist/cjs/compilers.d.cts +18 -0
  12. package/dist/cjs/config.cjs +16 -0
  13. package/dist/cjs/config.cjs.map +1 -0
  14. package/dist/cjs/config.d.cts +79 -0
  15. package/dist/cjs/constants.cjs +7 -0
  16. package/dist/cjs/constants.cjs.map +1 -0
  17. package/dist/cjs/constants.d.cts +2 -0
  18. package/dist/cjs/eliminateUnreferencedIdentifiers.cjs +149 -0
  19. package/dist/cjs/eliminateUnreferencedIdentifiers.cjs.map +1 -0
  20. package/dist/cjs/eliminateUnreferencedIdentifiers.d.cts +9 -0
  21. package/dist/cjs/index.cjs +7 -0
  22. package/dist/cjs/index.cjs.map +1 -0
  23. package/dist/cjs/index.d.cts +2 -0
  24. package/dist/cjs/router-generator.cjs +67 -0
  25. package/dist/cjs/router-generator.cjs.map +1 -0
  26. package/dist/cjs/router-generator.d.cts +18 -0
  27. package/dist/cjs/vite.cjs +16 -0
  28. package/dist/cjs/vite.cjs.map +1 -0
  29. package/dist/cjs/vite.d.cts +41 -0
  30. package/dist/esm/ast.d.ts +22 -0
  31. package/dist/esm/ast.js +34 -0
  32. package/dist/esm/ast.js.map +1 -0
  33. package/dist/esm/code-splitter.d.ts +22 -0
  34. package/dist/esm/code-splitter.js +157 -0
  35. package/dist/esm/code-splitter.js.map +1 -0
  36. package/dist/esm/compilers.d.ts +18 -0
  37. package/dist/esm/compilers.js +309 -0
  38. package/dist/esm/compilers.js.map +1 -0
  39. package/dist/esm/config.d.ts +79 -0
  40. package/dist/esm/config.js +16 -0
  41. package/dist/esm/config.js.map +1 -0
  42. package/dist/esm/constants.d.ts +2 -0
  43. package/dist/esm/constants.js +7 -0
  44. package/dist/esm/constants.js.map +1 -0
  45. package/dist/esm/eliminateUnreferencedIdentifiers.d.ts +9 -0
  46. package/dist/esm/eliminateUnreferencedIdentifiers.js +132 -0
  47. package/dist/esm/eliminateUnreferencedIdentifiers.js.map +1 -0
  48. package/dist/esm/index.d.ts +2 -0
  49. package/dist/esm/index.js +7 -0
  50. package/dist/esm/index.js.map +1 -0
  51. package/dist/esm/router-generator.d.ts +18 -0
  52. package/dist/esm/router-generator.js +67 -0
  53. package/dist/esm/router-generator.js.map +1 -0
  54. package/dist/esm/vite.d.ts +41 -0
  55. package/dist/esm/vite.js +16 -0
  56. package/dist/esm/vite.js.map +1 -0
  57. package/package.json +88 -0
  58. package/src/ast.ts +49 -0
  59. package/src/code-splitter.ts +162 -0
  60. package/src/compilers.ts +414 -0
  61. package/src/config.ts +25 -0
  62. package/src/constants.ts +2 -0
  63. package/src/eliminateUnreferencedIdentifiers.ts +211 -0
  64. package/src/index.ts +2 -0
  65. package/src/router-generator.ts +92 -0
  66. package/src/vite.ts +19 -0
@@ -0,0 +1,92 @@
1
+ import { isAbsolute, join, normalize, resolve } from 'node:path'
2
+ import { createUnplugin } from 'unplugin'
3
+ import { generator } from '@tanstack/router-generator'
4
+
5
+ import { getConfig } from './config'
6
+ import { CONFIG_FILE_NAME } from './constants'
7
+ import type { PluginOptions } from './config'
8
+ import type { UnpluginFactory } from 'unplugin'
9
+
10
+ let lock = false
11
+ const checkLock = () => lock
12
+ const setLock = (bool: boolean) => {
13
+ lock = bool
14
+ }
15
+
16
+ const unpluginFactory: UnpluginFactory<Partial<PluginOptions>> = (
17
+ options = {},
18
+ ) => {
19
+ let ROOT: string = process.cwd()
20
+ let userConfig = options as PluginOptions
21
+
22
+ const generate = async () => {
23
+ if (checkLock()) {
24
+ return
25
+ }
26
+
27
+ setLock(true)
28
+
29
+ try {
30
+ await generator(userConfig)
31
+ } catch (err) {
32
+ console.error(err)
33
+ console.info()
34
+ } finally {
35
+ setLock(false)
36
+ }
37
+ }
38
+
39
+ const handleFile = async (
40
+ file: string,
41
+ event: 'create' | 'update' | 'delete',
42
+ ) => {
43
+ const filePath = normalize(file)
44
+
45
+ if (filePath === join(ROOT, CONFIG_FILE_NAME)) {
46
+ userConfig = await getConfig(options, ROOT)
47
+ return
48
+ }
49
+
50
+ if (
51
+ event === 'update' &&
52
+ filePath === resolve(userConfig.generatedRouteTree)
53
+ ) {
54
+ // skip generating routes if the generated route tree is updated
55
+ return
56
+ }
57
+
58
+ const routesDirectoryPath = isAbsolute(userConfig.routesDirectory)
59
+ ? userConfig.routesDirectory
60
+ : join(ROOT, userConfig.routesDirectory)
61
+
62
+ if (filePath.startsWith(routesDirectoryPath)) {
63
+ await generate()
64
+ }
65
+ }
66
+
67
+ const run: (cb: () => Promise<void> | void) => Promise<void> = async (cb) => {
68
+ if (userConfig.enableRouteGeneration ?? true) {
69
+ await cb()
70
+ }
71
+ }
72
+
73
+ return {
74
+ name: 'router-generator-plugin',
75
+ async watchChange(id, { event }) {
76
+ await run(async () => {
77
+ await handleFile(id, event)
78
+ })
79
+ },
80
+ vite: {
81
+ async configResolved(config) {
82
+ ROOT = config.root
83
+ userConfig = await getConfig(options, ROOT)
84
+
85
+ await run(generate)
86
+ },
87
+ },
88
+ }
89
+ }
90
+
91
+ export const unpluginRouterGenerator =
92
+ /* #__PURE__ */ createUnplugin(unpluginFactory)
package/src/vite.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { unpluginRouterCodeSplitter } from './code-splitter'
2
+ import { unpluginRouterGenerator } from './router-generator'
3
+
4
+ import type { PluginOptions } from './config'
5
+ import type { VitePlugin } from 'unplugin'
6
+
7
+ export const TanStackRouterGeneratorVite = unpluginRouterGenerator.vite
8
+ export const TanStackRouterCodeSplitterVite = unpluginRouterCodeSplitter.vite
9
+
10
+ export function TanStackRouterVite(
11
+ inlineConfig: Partial<PluginOptions>,
12
+ ): Array<VitePlugin> {
13
+ return [
14
+ TanStackRouterGeneratorVite(inlineConfig) as VitePlugin,
15
+ TanStackRouterCodeSplitterVite(inlineConfig) as VitePlugin,
16
+ ]
17
+ }
18
+
19
+ export type Config = PluginOptions