@tanstack/router-plugin 1.121.0-alpha.27 → 1.121.0-alpha.28
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 +66 -40
- package/dist/cjs/core/code-splitter/compilers.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/compilers.d.cts +3 -3
- package/dist/cjs/core/code-splitter/framework-options.cjs.map +1 -1
- package/dist/cjs/core/code-splitter/path-ids.cjs.map +1 -1
- package/dist/cjs/core/config.cjs +11 -1
- package/dist/cjs/core/config.cjs.map +1 -1
- package/dist/cjs/core/config.d.cts +49 -4
- package/dist/cjs/core/route-autoimport-plugin.cjs +52 -45
- package/dist/cjs/core/route-autoimport-plugin.cjs.map +1 -1
- package/dist/cjs/core/route-hmr-statement.cjs.map +1 -1
- package/dist/cjs/core/router-code-splitter-plugin.cjs +84 -64
- package/dist/cjs/core/router-code-splitter-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-composed-plugin.cjs +12 -3
- package/dist/cjs/core/router-composed-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-generator-plugin.cjs +27 -25
- package/dist/cjs/core/router-generator-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.cjs +38 -29
- package/dist/cjs/core/router-hmr-plugin.cjs.map +1 -1
- package/dist/cjs/core/router-hmr-plugin.d.cts +1 -6
- 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/cjs/esbuild.cjs.map +1 -1
- package/dist/cjs/esbuild.d.cts +20 -0
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -0
- package/dist/cjs/rspack.cjs.map +1 -1
- package/dist/cjs/rspack.d.cts +20 -0
- package/dist/cjs/vite.cjs.map +1 -1
- package/dist/cjs/vite.d.cts +25 -0
- package/dist/cjs/webpack.cjs.map +1 -1
- package/dist/cjs/webpack.d.cts +20 -0
- package/dist/esm/core/code-splitter/compilers.d.ts +3 -3
- package/dist/esm/core/code-splitter/compilers.js +66 -40
- package/dist/esm/core/code-splitter/compilers.js.map +1 -1
- package/dist/esm/core/code-splitter/framework-options.js.map +1 -1
- package/dist/esm/core/code-splitter/path-ids.js.map +1 -1
- package/dist/esm/core/config.d.ts +49 -4
- package/dist/esm/core/config.js +11 -1
- package/dist/esm/core/config.js.map +1 -1
- package/dist/esm/core/route-autoimport-plugin.js +53 -46
- package/dist/esm/core/route-autoimport-plugin.js.map +1 -1
- package/dist/esm/core/route-hmr-statement.js.map +1 -1
- package/dist/esm/core/router-code-splitter-plugin.js +86 -66
- package/dist/esm/core/router-code-splitter-plugin.js.map +1 -1
- package/dist/esm/core/router-composed-plugin.js +11 -2
- package/dist/esm/core/router-composed-plugin.js.map +1 -1
- package/dist/esm/core/router-generator-plugin.js +28 -26
- package/dist/esm/core/router-generator-plugin.js.map +1 -1
- package/dist/esm/core/router-hmr-plugin.d.ts +1 -6
- package/dist/esm/core/router-hmr-plugin.js +39 -30
- 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/dist/esm/esbuild.d.ts +20 -0
- package/dist/esm/esbuild.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/rspack.d.ts +20 -0
- package/dist/esm/vite.d.ts +25 -0
- package/dist/esm/vite.js.map +1 -1
- package/dist/esm/webpack.d.ts +20 -0
- package/package.json +15 -15
- package/src/core/code-splitter/compilers.ts +95 -60
- package/src/core/config.ts +25 -0
- package/src/core/route-autoimport-plugin.ts +59 -53
- package/src/core/router-code-splitter-plugin.ts +95 -80
- package/src/core/router-composed-plugin.ts +12 -2
- package/src/core/router-generator-plugin.ts +32 -32
- package/src/core/router-hmr-plugin.ts +42 -37
- package/src/core/utils.ts +0 -15
- package/src/global.d.ts +7 -0
- package/src/index.ts +5 -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) {
|
|
@@ -120,11 +120,12 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
120
120
|
|
|
121
121
|
const compiledReferenceRoute = compileCodeSplitReferenceRoute({
|
|
122
122
|
code,
|
|
123
|
-
runtimeEnv: isProduction ? 'prod' : 'dev',
|
|
124
123
|
codeSplitGroupings: splitGroupings,
|
|
125
124
|
targetFramework: userConfig.target,
|
|
126
125
|
filename: id,
|
|
127
126
|
id,
|
|
127
|
+
deleteNodes: new Set(userConfig.codeSplittingOptions?.deleteNodes),
|
|
128
|
+
addHmr: (options.codeSplittingOptions?.addHmr ?? true) && !isProduction,
|
|
128
129
|
})
|
|
129
130
|
|
|
130
131
|
if (debug) {
|
|
@@ -171,83 +172,97 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
|
|
|
171
172
|
return result
|
|
172
173
|
}
|
|
173
174
|
|
|
174
|
-
|
|
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
|
-
|
|
175
|
+
const includedCode = [
|
|
176
|
+
'createFileRoute(',
|
|
177
|
+
'createRootRoute(',
|
|
178
|
+
'createRootRouteWithContext(',
|
|
179
|
+
]
|
|
180
|
+
return [
|
|
181
|
+
{
|
|
182
|
+
name: 'tanstack-router:code-splitter:compile-reference-file',
|
|
183
|
+
enforce: 'pre',
|
|
184
|
+
|
|
185
|
+
transform: {
|
|
186
|
+
filter: {
|
|
187
|
+
id: {
|
|
188
|
+
exclude: tsrSplit,
|
|
189
|
+
// this is necessary for webpack / rspack to avoid matching .html files
|
|
190
|
+
include: /\.(m|c)?(j|t)sx?$/,
|
|
191
|
+
},
|
|
192
|
+
code: {
|
|
193
|
+
include: includedCode,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
handler(code, id) {
|
|
197
|
+
const generatorFileInfo = globalThis.TSR_ROUTES_BY_ID_MAP?.get(id)
|
|
198
|
+
if (
|
|
199
|
+
generatorFileInfo &&
|
|
200
|
+
includedCode.some((included) => code.includes(included))
|
|
201
|
+
) {
|
|
202
|
+
for (const externalPlugin of bannedBeforeExternalPlugins) {
|
|
203
|
+
if (!externalPlugin.frameworks.includes(framework)) {
|
|
204
|
+
continue
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (code.includes(externalPlugin.identifier)) {
|
|
208
|
+
throw new FoundPluginInBeforeCode(externalPlugin, framework)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return handleCompilingReferenceFile(code, id, generatorFileInfo)
|
|
200
213
|
}
|
|
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
214
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
) {
|
|
218
|
-
return true
|
|
219
|
-
}
|
|
220
|
-
return false
|
|
221
|
-
},
|
|
215
|
+
return null
|
|
216
|
+
},
|
|
217
|
+
},
|
|
222
218
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
219
|
+
vite: {
|
|
220
|
+
configResolved(config) {
|
|
221
|
+
ROOT = config.root
|
|
222
|
+
userConfig = getConfig(options, ROOT)
|
|
223
|
+
},
|
|
224
|
+
applyToEnvironment(environment) {
|
|
225
|
+
if (userConfig.plugin?.vite?.environmentName) {
|
|
226
|
+
return userConfig.plugin.vite.environmentName === environment.name
|
|
227
|
+
}
|
|
228
|
+
return true
|
|
229
|
+
},
|
|
230
|
+
},
|
|
226
231
|
|
|
232
|
+
rspack() {
|
|
233
|
+
ROOT = process.cwd()
|
|
227
234
|
userConfig = getConfig(options, ROOT)
|
|
228
235
|
},
|
|
229
|
-
},
|
|
230
236
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
},
|
|
237
|
+
webpack(compiler) {
|
|
238
|
+
ROOT = process.cwd()
|
|
239
|
+
userConfig = getConfig(options, ROOT)
|
|
235
240
|
|
|
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)
|
|
241
|
+
if (compiler.options.mode === 'production') {
|
|
242
|
+
compiler.hooks.done.tap(PLUGIN_NAME, () => {
|
|
243
|
+
console.info('✅ ' + PLUGIN_NAME + ': code-splitting done!')
|
|
244
|
+
setTimeout(() => {
|
|
245
|
+
process.exit(0)
|
|
246
|
+
})
|
|
248
247
|
})
|
|
249
|
-
}
|
|
250
|
-
}
|
|
248
|
+
}
|
|
249
|
+
},
|
|
251
250
|
},
|
|
252
|
-
|
|
251
|
+
{
|
|
252
|
+
name: 'tanstack-router:code-splitter:compile-virtual-file',
|
|
253
|
+
enforce: 'pre',
|
|
254
|
+
|
|
255
|
+
transform: {
|
|
256
|
+
filter: {
|
|
257
|
+
id: /tsr-split/,
|
|
258
|
+
},
|
|
259
|
+
handler(code, id) {
|
|
260
|
+
const url = pathToFileURL(id)
|
|
261
|
+
url.searchParams.delete('v')
|
|
262
|
+
id = fileURLToPath(url).replace(/\\/g, '/')
|
|
263
|
+
return handleCompilingVirtualFile(code, id)
|
|
264
|
+
},
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
]
|
|
253
268
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getConfig } from '@tanstack/router-generator'
|
|
1
2
|
import { unpluginRouterGeneratorFactory } from './router-generator-plugin'
|
|
2
3
|
import { unpluginRouterCodeSplitterFactory } from './router-code-splitter-plugin'
|
|
3
4
|
import { unpluginRouterHmrFactory } from './router-hmr-plugin'
|
|
@@ -8,6 +9,9 @@ import type { UnpluginFactory } from 'unplugin'
|
|
|
8
9
|
export const unpluginRouterComposedFactory: UnpluginFactory<
|
|
9
10
|
Partial<Config> | undefined
|
|
10
11
|
> = (options = {}, meta) => {
|
|
12
|
+
const ROOT: string = process.cwd()
|
|
13
|
+
const userConfig = getConfig(options, ROOT)
|
|
14
|
+
|
|
11
15
|
const getPlugin = (pluginFactory: UnpluginFactory<Partial<Config>>) => {
|
|
12
16
|
const plugin = pluginFactory(options, meta)
|
|
13
17
|
if (!Array.isArray(plugin)) {
|
|
@@ -20,11 +24,17 @@ export const unpluginRouterComposedFactory: UnpluginFactory<
|
|
|
20
24
|
const routerCodeSplitter = getPlugin(unpluginRouterCodeSplitterFactory)
|
|
21
25
|
const routeAutoImport = getPlugin(unpluginRouteAutoImportFactory)
|
|
22
26
|
|
|
23
|
-
const result = [...routerGenerator
|
|
27
|
+
const result = [...routerGenerator]
|
|
28
|
+
if (userConfig.autoCodeSplitting) {
|
|
29
|
+
result.push(...routerCodeSplitter)
|
|
30
|
+
}
|
|
31
|
+
if (userConfig.verboseFileRoutes === false) {
|
|
32
|
+
result.push(...routeAutoImport)
|
|
33
|
+
}
|
|
24
34
|
|
|
25
35
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
26
36
|
|
|
27
|
-
if (!isProduction && !
|
|
37
|
+
if (!isProduction && !userConfig.autoCodeSplitting) {
|
|
28
38
|
const routerHmr = getPlugin(unpluginRouterHmrFactory)
|
|
29
39
|
result.push(...routerHmr)
|
|
30
40
|
}
|
|
@@ -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
51
|
|
|
45
|
-
const handleFile = async (
|
|
46
|
-
file: string,
|
|
47
|
-
event: 'create' | 'update' | 'delete',
|
|
48
|
-
) => {
|
|
49
|
-
const filePath = normalize(file)
|
|
50
|
-
|
|
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()
|
|
@@ -74,12 +73,13 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
74
73
|
configResolved() {
|
|
75
74
|
initConfigAndGenerator()
|
|
76
75
|
},
|
|
77
|
-
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
// for the client environment
|
|
81
|
-
return
|
|
76
|
+
applyToEnvironment(environment) {
|
|
77
|
+
if (userConfig.plugin?.vite?.environmentName) {
|
|
78
|
+
return userConfig.plugin.vite.environmentName === environment.name
|
|
82
79
|
}
|
|
80
|
+
return true
|
|
81
|
+
},
|
|
82
|
+
async buildStart() {
|
|
83
83
|
await generate()
|
|
84
84
|
},
|
|
85
85
|
sharedDuringBuild: true,
|
|
@@ -89,7 +89,7 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
89
89
|
|
|
90
90
|
let handle: FSWatcher | null = null
|
|
91
91
|
|
|
92
|
-
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate)
|
|
92
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate())
|
|
93
93
|
|
|
94
94
|
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
95
95
|
if (handle) {
|
|
@@ -101,7 +101,7 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
101
101
|
const chokidar = await import('chokidar')
|
|
102
102
|
handle = chokidar
|
|
103
103
|
.watch(routesDirectoryPath, { ignoreInitial: true })
|
|
104
|
-
.on('add', generate)
|
|
104
|
+
.on('add', (file) => generate({ file, event: 'create' }))
|
|
105
105
|
|
|
106
106
|
await generate()
|
|
107
107
|
})
|
|
@@ -113,11 +113,11 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
113
113
|
})
|
|
114
114
|
},
|
|
115
115
|
webpack(compiler) {
|
|
116
|
-
|
|
116
|
+
initConfigAndGenerator()
|
|
117
117
|
|
|
118
118
|
let handle: FSWatcher | null = null
|
|
119
119
|
|
|
120
|
-
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, generate)
|
|
120
|
+
compiler.hooks.beforeRun.tapPromise(PLUGIN_NAME, () => generate())
|
|
121
121
|
|
|
122
122
|
compiler.hooks.watchRun.tapPromise(PLUGIN_NAME, async () => {
|
|
123
123
|
if (handle) {
|
|
@@ -129,7 +129,7 @@ export const unpluginRouterGeneratorFactory: UnpluginFactory<
|
|
|
129
129
|
const chokidar = await import('chokidar')
|
|
130
130
|
handle = chokidar
|
|
131
131
|
.watch(routesDirectoryPath, { ignoreInitial: true })
|
|
132
|
-
.on('add', generate)
|
|
132
|
+
.on('add', (file) => generate({ file, event: 'create' }))
|
|
133
133
|
|
|
134
134
|
await generate()
|
|
135
135
|
})
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'
|
|
2
|
-
import { getConfig } from './config'
|
|
3
2
|
import { routeHmrStatement } from './route-hmr-statement'
|
|
4
|
-
import { debug
|
|
5
|
-
import
|
|
3
|
+
import { debug } from './utils'
|
|
4
|
+
import { getConfig } from './config'
|
|
6
5
|
import type { UnpluginFactory } from 'unplugin'
|
|
6
|
+
import type { Config } from './config'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* This plugin adds HMR support for file routes.
|
|
10
10
|
* It is only added to the composed plugin in dev when autoCodeSplitting is disabled, since the code splitting plugin
|
|
11
11
|
* handles HMR for code-split routes itself.
|
|
12
12
|
*/
|
|
13
|
+
|
|
14
|
+
const includeCode = [
|
|
15
|
+
'createFileRoute(',
|
|
16
|
+
'createRootRoute(',
|
|
17
|
+
'createRootRouteWithContext(',
|
|
18
|
+
]
|
|
13
19
|
export const unpluginRouterHmrFactory: UnpluginFactory<
|
|
14
20
|
Partial<Config> | undefined
|
|
15
21
|
> = (options = {}) => {
|
|
@@ -17,49 +23,48 @@ export const unpluginRouterHmrFactory: UnpluginFactory<
|
|
|
17
23
|
let userConfig = options as Config
|
|
18
24
|
|
|
19
25
|
return {
|
|
20
|
-
name: 'router
|
|
26
|
+
name: 'tanstack-router:hmr',
|
|
21
27
|
enforce: 'pre',
|
|
28
|
+
transform: {
|
|
29
|
+
filter: {
|
|
30
|
+
// this is necessary for webpack / rspack to avoid matching .html files
|
|
31
|
+
id: /\.(m|c)?(j|t)sx?$/,
|
|
32
|
+
code: {
|
|
33
|
+
include: includeCode,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
handler(code, id) {
|
|
37
|
+
if (!globalThis.TSR_ROUTES_BY_ID_MAP?.has(id)) {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
22
40
|
|
|
23
|
-
|
|
24
|
-
if (!code.includes('export const Route = createFileRoute(')) {
|
|
25
|
-
return null
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (debug) console.info('Adding HMR handling to route ', id)
|
|
29
|
-
|
|
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
|
-
},
|
|
41
|
+
if (debug) console.info('Adding HMR handling to route ', id)
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
const ast = parseAst({ code })
|
|
44
|
+
ast.program.body.push(routeHmrStatement)
|
|
45
|
+
const result = generateFromAst(ast, {
|
|
46
|
+
sourceMaps: true,
|
|
47
|
+
filename: id,
|
|
48
|
+
sourceFileName: id,
|
|
49
|
+
})
|
|
50
|
+
if (debug) {
|
|
51
|
+
logDiff(code, result.code)
|
|
52
|
+
console.log('Output:\n', result.code + '\n\n')
|
|
53
|
+
}
|
|
54
|
+
return result
|
|
55
|
+
},
|
|
46
56
|
},
|
|
47
|
-
|
|
48
57
|
vite: {
|
|
49
58
|
configResolved(config) {
|
|
50
59
|
ROOT = config.root
|
|
51
60
|
userConfig = getConfig(options, ROOT)
|
|
52
61
|
},
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
webpack() {
|
|
61
|
-
ROOT = process.cwd()
|
|
62
|
-
userConfig = getConfig(options, ROOT)
|
|
62
|
+
applyToEnvironment(environment) {
|
|
63
|
+
if (userConfig.plugin?.vite?.environmentName) {
|
|
64
|
+
return userConfig.plugin.vite.environmentName === environment.name
|
|
65
|
+
}
|
|
66
|
+
return true
|
|
67
|
+
},
|
|
63
68
|
},
|
|
64
69
|
}
|
|
65
70
|
}
|
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
|
-
}
|
package/src/global.d.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -2,3 +2,8 @@ export { configSchema, getConfig } from './core/config'
|
|
|
2
2
|
export { unpluginRouterCodeSplitterFactory } from './core/router-code-splitter-plugin'
|
|
3
3
|
export { unpluginRouterGeneratorFactory } from './core/router-generator-plugin'
|
|
4
4
|
export type { Config, ConfigInput, ConfigOutput } from './core/config'
|
|
5
|
+
export {
|
|
6
|
+
tsrSplit,
|
|
7
|
+
splitRouteIdentNodes,
|
|
8
|
+
defaultCodeSplitGroupings,
|
|
9
|
+
} from './core/constants'
|