@tanstack/start-plugin-core 1.132.0-alpha.8 → 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 +64 -0
- package/dist/esm/create-server-fn-plugin/compiler.js +364 -0
- package/dist/esm/create-server-fn-plugin/compiler.js.map +1 -0
- 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.d.ts +6 -0
- package/dist/esm/{start-compiler-plugin/serverFn.js → create-server-fn-plugin/handleCreateServerFn.js} +15 -17
- package/dist/esm/create-server-fn-plugin/handleCreateServerFn.js.map +1 -0
- package/dist/esm/create-server-fn-plugin/plugin.d.ts +3 -0
- package/dist/esm/create-server-fn-plugin/plugin.js +128 -0
- package/dist/esm/create-server-fn-plugin/plugin.js.map +1 -0
- 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 +56 -68
- 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 -55
- 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 -6
- package/dist/esm/start-compiler-plugin/constants.js.map +1 -1
- package/dist/esm/start-compiler-plugin/plugin.d.ts +1 -8
- package/dist/esm/start-compiler-plugin/plugin.js +6 -13
- package/dist/esm/start-compiler-plugin/plugin.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 +498 -0
- package/src/{start-compiler-plugin/middleware.ts → create-server-fn-plugin/handleCreateMiddleware.ts} +15 -12
- package/src/{start-compiler-plugin/serverFn.ts → create-server-fn-plugin/handleCreateServerFn.ts} +32 -39
- package/src/create-server-fn-plugin/plugin.ts +153 -0
- package/src/dev-server-plugin/plugin.ts +6 -3
- package/src/plugin.ts +78 -87
- package/src/resolve-entries.ts +1 -2
- package/src/schema.ts +31 -14
- package/src/start-compiler-plugin/compilers.ts +18 -57
- package/src/start-compiler-plugin/constants.ts +0 -5
- package/src/start-compiler-plugin/plugin.ts +7 -22
- 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-compiler-plugin/serverFn.d.ts +0 -4
- package/dist/esm/start-compiler-plugin/serverFn.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
|
@@ -7,10 +7,7 @@ import {
|
|
|
7
7
|
} from 'babel-dead-code-elimination'
|
|
8
8
|
import { generateFromAst, parseAst } from '@tanstack/router-utils'
|
|
9
9
|
import { transformFuncs } from './constants'
|
|
10
|
-
import { handleCreateServerFileRouteCallExpressionFactory } from './serverFileRoute'
|
|
11
10
|
import { handleCreateIsomorphicFnCallExpression } from './isomorphicFn'
|
|
12
|
-
import { handleCreateMiddlewareCallExpression } from './middleware'
|
|
13
|
-
import { handleCreateServerFnCallExpression } from './serverFn'
|
|
14
11
|
import {
|
|
15
12
|
handleCreateClientOnlyFnCallExpression,
|
|
16
13
|
handleCreateServerOnlyFnCallExpression,
|
|
@@ -20,64 +17,29 @@ import type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'
|
|
|
20
17
|
export type CompileStartFrameworkOptions = 'react' | 'solid'
|
|
21
18
|
|
|
22
19
|
type Identifiers = { [K in (typeof transformFuncs)[number]]: IdentifierConfig }
|
|
23
|
-
const getIdentifiers = (
|
|
24
|
-
framework: CompileStartFrameworkOptions,
|
|
25
|
-
): Identifiers => ({
|
|
26
|
-
createServerRootRoute: {
|
|
27
|
-
name: 'createServerRootRoute',
|
|
28
|
-
handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(
|
|
29
|
-
framework,
|
|
30
|
-
'createServerRootRoute',
|
|
31
|
-
),
|
|
32
|
-
paths: [],
|
|
33
|
-
},
|
|
34
|
-
createServerRoute: {
|
|
35
|
-
name: 'createServerRoute',
|
|
36
|
-
handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(
|
|
37
|
-
framework,
|
|
38
|
-
'createServerRoute',
|
|
39
|
-
),
|
|
40
|
-
paths: [],
|
|
41
|
-
},
|
|
42
|
-
createServerFileRoute: {
|
|
43
|
-
name: 'createServerFileRoute',
|
|
44
|
-
handleCallExpression: handleCreateServerFileRouteCallExpressionFactory(
|
|
45
|
-
framework,
|
|
46
|
-
'createServerFileRoute',
|
|
47
|
-
),
|
|
48
|
-
paths: [],
|
|
49
|
-
},
|
|
50
|
-
createServerFn: {
|
|
51
|
-
name: 'createServerFn',
|
|
52
|
-
handleCallExpression: handleCreateServerFnCallExpression,
|
|
53
|
-
paths: [],
|
|
54
|
-
},
|
|
55
|
-
createMiddleware: {
|
|
56
|
-
name: 'createMiddleware',
|
|
57
|
-
handleCallExpression: handleCreateMiddlewareCallExpression,
|
|
58
|
-
paths: [],
|
|
59
|
-
},
|
|
60
|
-
createServerOnlyFn: {
|
|
61
|
-
name: 'createServerOnlyFn',
|
|
62
|
-
handleCallExpression: handleCreateServerOnlyFnCallExpression,
|
|
63
|
-
paths: [],
|
|
64
|
-
},
|
|
65
|
-
createClientOnlyFn: {
|
|
66
|
-
name: 'createClientOnlyFn',
|
|
67
|
-
handleCallExpression: handleCreateClientOnlyFnCallExpression,
|
|
68
|
-
paths: [],
|
|
69
|
-
},
|
|
70
|
-
createIsomorphicFn: {
|
|
71
|
-
name: 'createIsomorphicFn',
|
|
72
|
-
handleCallExpression: handleCreateIsomorphicFnCallExpression,
|
|
73
|
-
paths: [],
|
|
74
|
-
},
|
|
75
|
-
})
|
|
76
20
|
|
|
77
21
|
export function compileStartOutputFactory(
|
|
78
22
|
framework: CompileStartFrameworkOptions,
|
|
79
23
|
) {
|
|
80
24
|
return function compileStartOutput(opts: CompileOptions): GeneratorResult {
|
|
25
|
+
const identifiers: Identifiers = {
|
|
26
|
+
createServerOnlyFn: {
|
|
27
|
+
name: 'createServerOnlyFn',
|
|
28
|
+
handleCallExpression: handleCreateServerOnlyFnCallExpression,
|
|
29
|
+
paths: [],
|
|
30
|
+
},
|
|
31
|
+
createClientOnlyFn: {
|
|
32
|
+
name: 'createClientOnlyFn',
|
|
33
|
+
handleCallExpression: handleCreateClientOnlyFnCallExpression,
|
|
34
|
+
paths: [],
|
|
35
|
+
},
|
|
36
|
+
createIsomorphicFn: {
|
|
37
|
+
name: 'createIsomorphicFn',
|
|
38
|
+
handleCallExpression: handleCreateIsomorphicFnCallExpression,
|
|
39
|
+
paths: [],
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
|
|
81
43
|
const ast = parseAst(opts)
|
|
82
44
|
|
|
83
45
|
const doDce = opts.dce ?? true
|
|
@@ -87,7 +49,6 @@ export function compileStartOutputFactory(
|
|
|
87
49
|
babel.traverse(ast, {
|
|
88
50
|
Program: {
|
|
89
51
|
enter(programPath) {
|
|
90
|
-
const identifiers = getIdentifiers(framework)
|
|
91
52
|
programPath.traverse({
|
|
92
53
|
ImportDeclaration: (path) => {
|
|
93
54
|
if (path.node.source.value !== `@tanstack/${framework}-start`) {
|
|
@@ -8,6 +8,7 @@ import path from 'pathe'
|
|
|
8
8
|
import { VITE_ENVIRONMENT_NAMES } from '../constants'
|
|
9
9
|
import { compileStartOutputFactory } from './compilers'
|
|
10
10
|
import { transformFuncs } from './constants'
|
|
11
|
+
import type { ViteEnvironmentNames } from '../constants'
|
|
11
12
|
import type { Plugin } from 'vite'
|
|
12
13
|
import type { CompileStartFrameworkOptions } from './compilers'
|
|
13
14
|
|
|
@@ -37,33 +38,17 @@ function resolvePackage(packageName: string): string {
|
|
|
37
38
|
|
|
38
39
|
export function startCompilerPlugin(
|
|
39
40
|
framework: CompileStartFrameworkOptions,
|
|
40
|
-
inputOpts?: {
|
|
41
|
-
client?: {
|
|
42
|
-
envName?: string
|
|
43
|
-
}
|
|
44
|
-
server?: {
|
|
45
|
-
envName?: string
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
41
|
): Plugin {
|
|
49
|
-
const opts = {
|
|
50
|
-
client: {
|
|
51
|
-
envName: VITE_ENVIRONMENT_NAMES.client,
|
|
52
|
-
...inputOpts?.client,
|
|
53
|
-
},
|
|
54
|
-
server: {
|
|
55
|
-
envName: VITE_ENVIRONMENT_NAMES.server,
|
|
56
|
-
...inputOpts?.server,
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
|
|
60
42
|
const compileStartOutput = compileStartOutputFactory(framework)
|
|
61
43
|
|
|
62
44
|
return {
|
|
63
45
|
name: 'tanstack-start-core:compiler',
|
|
64
46
|
enforce: 'pre',
|
|
65
47
|
applyToEnvironment(env) {
|
|
66
|
-
return [
|
|
48
|
+
return [
|
|
49
|
+
VITE_ENVIRONMENT_NAMES.client,
|
|
50
|
+
VITE_ENVIRONMENT_NAMES.server,
|
|
51
|
+
].includes(env.name as ViteEnvironmentNames)
|
|
67
52
|
},
|
|
68
53
|
transform: {
|
|
69
54
|
filter: {
|
|
@@ -103,9 +88,9 @@ export function startCompilerPlugin(
|
|
|
103
88
|
},
|
|
104
89
|
handler(code, id) {
|
|
105
90
|
const env =
|
|
106
|
-
this.environment.name ===
|
|
91
|
+
this.environment.name === VITE_ENVIRONMENT_NAMES.client
|
|
107
92
|
? 'client'
|
|
108
|
-
: this.environment.name ===
|
|
93
|
+
: this.environment.name === VITE_ENVIRONMENT_NAMES.server
|
|
109
94
|
? 'server'
|
|
110
95
|
: (() => {
|
|
111
96
|
throw new Error(
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const SERVER_PROP = 'server'
|
|
@@ -9,20 +9,14 @@ import type { GeneratorPlugin } from '@tanstack/router-generator'
|
|
|
9
9
|
export function routesManifestPlugin(): GeneratorPlugin {
|
|
10
10
|
return {
|
|
11
11
|
name: 'routes-manifest-plugin',
|
|
12
|
-
|
|
13
|
-
const routeTree = routeTrees.find((tree) => tree.exportName === 'Route')
|
|
14
|
-
if (!routeTree) {
|
|
15
|
-
throw new Error(
|
|
16
|
-
'No route tree found with export name "Route". Please ensure your routes are correctly defined.',
|
|
17
|
-
)
|
|
18
|
-
}
|
|
12
|
+
onRouteTreeChanged: ({ routeTree, rootRouteNode, routeNodes }) => {
|
|
19
13
|
const routesManifest = {
|
|
20
14
|
[rootRouteId]: {
|
|
21
15
|
filePath: rootRouteNode.fullPath,
|
|
22
|
-
children: routeTree.
|
|
16
|
+
children: routeTree.map((d) => d.routePath),
|
|
23
17
|
},
|
|
24
18
|
...Object.fromEntries(
|
|
25
|
-
|
|
19
|
+
routeNodes.map((d) => {
|
|
26
20
|
const filePathId = d.routePath
|
|
27
21
|
|
|
28
22
|
return [
|
|
@@ -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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"serverFn.js","sources":["../../../src/start-compiler-plugin/serverFn.ts"],"sourcesContent":["import * as t from '@babel/types'\nimport { codeFrameError, getRootCallExpression } from './utils'\nimport type * as babel from '@babel/core'\n\nimport type { CompileOptions } from './compilers'\n\nexport function handleCreateServerFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const calledOptions = path.node.arguments[0]\n ? (path.get('arguments.0') as babel.NodePath<t.ObjectExpression>)\n : null\n\n const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {\n return (\n t.isObjectProperty(prop) &&\n t.isIdentifier(prop.key) &&\n prop.key.name === 'validateClient' &&\n t.isBooleanLiteral(prop.value) &&\n prop.value.value === true\n )\n })\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n handler: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\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 'createServerFn().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, and we're not validating the client, remove the validator call expression\n if (\n opts.env === 'client' &&\n !shouldValidateClient &&\n t.isMemberExpression(callExpressionPaths.validator.node.callee)\n ) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral('use server'))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push(handlerFn)\n }\n}\n"],"names":[],"mappings":";;AAMO,SAAS,mCACd,MACA,MACA;AAKA,QAAM,gBAAgB,KAAK,KAAK,UAAU,CAAC,IACtC,KAAK,IAAI,aAAa,IACvB;AAEJ,QAAM,uBAAuB,CAAC,CAAC,eAAe,KAAK,WAAW,KAAK,CAAC,SAAS;AAC3E,WACE,EAAE,iBAAiB,IAAI,KACvB,EAAE,aAAa,KAAK,GAAG,KACvB,KAAK,IAAI,SAAS,oBAClB,EAAE,iBAAiB,KAAK,KAAK,KAC7B,KAAK,MAAM,UAAU;AAAA,EAEzB,CAAC;AAED,QAAM,sBAAsB;AAAA,IAC1B,YAAY;AAAA,IACZ,WAAW;AAAA,IACX,SAAS;AAAA,EAAA;AAGX,QAAM,eAAe,OAAO,KAAK,mBAAmB;AAEpD,QAAM,qBAAqB,sBAAsB,IAAI;AASrD,MAAI,CAAC,mBAAmB,WAAW,wBAAwB;AACzD,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AAGA,QAAM,qBAAqB,mBAAmB,WAAW;AACzD,QAAM,uBAAwB,mBAAmB,GAAoB;AAErE,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,QACE,KAAK,QAAQ,YACb,CAAC,wBACD,EAAE,mBAAmB,oBAAoB,UAAU,KAAK,MAAM,GAC9D;AACA,0BAAoB,UAAU;AAAA,QAC5B,oBAAoB,UAAU,KAAK,OAAO;AAAA,MAAA;AAAA,IAE9C;AAAA,EACF;AAKA,QAAM,gBAAgB,oBAAoB,SAAS;AAAA,IACjD;AAAA,EAAA;AAGF,MAAI,CAAC,oBAAoB,WAAW,CAAC,cAAc,MAAM;AACvD,UAAM;AAAA,MACJ,KAAK;AAAA,MACL,KAAK,KAAK,OAAO;AAAA,MACjB;AAAA,IAAA;AAAA,EAEJ;AAEA,QAAM,YAAY,cAAc;AAuBhC,MAAI,EAAE,aAAa,SAAS,GAAG;AAC7B,QAAI,KAAK,QAAQ,UAAU;AAEzB,YAAM,UAAU,cAAc,MAAM,WAAW,UAAU,IAAI;AAE7D,UAAI,SAAS;AACX,gBAAQ,KAAK,OAAA;AAAA,MACf;AAAA,IACF;AAAA,EAEF;AAEA,gBAAc;AAAA,IACZ,EAAE;AAAA,MACA,CAAC,EAAE,WAAW,MAAM,GAAG,EAAE,WAAW,QAAQ,CAAC;AAAA,MAC7C,EAAE;AAAA;AAAA;AAAA,QAGA;AAAA,UACE,EAAE;AAAA,YACA,EAAE;AAAA,cACA,EAAE,WAAW,GAAG,oBAAoB,kBAAkB;AAAA,cACtD,CAAC,EAAE,WAAW,MAAM,GAAG,EAAE,WAAW,QAAQ,CAAC;AAAA,YAAA;AAAA,UAC/C;AAAA,QACF;AAAA,QAEF,CAAC,EAAE,UAAU,EAAE,iBAAiB,YAAY,CAAC,CAAC;AAAA,MAAA;AAAA,IAChD;AAAA,EACF;AAGF,MAAI,KAAK,QAAQ,UAAU;AACzB,wBAAoB,QAAQ,KAAK,UAAU,KAAK,SAAS;AAAA,EAC3D;AACF;"}
|