@tanstack/start-plugin-core 1.132.0-alpha.9 → 1.132.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.
- package/dist/esm/constants.d.ts +2 -1
- package/dist/esm/constants.js +3 -2
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/create-server-fn-plugin/compiler.d.ts +8 -5
- package/dist/esm/create-server-fn-plugin/compiler.js +74 -46
- package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -1
- package/dist/esm/create-server-fn-plugin/handleCreateMiddleware.d.ts +5 -0
- package/dist/esm/{start-compiler-plugin/middleware.js → create-server-fn-plugin/handleCreateMiddleware.js} +11 -11
- package/dist/esm/create-server-fn-plugin/handleCreateMiddleware.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js +8 -8
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -1
- package/dist/esm/create-server-fn-plugin/plugin.d.ts +1 -1
- package/dist/esm/create-server-fn-plugin/plugin.js +22 -7
- package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -1
- package/dist/esm/dev-server-plugin/plugin.d.ts +4 -2
- package/dist/esm/dev-server-plugin/plugin.js +6 -2
- package/dist/esm/dev-server-plugin/plugin.js.map +1 -1
- package/dist/esm/plugin.d.ts +12 -6
- package/dist/esm/plugin.js +54 -65
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/resolve-entries.d.ts +0 -1
- package/dist/esm/resolve-entries.js +1 -1
- package/dist/esm/resolve-entries.js.map +1 -1
- package/dist/esm/schema.d.ts +375 -308
- package/dist/esm/schema.js +23 -11
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/start-compiler-plugin/compilers.js +17 -49
- package/dist/esm/start-compiler-plugin/compilers.js.map +1 -1
- package/dist/esm/start-compiler-plugin/constants.d.ts +1 -1
- package/dist/esm/start-compiler-plugin/constants.js +1 -5
- package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
- package/dist/esm/start-router-plugin/constants.d.ts +1 -0
- package/dist/esm/start-router-plugin/constants.js +5 -0
- package/dist/esm/start-router-plugin/constants.js.map +1 -0
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js +3 -9
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/plugin.d.ts +3 -2
- package/dist/esm/start-router-plugin/plugin.js +191 -31
- package/dist/esm/start-router-plugin/plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.d.ts +8 -0
- package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.js +34 -0
- package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.js.map +1 -0
- package/package.json +8 -8
- package/src/constants.ts +3 -2
- package/src/create-server-fn-plugin/compiler.ts +99 -57
- package/src/{start-compiler-plugin/middleware.ts → create-server-fn-plugin/handleCreateMiddleware.ts} +15 -12
- package/src/create-server-fn-plugin/handleCreateServerFn.ts +11 -8
- package/src/create-server-fn-plugin/plugin.ts +24 -9
- package/src/dev-server-plugin/plugin.ts +6 -3
- package/src/plugin.ts +76 -84
- package/src/resolve-entries.ts +1 -2
- package/src/schema.ts +31 -14
- package/src/start-compiler-plugin/compilers.ts +18 -51
- package/src/start-compiler-plugin/constants.ts +0 -4
- package/src/start-router-plugin/constants.ts +1 -0
- package/src/start-router-plugin/generator-plugins/routes-manifest-plugin.ts +3 -9
- package/src/start-router-plugin/plugin.ts +233 -45
- package/src/start-router-plugin/pruneServerOnlySubtrees.ts +51 -0
- package/dist/esm/debug.js +0 -5
- package/dist/esm/debug.js.map +0 -1
- package/dist/esm/start-compiler-plugin/middleware.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/middleware.js.map +0 -1
- package/dist/esm/start-compiler-plugin/serverFileRoute.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/serverFileRoute.js +0 -38
- package/dist/esm/start-compiler-plugin/serverFileRoute.js.map +0 -1
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.d.ts +0 -2
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js +0 -119
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js.map +0 -1
- package/dist/esm/start-router-plugin/route-tree-client-plugin.d.ts +0 -6
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js +0 -56
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js.map +0 -1
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.d.ts +0 -3
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js +0 -29
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js.map +0 -1
- package/src/start-compiler-plugin/serverFileRoute.ts +0 -59
- package/src/start-router-plugin/generator-plugins/server-routes-plugin.ts +0 -138
- package/src/start-router-plugin/route-tree-client-plugin.ts +0 -77
- package/src/start-router-plugin/virtual-route-tree-plugin.ts +0 -29
|
@@ -1,59 +1,247 @@
|
|
|
1
|
-
/*
|
|
2
|
-
what is this plugin doing, especially compared to one already existing in the @tanstack/router-plugin package?
|
|
3
|
-
|
|
4
|
-
it configures:
|
|
5
|
-
1. the generator to generate both the render-route-tree as well as the server-route-tree
|
|
6
|
-
2. the code-splitter plugin, so it could possibly be enabled per environment (e.g. disable on the server)
|
|
7
|
-
3. the auto import plugin for both environments
|
|
8
|
-
4. the route tree client plugin, which removes the server part from the generated route tree
|
|
9
|
-
5. the virtual route tree plugin, which provides the route tree to the server
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
1
|
import {
|
|
13
2
|
tanStackRouterCodeSplitter,
|
|
14
3
|
tanstackRouterAutoImport,
|
|
15
4
|
tanstackRouterGenerator,
|
|
16
5
|
} from '@tanstack/router-plugin/vite'
|
|
6
|
+
import { normalizePath } from 'vite'
|
|
7
|
+
import path from 'pathe'
|
|
17
8
|
import { VITE_ENVIRONMENT_NAMES } from '../constants'
|
|
18
|
-
import { routeTreeClientPlugin } from './route-tree-client-plugin'
|
|
19
|
-
import { virtualRouteTreePlugin } from './virtual-route-tree-plugin'
|
|
20
9
|
import { routesManifestPlugin } from './generator-plugins/routes-manifest-plugin'
|
|
21
|
-
import {
|
|
22
|
-
import
|
|
23
|
-
import type {
|
|
10
|
+
import { pruneServerOnlySubtrees } from './pruneServerOnlySubtrees'
|
|
11
|
+
import { SERVER_PROP } from './constants'
|
|
12
|
+
import type {
|
|
13
|
+
Generator,
|
|
14
|
+
GeneratorPlugin,
|
|
15
|
+
RouteNode,
|
|
16
|
+
} from '@tanstack/router-generator'
|
|
17
|
+
import type { DevEnvironment, Plugin, PluginOption } from 'vite'
|
|
18
|
+
import type { TanStackStartInputConfig } from '../schema'
|
|
19
|
+
import type { GetConfigFn, TanStackStartVitePluginCoreOptions } from '../plugin'
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
21
|
+
function isServerOnlyNode(node: RouteNode | undefined) {
|
|
22
|
+
if (!node?.createFileRouteProps) {
|
|
23
|
+
return false
|
|
24
|
+
}
|
|
25
|
+
return (
|
|
26
|
+
node.createFileRouteProps.has(SERVER_PROP) &&
|
|
27
|
+
node.createFileRouteProps.size === 1
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function moduleDeclaration({
|
|
32
|
+
startFilePath,
|
|
33
|
+
routerFilePath,
|
|
34
|
+
corePluginOpts,
|
|
35
|
+
generatedRouteTreePath,
|
|
36
|
+
}: {
|
|
37
|
+
startFilePath: string | undefined
|
|
38
|
+
routerFilePath: string
|
|
39
|
+
corePluginOpts: TanStackStartVitePluginCoreOptions
|
|
40
|
+
generatedRouteTreePath: string
|
|
41
|
+
}): string {
|
|
42
|
+
function getImportPath(absolutePath: string) {
|
|
43
|
+
let relativePath = path.relative(
|
|
44
|
+
path.dirname(generatedRouteTreePath),
|
|
45
|
+
absolutePath,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
if (!relativePath.startsWith('.')) {
|
|
49
|
+
relativePath = './' + relativePath
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// convert to POSIX-style for ESM imports (important on Windows)
|
|
53
|
+
relativePath = relativePath.split(path.sep).join('/')
|
|
54
|
+
return relativePath
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const result: Array<string> = [
|
|
58
|
+
`import type { getRouter } from '${getImportPath(routerFilePath)}'`,
|
|
59
|
+
]
|
|
60
|
+
if (startFilePath) {
|
|
61
|
+
result.push(
|
|
62
|
+
`import type { startInstance } from '${getImportPath(startFilePath)}'`,
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
// make sure we import something from start to get the server route declaration merge
|
|
66
|
+
else {
|
|
67
|
+
result.push(
|
|
68
|
+
`import type { createStart } from '@tanstack/${corePluginOpts.framework}-start'`,
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
result.push(
|
|
72
|
+
`declare module '@tanstack/${corePluginOpts.framework}-start' {
|
|
73
|
+
interface Register {
|
|
74
|
+
router: Awaited<ReturnType<typeof getRouter>>`,
|
|
75
|
+
)
|
|
76
|
+
if (startFilePath) {
|
|
77
|
+
result.push(
|
|
78
|
+
` config: Awaited<ReturnType<typeof startInstance.getOptions>>`,
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
result.push(` }
|
|
82
|
+
}`)
|
|
83
|
+
|
|
84
|
+
return result.join('\n')
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function tanStackStartRouter(
|
|
88
|
+
startPluginOpts: TanStackStartInputConfig,
|
|
89
|
+
getConfig: GetConfigFn,
|
|
90
|
+
corePluginOpts: TanStackStartVitePluginCoreOptions,
|
|
91
|
+
): Array<PluginOption> {
|
|
92
|
+
const getGeneratedRouteTreePath = () => {
|
|
93
|
+
const { startConfig } = getConfig()
|
|
94
|
+
return path.resolve(startConfig.router.generatedRouteTree)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let clientEnvironment: DevEnvironment | null = null
|
|
98
|
+
function invalidate() {
|
|
99
|
+
if (!clientEnvironment) {
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const mod = clientEnvironment.moduleGraph.getModuleById(
|
|
104
|
+
getGeneratedRouteTreePath(),
|
|
105
|
+
)
|
|
106
|
+
if (mod) {
|
|
107
|
+
clientEnvironment.moduleGraph.invalidateModule(mod)
|
|
108
|
+
}
|
|
109
|
+
clientEnvironment.hot.send({ type: 'full-reload', path: '*' })
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
let generatorInstance: Generator | null = null
|
|
113
|
+
|
|
114
|
+
const clientTreeGeneratorPlugin: GeneratorPlugin = {
|
|
115
|
+
name: 'start-client-tree-plugin',
|
|
116
|
+
init({ generator }) {
|
|
117
|
+
generatorInstance = generator
|
|
118
|
+
},
|
|
119
|
+
afterTransform({ node, prevNode }) {
|
|
120
|
+
if (isServerOnlyNode(node) !== isServerOnlyNode(prevNode)) {
|
|
121
|
+
invalidate()
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
let routeTreeFileFooter: Array<string> | null = null
|
|
127
|
+
|
|
128
|
+
function getRouteTreeFileFooter() {
|
|
129
|
+
if (routeTreeFileFooter) {
|
|
130
|
+
return routeTreeFileFooter
|
|
131
|
+
}
|
|
132
|
+
const { startConfig, resolvedStartConfig } = getConfig()
|
|
133
|
+
const ogRouteTreeFileFooter = startConfig.router.routeTreeFileFooter
|
|
134
|
+
if (ogRouteTreeFileFooter) {
|
|
135
|
+
if (Array.isArray(ogRouteTreeFileFooter)) {
|
|
136
|
+
routeTreeFileFooter = ogRouteTreeFileFooter
|
|
137
|
+
} else {
|
|
138
|
+
routeTreeFileFooter = ogRouteTreeFileFooter()
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
routeTreeFileFooter = [
|
|
142
|
+
moduleDeclaration({
|
|
143
|
+
generatedRouteTreePath: getGeneratedRouteTreePath(),
|
|
144
|
+
corePluginOpts,
|
|
145
|
+
startFilePath: resolvedStartConfig.startFilePath,
|
|
146
|
+
routerFilePath: resolvedStartConfig.routerFilePath,
|
|
147
|
+
}),
|
|
148
|
+
...(routeTreeFileFooter ?? []),
|
|
149
|
+
]
|
|
150
|
+
return routeTreeFileFooter
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let resolvedGeneratedRouteTreePath: string | null = null
|
|
154
|
+
const clientTreePlugin: Plugin = {
|
|
155
|
+
name: 'tanstack-start:route-tree-client-plugin',
|
|
156
|
+
enforce: 'pre',
|
|
157
|
+
applyToEnvironment: (env) => env.name === VITE_ENVIRONMENT_NAMES.client,
|
|
158
|
+
configureServer(server) {
|
|
159
|
+
clientEnvironment = server.environments[VITE_ENVIRONMENT_NAMES.client]
|
|
160
|
+
},
|
|
161
|
+
config() {
|
|
162
|
+
type LoadObjectHook = Extract<
|
|
163
|
+
typeof clientTreePlugin.load,
|
|
164
|
+
{ filter?: unknown }
|
|
165
|
+
>
|
|
166
|
+
resolvedGeneratedRouteTreePath = normalizePath(
|
|
167
|
+
getGeneratedRouteTreePath(),
|
|
168
|
+
)
|
|
169
|
+
;(clientTreePlugin.load as LoadObjectHook).filter = {
|
|
170
|
+
id: { include: new RegExp(resolvedGeneratedRouteTreePath) },
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
|
|
174
|
+
load: {
|
|
175
|
+
filter: {
|
|
176
|
+
// this will be set in the config hook above since it relies on `config` hook being called first
|
|
40
177
|
},
|
|
41
|
-
|
|
42
|
-
|
|
178
|
+
async handler() {
|
|
179
|
+
if (!generatorInstance) {
|
|
180
|
+
throw new Error('Generator instance not initialized')
|
|
181
|
+
}
|
|
182
|
+
const crawlingResult = await generatorInstance.getCrawlingResult()
|
|
183
|
+
if (!crawlingResult) {
|
|
184
|
+
throw new Error('Crawling result not available')
|
|
185
|
+
}
|
|
186
|
+
const prunedAcc = pruneServerOnlySubtrees(crawlingResult)
|
|
187
|
+
const acc = {
|
|
188
|
+
...crawlingResult.acc,
|
|
189
|
+
...prunedAcc,
|
|
190
|
+
}
|
|
191
|
+
const buildResult = generatorInstance.buildRouteTree({
|
|
192
|
+
...crawlingResult,
|
|
193
|
+
acc,
|
|
194
|
+
config: {
|
|
195
|
+
// importRoutesUsingAbsolutePaths: true,
|
|
196
|
+
// addExtensions: true,
|
|
197
|
+
disableTypes: true,
|
|
198
|
+
enableRouteTreeFormatting: false,
|
|
199
|
+
routeTreeFileHeader: [],
|
|
200
|
+
routeTreeFileFooter: [],
|
|
201
|
+
},
|
|
202
|
+
})
|
|
203
|
+
return { code: buildResult.routeTreeContent, map: null }
|
|
43
204
|
},
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
return [
|
|
208
|
+
clientTreePlugin,
|
|
209
|
+
tanstackRouterGenerator(() => {
|
|
210
|
+
const routerConfig = getConfig().startConfig.router
|
|
211
|
+
return {
|
|
212
|
+
...routerConfig,
|
|
213
|
+
target: corePluginOpts.framework,
|
|
214
|
+
routeTreeFileFooter: getRouteTreeFileFooter,
|
|
215
|
+
plugins: [clientTreeGeneratorPlugin, routesManifestPlugin()],
|
|
216
|
+
}
|
|
44
217
|
}),
|
|
45
|
-
tanStackRouterCodeSplitter({
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
...
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
218
|
+
tanStackRouterCodeSplitter(() => {
|
|
219
|
+
const routerConfig = getConfig().startConfig.router
|
|
220
|
+
return {
|
|
221
|
+
...routerConfig,
|
|
222
|
+
codeSplittingOptions: {
|
|
223
|
+
...routerConfig.codeSplittingOptions,
|
|
224
|
+
deleteNodes: ['ssr', 'server'],
|
|
225
|
+
addHmr: true,
|
|
226
|
+
},
|
|
227
|
+
plugin: {
|
|
228
|
+
vite: { environmentName: VITE_ENVIRONMENT_NAMES.client },
|
|
229
|
+
},
|
|
230
|
+
}
|
|
231
|
+
}),
|
|
232
|
+
tanStackRouterCodeSplitter(() => {
|
|
233
|
+
const routerConfig = getConfig().startConfig.router
|
|
234
|
+
return {
|
|
235
|
+
...routerConfig,
|
|
236
|
+
codeSplittingOptions: {
|
|
237
|
+
...routerConfig.codeSplittingOptions,
|
|
238
|
+
addHmr: false,
|
|
239
|
+
},
|
|
240
|
+
plugin: {
|
|
241
|
+
vite: { environmentName: VITE_ENVIRONMENT_NAMES.server },
|
|
242
|
+
},
|
|
243
|
+
}
|
|
54
244
|
}),
|
|
55
|
-
tanstackRouterAutoImport(
|
|
56
|
-
routeTreeClientPlugin(config),
|
|
57
|
-
virtualRouteTreePlugin(config),
|
|
245
|
+
tanstackRouterAutoImport(startPluginOpts?.router),
|
|
58
246
|
]
|
|
59
247
|
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SERVER_PROP } from './constants'
|
|
2
|
+
import type {
|
|
3
|
+
HandleNodeAccumulator,
|
|
4
|
+
RouteNode,
|
|
5
|
+
} from '@tanstack/router-generator'
|
|
6
|
+
|
|
7
|
+
export function pruneServerOnlySubtrees({
|
|
8
|
+
rootRouteNode,
|
|
9
|
+
acc,
|
|
10
|
+
}: {
|
|
11
|
+
rootRouteNode: RouteNode
|
|
12
|
+
acc: HandleNodeAccumulator
|
|
13
|
+
}) {
|
|
14
|
+
const routeNodes: Array<RouteNode> = []
|
|
15
|
+
const routeTree =
|
|
16
|
+
prune({ ...rootRouteNode, children: acc.routeTree }, routeNodes)
|
|
17
|
+
?.children || []
|
|
18
|
+
// remove root node from routeNodes
|
|
19
|
+
routeNodes.pop()
|
|
20
|
+
return {
|
|
21
|
+
routeTree,
|
|
22
|
+
routeNodes,
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function prune(
|
|
26
|
+
node: RouteNode,
|
|
27
|
+
collectedRouteNodes: Array<RouteNode>,
|
|
28
|
+
): RouteNode | null {
|
|
29
|
+
const newChildren: Array<RouteNode> = []
|
|
30
|
+
let allChildrenServerOnly = true
|
|
31
|
+
|
|
32
|
+
for (const child of node.children || []) {
|
|
33
|
+
const newChild = prune(child, collectedRouteNodes)
|
|
34
|
+
if (newChild) {
|
|
35
|
+
newChildren.push(newChild)
|
|
36
|
+
// at least one child survived pruning
|
|
37
|
+
allChildrenServerOnly = false
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const allServerOnly =
|
|
42
|
+
node.createFileRouteProps?.has(SERVER_PROP) &&
|
|
43
|
+
node.createFileRouteProps.size === 1 &&
|
|
44
|
+
allChildrenServerOnly
|
|
45
|
+
// prune this subtree
|
|
46
|
+
if (allServerOnly) {
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
collectedRouteNodes.push(node)
|
|
50
|
+
return { ...node, children: newChildren }
|
|
51
|
+
}
|
package/dist/esm/debug.js
DELETED
package/dist/esm/debug.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"debug.js","sources":["../../src/debug.ts"],"sourcesContent":["export const debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin-core'].includes(process.env.TSR_VITE_DEBUG)\n"],"names":[],"mappings":"AAAO,MAAM,QACX,QAAQ,IAAI,kBACZ,CAAC,QAAQ,mBAAmB,EAAE,SAAS,QAAQ,IAAI,cAAc;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","sources":["../../../src/start-compiler-plugin/middleware.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { getRootCallExpression } from './utils'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nexport function handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createMiddleware().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n"],"names":[],"mappings":";;AAMO,SAAS,qCACd,MACA,MACA;AACA,QAAM,qBAAqB,sBAAsB,IAAI;AAQrD,QAAM,sBAAsB;AAAA,IAC1B,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,QAAQ;AAAA,IACR,QAAQ;AAAA,EAAA;AAGV,QAAM,eAAe,OAAO,KAAK,mBAAmB;AAEpD,qBAAmB,SAAS;AAAA,IAC1B,iBAAiB,sBAAsB;AACrC,UAAI,EAAE,aAAa,qBAAqB,KAAK,QAAQ,GAAG;AACtD,cAAM,OAAO,qBAAqB,KAAK,SACpC;AAEH,YACE,aAAa,SAAS,IAAI,KAC1B,qBAAqB,WAAW,oBAChC;AACA,8BAAoB,IAAI,IAAI,qBAAqB;AAAA,QACnD;AAAA,MACF;AAAA,IACF;AAAA,EAAA,CACD;AAED,MAAI,oBAAoB,WAAW;AACjC,UAAM,uBAAuB,oBAAoB,UAAU,KAAK,UAAU,CAAC;AAE3E,QAAI,CAAC,sBAAsB;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,MAAA;AAAA,IAEJ;AAGA,QAAI,KAAK,QAAQ,UAAU;AACzB,UAAI,EAAE,mBAAmB,oBAAoB,UAAU,KAAK,MAAM,GAAG;AACnE,4BAAoB,UAAU;AAAA,UAC5B,oBAAoB,UAAU,KAAK,OAAO;AAAA,QAAA;AAAA,MAE9C;AAAA,IACF;AAAA,EACF;AAEA,QAAM,eAAe,oBAAoB,QAAQ;AAAA,IAC/C;AAAA,EAAA;AAGF,MACE,oBAAoB,UACpB,aAAa,QACb,KAAK,QAAQ,UACb;AAEA,QAAI,EAAE,mBAAmB,oBAAoB,OAAO,KAAK,MAAM,GAAG;AAChE,0BAAoB,OAAO;AAAA,QACzB,oBAAoB,OAAO,KAAK,OAAO;AAAA,MAAA;AAAA,IAE3C;AAAA,EACF;AACF;"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { CompileOptions, CompileStartFrameworkOptions } from './compilers.js';
|
|
2
|
-
import * as t from '@babel/types';
|
|
3
|
-
import type * as babel from '@babel/core';
|
|
4
|
-
export declare function handleCreateServerFileRouteCallExpressionFactory(framework: CompileStartFrameworkOptions, method: 'createServerFileRoute' | 'createServerRoute' | 'createServerRootRoute'): (path: babel.NodePath<t.CallExpression>, opts: CompileOptions) => void;
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import * as t from "@babel/types";
|
|
2
|
-
function handleCreateServerFileRouteCallExpressionFactory(framework, method) {
|
|
3
|
-
return function handleCreateServerFileRouteCallExpression(path, opts) {
|
|
4
|
-
const PACKAGES = { start: `@tanstack/${framework}-start/server` };
|
|
5
|
-
let highestParent = path;
|
|
6
|
-
while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {
|
|
7
|
-
highestParent = highestParent.parentPath;
|
|
8
|
-
}
|
|
9
|
-
const programPath = highestParent.parentPath;
|
|
10
|
-
if (opts.env === "client") {
|
|
11
|
-
highestParent.remove();
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
let isCreateServerFileRouteImported = false;
|
|
15
|
-
programPath.traverse({
|
|
16
|
-
ImportDeclaration(importPath) {
|
|
17
|
-
const importSource = importPath.node.source.value;
|
|
18
|
-
if (importSource === PACKAGES.start) {
|
|
19
|
-
const specifiers = importPath.node.specifiers;
|
|
20
|
-
isCreateServerFileRouteImported ||= specifiers.some((specifier) => {
|
|
21
|
-
return t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported) && specifier.imported.name === method;
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
if (!isCreateServerFileRouteImported) {
|
|
27
|
-
const importDeclaration = t.importDeclaration(
|
|
28
|
-
[t.importSpecifier(t.identifier(method), t.identifier(method))],
|
|
29
|
-
t.stringLiteral(PACKAGES.start)
|
|
30
|
-
);
|
|
31
|
-
programPath.node.body.unshift(importDeclaration);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
export {
|
|
36
|
-
handleCreateServerFileRouteCallExpressionFactory
|
|
37
|
-
};
|
|
38
|
-
//# sourceMappingURL=serverFileRoute.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serverFileRoute.js","sources":["../../../src/start-compiler-plugin/serverFileRoute.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions, CompileStartFrameworkOptions } from './compilers'\n\nexport function handleCreateServerFileRouteCallExpressionFactory(\n framework: CompileStartFrameworkOptions,\n method:\n | 'createServerFileRoute'\n | 'createServerRoute'\n | 'createServerRootRoute',\n) {\n return function handleCreateServerFileRouteCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n const PACKAGES = { start: `@tanstack/${framework}-start/server` }\n\n let highestParent: babel.NodePath<any> = path\n\n while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {\n highestParent = highestParent.parentPath\n }\n\n const programPath = highestParent.parentPath as babel.NodePath<t.Program>\n\n // If we're on the client, remove the entire variable\n if (opts.env === 'client') {\n highestParent.remove()\n return\n }\n\n let isCreateServerFileRouteImported = false as boolean\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const importSource = importPath.node.source.value\n if (importSource === PACKAGES.start) {\n const specifiers = importPath.node.specifiers\n isCreateServerFileRouteImported ||= specifiers.some((specifier) => {\n return (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === method\n )\n })\n }\n },\n })\n\n if (!isCreateServerFileRouteImported) {\n const importDeclaration = t.importDeclaration(\n [t.importSpecifier(t.identifier(method), t.identifier(method))],\n t.stringLiteral(PACKAGES.start),\n )\n programPath.node.body.unshift(importDeclaration)\n }\n }\n}\n"],"names":[],"mappings":";AAKO,SAAS,iDACd,WACA,QAIA;AACA,SAAO,SAAS,0CACd,MACA,MACA;AACA,UAAM,WAAW,EAAE,OAAO,aAAa,SAAS,gBAAA;AAEhD,QAAI,gBAAqC;AAEzC,WAAO,cAAc,cAAc,CAAC,cAAc,WAAW,aAAa;AACxE,sBAAgB,cAAc;AAAA,IAChC;AAEA,UAAM,cAAc,cAAc;AAGlC,QAAI,KAAK,QAAQ,UAAU;AACzB,oBAAc,OAAA;AACd;AAAA,IACF;AAEA,QAAI,kCAAkC;AAEtC,gBAAY,SAAS;AAAA,MACnB,kBAAkB,YAAY;AAC5B,cAAM,eAAe,WAAW,KAAK,OAAO;AAC5C,YAAI,iBAAiB,SAAS,OAAO;AACnC,gBAAM,aAAa,WAAW,KAAK;AACnC,8CAAoC,WAAW,KAAK,CAAC,cAAc;AACjE,mBACE,EAAE,kBAAkB,SAAS,KAC7B,EAAE,aAAa,UAAU,QAAQ,KACjC,UAAU,SAAS,SAAS;AAAA,UAEhC,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IAAA,CACD;AAED,QAAI,CAAC,iCAAiC;AACpC,YAAM,oBAAoB,EAAE;AAAA,QAC1B,CAAC,EAAE,gBAAgB,EAAE,WAAW,MAAM,GAAG,EAAE,WAAW,MAAM,CAAC,CAAC;AAAA,QAC9D,EAAE,cAAc,SAAS,KAAK;AAAA,MAAA;AAEhC,kBAAY,KAAK,KAAK,QAAQ,iBAAiB;AAAA,IACjD;AAAA,EACF;AACF;"}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { hasChildWithExport, checkRouteFullPathUniqueness, ensureStringArgument } from "@tanstack/router-generator";
|
|
2
|
-
const EXPORT_NAME = "ServerRoute";
|
|
3
|
-
function serverRoutesPlugin() {
|
|
4
|
-
return {
|
|
5
|
-
name: "server-routes-plugin",
|
|
6
|
-
transformPlugin: {
|
|
7
|
-
name: "server-routes-transform",
|
|
8
|
-
exportName: EXPORT_NAME,
|
|
9
|
-
imports: (ctx) => {
|
|
10
|
-
const targetModule = `@tanstack/${ctx.target}-start/server`;
|
|
11
|
-
const imports = {};
|
|
12
|
-
if (ctx.verboseFileRoutes === false) {
|
|
13
|
-
imports.banned = [
|
|
14
|
-
{
|
|
15
|
-
source: targetModule,
|
|
16
|
-
specifiers: [{ imported: "createServerFileRoute" }]
|
|
17
|
-
}
|
|
18
|
-
];
|
|
19
|
-
} else {
|
|
20
|
-
imports.required = [
|
|
21
|
-
{
|
|
22
|
-
source: targetModule,
|
|
23
|
-
specifiers: [{ imported: "createServerFileRoute" }]
|
|
24
|
-
}
|
|
25
|
-
];
|
|
26
|
-
}
|
|
27
|
-
return imports;
|
|
28
|
-
},
|
|
29
|
-
onExportFound: ({ ctx, decl }) => {
|
|
30
|
-
let appliedChanges = false;
|
|
31
|
-
if (decl.init?.type === "CallExpression") {
|
|
32
|
-
let call = decl.init;
|
|
33
|
-
let callee = call.callee;
|
|
34
|
-
while (callee.type === "MemberExpression" && callee.object.type === "CallExpression") {
|
|
35
|
-
call = callee.object;
|
|
36
|
-
callee = call.callee;
|
|
37
|
-
}
|
|
38
|
-
if (call.callee.type === "Identifier" && call.callee.name === "createServerFileRoute") {
|
|
39
|
-
if (!ctx.verboseFileRoutes) {
|
|
40
|
-
if (call.arguments.length) {
|
|
41
|
-
call.arguments = [];
|
|
42
|
-
appliedChanges = true;
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
appliedChanges = ensureStringArgument(
|
|
46
|
-
call,
|
|
47
|
-
ctx.routeId,
|
|
48
|
-
ctx.preferredQuote
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
throw new Error(
|
|
53
|
-
`Expected "createServerFileRoute" call, but got "${call.callee.type}"`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return appliedChanges;
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
moduleAugmentation: ({ generator }) => ({
|
|
61
|
-
module: `@tanstack/${generator.config.target}-start/server`,
|
|
62
|
-
interfaceName: "ServerFileRoutesByPath"
|
|
63
|
-
}),
|
|
64
|
-
onRouteTreesChanged: ({ routeTrees, generator }) => {
|
|
65
|
-
const tree = routeTrees.find((tree2) => tree2.exportName === EXPORT_NAME);
|
|
66
|
-
if (tree) {
|
|
67
|
-
checkRouteFullPathUniqueness(tree.sortedRouteNodes, generator.config);
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
imports: (ctx) => {
|
|
71
|
-
const imports = [];
|
|
72
|
-
const targetModule = `@tanstack/${ctx.generator.config.target}-start/server`;
|
|
73
|
-
if (ctx.generator.config.verboseFileRoutes === false) {
|
|
74
|
-
imports.push({
|
|
75
|
-
specifiers: [
|
|
76
|
-
{ imported: "CreateServerFileRoute" },
|
|
77
|
-
{ imported: "ServerFileRoutesByPath" }
|
|
78
|
-
],
|
|
79
|
-
source: targetModule,
|
|
80
|
-
importKind: "type"
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
const hasMatchingRouteFiles = ctx.acc.routeNodes.length > 0;
|
|
84
|
-
if (hasMatchingRouteFiles) {
|
|
85
|
-
if (!ctx.rootRouteNode.exports?.includes(EXPORT_NAME)) {
|
|
86
|
-
imports.push({
|
|
87
|
-
specifiers: [{ imported: "createServerRootRoute" }],
|
|
88
|
-
source: targetModule
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return imports;
|
|
93
|
-
},
|
|
94
|
-
routeModuleAugmentation: ({ routeNode }) => {
|
|
95
|
-
if (routeNode._fsRouteType === "lazy") {
|
|
96
|
-
return void 0;
|
|
97
|
-
}
|
|
98
|
-
return `const createServerFileRoute: CreateServerFileRoute<
|
|
99
|
-
ServerFileRoutesByPath['${routeNode.routePath}']['parentRoute'],
|
|
100
|
-
ServerFileRoutesByPath['${routeNode.routePath}']['id'],
|
|
101
|
-
ServerFileRoutesByPath['${routeNode.routePath}']['path'],
|
|
102
|
-
ServerFileRoutesByPath['${routeNode.routePath}']['fullPath'],
|
|
103
|
-
${hasChildWithExport(routeNode, "ServerRoute") ? `${routeNode.variableName}ServerRouteChildren` : "unknown"}
|
|
104
|
-
>`;
|
|
105
|
-
},
|
|
106
|
-
createRootRouteCode: () => `createServerRootRoute()`,
|
|
107
|
-
createVirtualRouteCode: ({ node }) => `createServerFileRoute('${node.routePath}')`,
|
|
108
|
-
config: ({ sortedRouteNodes }) => {
|
|
109
|
-
const hasMatchingRouteFiles = sortedRouteNodes.length > 0;
|
|
110
|
-
return {
|
|
111
|
-
virtualRootRoute: hasMatchingRouteFiles
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
export {
|
|
117
|
-
serverRoutesPlugin
|
|
118
|
-
};
|
|
119
|
-
//# sourceMappingURL=server-routes-plugin.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server-routes-plugin.js","sources":["../../../../src/start-router-plugin/generator-plugins/server-routes-plugin.ts"],"sourcesContent":["import {\n checkRouteFullPathUniqueness,\n ensureStringArgument,\n hasChildWithExport,\n} from '@tanstack/router-generator'\n\nimport type {\n GeneratorPluginWithTransform,\n ImportDeclaration,\n TransformImportsConfig,\n} from '@tanstack/router-generator'\n\nconst EXPORT_NAME = 'ServerRoute'\nexport function serverRoutesPlugin(): GeneratorPluginWithTransform {\n return {\n name: 'server-routes-plugin',\n transformPlugin: {\n name: 'server-routes-transform',\n exportName: EXPORT_NAME,\n imports: (ctx) => {\n const targetModule = `@tanstack/${ctx.target}-start/server`\n const imports: TransformImportsConfig = {}\n if (ctx.verboseFileRoutes === false) {\n imports.banned = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createServerFileRoute' }],\n },\n ]\n } else {\n imports.required = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createServerFileRoute' }],\n },\n ]\n }\n return imports\n },\n onExportFound: ({ ctx, decl }) => {\n let appliedChanges = false\n if (decl.init?.type === 'CallExpression') {\n let call = decl.init\n let callee = call.callee\n\n while (\n callee.type === 'MemberExpression' &&\n callee.object.type === 'CallExpression'\n ) {\n call = callee.object\n callee = call.callee\n }\n if (\n call.callee.type === 'Identifier' &&\n call.callee.name === 'createServerFileRoute'\n ) {\n if (!ctx.verboseFileRoutes) {\n if (call.arguments.length) {\n call.arguments = []\n appliedChanges = true\n }\n } else {\n appliedChanges = ensureStringArgument(\n call,\n ctx.routeId,\n ctx.preferredQuote,\n )\n }\n } else {\n throw new Error(\n `Expected \"createServerFileRoute\" call, but got \"${call.callee.type}\"`,\n )\n }\n }\n return appliedChanges\n },\n },\n moduleAugmentation: ({ generator }) => ({\n module: `@tanstack/${generator.config.target}-start/server`,\n interfaceName: 'ServerFileRoutesByPath',\n }),\n onRouteTreesChanged: ({ routeTrees, generator }) => {\n const tree = routeTrees.find((tree) => tree.exportName === EXPORT_NAME)\n if (tree) {\n checkRouteFullPathUniqueness(tree.sortedRouteNodes, generator.config)\n }\n },\n imports: (ctx) => {\n const imports: Array<ImportDeclaration> = []\n\n const targetModule = `@tanstack/${ctx.generator.config.target}-start/server`\n if (ctx.generator.config.verboseFileRoutes === false) {\n imports.push({\n specifiers: [\n { imported: 'CreateServerFileRoute' },\n { imported: 'ServerFileRoutesByPath' },\n ],\n source: targetModule,\n importKind: 'type',\n })\n }\n // don't add the import if there are no server routes defined\n const hasMatchingRouteFiles = ctx.acc.routeNodes.length > 0\n if (hasMatchingRouteFiles) {\n // needs a virtual root route\n if (!ctx.rootRouteNode.exports?.includes(EXPORT_NAME)) {\n imports.push({\n specifiers: [{ imported: 'createServerRootRoute' }],\n source: targetModule,\n })\n }\n }\n return imports\n },\n routeModuleAugmentation: ({ routeNode }) => {\n // server routes don't support lazy routes\n if (routeNode._fsRouteType === 'lazy') {\n return undefined\n }\n return `const createServerFileRoute: CreateServerFileRoute<\n ServerFileRoutesByPath['${routeNode.routePath}']['parentRoute'],\n ServerFileRoutesByPath['${routeNode.routePath}']['id'],\n ServerFileRoutesByPath['${routeNode.routePath}']['path'],\n ServerFileRoutesByPath['${routeNode.routePath}']['fullPath'],\n ${hasChildWithExport(routeNode, 'ServerRoute') ? `${routeNode.variableName}ServerRouteChildren` : 'unknown'}\n >`\n },\n createRootRouteCode: () => `createServerRootRoute()`,\n createVirtualRouteCode: ({ node }) =>\n `createServerFileRoute('${node.routePath}')`,\n config: ({ sortedRouteNodes }) => {\n const hasMatchingRouteFiles = sortedRouteNodes.length > 0\n return {\n virtualRootRoute: hasMatchingRouteFiles,\n }\n },\n }\n}\n"],"names":["tree"],"mappings":";AAYA,MAAM,cAAc;AACb,SAAS,qBAAmD;AACjE,SAAO;AAAA,IACL,MAAM;AAAA,IACN,iBAAiB;AAA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BAAiB,SAAS;AACxD,aAAO;AAAA,QACL,kBAAkB;AAAA,MAAA;AAAA,IAEtB;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Plugin } from 'vite';
|
|
2
|
-
import { Config } from '@tanstack/router-generator';
|
|
3
|
-
/**
|
|
4
|
-
* This removes the server part from the generated route tree so that it can be used on the client.
|
|
5
|
-
*/
|
|
6
|
-
export declare function routeTreeClientPlugin(config: Config): Plugin;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import * as t from "@babel/types";
|
|
3
|
-
import { parseAst, generateFromAst, logDiff } from "@tanstack/router-utils";
|
|
4
|
-
import { normalizePath } from "vite";
|
|
5
|
-
import { deadCodeElimination } from "babel-dead-code-elimination";
|
|
6
|
-
import { debug } from "../debug.js";
|
|
7
|
-
import { VITE_ENVIRONMENT_NAMES } from "../constants.js";
|
|
8
|
-
function routeTreeClientPlugin(config) {
|
|
9
|
-
const generatedRouteTreePath = normalizePath(
|
|
10
|
-
path.resolve(config.generatedRouteTree)
|
|
11
|
-
);
|
|
12
|
-
return {
|
|
13
|
-
name: "tanstack-start:route-tree-client-plugin",
|
|
14
|
-
enforce: "pre",
|
|
15
|
-
// only run this plugin in the client environment
|
|
16
|
-
applyToEnvironment: (env) => env.name === VITE_ENVIRONMENT_NAMES.client,
|
|
17
|
-
transform: {
|
|
18
|
-
filter: { id: generatedRouteTreePath },
|
|
19
|
-
handler(code, id) {
|
|
20
|
-
if (id !== generatedRouteTreePath) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
if (debug) console.info(`Compiling route tree for the client`, id);
|
|
24
|
-
const ast = parseAst({ code, sourceFilename: id });
|
|
25
|
-
const filteredBody = ast.program.body.filter((node) => {
|
|
26
|
-
if (t.isExportNamedDeclaration(node)) {
|
|
27
|
-
if (node.declaration && t.isVariableDeclaration(node.declaration) && node.declaration.declarations.length === 1 && node.declaration.declarations[0] && t.isVariableDeclarator(node.declaration.declarations[0]) && t.isIdentifier(node.declaration.declarations[0].id) && node.declaration.declarations[0].id.name === "routeTree") {
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
if (t.isTSInterfaceDeclaration(node) || t.isTSModuleDeclaration(node)) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
return true;
|
|
36
|
-
});
|
|
37
|
-
ast.program.body = filteredBody;
|
|
38
|
-
deadCodeElimination(ast);
|
|
39
|
-
const compiled = generateFromAst(ast, {
|
|
40
|
-
sourceMaps: true,
|
|
41
|
-
sourceFileName: id,
|
|
42
|
-
filename: id
|
|
43
|
-
});
|
|
44
|
-
if (debug) {
|
|
45
|
-
logDiff(code, compiled.code);
|
|
46
|
-
console.log("Output:\n", compiled.code, "\n\n");
|
|
47
|
-
}
|
|
48
|
-
return compiled;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
export {
|
|
54
|
-
routeTreeClientPlugin
|
|
55
|
-
};
|
|
56
|
-
//# sourceMappingURL=route-tree-client-plugin.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"route-tree-client-plugin.js","sources":["../../../src/start-router-plugin/route-tree-client-plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport * as t from '@babel/types'\nimport { generateFromAst, logDiff, parseAst } from '@tanstack/router-utils'\nimport { normalizePath } from 'vite'\nimport { deadCodeElimination } from 'babel-dead-code-elimination'\nimport { debug } from '../debug'\nimport { VITE_ENVIRONMENT_NAMES } from '../constants'\nimport type { Plugin } from 'vite'\nimport type { Config } from '@tanstack/router-generator'\n\n/**\n * This removes the server part from the generated route tree so that it can be used on the client.\n */\nexport function routeTreeClientPlugin(config: Config): Plugin {\n const generatedRouteTreePath = normalizePath(\n path.resolve(config.generatedRouteTree),\n )\n\n return {\n name: 'tanstack-start:route-tree-client-plugin',\n enforce: 'pre',\n // only run this plugin in the client environment\n applyToEnvironment: (env) => env.name === VITE_ENVIRONMENT_NAMES.client,\n transform: {\n filter: { id: generatedRouteTreePath },\n handler(code, id) {\n if (id !== generatedRouteTreePath) {\n return null\n }\n if (debug) console.info(`Compiling route tree for the client`, id)\n const ast = parseAst({ code, sourceFilename: id })\n\n // only keep `export const routeTree = ... `\n const filteredBody = ast.program.body.filter((node) => {\n if (t.isExportNamedDeclaration(node)) {\n if (\n node.declaration &&\n t.isVariableDeclaration(node.declaration) &&\n node.declaration.declarations.length === 1 &&\n node.declaration.declarations[0] &&\n t.isVariableDeclarator(node.declaration.declarations[0]) &&\n t.isIdentifier(node.declaration.declarations[0].id) &&\n node.declaration.declarations[0].id.name === 'routeTree'\n ) {\n return true\n }\n return false\n }\n // strip off the typescript interface & module declarations since they also reference the server routes\n if (\n t.isTSInterfaceDeclaration(node) ||\n t.isTSModuleDeclaration(node)\n ) {\n return false\n }\n return true\n })\n\n ast.program.body = filteredBody\n\n deadCodeElimination(ast)\n\n const compiled = generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: id,\n filename: id,\n })\n if (debug) {\n logDiff(code, compiled.code)\n console.log('Output:\\n', compiled.code, '\\n\\n')\n }\n\n return compiled\n },\n },\n }\n}\n"],"names":[],"mappings":";;;;;;;AAaO,SAAS,sBAAsB,QAAwB;AAC5D,QAAM,yBAAyB;AAAA,IAC7B,KAAK,QAAQ,OAAO,kBAAkB;AAAA,EAAA;AAGxC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,SAAS;AAAA;AAAA,IAET,oBAAoB,CAAC,QAAQ,IAAI,SAAS,uBAAuB;AAAA,IACjE,WAAW;AAAA,MACT,QAAQ,EAAE,IAAI,uBAAA;AAAA,MACd,QAAQ,MAAM,IAAI;AAChB,YAAI,OAAO,wBAAwB;AACjC,iBAAO;AAAA,QACT;AACA,YAAI,MAAO,SAAQ,KAAK,uCAAuC,EAAE;AACjE,cAAM,MAAM,SAAS,EAAE,MAAM,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