@tanstack/start-plugin-core 1.167.18 → 1.167.20

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 (184) 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 +36 -25
  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/LICENSE +0 -21
  140. package/dist/esm/dev-server-plugin/dev-styles.js.map +0 -1
  141. package/dist/esm/dev-server-plugin/extract-html-scripts.js.map +0 -1
  142. package/dist/esm/dev-server-plugin/plugin.d.ts +0 -6
  143. package/dist/esm/dev-server-plugin/plugin.js.map +0 -1
  144. package/dist/esm/load-env-plugin/plugin.js.map +0 -1
  145. package/dist/esm/output-directory.js.map +0 -1
  146. package/dist/esm/plugin.d.ts +0 -4
  147. package/dist/esm/plugin.js +0 -301
  148. package/dist/esm/plugin.js.map +0 -1
  149. package/dist/esm/post-server-build.js.map +0 -1
  150. package/dist/esm/prerender.js.map +0 -1
  151. package/dist/esm/preview-server-plugin/plugin.js.map +0 -1
  152. package/dist/esm/start-compiler-plugin/compiler.js.map +0 -1
  153. package/dist/esm/start-compiler-plugin/handleClientOnlyJSX.js.map +0 -1
  154. package/dist/esm/start-compiler-plugin/handleCreateIsomorphicFn.js.map +0 -1
  155. package/dist/esm/start-compiler-plugin/handleCreateMiddleware.js.map +0 -1
  156. package/dist/esm/start-compiler-plugin/handleCreateServerFn.js.map +0 -1
  157. package/dist/esm/start-compiler-plugin/handleEnvOnly.js.map +0 -1
  158. package/dist/esm/start-compiler-plugin/plugin.js +0 -297
  159. package/dist/esm/start-compiler-plugin/plugin.js.map +0 -1
  160. package/dist/esm/start-compiler-plugin/utils.js.map +0 -1
  161. package/dist/esm/start-manifest-plugin/plugin.d.ts +0 -6
  162. package/dist/esm/start-manifest-plugin/plugin.js.map +0 -1
  163. package/dist/esm/start-router-plugin/plugin.js.map +0 -1
  164. package/src/plugin.ts +0 -471
  165. package/src/start-compiler-plugin/plugin.ts +0 -478
  166. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleClientOnlyJSX.d.ts +0 -0
  167. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateIsomorphicFn.d.ts +0 -0
  168. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateMiddleware.d.ts +0 -0
  169. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleCreateServerFn.d.ts +0 -0
  170. /package/dist/esm/{start-compiler-plugin → start-compiler}/handleEnvOnly.d.ts +0 -0
  171. /package/dist/esm/{start-compiler-plugin → start-compiler}/utils.d.ts +0 -0
  172. /package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/dev-styles.d.ts +0 -0
  173. /package/dist/esm/{dev-server-plugin → vite/dev-server-plugin}/extract-html-scripts.d.ts +0 -0
  174. /package/dist/esm/{load-env-plugin → vite/load-env-plugin}/plugin.d.ts +0 -0
  175. /package/dist/esm/{output-directory.d.ts → vite/output-directory.d.ts} +0 -0
  176. /package/dist/esm/{preview-server-plugin → vite/preview-server-plugin}/plugin.d.ts +0 -0
  177. /package/src/{start-compiler-plugin → start-compiler}/handleClientOnlyJSX.ts +0 -0
  178. /package/src/{start-compiler-plugin → start-compiler}/handleCreateIsomorphicFn.ts +0 -0
  179. /package/src/{start-compiler-plugin → start-compiler}/handleCreateMiddleware.ts +0 -0
  180. /package/src/{start-compiler-plugin → start-compiler}/handleEnvOnly.ts +0 -0
  181. /package/src/{start-compiler-plugin → start-compiler}/utils.ts +0 -0
  182. /package/src/{dev-server-plugin → vite/dev-server-plugin}/dev-styles.ts +0 -0
  183. /package/src/{dev-server-plugin → vite/dev-server-plugin}/extract-html-scripts.ts +0 -0
  184. /package/src/{load-env-plugin → vite/load-env-plugin}/plugin.ts +0 -0
package/src/plugin.ts DELETED
@@ -1,471 +0,0 @@
1
- import { joinPaths } from '@tanstack/router-core'
2
- import * as vite from 'vite'
3
- import { crawlFrameworkPkgs } from 'vitefu'
4
- import { join } from 'pathe'
5
- import { escapePath } from 'tinyglobby'
6
- import { startManifestPlugin } from './start-manifest-plugin/plugin'
7
- import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from './constants'
8
- import { getBundlerOptions } from './utils'
9
- import { tanStackStartRouter } from './start-router-plugin/plugin'
10
- import { loadEnvPlugin } from './load-env-plugin/plugin'
11
- import { devServerPlugin } from './dev-server-plugin/plugin'
12
- import { previewServerPlugin } from './preview-server-plugin/plugin'
13
- import { parseStartConfig } from './schema'
14
- import { resolveEntry } from './resolve-entries'
15
- import {
16
- getClientOutputDirectory,
17
- getServerOutputDirectory,
18
- } from './output-directory'
19
- import { postServerBuild } from './post-server-build'
20
- import { startCompilerPlugin } from './start-compiler-plugin/plugin'
21
- import { importProtectionPlugin } from './import-protection-plugin/plugin'
22
- import type {
23
- GetConfigFn,
24
- ResolvedStartConfig,
25
- TanStackStartVitePluginCoreOptions,
26
- } from './types'
27
- import type { ViteEnvironmentNames } from './constants'
28
- import type {
29
- TanStackStartInputConfig,
30
- TanStackStartOutputConfig,
31
- } from './schema'
32
- import type { PluginOption } from 'vite'
33
-
34
- function isFullUrl(str: string): boolean {
35
- try {
36
- new URL(str)
37
- return true
38
- } catch {
39
- return false
40
- }
41
- }
42
-
43
- export function TanStackStartVitePluginCore(
44
- corePluginOpts: TanStackStartVitePluginCoreOptions,
45
- startPluginOpts: TanStackStartInputConfig,
46
- ): Array<PluginOption> {
47
- // Determine the provider environment for server functions
48
- // If providerEnv is set, use that; otherwise default to SSR as the provider
49
- const serverFnProviderEnv =
50
- corePluginOpts.serverFn?.providerEnv || VITE_ENVIRONMENT_NAMES.server
51
- const ssrIsProvider = serverFnProviderEnv === VITE_ENVIRONMENT_NAMES.server
52
-
53
- const resolvedStartConfig: ResolvedStartConfig = {
54
- root: '',
55
- startFilePath: undefined,
56
- routerFilePath: '',
57
- srcDirectory: '',
58
- viteAppBase: '',
59
- serverFnProviderEnv,
60
- }
61
-
62
- let startConfig: TanStackStartOutputConfig | null
63
- const getConfig: GetConfigFn = () => {
64
- if (!resolvedStartConfig.root) {
65
- throw new Error(`Cannot get config before root is resolved`)
66
- }
67
- if (!startConfig) {
68
- startConfig = parseStartConfig(
69
- startPluginOpts,
70
- corePluginOpts,
71
- resolvedStartConfig.root,
72
- )
73
- }
74
- return { startConfig, resolvedStartConfig, corePluginOpts }
75
- }
76
-
77
- // When the router basepath and vite base are misaligned during dev,
78
- // we install a URL rewrite middleware instead of erroring.
79
- let needsDevBaseRewrite = false
80
-
81
- const capturedBundle: Partial<
82
- Record<ViteEnvironmentNames, vite.Rollup.OutputBundle>
83
- > = {}
84
-
85
- function getBundle(envName: ViteEnvironmentNames): vite.Rollup.OutputBundle {
86
- const bundle = capturedBundle[envName]
87
- if (!bundle) {
88
- throw new Error(`No bundle captured for environment: ${envName}`)
89
- }
90
- return bundle
91
- }
92
-
93
- const environments: Array<{ name: string; type: 'client' | 'server' }> = [
94
- { name: VITE_ENVIRONMENT_NAMES.client, type: 'client' },
95
- { name: VITE_ENVIRONMENT_NAMES.server, type: 'server' },
96
- ]
97
- if (
98
- corePluginOpts.serverFn?.providerEnv &&
99
- !environments.find((e) => e.name === corePluginOpts.serverFn?.providerEnv)
100
- ) {
101
- environments.push({
102
- name: corePluginOpts.serverFn.providerEnv,
103
- type: 'server',
104
- })
105
- }
106
- return [
107
- {
108
- name: 'tanstack-start-core:config',
109
- enforce: 'pre',
110
- async config(viteConfig, { command }) {
111
- resolvedStartConfig.viteAppBase = viteConfig.base ?? '/'
112
- if (!isFullUrl(resolvedStartConfig.viteAppBase)) {
113
- resolvedStartConfig.viteAppBase = joinPaths([
114
- '/',
115
- viteConfig.base,
116
- '/',
117
- ])
118
- }
119
- const root = viteConfig.root || process.cwd()
120
- resolvedStartConfig.root = root
121
-
122
- const { startConfig } = getConfig()
123
- if (startConfig.router.basepath === undefined) {
124
- if (!isFullUrl(resolvedStartConfig.viteAppBase)) {
125
- startConfig.router.basepath =
126
- resolvedStartConfig.viteAppBase.replace(/^\/|\/$/g, '')
127
- } else {
128
- startConfig.router.basepath = '/'
129
- }
130
- } else {
131
- if (command === 'serve' && !viteConfig.server?.middlewareMode) {
132
- // when serving, we must ensure that router basepath and viteAppBase are aligned
133
- if (
134
- !joinPaths(['/', startConfig.router.basepath, '/']).startsWith(
135
- joinPaths(['/', resolvedStartConfig.viteAppBase, '/']),
136
- )
137
- ) {
138
- // The router basepath and vite base are misaligned.
139
- // Instead of erroring, we install a dev-server middleware that
140
- // rewrites incoming request URLs to prepend the vite base prefix.
141
- // This allows users to have e.g. base: '/_ui/' for asset URLs
142
- // while keeping router basepath at '/' for page navigation.
143
- needsDevBaseRewrite = true
144
- }
145
- }
146
- }
147
-
148
- const TSS_SERVER_FN_BASE = joinPaths([
149
- '/',
150
- startConfig.router.basepath,
151
- startConfig.serverFns.base,
152
- '/',
153
- ])
154
- const resolvedSrcDirectory = join(root, startConfig.srcDirectory)
155
- resolvedStartConfig.srcDirectory = resolvedSrcDirectory
156
-
157
- const startFilePath = resolveEntry({
158
- type: 'start entry',
159
- configuredEntry: startConfig.start.entry,
160
- defaultEntry: 'start',
161
- resolvedSrcDirectory,
162
- required: false,
163
- })
164
- resolvedStartConfig.startFilePath = startFilePath
165
-
166
- const routerFilePath = resolveEntry({
167
- type: 'router entry',
168
- configuredEntry: startConfig.router.entry,
169
- defaultEntry: 'router',
170
- resolvedSrcDirectory,
171
- required: true,
172
- })
173
- resolvedStartConfig.routerFilePath = routerFilePath
174
-
175
- const clientEntryPath = resolveEntry({
176
- type: 'client entry',
177
- configuredEntry: startConfig.client.entry,
178
- defaultEntry: 'client',
179
- resolvedSrcDirectory,
180
- required: false,
181
- })
182
-
183
- const serverEntryPath = resolveEntry({
184
- type: 'server entry',
185
- configuredEntry: startConfig.server.entry,
186
- defaultEntry: 'server',
187
- resolvedSrcDirectory,
188
- required: false,
189
- })
190
-
191
- const clientAlias = vite.normalizePath(
192
- clientEntryPath ?? corePluginOpts.defaultEntryPaths.client,
193
- )
194
- const serverAlias = vite.normalizePath(
195
- serverEntryPath ?? corePluginOpts.defaultEntryPaths.server,
196
- )
197
- const startAlias = vite.normalizePath(
198
- startFilePath ?? corePluginOpts.defaultEntryPaths.start,
199
- )
200
- const routerAlias = vite.normalizePath(routerFilePath)
201
-
202
- const entryAliasConfiguration: Record<
203
- (typeof ENTRY_POINTS)[keyof typeof ENTRY_POINTS],
204
- string
205
- > = {
206
- [ENTRY_POINTS.client]: clientAlias,
207
- [ENTRY_POINTS.server]: serverAlias,
208
- [ENTRY_POINTS.start]: startAlias,
209
- [ENTRY_POINTS.router]: routerAlias,
210
- }
211
-
212
- const startPackageName =
213
- `@tanstack/${corePluginOpts.framework}-start` as const
214
-
215
- // crawl packages that have start in "peerDependencies"
216
- // see https://github.com/svitejs/vitefu/blob/d8d82fa121e3b2215ba437107093c77bde51b63b/src/index.js#L95-L101
217
-
218
- // this is currently uncached; could be implemented similarly as vite handles lock file changes
219
- // see https://github.com/vitejs/vite/blob/557f797d29422027e8c451ca50dd84bf8c41b5f0/packages/vite/src/node/optimizer/index.ts#L1282
220
-
221
- const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({
222
- root: process.cwd(),
223
- isBuild: command === 'build',
224
- isFrameworkPkgByJson(pkgJson) {
225
- const peerDependencies = pkgJson['peerDependencies']
226
-
227
- if (peerDependencies) {
228
- if (
229
- startPackageName in peerDependencies ||
230
- '@tanstack/start-client-core' in peerDependencies
231
- ) {
232
- return true
233
- }
234
- }
235
-
236
- return false
237
- },
238
- })
239
-
240
- return {
241
- // see https://vite.dev/config/shared-options.html#apptype
242
- // this will prevent vite from injecting middlewares that we don't want
243
- appType: viteConfig.appType ?? 'custom',
244
- environments: {
245
- [VITE_ENVIRONMENT_NAMES.client]: {
246
- consumer: 'client',
247
- build: (() => {
248
- // Use the same object reference for both keys to avoid
249
- // Vite 8's deprecation warning when both are present.
250
- // Vite 7 reads rollupOptions, Vite 8 reads rolldownOptions.
251
- const bundlerOptions = {
252
- input: {
253
- main: ENTRY_POINTS.client,
254
- },
255
- }
256
- return {
257
- rollupOptions: bundlerOptions,
258
- rolldownOptions: bundlerOptions,
259
- outDir: getClientOutputDirectory(viteConfig),
260
- }
261
- })(),
262
- optimizeDeps: {
263
- exclude: crawlFrameworkPkgsResult.optimizeDeps.exclude,
264
- // Ensure user code can be crawled for dependencies
265
- entries: [clientAlias, routerAlias].map((entry) =>
266
- // Entries are treated as `tinyglobby` patterns so need to be escaped
267
- escapePath(entry),
268
- ),
269
- },
270
- },
271
- [VITE_ENVIRONMENT_NAMES.server]: {
272
- consumer: 'server',
273
- build: {
274
- ssr: true,
275
- ...(() => {
276
- const bundlerOptions = {
277
- input:
278
- getBundlerOptions(
279
- viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]
280
- ?.build,
281
- )?.input ?? serverAlias,
282
- }
283
- return {
284
- rollupOptions: bundlerOptions,
285
- rolldownOptions: bundlerOptions,
286
- }
287
- })(),
288
- outDir: getServerOutputDirectory(viteConfig),
289
- commonjsOptions: {
290
- include: [/node_modules/],
291
- },
292
- copyPublicDir:
293
- viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]
294
- ?.build?.copyPublicDir ?? false,
295
- },
296
- optimizeDeps: {
297
- // Ensure user code can be crawled for dependencies
298
- entries: [serverAlias, startAlias, routerAlias].map((entry) =>
299
- // Entries are treated as `tinyglobby` patterns so need to be escaped
300
- escapePath(entry),
301
- ),
302
- },
303
- },
304
- },
305
-
306
- resolve: {
307
- noExternal: [
308
- // ENTRY_POINTS.start,
309
- '@tanstack/start**',
310
- `@tanstack/${corePluginOpts.framework}-start**`,
311
- ...crawlFrameworkPkgsResult.ssr.noExternal.sort(),
312
- ],
313
- alias: {
314
- ...entryAliasConfiguration,
315
- },
316
- },
317
- /* prettier-ignore */
318
- define: {
319
- // define is an esbuild function that replaces the any instances of given keys with the given values
320
- // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify("TanStack Start")
321
- // This is not the same as injecting environment variables.
322
-
323
- ...defineReplaceEnv('TSS_SERVER_FN_BASE', TSS_SERVER_FN_BASE),
324
- ...defineReplaceEnv('TSS_ROUTER_BASEPATH', startConfig.router.basepath),
325
- ...(command === 'serve' ? defineReplaceEnv('TSS_SHELL', startConfig.spa?.enabled ? 'true' : 'false') : {}),
326
- ...defineReplaceEnv('TSS_DEV_SERVER', command === 'serve' ? 'true' : 'false'),
327
- // Dev SSR styles: enabled flag and basepath (defaults to vite base for asset URL alignment)
328
- ...defineReplaceEnv('TSS_DEV_SSR_STYLES_ENABLED', startConfig.dev.ssrStyles.enabled ? 'true' : 'false'),
329
- ...defineReplaceEnv('TSS_DEV_SSR_STYLES_BASEPATH', startConfig.dev.ssrStyles.basepath ?? resolvedStartConfig.viteAppBase),
330
- // Replace NODE_ENV during build (unless opted out) for dead code elimination in server bundles
331
- ...(command === 'build' && startConfig.server.build.staticNodeEnv ? {
332
- 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || viteConfig.mode || 'production'),
333
- } : {}),
334
- },
335
- builder: {
336
- sharedPlugins: true,
337
- async buildApp(builder) {
338
- const client = builder.environments[VITE_ENVIRONMENT_NAMES.client]
339
- const server = builder.environments[VITE_ENVIRONMENT_NAMES.server]
340
-
341
- if (!client) {
342
- throw new Error('Client environment not found')
343
- }
344
-
345
- if (!server) {
346
- throw new Error('SSR environment not found')
347
- }
348
-
349
- if (!client.isBuilt) {
350
- // Build the client bundle first
351
- await builder.build(client)
352
- }
353
- if (!server.isBuilt) {
354
- // Build the SSR bundle
355
- await builder.build(server)
356
- }
357
-
358
- // If a custom provider environment is configured (not SSR),
359
- // build it last so the manifest includes functions from all environments
360
- if (!ssrIsProvider) {
361
- const providerEnv = builder.environments[serverFnProviderEnv]
362
- if (!providerEnv) {
363
- throw new Error(
364
- `Provider environment "${serverFnProviderEnv}" not found`,
365
- )
366
- }
367
- if (!providerEnv.isBuilt) {
368
- // Build the provider environment last
369
- // This ensures all server functions are discovered from client/ssr builds
370
- await builder.build(providerEnv)
371
- }
372
- }
373
- },
374
- },
375
- }
376
- },
377
- },
378
- // Separate plugin for buildApp hook with enforce: 'post'
379
- // This ensures proper ordering with other plugins that also have
380
- // buildApp hooks with order: 'post'. The enforce: 'post' ensures this
381
- // runs after other plugins (like Nitro) complete their builds.
382
- {
383
- name: 'tanstack-start-core:post-build',
384
- enforce: 'post',
385
- buildApp: {
386
- order: 'post',
387
- async handler(builder) {
388
- const { startConfig } = getConfig()
389
- await postServerBuild({ builder, startConfig })
390
- },
391
- },
392
- },
393
- // Server function plugin handles:
394
- // 1. Identifying createServerFn().handler() calls
395
- // 2. Extracting server functions to separate modules
396
- // 3. Replacing call sites with RPC stubs
397
- // 4. Generating the server function manifest
398
- // Also handles createIsomorphicFn, createServerOnlyFn, createClientOnlyFn, createMiddleware
399
- startCompilerPlugin({
400
- framework: corePluginOpts.framework,
401
- environments,
402
- generateFunctionId: startPluginOpts?.serverFns?.generateFunctionId,
403
- providerEnvName: serverFnProviderEnv,
404
- }),
405
- importProtectionPlugin({
406
- getConfig,
407
- framework: corePluginOpts.framework,
408
- environments,
409
- providerEnvName: serverFnProviderEnv,
410
- }),
411
- tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),
412
- loadEnvPlugin(),
413
- startManifestPlugin({
414
- getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),
415
- getConfig,
416
- }),
417
- // When the vite base and router basepath are misaligned (e.g. base: '/_ui/', basepath: '/'),
418
- // install a middleware that rewrites incoming request URLs to prepend the vite base prefix.
419
- // This allows Vite's internal base middleware to accept the requests, then strips the prefix
420
- // before passing to the SSR handler.
421
- // Registered BEFORE devServerPlugin so this middleware is added to the Connect stack first,
422
- // ensuring all subsequent middlewares (CSS, SSR, etc.) see the rewritten URL.
423
- {
424
- name: 'tanstack-start-core:dev-base-rewrite',
425
- configureServer(server) {
426
- if (!needsDevBaseRewrite) {
427
- return
428
- }
429
- const basePrefix = resolvedStartConfig.viteAppBase.replace(/\/$/, '')
430
- server.middlewares.use((req, _res, next) => {
431
- if (req.url && !req.url.startsWith(basePrefix)) {
432
- req.url = basePrefix + req.url
433
- }
434
- next()
435
- })
436
- },
437
- },
438
- devServerPlugin({
439
- getConfig,
440
- devSsrStylesEnabled: startPluginOpts?.dev?.ssrStyles?.enabled ?? true,
441
- }),
442
- previewServerPlugin(),
443
- {
444
- name: 'tanstack-start:core:capture-bundle',
445
- applyToEnvironment(e) {
446
- return (
447
- e.name === VITE_ENVIRONMENT_NAMES.client ||
448
- e.name === VITE_ENVIRONMENT_NAMES.server
449
- )
450
- },
451
- enforce: 'post',
452
- generateBundle(_options, bundle) {
453
- const environment = this.environment.name as ViteEnvironmentNames
454
- if (!Object.values(VITE_ENVIRONMENT_NAMES).includes(environment)) {
455
- throw new Error(`Unknown environment: ${environment}`)
456
- }
457
- capturedBundle[environment] = bundle
458
- },
459
- },
460
- ]
461
- }
462
-
463
- function defineReplaceEnv<TKey extends string, TValue extends string>(
464
- key: TKey,
465
- value: TValue,
466
- ): { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue } {
467
- return {
468
- [`process.env.${key}`]: JSON.stringify(value),
469
- [`import.meta.env.${key}`]: JSON.stringify(value),
470
- } as { [P in `process.env.${TKey}` | `import.meta.env.${TKey}`]: TValue }
471
- }