@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.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cjs/ast.cjs +51 -0
- package/dist/cjs/ast.cjs.map +1 -0
- package/dist/cjs/ast.d.cts +22 -0
- package/dist/cjs/code-splitter.cjs +157 -0
- package/dist/cjs/code-splitter.cjs.map +1 -0
- package/dist/cjs/code-splitter.d.cts +22 -0
- package/dist/cjs/compilers.cjs +327 -0
- package/dist/cjs/compilers.cjs.map +1 -0
- package/dist/cjs/compilers.d.cts +18 -0
- package/dist/cjs/config.cjs +16 -0
- package/dist/cjs/config.cjs.map +1 -0
- package/dist/cjs/config.d.cts +79 -0
- package/dist/cjs/constants.cjs +7 -0
- package/dist/cjs/constants.cjs.map +1 -0
- package/dist/cjs/constants.d.cts +2 -0
- package/dist/cjs/eliminateUnreferencedIdentifiers.cjs +149 -0
- package/dist/cjs/eliminateUnreferencedIdentifiers.cjs.map +1 -0
- package/dist/cjs/eliminateUnreferencedIdentifiers.d.cts +9 -0
- package/dist/cjs/index.cjs +7 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +2 -0
- package/dist/cjs/router-generator.cjs +67 -0
- package/dist/cjs/router-generator.cjs.map +1 -0
- package/dist/cjs/router-generator.d.cts +18 -0
- package/dist/cjs/vite.cjs +16 -0
- package/dist/cjs/vite.cjs.map +1 -0
- package/dist/cjs/vite.d.cts +41 -0
- package/dist/esm/ast.d.ts +22 -0
- package/dist/esm/ast.js +34 -0
- package/dist/esm/ast.js.map +1 -0
- package/dist/esm/code-splitter.d.ts +22 -0
- package/dist/esm/code-splitter.js +157 -0
- package/dist/esm/code-splitter.js.map +1 -0
- package/dist/esm/compilers.d.ts +18 -0
- package/dist/esm/compilers.js +309 -0
- package/dist/esm/compilers.js.map +1 -0
- package/dist/esm/config.d.ts +79 -0
- package/dist/esm/config.js +16 -0
- package/dist/esm/config.js.map +1 -0
- package/dist/esm/constants.d.ts +2 -0
- package/dist/esm/constants.js +7 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/eliminateUnreferencedIdentifiers.d.ts +9 -0
- package/dist/esm/eliminateUnreferencedIdentifiers.js +132 -0
- package/dist/esm/eliminateUnreferencedIdentifiers.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/router-generator.d.ts +18 -0
- package/dist/esm/router-generator.js +67 -0
- package/dist/esm/router-generator.js.map +1 -0
- package/dist/esm/vite.d.ts +41 -0
- package/dist/esm/vite.js +16 -0
- package/dist/esm/vite.js.map +1 -0
- package/package.json +88 -0
- package/src/ast.ts +49 -0
- package/src/code-splitter.ts +162 -0
- package/src/compilers.ts +414 -0
- package/src/config.ts +25 -0
- package/src/constants.ts +2 -0
- package/src/eliminateUnreferencedIdentifiers.ts +211 -0
- package/src/index.ts +2 -0
- package/src/router-generator.ts +92 -0
- 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
|