@tanstack/router-plugin 1.121.0-alpha.4 → 1.121.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.
@@ -1,45 +1,44 @@
1
- import { isAbsolute, join, normalize, resolve } from 'node:path'
2
- import { generator, resolveConfigPath } from '@tanstack/router-generator'
1
+ import { isAbsolute, join, normalize } from 'node:path'
2
+ import { Generator, resolveConfigPath } from '@tanstack/router-generator'
3
3
 
4
4
  import { getConfig } from './config'
5
5
  import type { FSWatcher } from 'chokidar'
6
6
  import type { UnpluginFactory } from 'unplugin'
7
7
  import type { Config } from './config'
8
8
 
9
- let lock = false
10
- const checkLock = () => lock
11
- const setLock = (bool: boolean) => {
12
- lock = bool
13
- }
14
-
15
9
  const PLUGIN_NAME = 'unplugin:router-generator'
16
10
 
17
11
  export const unpluginRouterGeneratorFactory: UnpluginFactory<
18
12
  Partial<Config> | undefined
19
13
  > = (options = {}) => {
20
- let ROOT: string = process.cwd()
14
+ const ROOT: string = process.cwd()
21
15
  let userConfig = options as Config
16
+ let generator: Generator
22
17
 
18
+ const routeGenerationDisabled = () =>
19
+ userConfig.enableRouteGeneration === false
23
20
  const getRoutesDirectoryPath = () => {
24
21
  return isAbsolute(userConfig.routesDirectory)
25
22
  ? userConfig.routesDirectory
26
23
  : join(ROOT, userConfig.routesDirectory)
27
24
  }
28
25
 
26
+ const initConfigAndGenerator = () => {
27
+ userConfig = getConfig(options, ROOT)
28
+ generator = new Generator({
29
+ config: userConfig,
30
+ root: ROOT,
31
+ })
32
+ }
33
+
29
34
  const generate = async () => {
30
- if (checkLock()) {
35
+ if (routeGenerationDisabled()) {
31
36
  return
32
37
  }
33
-
34
- setLock(true)
35
-
36
38
  try {
37
- await generator(userConfig, process.cwd())
38
- } catch (err) {
39
- console.error(err)
40
- console.info()
41
- } finally {
42
- setLock(false)
39
+ await generator.run()
40
+ } catch (e) {
41
+ console.error(e)
43
42
  }
44
43
  }
45
44
 
@@ -50,45 +49,30 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
50
49
  const filePath = normalize(file)
51
50
 
52
51
  if (filePath === resolveConfigPath({ configDirectory: ROOT })) {
53
- userConfig = getConfig(options, ROOT)
54
- return
55
- }
56
-
57
- if (
58
- event === 'update' &&
59
- filePath === resolve(userConfig.generatedRouteTree)
60
- ) {
61
- // skip generating routes if the generated route tree is updated
52
+ initConfigAndGenerator()
62
53
  return
63
54
  }
64
-
65
- const routesDirectoryPath = getRoutesDirectoryPath()
66
- if (filePath.startsWith(routesDirectoryPath)) {
67
- await generate()
68
- }
69
- }
70
-
71
- const run: (cb: () => Promise<void> | void) => Promise<void> = async (cb) => {
72
- if (userConfig.enableRouteGeneration ?? true) {
73
- await cb()
55
+ try {
56
+ await generator.run({ path: filePath, type: event })
57
+ } catch (e) {
58
+ console.error(e)
74
59
  }
75
60
  }
76
61
 
77
62
  return {
78
63
  name: 'router-generator-plugin',
64
+ enforce: 'pre',
79
65
  async watchChange(id, { event }) {
80
- await run(async () => {
66
+ if (!routeGenerationDisabled()) {
81
67
  await handleFile(id, event)
82
- })
68
+ }
69
+ },
70
+ async buildStart() {
71
+ await generate()
83
72
  },
84
73
  vite: {
85
- configResolved(config) {
86
- ROOT = config.root
87
- userConfig = getConfig(options, ROOT)
88
-
89
- // if (config.command === 'serve') {
90
- // await run(generate)
91
- // }
74
+ configResolved() {
75
+ initConfigAndGenerator()
92
76
  },
93
77
  async buildStart() {
94
78
  if (this.environment.config.consumer === 'server') {
@@ -96,18 +80,16 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
96
80
  // for the client environment
97
81
  return
98
82
  }
99
- await run(generate)
83
+ await generate()
100
84
  },
101
85
  sharedDuringBuild: true,
102
86
  },
103
87
  rspack(compiler) {
104
- userConfig = getConfig(options, ROOT)
88
+ initConfigAndGenerator()
105
89
 
106
90
  let handle: FSWatcher | null = null
107
91
 
108
- compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, async () => {
109
- await run(generate)
110
- })
92
+ compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate)
111
93
 
112
94
  compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
113
95
  if (handle) {
@@ -119,11 +101,9 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
119
101
  const chokidar = await import('chokidar')
120
102
  handle = chokidar
121
103
  .watch(routesDirectoryPath, { ignoreInitial: true })
122
- .on('add', async () => {
123
- await run(generate)
124
- })
104
+ .on('add', generate)
125
105
 
126
- await run(generate)
106
+ await generate()
127
107
  })
128
108
 
129
109
  compiler.hooks.watchClose.tap(PLUGIN_NAME, async () => {
@@ -137,9 +117,7 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
137
117
 
138
118
  let handle: FSWatcher | null = null
139
119
 
140
- compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, async () => {
141
- await run(generate)
142
- })
120
+ compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate)
143
121
 
144
122
  compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
145
123
  if (handle) {
@@ -151,11 +129,9 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
151
129
  const chokidar = await import('chokidar')
152
130
  handle = chokidar
153
131
  .watch(routesDirectoryPath, { ignoreInitial: true })
154
- .on('add', async () => {
155
- await run(generate)
156
- })
132
+ .on('add', generate)
157
133
 
158
- await run(generate)
134
+ await generate()
159
135
  })
160
136
 
161
137
  compiler.hooks.watchClose.tap(PLUGIN_NAME, async () => {