@tanstack/start-plugin-core 1.167.18 → 1.167.19

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.
Files changed (183) hide show
  1. package/dist/esm/config-context.d.ts +26 -0
  2. package/dist/esm/config-context.js +81 -0
  3. package/dist/esm/config-context.js.map +1 -0
  4. package/dist/esm/constants.d.ts +6 -1
  5. package/dist/esm/constants.js +3 -2
  6. package/dist/esm/constants.js.map +1 -1
  7. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js +1 -1
  8. package/dist/esm/import-protection-plugin/plugin.js +1 -1
  9. package/dist/esm/import-protection-plugin/virtualModules.js +1 -1
  10. package/dist/esm/index.d.ts +5 -3
  11. package/dist/esm/index.js +3 -4
  12. package/dist/esm/planning.d.ts +40 -0
  13. package/dist/esm/planning.js +107 -0
  14. package/dist/esm/planning.js.map +1 -0
  15. package/dist/esm/schema.d.ts +3093 -44
  16. package/dist/esm/schema.js +5 -5
  17. package/dist/esm/schema.js.map +1 -1
  18. package/dist/esm/serialization-adapters-module.d.ts +17 -0
  19. package/dist/esm/serialization-adapters-module.js +39 -0
  20. package/dist/esm/serialization-adapters-module.js.map +1 -0
  21. package/dist/esm/{start-compiler-plugin → start-compiler}/compiler.d.ts +2 -3
  22. package/dist/esm/{start-compiler-plugin → start-compiler}/compiler.js +17 -16
  23. package/dist/esm/start-compiler/compiler.js.map +1 -0
  24. package/dist/esm/start-compiler/config.d.ts +4 -0
  25. package/dist/esm/start-compiler/config.js +54 -0
  26. package/dist/esm/start-compiler/config.js.map +1 -0
  27. package/dist/esm/{start-compiler-plugin → start-compiler}/handleClientOnlyJSX.js +1 -1
  28. package/dist/esm/start-compiler/handleClientOnlyJSX.js.map +1 -0
  29. package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateIsomorphicFn.js +1 -1
  30. package/dist/esm/start-compiler/handleCreateIsomorphicFn.js.map +1 -0
  31. package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateMiddleware.js +1 -1
  32. package/dist/esm/start-compiler/handleCreateMiddleware.js.map +1 -0
  33. package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateServerFn.js +6 -17
  34. package/dist/esm/start-compiler/handleCreateServerFn.js.map +1 -0
  35. package/dist/esm/{start-compiler-plugin → start-compiler}/handleEnvOnly.js +1 -1
  36. package/dist/esm/start-compiler/handleEnvOnly.js.map +1 -0
  37. package/dist/esm/start-compiler/host.d.ts +20 -0
  38. package/dist/esm/start-compiler/host.js +38 -0
  39. package/dist/esm/start-compiler/host.js.map +1 -0
  40. package/dist/esm/start-compiler/load-module.d.ts +14 -0
  41. package/dist/esm/start-compiler/load-module.js +18 -0
  42. package/dist/esm/start-compiler/load-module.js.map +1 -0
  43. package/dist/esm/start-compiler/server-fn-resolver-module.d.ts +8 -0
  44. package/dist/esm/start-compiler/server-fn-resolver-module.js +77 -0
  45. package/dist/esm/start-compiler/server-fn-resolver-module.js.map +1 -0
  46. package/dist/esm/{start-compiler-plugin → start-compiler}/types.d.ts +4 -0
  47. package/dist/esm/{start-compiler-plugin → start-compiler}/utils.js +1 -1
  48. package/dist/esm/start-compiler/utils.js.map +1 -0
  49. package/dist/esm/start-manifest-plugin/manifestBuilder.d.ts +16 -16
  50. package/dist/esm/start-manifest-plugin/manifestBuilder.js +14 -45
  51. package/dist/esm/start-manifest-plugin/manifestBuilder.js.map +1 -1
  52. package/dist/esm/start-router-plugin/route-tree-footer.d.ts +6 -0
  53. package/dist/esm/start-router-plugin/route-tree-footer.js +44 -0
  54. package/dist/esm/start-router-plugin/route-tree-footer.js.map +1 -0
  55. package/dist/esm/types.d.ts +44 -10
  56. package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/dev-styles.js +1 -1
  57. package/dist/esm/vite/dev-server-plugin/dev-styles.js.map +1 -0
  58. package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/extract-html-scripts.js +1 -1
  59. package/dist/esm/vite/dev-server-plugin/extract-html-scripts.js.map +1 -0
  60. package/dist/esm/vite/dev-server-plugin/plugin.d.ts +7 -0
  61. package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/plugin.js +5 -6
  62. package/dist/esm/vite/dev-server-plugin/plugin.js.map +1 -0
  63. package/dist/esm/{load-env-plugin → vite/load-env-plugin}/plugin.js +1 -1
  64. package/dist/esm/vite/load-env-plugin/plugin.js.map +1 -0
  65. package/dist/esm/{output-directory.js → vite/output-directory.js} +2 -2
  66. package/dist/esm/vite/output-directory.js.map +1 -0
  67. package/dist/esm/vite/planning.d.ts +105 -0
  68. package/dist/esm/vite/planning.js +116 -0
  69. package/dist/esm/vite/planning.js.map +1 -0
  70. package/dist/esm/vite/plugin.d.ts +4 -0
  71. package/dist/esm/vite/plugin.js +169 -0
  72. package/dist/esm/vite/plugin.js.map +1 -0
  73. package/dist/esm/vite/plugins.d.ts +17 -0
  74. package/dist/esm/vite/plugins.js +50 -0
  75. package/dist/esm/vite/plugins.js.map +1 -0
  76. package/dist/esm/{post-server-build.d.ts → vite/post-server-build.d.ts} +1 -1
  77. package/dist/esm/{post-server-build.js → vite/post-server-build.js} +4 -4
  78. package/dist/esm/vite/post-server-build.js.map +1 -0
  79. package/dist/esm/{prerender.d.ts → vite/prerender.d.ts} +1 -1
  80. package/dist/esm/{prerender.js → vite/prerender.js} +5 -10
  81. package/dist/esm/vite/prerender.js.map +1 -0
  82. package/dist/esm/{preview-server-plugin → vite/preview-server-plugin}/plugin.js +4 -4
  83. package/dist/esm/vite/preview-server-plugin/plugin.js.map +1 -0
  84. package/dist/esm/vite/schema.d.ts +3373 -0
  85. package/dist/esm/vite/schema.js +12 -0
  86. package/dist/esm/vite/schema.js.map +1 -0
  87. package/dist/esm/vite/serialization-adapters-plugin.d.ts +5 -0
  88. package/dist/esm/vite/serialization-adapters-plugin.js +42 -0
  89. package/dist/esm/vite/serialization-adapters-plugin.js.map +1 -0
  90. package/dist/esm/vite/start-compiler-plugin/module-specifier.d.ts +3 -0
  91. package/dist/esm/vite/start-compiler-plugin/module-specifier.js +19 -0
  92. package/dist/esm/vite/start-compiler-plugin/module-specifier.js.map +1 -0
  93. package/dist/esm/{start-compiler-plugin → vite/start-compiler-plugin}/plugin.d.ts +4 -3
  94. package/dist/esm/vite/start-compiler-plugin/plugin.js +202 -0
  95. package/dist/esm/vite/start-compiler-plugin/plugin.js.map +1 -0
  96. package/dist/esm/vite/start-manifest-plugin/normalized-client-build.d.ts +6 -0
  97. package/dist/esm/vite/start-manifest-plugin/normalized-client-build.js +81 -0
  98. package/dist/esm/vite/start-manifest-plugin/normalized-client-build.js.map +1 -0
  99. package/dist/esm/vite/start-manifest-plugin/plugin.d.ts +6 -0
  100. package/dist/esm/{start-manifest-plugin → vite/start-manifest-plugin}/plugin.js +14 -9
  101. package/dist/esm/vite/start-manifest-plugin/plugin.js.map +1 -0
  102. package/dist/esm/{start-router-plugin → vite/start-router-plugin}/plugin.d.ts +3 -2
  103. package/dist/esm/{start-router-plugin → vite/start-router-plugin}/plugin.js +14 -37
  104. package/dist/esm/vite/start-router-plugin/plugin.js.map +1 -0
  105. package/dist/esm/vite/types.d.ts +15 -0
  106. package/package.json +12 -1
  107. package/src/config-context.ts +138 -0
  108. package/src/constants.ts +7 -3
  109. package/src/index.ts +5 -5
  110. package/src/planning.ts +151 -0
  111. package/src/schema.ts +93 -93
  112. package/src/serialization-adapters-module.ts +82 -0
  113. package/src/{start-compiler-plugin → start-compiler}/compiler.ts +67 -61
  114. package/src/start-compiler/config.ts +73 -0
  115. package/src/{start-compiler-plugin → start-compiler}/handleCreateServerFn.ts +14 -41
  116. package/src/start-compiler/host.ts +80 -0
  117. package/src/start-compiler/load-module.ts +31 -0
  118. package/src/start-compiler/server-fn-resolver-module.ts +129 -0
  119. package/src/{start-compiler-plugin → start-compiler}/types.ts +5 -0
  120. package/src/start-manifest-plugin/manifestBuilder.ts +65 -107
  121. package/src/start-router-plugin/route-tree-footer.ts +99 -0
  122. package/src/types.ts +53 -10
  123. package/src/{dev-server-plugin → vite/dev-server-plugin}/plugin.ts +7 -6
  124. package/src/{output-directory.ts → vite/output-directory.ts} +2 -2
  125. package/src/vite/planning.ts +234 -0
  126. package/src/vite/plugin.ts +276 -0
  127. package/src/vite/plugins.ts +81 -0
  128. package/src/{post-server-build.ts → vite/post-server-build.ts} +4 -6
  129. package/src/{prerender.ts → vite/prerender.ts} +21 -46
  130. package/src/{preview-server-plugin → vite/preview-server-plugin}/plugin.ts +2 -2
  131. package/src/vite/schema.ts +30 -0
  132. package/src/vite/serialization-adapters-plugin.ts +69 -0
  133. package/src/vite/start-compiler-plugin/module-specifier.ts +31 -0
  134. package/src/vite/start-compiler-plugin/plugin.ts +345 -0
  135. package/src/vite/start-manifest-plugin/normalized-client-build.ts +131 -0
  136. package/src/{start-manifest-plugin → vite/start-manifest-plugin}/plugin.ts +21 -13
  137. package/src/{start-router-plugin → vite/start-router-plugin}/plugin.ts +14 -80
  138. package/src/vite/types.ts +18 -0
  139. package/dist/esm/dev-server-plugin/dev-styles.js.map +0 -1
  140. package/dist/esm/dev-server-plugin/extract-html-scripts.js.map +0 -1
  141. package/dist/esm/dev-server-plugin/plugin.d.ts +0 -6
  142. package/dist/esm/dev-server-plugin/plugin.js.map +0 -1
  143. package/dist/esm/load-env-plugin/plugin.js.map +0 -1
  144. package/dist/esm/output-directory.js.map +0 -1
  145. package/dist/esm/plugin.d.ts +0 -4
  146. package/dist/esm/plugin.js +0 -301
  147. package/dist/esm/plugin.js.map +0 -1
  148. package/dist/esm/post-server-build.js.map +0 -1
  149. package/dist/esm/prerender.js.map +0 -1
  150. package/dist/esm/preview-server-plugin/plugin.js.map +0 -1
  151. package/dist/esm/start-compiler-plugin/compiler.js.map +0 -1
  152. package/dist/esm/start-compiler-plugin/handleClientOnlyJSX.js.map +0 -1
  153. package/dist/esm/start-compiler-plugin/handleCreateIsomorphicFn.js.map +0 -1
  154. package/dist/esm/start-compiler-plugin/handleCreateMiddleware.js.map +0 -1
  155. package/dist/esm/start-compiler-plugin/handleCreateServerFn.js.map +0 -1
  156. package/dist/esm/start-compiler-plugin/handleEnvOnly.js.map +0 -1
  157. package/dist/esm/start-compiler-plugin/plugin.js +0 -297
  158. package/dist/esm/start-compiler-plugin/plugin.js.map +0 -1
  159. package/dist/esm/start-compiler-plugin/utils.js.map +0 -1
  160. package/dist/esm/start-manifest-plugin/plugin.d.ts +0 -6
  161. package/dist/esm/start-manifest-plugin/plugin.js.map +0 -1
  162. package/dist/esm/start-router-plugin/plugin.js.map +0 -1
  163. package/src/plugin.ts +0 -471
  164. package/src/start-compiler-plugin/plugin.ts +0 -478
  165. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleClientOnlyJSX.d.ts +0 -0
  166. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateIsomorphicFn.d.ts +0 -0
  167. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateMiddleware.d.ts +0 -0
  168. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateServerFn.d.ts +0 -0
  169. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleEnvOnly.d.ts +0 -0
  170. /package/dist/esm/{start-compiler-plugin → start-compiler}/utils.d.ts +0 -0
  171. /package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/dev-styles.d.ts +0 -0
  172. /package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/extract-html-scripts.d.ts +0 -0
  173. /package/dist/esm/{load-env-plugin → vite/load-env-plugin}/plugin.d.ts +0 -0
  174. /package/dist/esm/{output-directory.d.ts → vite/output-directory.d.ts} +0 -0
  175. /package/dist/esm/{preview-server-plugin → vite/preview-server-plugin}/plugin.d.ts +0 -0
  176. /package/src/{start-compiler-plugin → start-compiler}/handleClientOnlyJSX.ts +0 -0
  177. /package/src/{start-compiler-plugin → start-compiler}/handleCreateIsomorphicFn.ts +0 -0
  178. /package/src/{start-compiler-plugin → start-compiler}/handleCreateMiddleware.ts +0 -0
  179. /package/src/{start-compiler-plugin → start-compiler}/handleEnvOnly.ts +0 -0
  180. /package/src/{start-compiler-plugin → start-compiler}/utils.ts +0 -0
  181. /package/src/{dev-server-plugin → vite/dev-server-plugin}/dev-styles.ts +0 -0
  182. /package/src/{dev-server-plugin → vite/dev-server-plugin}/extract-html-scripts.ts +0 -0
  183. /package/src/{load-env-plugin → vite/load-env-plugin}/plugin.ts +0 -0
@@ -0,0 +1,234 @@
1
+ import { normalizePath } from 'vite'
2
+ import { join } from 'pathe'
3
+ import { escapePath } from 'tinyglobby'
4
+ import { ENTRY_POINTS, START_ENVIRONMENT_NAMES } from '../constants'
5
+ import { getBundlerOptions } from '../utils'
6
+ import type { CompileStartFrameworkOptions } from '../types'
7
+ import type { ResolvedStartEntryPlan } from '../planning'
8
+ import type * as vite from 'vite'
9
+
10
+ export interface ViteResolvedEntryAliases {
11
+ client: string
12
+ server: string
13
+ start: string
14
+ router: string
15
+ alias: Record<(typeof ENTRY_POINTS)[keyof typeof ENTRY_POINTS], string>
16
+ }
17
+
18
+ export function createViteResolvedEntryAliases(opts: {
19
+ entryPaths: ResolvedStartEntryPlan['entryPaths']
20
+ }): ViteResolvedEntryAliases {
21
+ const client = normalizePath(opts.entryPaths.client)
22
+ const server = normalizePath(opts.entryPaths.server)
23
+ const start = normalizePath(opts.entryPaths.start)
24
+ const router = normalizePath(opts.entryPaths.router)
25
+
26
+ return {
27
+ client,
28
+ server,
29
+ start,
30
+ router,
31
+ alias: {
32
+ [ENTRY_POINTS.client]: client,
33
+ [ENTRY_POINTS.server]: server,
34
+ [ENTRY_POINTS.start]: start,
35
+ [ENTRY_POINTS.router]: router,
36
+ },
37
+ }
38
+ }
39
+
40
+ export function createViteConfigPlan(opts: {
41
+ viteConfig: vite.UserConfig
42
+ framework: CompileStartFrameworkOptions
43
+ entryAliases: ViteResolvedEntryAliases
44
+ clientOutputDirectory: string
45
+ serverOutputDirectory: string
46
+ serverFnProviderEnv: string
47
+ optimizeDepsExclude: Array<string>
48
+ noExternal: Array<string>
49
+ }) {
50
+ return {
51
+ environments: {
52
+ [START_ENVIRONMENT_NAMES.client]: {
53
+ consumer: 'client',
54
+ build: (() => {
55
+ const bundlerOptions = {
56
+ input: {
57
+ index: ENTRY_POINTS.client,
58
+ },
59
+ }
60
+ return {
61
+ rollupOptions: bundlerOptions,
62
+ rolldownOptions: bundlerOptions,
63
+ outDir: opts.clientOutputDirectory,
64
+ }
65
+ })(),
66
+ optimizeDeps: {
67
+ exclude: opts.optimizeDepsExclude,
68
+ entries: escapeEntries([
69
+ opts.entryAliases.client,
70
+ opts.entryAliases.router,
71
+ ]),
72
+ },
73
+ },
74
+ [START_ENVIRONMENT_NAMES.server]: {
75
+ consumer: 'server',
76
+ build: {
77
+ ssr: true,
78
+ ...(() => {
79
+ const bundlerOptions = {
80
+ input:
81
+ getBundlerOptions(
82
+ opts.viteConfig.environments?.[START_ENVIRONMENT_NAMES.server]
83
+ ?.build,
84
+ )?.input ?? opts.entryAliases.server,
85
+ }
86
+ return {
87
+ rollupOptions: bundlerOptions,
88
+ rolldownOptions: bundlerOptions,
89
+ }
90
+ })(),
91
+ outDir: opts.serverOutputDirectory,
92
+ commonjsOptions: {
93
+ include: [/node_modules/],
94
+ },
95
+ copyPublicDir:
96
+ opts.viteConfig.environments?.[START_ENVIRONMENT_NAMES.server]
97
+ ?.build?.copyPublicDir ?? false,
98
+ },
99
+ optimizeDeps: {
100
+ entries: escapeEntries([
101
+ opts.entryAliases.server,
102
+ opts.entryAliases.start,
103
+ opts.entryAliases.router,
104
+ ]),
105
+ },
106
+ },
107
+ ...(opts.serverFnProviderEnv !== START_ENVIRONMENT_NAMES.server && {
108
+ [opts.serverFnProviderEnv]: {
109
+ build: {
110
+ outDir: join(opts.serverOutputDirectory, opts.serverFnProviderEnv),
111
+ },
112
+ optimizeDeps: {
113
+ entries: escapeEntries([opts.entryAliases.router]),
114
+ },
115
+ },
116
+ }),
117
+ },
118
+ resolve: {
119
+ noExternal: [
120
+ '@tanstack/start**',
121
+ `@tanstack/${opts.framework}-start**`,
122
+ ...opts.noExternal,
123
+ ],
124
+ alias: {
125
+ ...opts.entryAliases.alias,
126
+ },
127
+ },
128
+ } satisfies Pick<vite.UserConfig, 'environments' | 'resolve'>
129
+ }
130
+
131
+ export function createViteDefineConfig(opts: {
132
+ command: 'serve' | 'build'
133
+ mode: string | undefined
134
+ serverFnBase: string
135
+ routerBasepath: string
136
+ spaEnabled: boolean | undefined
137
+ devSsrStylesEnabled: boolean
138
+ devSsrStylesBasepath: string
139
+ staticNodeEnv: boolean
140
+ }) {
141
+ return {
142
+ ...defineReplaceEnv('TSS_SERVER_FN_BASE', opts.serverFnBase),
143
+ ...defineReplaceEnv('TSS_ROUTER_BASEPATH', opts.routerBasepath),
144
+ ...(opts.command === 'serve'
145
+ ? defineReplaceEnv('TSS_SHELL', opts.spaEnabled ? 'true' : 'false')
146
+ : {}),
147
+ ...defineReplaceEnv(
148
+ 'TSS_DEV_SERVER',
149
+ opts.command === 'serve' ? 'true' : 'false',
150
+ ),
151
+ ...defineReplaceEnv(
152
+ 'TSS_DEV_SSR_STYLES_ENABLED',
153
+ opts.devSsrStylesEnabled ? 'true' : 'false',
154
+ ),
155
+ ...defineReplaceEnv(
156
+ 'TSS_DEV_SSR_STYLES_BASEPATH',
157
+ opts.devSsrStylesBasepath,
158
+ ),
159
+ ...(opts.command === 'build' && opts.staticNodeEnv
160
+ ? {
161
+ 'process.env.NODE_ENV': JSON.stringify(
162
+ process.env.NODE_ENV || opts.mode || 'production',
163
+ ),
164
+ }
165
+ : {}),
166
+ } satisfies NonNullable<vite.UserConfig['define']>
167
+ }
168
+
169
+ export async function buildStartViteEnvironments(opts: {
170
+ builder: vite.ViteBuilder
171
+ providerEnvironmentName: string
172
+ ssrIsProvider: boolean
173
+ }) {
174
+ const client = getRequiredBuilderEnvironment(
175
+ opts.builder,
176
+ START_ENVIRONMENT_NAMES.client,
177
+ 'Client environment not found',
178
+ )
179
+ const server = getRequiredBuilderEnvironment(
180
+ opts.builder,
181
+ START_ENVIRONMENT_NAMES.server,
182
+ 'SSR environment not found',
183
+ )
184
+
185
+ if (!client.isBuilt) {
186
+ await opts.builder.build(client)
187
+ }
188
+
189
+ if (!server.isBuilt) {
190
+ await opts.builder.build(server)
191
+ }
192
+
193
+ if (opts.ssrIsProvider) {
194
+ return
195
+ }
196
+
197
+ const providerEnv = getRequiredBuilderEnvironment(
198
+ opts.builder,
199
+ opts.providerEnvironmentName,
200
+ `Provider environment "${opts.providerEnvironmentName}" not found`,
201
+ )
202
+
203
+ if (!providerEnv.isBuilt) {
204
+ await opts.builder.build(providerEnv)
205
+ }
206
+ }
207
+
208
+ function escapeEntries(entries: Array<string>) {
209
+ return entries.map((entry) => escapePath(entry))
210
+ }
211
+
212
+ function defineReplaceEnv<TKey extends string, TValue extends string>(
213
+ key: TKey,
214
+ value: TValue,
215
+ ): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {
216
+ return {
217
+ [`process.env.${key}`]: JSON.stringify(value),
218
+ [`import.meta.env.${key}`]: JSON.stringify(value),
219
+ } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }
220
+ }
221
+
222
+ function getRequiredBuilderEnvironment(
223
+ builder: vite.ViteBuilder,
224
+ environmentName: string,
225
+ errorMessage: string,
226
+ ) {
227
+ const environment = builder.environments[environmentName]
228
+
229
+ if (!environment) {
230
+ throw new Error(errorMessage)
231
+ }
232
+
233
+ return environment
234
+ }
@@ -0,0 +1,276 @@
1
+ import { crawlFrameworkPkgs } from 'vitefu'
2
+ import {
3
+ applyResolvedBaseAndOutput,
4
+ applyResolvedRouterBasepath,
5
+ createStartConfigContext,
6
+ } from '../config-context'
7
+ import { START_ENVIRONMENT_NAMES } from '../constants'
8
+ import { importProtectionPlugin } from '../import-protection-plugin/plugin'
9
+ import {
10
+ createServerFnBasePath,
11
+ normalizePublicBase,
12
+ shouldRewriteDevBasepath,
13
+ } from '../planning'
14
+ import { startCompilerPlugin } from './start-compiler-plugin/plugin'
15
+ import { loadEnvPlugin } from './load-env-plugin/plugin'
16
+ import {
17
+ buildStartViteEnvironments,
18
+ createViteConfigPlan,
19
+ createViteDefineConfig,
20
+ createViteResolvedEntryAliases,
21
+ } from './planning'
22
+ import { devServerPlugin } from './dev-server-plugin/plugin'
23
+ import { previewServerPlugin } from './preview-server-plugin/plugin'
24
+ import {
25
+ createCaptureClientBuildPlugin,
26
+ createDevBaseRewritePlugin,
27
+ createPostBuildPlugin,
28
+ } from './plugins'
29
+ import { parseStartConfig } from './schema'
30
+ import { startManifestPlugin } from './start-manifest-plugin/plugin'
31
+ import { tanStackStartRouter } from './start-router-plugin/plugin'
32
+ import {
33
+ getClientOutputDirectory,
34
+ getServerOutputDirectory,
35
+ } from './output-directory'
36
+ import { postServerBuild } from './post-server-build'
37
+ import { serializationAdaptersPlugin } from './serialization-adapters-plugin'
38
+ import type { NormalizedClientBuild } from '../types'
39
+ import type {
40
+ TanStackStartVitePluginCoreOptions,
41
+ ViteRscForwardSsrResolverStrategy,
42
+ } from './types'
43
+ import type { StartEnvironmentName } from '../constants'
44
+ import type { TanStackStartViteInputConfig } from './schema'
45
+ import type { PluginOption } from 'vite'
46
+
47
+ export function tanStackStartVite(
48
+ corePluginOpts: TanStackStartVitePluginCoreOptions,
49
+ startPluginOpts: TanStackStartViteInputConfig | undefined,
50
+ ): Array<PluginOption> {
51
+ const normalizedStartPluginOpts = startPluginOpts ?? {}
52
+
53
+ const configContext = createStartConfigContext({
54
+ corePluginOpts,
55
+ startPluginOpts: normalizedStartPluginOpts,
56
+ parseConfig: parseStartConfig,
57
+ })
58
+ const { getConfig, resolvedStartConfig } = configContext
59
+ const serverFnProviderEnv = corePluginOpts.providerEnvironmentName
60
+ const ssrIsProvider = corePluginOpts.ssrIsProvider
61
+
62
+ // When the router basepath and vite base are misaligned during dev,
63
+ // we install a URL rewrite middleware instead of erroring.
64
+ let needsDevBaseRewrite = false
65
+
66
+ const capturedClientBuild: Partial<
67
+ Record<StartEnvironmentName, NormalizedClientBuild>
68
+ > = {}
69
+
70
+ function getClientBuild(
71
+ envName: StartEnvironmentName,
72
+ ): NormalizedClientBuild | undefined {
73
+ return capturedClientBuild[envName]
74
+ }
75
+
76
+ const environments: Array<{
77
+ name: string
78
+ type: 'client' | 'server'
79
+ getServerFnById?: string
80
+ }> = [
81
+ { name: START_ENVIRONMENT_NAMES.client, type: 'client' },
82
+ {
83
+ name: START_ENVIRONMENT_NAMES.server,
84
+ type: 'server',
85
+ getServerFnById:
86
+ corePluginOpts.ssrResolverStrategy.type === 'vite-rsc-forward'
87
+ ? createViteRscForwarder(corePluginOpts.ssrResolverStrategy)
88
+ : undefined,
89
+ },
90
+ ]
91
+ if (
92
+ serverFnProviderEnv !== START_ENVIRONMENT_NAMES.server &&
93
+ !environments.find((e) => e.name === serverFnProviderEnv)
94
+ ) {
95
+ environments.push({
96
+ name: serverFnProviderEnv,
97
+ type: 'server',
98
+ })
99
+ }
100
+ return [
101
+ {
102
+ name: 'tanstack-start-core:config',
103
+ enforce: 'pre',
104
+ async config(viteConfig, { command }) {
105
+ const publicBase = normalizePublicBase(viteConfig.base)
106
+ applyResolvedBaseAndOutput({
107
+ resolvedStartConfig,
108
+ root: viteConfig.root || process.cwd(),
109
+ publicBase,
110
+ clientOutputDirectory: getClientOutputDirectory(viteConfig),
111
+ serverOutputDirectory: getServerOutputDirectory(viteConfig),
112
+ })
113
+ const { startConfig } = getConfig()
114
+ const routerBasepath = applyResolvedRouterBasepath({
115
+ resolvedStartConfig,
116
+ startConfig,
117
+ })
118
+
119
+ if (
120
+ shouldRewriteDevBasepath({
121
+ command,
122
+ middlewareMode: Boolean(viteConfig.server?.middlewareMode),
123
+ routerBasepath,
124
+ publicBase: resolvedStartConfig.basePaths.publicBase,
125
+ })
126
+ ) {
127
+ // The router basepath and vite base are misaligned.
128
+ // Instead of erroring, we install a dev-server middleware that
129
+ // rewrites incoming request URLs to prepend the vite base prefix.
130
+ // This allows users to have e.g. base: '/_ui/' for asset URLs
131
+ // while keeping router basepath at '/' for page navigation.
132
+ needsDevBaseRewrite = true
133
+ }
134
+
135
+ const TSS_SERVER_FN_BASE = createServerFnBasePath({
136
+ routerBasepath,
137
+ serverFnBase: startConfig.serverFns.base,
138
+ })
139
+ const resolvedEntryPlan = configContext.resolveEntries()
140
+
141
+ const entryAliases = createViteResolvedEntryAliases({
142
+ entryPaths: resolvedEntryPlan.entryPaths,
143
+ })
144
+
145
+ const startPackageName =
146
+ `@tanstack/${corePluginOpts.framework}-start` as const
147
+
148
+ // crawl packages that have start in "peerDependencies"
149
+ // see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101
150
+
151
+ // this is currently uncached; could be implemented similarly as vite handles lock file changes
152
+ // see https://github.com/vitejs/vite/blob/557f797d29422027e8c451ca50dd84bf8c41b5f0/packages/vite/src/node/optimizer/index.ts#L1282
153
+
154
+ const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({
155
+ root: process.cwd(),
156
+ isBuild: command === 'build',
157
+ isFrameworkPkgByJson(pkgJson) {
158
+ const peerDependencies = pkgJson['peerDependencies']
159
+
160
+ if (peerDependencies) {
161
+ if (
162
+ startPackageName in peerDependencies ||
163
+ '@tanstack/start-client-core' in peerDependencies
164
+ ) {
165
+ return true
166
+ }
167
+ }
168
+
169
+ return false
170
+ },
171
+ })
172
+
173
+ const viteConfigPlan = createViteConfigPlan({
174
+ viteConfig,
175
+ framework: corePluginOpts.framework,
176
+ entryAliases,
177
+ clientOutputDirectory: resolvedStartConfig.outputDirectories.client,
178
+ serverOutputDirectory: resolvedStartConfig.outputDirectories.server,
179
+ serverFnProviderEnv,
180
+ optimizeDepsExclude: crawlFrameworkPkgsResult.optimizeDeps.exclude,
181
+ noExternal: crawlFrameworkPkgsResult.ssr.noExternal.sort(),
182
+ })
183
+
184
+ return {
185
+ // see https://vite.dev/config/shared-options.html#apptype
186
+ // this will prevent vite from injecting middlewares that we don't want
187
+ appType: viteConfig.appType ?? 'custom',
188
+ environments: viteConfigPlan.environments,
189
+ resolve: viteConfigPlan.resolve,
190
+ define: createViteDefineConfig({
191
+ command,
192
+ mode: viteConfig.mode,
193
+ serverFnBase: TSS_SERVER_FN_BASE,
194
+ routerBasepath,
195
+ spaEnabled: startConfig.spa?.enabled,
196
+ devSsrStylesEnabled: startConfig.dev.ssrStyles.enabled,
197
+ devSsrStylesBasepath:
198
+ startConfig.dev.ssrStyles.basepath ??
199
+ resolvedStartConfig.basePaths.publicBase,
200
+ staticNodeEnv: startConfig.server.build.staticNodeEnv,
201
+ }),
202
+ builder: {
203
+ sharedPlugins: true,
204
+ async buildApp(builder) {
205
+ await buildStartViteEnvironments({
206
+ builder,
207
+ providerEnvironmentName: serverFnProviderEnv,
208
+ ssrIsProvider,
209
+ })
210
+ },
211
+ },
212
+ }
213
+ },
214
+ },
215
+ createPostBuildPlugin({
216
+ getConfig,
217
+ postServerBuild,
218
+ }),
219
+ // Server function plugin handles:
220
+ // 1. Identifying createServerFn().handler() calls
221
+ // 2. Extracting server functions to separate modules
222
+ // 3. Replacing call sites with RPC stubs
223
+ // 4. Generating the server function manifest
224
+ // Also handles createIsomorphicFn, createServerOnlyFn, createClientOnlyFn, createMiddleware
225
+ startCompilerPlugin({
226
+ framework: corePluginOpts.framework,
227
+ environments,
228
+ generateFunctionId:
229
+ normalizedStartPluginOpts.serverFns?.generateFunctionId,
230
+ providerEnvName: serverFnProviderEnv,
231
+ }),
232
+ importProtectionPlugin({
233
+ getConfig,
234
+ framework: corePluginOpts.framework,
235
+ environments,
236
+ providerEnvName: serverFnProviderEnv,
237
+ }),
238
+ tanStackStartRouter(normalizedStartPluginOpts, getConfig, corePluginOpts),
239
+ loadEnvPlugin(),
240
+ startManifestPlugin({
241
+ getClientBuild: () => getClientBuild(START_ENVIRONMENT_NAMES.client),
242
+ getConfig,
243
+ }),
244
+ // When the vite base and router basepath are misaligned (e.g. base: '/_ui/', basepath: '/'),
245
+ // install a middleware that rewrites incoming request URLs to prepend the vite base prefix.
246
+ // This allows Vite's internal base middleware to accept the requests, then strips the prefix
247
+ // before passing to the SSR handler.
248
+ // Registered BEFORE devServerPlugin so this middleware is added to the Connect stack first,
249
+ // ensuring all subsequent middlewares (CSS, SSR, etc.) see the rewritten URL.
250
+ createDevBaseRewritePlugin({
251
+ shouldRewriteDevBase: () => needsDevBaseRewrite,
252
+ resolvedStartConfig,
253
+ }),
254
+ devServerPlugin({
255
+ getConfig,
256
+ devSsrStylesEnabled:
257
+ normalizedStartPluginOpts.dev?.ssrStyles?.enabled ?? true,
258
+ installDevServerMiddleware:
259
+ normalizedStartPluginOpts.vite?.installDevServerMiddleware,
260
+ }),
261
+ previewServerPlugin(),
262
+ serializationAdaptersPlugin({
263
+ adapters: corePluginOpts.serializationAdapters,
264
+ }),
265
+ createCaptureClientBuildPlugin({
266
+ capturedClientBuild,
267
+ }),
268
+ ]
269
+ }
270
+
271
+ function createViteRscForwarder(strategy: ViteRscForwardSsrResolverStrategy) {
272
+ return `export async function getServerFnById(id, access) {
273
+ const m = await import.meta.viteRsc.loadModule(${JSON.stringify(strategy.sourceEnvironmentName)}, ${JSON.stringify(strategy.sourceEntry)})
274
+ return m[${JSON.stringify(strategy.exportName)}](id, access)
275
+ }`
276
+ }
@@ -0,0 +1,81 @@
1
+ import { START_ENVIRONMENT_NAMES } from '../constants'
2
+ import { normalizeViteClientBuild } from './start-manifest-plugin/normalized-client-build'
3
+ import type {
4
+ GetConfigFn,
5
+ NormalizedClientBuild,
6
+ ResolvedStartConfig,
7
+ } from '../types'
8
+ import type { StartEnvironmentName } from '../constants'
9
+ import type { PluginOption, ViteBuilder } from 'vite'
10
+
11
+ export function createPostBuildPlugin(opts: {
12
+ getConfig: GetConfigFn
13
+ postServerBuild: (opts: {
14
+ startConfig: ReturnType<GetConfigFn>['startConfig']
15
+ builder: ViteBuilder
16
+ }) => Promise<void>
17
+ }): PluginOption {
18
+ return {
19
+ name: 'tanstack-start-core:post-build',
20
+ enforce: 'post',
21
+ buildApp: {
22
+ order: 'post',
23
+ async handler(builder) {
24
+ const { startConfig } = opts.getConfig()
25
+ await opts.postServerBuild({ builder, startConfig })
26
+ },
27
+ },
28
+ }
29
+ }
30
+
31
+ export function createDevBaseRewritePlugin(opts: {
32
+ shouldRewriteDevBase: () => boolean
33
+ resolvedStartConfig: ResolvedStartConfig
34
+ }): PluginOption {
35
+ return {
36
+ name: 'tanstack-start-core:dev-base-rewrite',
37
+ configureServer(server) {
38
+ if (!opts.shouldRewriteDevBase()) {
39
+ return
40
+ }
41
+
42
+ const basePrefix = opts.resolvedStartConfig.basePaths.publicBase.replace(
43
+ /\/$/,
44
+ '',
45
+ )
46
+
47
+ server.middlewares.use((req, _res, next) => {
48
+ if (req.url && !req.url.startsWith(basePrefix)) {
49
+ req.url = basePrefix + req.url
50
+ }
51
+
52
+ next()
53
+ })
54
+ },
55
+ }
56
+ }
57
+
58
+ export function createCaptureClientBuildPlugin(opts: {
59
+ capturedClientBuild: Partial<
60
+ Record<StartEnvironmentName, NormalizedClientBuild>
61
+ >
62
+ }): PluginOption {
63
+ return {
64
+ name: 'tanstack-start:core:capture-bundle',
65
+ applyToEnvironment(environment) {
66
+ return environment.name === START_ENVIRONMENT_NAMES.client
67
+ },
68
+ enforce: 'post',
69
+ generateBundle(_options, bundle) {
70
+ const environment = this.environment.name as StartEnvironmentName
71
+
72
+ if (environment !== START_ENVIRONMENT_NAMES.client) {
73
+ throw new Error(
74
+ `Unexpected environment for client build capture: ${environment}`,
75
+ )
76
+ }
77
+
78
+ opts.capturedClientBuild[environment] = normalizeViteClientBuild(bundle)
79
+ },
80
+ }
81
+ }
@@ -1,8 +1,8 @@
1
1
  import { HEADERS } from '@tanstack/start-server-core'
2
- import { buildSitemap } from './build-sitemap'
3
- import { VITE_ENVIRONMENT_NAMES } from './constants'
2
+ import { buildSitemap } from '../build-sitemap'
4
3
  import { prerender } from './prerender'
5
- import type { TanStackStartOutputConfig } from './schema'
4
+ import { getClientOutputDirectory } from './output-directory'
5
+ import type { TanStackStartOutputConfig } from '../schema'
6
6
  import type { ViteBuilder } from 'vite'
7
7
 
8
8
  export async function postServerBuild({
@@ -65,9 +65,7 @@ export async function postServerBuild({
65
65
  if (startConfig.sitemap?.enabled) {
66
66
  buildSitemap({
67
67
  startConfig,
68
- publicDir:
69
- builder.environments[VITE_ENVIRONMENT_NAMES.client]?.config.build
70
- .outDir ?? builder.config.build.outDir,
68
+ publicDir: getClientOutputDirectory(builder.config),
71
69
  })
72
70
  }
73
71
  }