@tanstack/start-plugin-core 1.166.11 → 1.166.13

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 (87) hide show
  1. package/dist/esm/build-sitemap.js +94 -123
  2. package/dist/esm/build-sitemap.js.map +1 -1
  3. package/dist/esm/constants.js +15 -20
  4. package/dist/esm/constants.js.map +1 -1
  5. package/dist/esm/dev-server-plugin/dev-styles.js +137 -150
  6. package/dist/esm/dev-server-plugin/dev-styles.js.map +1 -1
  7. package/dist/esm/dev-server-plugin/extract-html-scripts.js +16 -15
  8. package/dist/esm/dev-server-plugin/extract-html-scripts.js.map +1 -1
  9. package/dist/esm/dev-server-plugin/plugin.js +125 -195
  10. package/dist/esm/dev-server-plugin/plugin.js.map +1 -1
  11. package/dist/esm/import-protection-plugin/ast.js +6 -5
  12. package/dist/esm/import-protection-plugin/ast.js.map +1 -1
  13. package/dist/esm/import-protection-plugin/constants.js +20 -22
  14. package/dist/esm/import-protection-plugin/constants.js.map +1 -1
  15. package/dist/esm/import-protection-plugin/defaults.js +35 -25
  16. package/dist/esm/import-protection-plugin/defaults.js.map +1 -1
  17. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js +93 -92
  18. package/dist/esm/import-protection-plugin/extensionlessAbsoluteIdResolver.js.map +1 -1
  19. package/dist/esm/import-protection-plugin/matchers.js +23 -24
  20. package/dist/esm/import-protection-plugin/matchers.js.map +1 -1
  21. package/dist/esm/import-protection-plugin/plugin.js +1045 -1361
  22. package/dist/esm/import-protection-plugin/plugin.js.map +1 -1
  23. package/dist/esm/import-protection-plugin/postCompileUsage.js +58 -55
  24. package/dist/esm/import-protection-plugin/postCompileUsage.js.map +1 -1
  25. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js +187 -259
  26. package/dist/esm/import-protection-plugin/rewriteDeniedImports.js.map +1 -1
  27. package/dist/esm/import-protection-plugin/sourceLocation.js +238 -248
  28. package/dist/esm/import-protection-plugin/sourceLocation.js.map +1 -1
  29. package/dist/esm/import-protection-plugin/trace.js +173 -184
  30. package/dist/esm/import-protection-plugin/trace.js.map +1 -1
  31. package/dist/esm/import-protection-plugin/utils.js +132 -111
  32. package/dist/esm/import-protection-plugin/utils.js.map +1 -1
  33. package/dist/esm/import-protection-plugin/virtualModules.js +214 -194
  34. package/dist/esm/import-protection-plugin/virtualModules.js.map +1 -1
  35. package/dist/esm/index.js +2 -7
  36. package/dist/esm/load-env-plugin/plugin.js +12 -11
  37. package/dist/esm/load-env-plugin/plugin.js.map +1 -1
  38. package/dist/esm/output-directory.js +10 -10
  39. package/dist/esm/output-directory.js.map +1 -1
  40. package/dist/esm/plugin.js +275 -355
  41. package/dist/esm/plugin.js.map +1 -1
  42. package/dist/esm/post-server-build.js +39 -53
  43. package/dist/esm/post-server-build.js.map +1 -1
  44. package/dist/esm/prerender.js +177 -239
  45. package/dist/esm/prerender.js.map +1 -1
  46. package/dist/esm/preview-server-plugin/plugin.js +41 -44
  47. package/dist/esm/preview-server-plugin/plugin.js.map +1 -1
  48. package/dist/esm/queue.js +115 -126
  49. package/dist/esm/queue.js.map +1 -1
  50. package/dist/esm/resolve-entries.js +31 -32
  51. package/dist/esm/resolve-entries.js.map +1 -1
  52. package/dist/esm/schema.js +156 -179
  53. package/dist/esm/schema.js.map +1 -1
  54. package/dist/esm/start-compiler-plugin/compiler.js +655 -812
  55. package/dist/esm/start-compiler-plugin/compiler.js.map +1 -1
  56. package/dist/esm/start-compiler-plugin/handleClientOnlyJSX.js +25 -8
  57. package/dist/esm/start-compiler-plugin/handleClientOnlyJSX.js.map +1 -1
  58. package/dist/esm/start-compiler-plugin/handleCreateIsomorphicFn.js +22 -19
  59. package/dist/esm/start-compiler-plugin/handleCreateIsomorphicFn.js.map +1 -1
  60. package/dist/esm/start-compiler-plugin/handleCreateMiddleware.js +20 -22
  61. package/dist/esm/start-compiler-plugin/handleCreateMiddleware.js.map +1 -1
  62. package/dist/esm/start-compiler-plugin/handleCreateServerFn.js +187 -255
  63. package/dist/esm/start-compiler-plugin/handleCreateServerFn.js.map +1 -1
  64. package/dist/esm/start-compiler-plugin/handleEnvOnly.js +23 -33
  65. package/dist/esm/start-compiler-plugin/handleEnvOnly.js.map +1 -1
  66. package/dist/esm/start-compiler-plugin/plugin.js +247 -291
  67. package/dist/esm/start-compiler-plugin/plugin.js.map +1 -1
  68. package/dist/esm/start-compiler-plugin/utils.js +27 -27
  69. package/dist/esm/start-compiler-plugin/utils.js.map +1 -1
  70. package/dist/esm/start-manifest-plugin/manifestBuilder.js +272 -378
  71. package/dist/esm/start-manifest-plugin/manifestBuilder.js.map +1 -1
  72. package/dist/esm/start-manifest-plugin/plugin.js +35 -44
  73. package/dist/esm/start-manifest-plugin/plugin.js.map +1 -1
  74. package/dist/esm/start-router-plugin/constants.js +6 -5
  75. package/dist/esm/start-router-plugin/constants.js.map +1 -1
  76. package/dist/esm/start-router-plugin/generator-plugins/prerender-routes-plugin.js +24 -19
  77. package/dist/esm/start-router-plugin/generator-plugins/prerender-routes-plugin.js.map +1 -1
  78. package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js +28 -29
  79. package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js.map +1 -1
  80. package/dist/esm/start-router-plugin/plugin.js +146 -199
  81. package/dist/esm/start-router-plugin/plugin.js.map +1 -1
  82. package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.js +32 -31
  83. package/dist/esm/start-router-plugin/pruneServerOnlySubtrees.js.map +1 -1
  84. package/dist/esm/utils.js +14 -14
  85. package/dist/esm/utils.js.map +1 -1
  86. package/package.json +7 -7
  87. package/dist/esm/index.js.map +0 -1
@@ -1,371 +1,291 @@
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";
1
+ import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from "./constants.js";
6
2
  import { startManifestPlugin } from "./start-manifest-plugin/plugin.js";
7
- import { VITE_ENVIRONMENT_NAMES, ENTRY_POINTS } from "./constants.js";
8
3
  import { tanStackStartRouter } from "./start-router-plugin/plugin.js";
9
4
  import { loadEnvPlugin } from "./load-env-plugin/plugin.js";
10
5
  import { devServerPlugin } from "./dev-server-plugin/plugin.js";
6
+ import { getClientOutputDirectory, getServerOutputDirectory } from "./output-directory.js";
11
7
  import { previewServerPlugin } from "./preview-server-plugin/plugin.js";
12
8
  import { parseStartConfig } from "./schema.js";
13
9
  import { resolveEntry } from "./resolve-entries.js";
14
- import { getServerOutputDirectory, getClientOutputDirectory } from "./output-directory.js";
15
10
  import { postServerBuild } from "./post-server-build.js";
16
11
  import { startCompilerPlugin } from "./start-compiler-plugin/plugin.js";
17
12
  import { importProtectionPlugin } from "./import-protection-plugin/plugin.js";
13
+ import { joinPaths } from "@tanstack/router-core";
14
+ import * as vite from "vite";
15
+ import { crawlFrameworkPkgs } from "vitefu";
16
+ import { join } from "pathe";
17
+ import { escapePath } from "tinyglobby";
18
+ //#region src/plugin.ts
18
19
  function isFullUrl(str) {
19
- try {
20
- new URL(str);
21
- return true;
22
- } catch {
23
- return false;
24
- }
20
+ try {
21
+ new URL(str);
22
+ return true;
23
+ } catch {
24
+ return false;
25
+ }
25
26
  }
26
27
  function TanStackStartVitePluginCore(corePluginOpts, startPluginOpts) {
27
- const serverFnProviderEnv = corePluginOpts.serverFn?.providerEnv || VITE_ENVIRONMENT_NAMES.server;
28
- const ssrIsProvider = serverFnProviderEnv === VITE_ENVIRONMENT_NAMES.server;
29
- const resolvedStartConfig = {
30
- root: "",
31
- startFilePath: void 0,
32
- routerFilePath: "",
33
- srcDirectory: "",
34
- viteAppBase: "",
35
- serverFnProviderEnv
36
- };
37
- let startConfig;
38
- const getConfig = () => {
39
- if (!resolvedStartConfig.root) {
40
- throw new Error(`Cannot get config before root is resolved`);
41
- }
42
- if (!startConfig) {
43
- startConfig = parseStartConfig(
44
- startPluginOpts,
45
- corePluginOpts,
46
- resolvedStartConfig.root
47
- );
48
- }
49
- return { startConfig, resolvedStartConfig, corePluginOpts };
50
- };
51
- let needsDevBaseRewrite = false;
52
- const capturedBundle = {};
53
- function getBundle(envName) {
54
- const bundle = capturedBundle[envName];
55
- if (!bundle) {
56
- throw new Error(`No bundle captured for environment: ${envName}`);
57
- }
58
- return bundle;
59
- }
60
- const environments = [
61
- { name: VITE_ENVIRONMENT_NAMES.client, type: "client" },
62
- { name: VITE_ENVIRONMENT_NAMES.server, type: "server" }
63
- ];
64
- if (corePluginOpts.serverFn?.providerEnv && !environments.find((e) => e.name === corePluginOpts.serverFn?.providerEnv)) {
65
- environments.push({
66
- name: corePluginOpts.serverFn.providerEnv,
67
- type: "server"
68
- });
69
- }
70
- return [
71
- {
72
- name: "tanstack-start-core:config",
73
- enforce: "pre",
74
- async config(viteConfig, { command }) {
75
- resolvedStartConfig.viteAppBase = viteConfig.base ?? "/";
76
- if (!isFullUrl(resolvedStartConfig.viteAppBase)) {
77
- resolvedStartConfig.viteAppBase = joinPaths([
78
- "/",
79
- viteConfig.base,
80
- "/"
81
- ]);
82
- }
83
- const root = viteConfig.root || process.cwd();
84
- resolvedStartConfig.root = root;
85
- const { startConfig: startConfig2 } = getConfig();
86
- if (startConfig2.router.basepath === void 0) {
87
- if (!isFullUrl(resolvedStartConfig.viteAppBase)) {
88
- startConfig2.router.basepath = resolvedStartConfig.viteAppBase.replace(/^\/|\/$/g, "");
89
- } else {
90
- startConfig2.router.basepath = "/";
91
- }
92
- } else {
93
- if (command === "serve" && !viteConfig.server?.middlewareMode) {
94
- if (!joinPaths(["/", startConfig2.router.basepath, "/"]).startsWith(
95
- joinPaths(["/", resolvedStartConfig.viteAppBase, "/"])
96
- )) {
97
- needsDevBaseRewrite = true;
98
- }
99
- }
100
- }
101
- const TSS_SERVER_FN_BASE = joinPaths([
102
- "/",
103
- startConfig2.router.basepath,
104
- startConfig2.serverFns.base,
105
- "/"
106
- ]);
107
- const resolvedSrcDirectory = join(root, startConfig2.srcDirectory);
108
- resolvedStartConfig.srcDirectory = resolvedSrcDirectory;
109
- const startFilePath = resolveEntry({
110
- type: "start entry",
111
- configuredEntry: startConfig2.start.entry,
112
- defaultEntry: "start",
113
- resolvedSrcDirectory,
114
- required: false
115
- });
116
- resolvedStartConfig.startFilePath = startFilePath;
117
- const routerFilePath = resolveEntry({
118
- type: "router entry",
119
- configuredEntry: startConfig2.router.entry,
120
- defaultEntry: "router",
121
- resolvedSrcDirectory,
122
- required: true
123
- });
124
- resolvedStartConfig.routerFilePath = routerFilePath;
125
- const clientEntryPath = resolveEntry({
126
- type: "client entry",
127
- configuredEntry: startConfig2.client.entry,
128
- defaultEntry: "client",
129
- resolvedSrcDirectory,
130
- required: false
131
- });
132
- const serverEntryPath = resolveEntry({
133
- type: "server entry",
134
- configuredEntry: startConfig2.server.entry,
135
- defaultEntry: "server",
136
- resolvedSrcDirectory,
137
- required: false
138
- });
139
- const clientAlias = vite.normalizePath(
140
- clientEntryPath ?? corePluginOpts.defaultEntryPaths.client
141
- );
142
- const serverAlias = vite.normalizePath(
143
- serverEntryPath ?? corePluginOpts.defaultEntryPaths.server
144
- );
145
- const startAlias = vite.normalizePath(
146
- startFilePath ?? corePluginOpts.defaultEntryPaths.start
147
- );
148
- const routerAlias = vite.normalizePath(routerFilePath);
149
- const entryAliasConfiguration = {
150
- [ENTRY_POINTS.client]: clientAlias,
151
- [ENTRY_POINTS.server]: serverAlias,
152
- [ENTRY_POINTS.start]: startAlias,
153
- [ENTRY_POINTS.router]: routerAlias
154
- };
155
- const startPackageName = `@tanstack/${corePluginOpts.framework}-start`;
156
- const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({
157
- root: process.cwd(),
158
- isBuild: command === "build",
159
- isFrameworkPkgByJson(pkgJson) {
160
- const peerDependencies = pkgJson["peerDependencies"];
161
- if (peerDependencies) {
162
- if (startPackageName in peerDependencies || "@tanstack/start-client-core" in peerDependencies) {
163
- return true;
164
- }
165
- }
166
- return false;
167
- }
168
- });
169
- return {
170
- // see https://vite.dev/config/shared-options.html#apptype
171
- // this will prevent vite from injecting middlewares that we don't want
172
- appType: viteConfig.appType ?? "custom",
173
- environments: {
174
- [VITE_ENVIRONMENT_NAMES.client]: {
175
- consumer: "client",
176
- build: {
177
- rollupOptions: {
178
- input: {
179
- main: ENTRY_POINTS.client
180
- }
181
- },
182
- outDir: getClientOutputDirectory(viteConfig)
183
- },
184
- optimizeDeps: {
185
- exclude: crawlFrameworkPkgsResult.optimizeDeps.exclude,
186
- // Ensure user code can be crawled for dependencies
187
- entries: [clientAlias, routerAlias].map(
188
- (entry) => (
189
- // Entries are treated as `tinyglobby` patterns so need to be escaped
190
- escapePath(entry)
191
- )
192
- )
193
- }
194
- },
195
- [VITE_ENVIRONMENT_NAMES.server]: {
196
- consumer: "server",
197
- build: {
198
- ssr: true,
199
- rollupOptions: {
200
- input: viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]?.build?.rollupOptions?.input ?? serverAlias
201
- },
202
- outDir: getServerOutputDirectory(viteConfig),
203
- commonjsOptions: {
204
- include: [/node_modules/]
205
- },
206
- copyPublicDir: viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]?.build?.copyPublicDir ?? false
207
- },
208
- optimizeDeps: {
209
- // Ensure user code can be crawled for dependencies
210
- entries: [serverAlias, startAlias, routerAlias].map(
211
- (entry) => (
212
- // Entries are treated as `tinyglobby` patterns so need to be escaped
213
- escapePath(entry)
214
- )
215
- )
216
- }
217
- }
218
- },
219
- resolve: {
220
- noExternal: [
221
- // ENTRY_POINTS.start,
222
- "@tanstack/start**",
223
- `@tanstack/${corePluginOpts.framework}-start**`,
224
- ...crawlFrameworkPkgsResult.ssr.noExternal.sort()
225
- ],
226
- alias: {
227
- ...entryAliasConfiguration
228
- }
229
- },
230
- /* prettier-ignore */
231
- define: {
232
- // define is an esbuild function that replaces the any instances of given keys with the given values
233
- // i.e: __FRAMEWORK_NAME__ can be replaced with JSON.stringify("TanStack Start")
234
- // This is not the same as injecting environment variables.
235
- ...defineReplaceEnv("TSS_SERVER_FN_BASE", TSS_SERVER_FN_BASE),
236
- ...defineReplaceEnv("TSS_CLIENT_OUTPUT_DIR", getClientOutputDirectory(viteConfig)),
237
- ...defineReplaceEnv("TSS_ROUTER_BASEPATH", startConfig2.router.basepath),
238
- ...command === "serve" ? defineReplaceEnv("TSS_SHELL", startConfig2.spa?.enabled ? "true" : "false") : {},
239
- ...defineReplaceEnv("TSS_DEV_SERVER", command === "serve" ? "true" : "false"),
240
- // Dev SSR styles: enabled flag and basepath (defaults to vite base for asset URL alignment)
241
- ...defineReplaceEnv("TSS_DEV_SSR_STYLES_ENABLED", startConfig2.dev.ssrStyles.enabled ? "true" : "false"),
242
- ...defineReplaceEnv("TSS_DEV_SSR_STYLES_BASEPATH", startConfig2.dev.ssrStyles.basepath ?? resolvedStartConfig.viteAppBase),
243
- // Replace NODE_ENV during build (unless opted out) for dead code elimination in server bundles
244
- ...command === "build" && startConfig2.server.build.staticNodeEnv ? {
245
- "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || viteConfig.mode || "production")
246
- } : {}
247
- },
248
- builder: {
249
- sharedPlugins: true,
250
- async buildApp(builder) {
251
- const client = builder.environments[VITE_ENVIRONMENT_NAMES.client];
252
- const server = builder.environments[VITE_ENVIRONMENT_NAMES.server];
253
- if (!client) {
254
- throw new Error("Client environment not found");
255
- }
256
- if (!server) {
257
- throw new Error("SSR environment not found");
258
- }
259
- if (!client.isBuilt) {
260
- await builder.build(client);
261
- }
262
- if (!server.isBuilt) {
263
- await builder.build(server);
264
- }
265
- if (!ssrIsProvider) {
266
- const providerEnv = builder.environments[serverFnProviderEnv];
267
- if (!providerEnv) {
268
- throw new Error(
269
- `Provider environment "${serverFnProviderEnv}" not found`
270
- );
271
- }
272
- if (!providerEnv.isBuilt) {
273
- await builder.build(providerEnv);
274
- }
275
- }
276
- }
277
- }
278
- };
279
- }
280
- },
281
- // Separate plugin for buildApp hook with enforce: 'post'
282
- // This ensures proper ordering with other plugins that also have
283
- // buildApp hooks with order: 'post'. The enforce: 'post' ensures this
284
- // runs after other plugins (like Nitro) complete their builds.
285
- {
286
- name: "tanstack-start-core:post-build",
287
- enforce: "post",
288
- buildApp: {
289
- order: "post",
290
- async handler(builder) {
291
- const { startConfig: startConfig2 } = getConfig();
292
- await postServerBuild({ builder, startConfig: startConfig2 });
293
- }
294
- }
295
- },
296
- // Server function plugin handles:
297
- // 1. Identifying createServerFn().handler() calls
298
- // 2. Extracting server functions to separate modules
299
- // 3. Replacing call sites with RPC stubs
300
- // 4. Generating the server function manifest
301
- // Also handles createIsomorphicFn, createServerOnlyFn, createClientOnlyFn, createMiddleware
302
- startCompilerPlugin({
303
- framework: corePluginOpts.framework,
304
- environments,
305
- generateFunctionId: startPluginOpts?.serverFns?.generateFunctionId,
306
- providerEnvName: serverFnProviderEnv
307
- }),
308
- importProtectionPlugin({
309
- getConfig,
310
- framework: corePluginOpts.framework,
311
- environments,
312
- providerEnvName: serverFnProviderEnv
313
- }),
314
- tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),
315
- loadEnvPlugin(),
316
- startManifestPlugin({
317
- getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),
318
- getConfig
319
- }),
320
- // When the vite base and router basepath are misaligned (e.g. base: '/_ui/', basepath: '/'),
321
- // install a middleware that rewrites incoming request URLs to prepend the vite base prefix.
322
- // This allows Vite's internal base middleware to accept the requests, then strips the prefix
323
- // before passing to the SSR handler.
324
- // Registered BEFORE devServerPlugin so this middleware is added to the Connect stack first,
325
- // ensuring all subsequent middlewares (CSS, SSR, etc.) see the rewritten URL.
326
- {
327
- name: "tanstack-start-core:dev-base-rewrite",
328
- configureServer(server) {
329
- if (!needsDevBaseRewrite) {
330
- return;
331
- }
332
- const basePrefix = resolvedStartConfig.viteAppBase.replace(/\/$/, "");
333
- server.middlewares.use((req, _res, next) => {
334
- if (req.url && !req.url.startsWith(basePrefix)) {
335
- req.url = basePrefix + req.url;
336
- }
337
- next();
338
- });
339
- }
340
- },
341
- devServerPlugin({
342
- getConfig,
343
- devSsrStylesEnabled: startPluginOpts?.dev?.ssrStyles?.enabled ?? true
344
- }),
345
- previewServerPlugin(),
346
- {
347
- name: "tanstack-start:core:capture-bundle",
348
- applyToEnvironment(e) {
349
- return e.name === VITE_ENVIRONMENT_NAMES.client || e.name === VITE_ENVIRONMENT_NAMES.server;
350
- },
351
- enforce: "post",
352
- generateBundle(_options, bundle) {
353
- const environment = this.environment.name;
354
- if (!Object.values(VITE_ENVIRONMENT_NAMES).includes(environment)) {
355
- throw new Error(`Unknown environment: ${environment}`);
356
- }
357
- capturedBundle[environment] = bundle;
358
- }
359
- }
360
- ];
28
+ const serverFnProviderEnv = corePluginOpts.serverFn?.providerEnv || VITE_ENVIRONMENT_NAMES.server;
29
+ const ssrIsProvider = serverFnProviderEnv === VITE_ENVIRONMENT_NAMES.server;
30
+ const resolvedStartConfig = {
31
+ root: "",
32
+ startFilePath: void 0,
33
+ routerFilePath: "",
34
+ srcDirectory: "",
35
+ viteAppBase: "",
36
+ serverFnProviderEnv
37
+ };
38
+ let startConfig;
39
+ const getConfig = () => {
40
+ if (!resolvedStartConfig.root) throw new Error(`Cannot get config before root is resolved`);
41
+ if (!startConfig) startConfig = parseStartConfig(startPluginOpts, corePluginOpts, resolvedStartConfig.root);
42
+ return {
43
+ startConfig,
44
+ resolvedStartConfig,
45
+ corePluginOpts
46
+ };
47
+ };
48
+ let needsDevBaseRewrite = false;
49
+ const capturedBundle = {};
50
+ function getBundle(envName) {
51
+ const bundle = capturedBundle[envName];
52
+ if (!bundle) throw new Error(`No bundle captured for environment: ${envName}`);
53
+ return bundle;
54
+ }
55
+ const environments = [{
56
+ name: VITE_ENVIRONMENT_NAMES.client,
57
+ type: "client"
58
+ }, {
59
+ name: VITE_ENVIRONMENT_NAMES.server,
60
+ type: "server"
61
+ }];
62
+ if (corePluginOpts.serverFn?.providerEnv && !environments.find((e) => e.name === corePluginOpts.serverFn?.providerEnv)) environments.push({
63
+ name: corePluginOpts.serverFn.providerEnv,
64
+ type: "server"
65
+ });
66
+ return [
67
+ {
68
+ name: "tanstack-start-core:config",
69
+ enforce: "pre",
70
+ async config(viteConfig, { command }) {
71
+ resolvedStartConfig.viteAppBase = viteConfig.base ?? "/";
72
+ if (!isFullUrl(resolvedStartConfig.viteAppBase)) resolvedStartConfig.viteAppBase = joinPaths([
73
+ "/",
74
+ viteConfig.base,
75
+ "/"
76
+ ]);
77
+ const root = viteConfig.root || process.cwd();
78
+ resolvedStartConfig.root = root;
79
+ const { startConfig } = getConfig();
80
+ if (startConfig.router.basepath === void 0) if (!isFullUrl(resolvedStartConfig.viteAppBase)) startConfig.router.basepath = resolvedStartConfig.viteAppBase.replace(/^\/|\/$/g, "");
81
+ else startConfig.router.basepath = "/";
82
+ else if (command === "serve" && !viteConfig.server?.middlewareMode) {
83
+ if (!joinPaths([
84
+ "/",
85
+ startConfig.router.basepath,
86
+ "/"
87
+ ]).startsWith(joinPaths([
88
+ "/",
89
+ resolvedStartConfig.viteAppBase,
90
+ "/"
91
+ ]))) needsDevBaseRewrite = true;
92
+ }
93
+ const TSS_SERVER_FN_BASE = joinPaths([
94
+ "/",
95
+ startConfig.router.basepath,
96
+ startConfig.serverFns.base,
97
+ "/"
98
+ ]);
99
+ const resolvedSrcDirectory = join(root, startConfig.srcDirectory);
100
+ resolvedStartConfig.srcDirectory = resolvedSrcDirectory;
101
+ const startFilePath = resolveEntry({
102
+ type: "start entry",
103
+ configuredEntry: startConfig.start.entry,
104
+ defaultEntry: "start",
105
+ resolvedSrcDirectory,
106
+ required: false
107
+ });
108
+ resolvedStartConfig.startFilePath = startFilePath;
109
+ const routerFilePath = resolveEntry({
110
+ type: "router entry",
111
+ configuredEntry: startConfig.router.entry,
112
+ defaultEntry: "router",
113
+ resolvedSrcDirectory,
114
+ required: true
115
+ });
116
+ resolvedStartConfig.routerFilePath = routerFilePath;
117
+ const clientEntryPath = resolveEntry({
118
+ type: "client entry",
119
+ configuredEntry: startConfig.client.entry,
120
+ defaultEntry: "client",
121
+ resolvedSrcDirectory,
122
+ required: false
123
+ });
124
+ const serverEntryPath = resolveEntry({
125
+ type: "server entry",
126
+ configuredEntry: startConfig.server.entry,
127
+ defaultEntry: "server",
128
+ resolvedSrcDirectory,
129
+ required: false
130
+ });
131
+ const clientAlias = vite.normalizePath(clientEntryPath ?? corePluginOpts.defaultEntryPaths.client);
132
+ const serverAlias = vite.normalizePath(serverEntryPath ?? corePluginOpts.defaultEntryPaths.server);
133
+ const startAlias = vite.normalizePath(startFilePath ?? corePluginOpts.defaultEntryPaths.start);
134
+ const routerAlias = vite.normalizePath(routerFilePath);
135
+ const entryAliasConfiguration = {
136
+ [ENTRY_POINTS.client]: clientAlias,
137
+ [ENTRY_POINTS.server]: serverAlias,
138
+ [ENTRY_POINTS.start]: startAlias,
139
+ [ENTRY_POINTS.router]: routerAlias
140
+ };
141
+ const startPackageName = `@tanstack/${corePluginOpts.framework}-start`;
142
+ const crawlFrameworkPkgsResult = await crawlFrameworkPkgs({
143
+ root: process.cwd(),
144
+ isBuild: command === "build",
145
+ isFrameworkPkgByJson(pkgJson) {
146
+ const peerDependencies = pkgJson["peerDependencies"];
147
+ if (peerDependencies) {
148
+ if (startPackageName in peerDependencies || "@tanstack/start-client-core" in peerDependencies) return true;
149
+ }
150
+ return false;
151
+ }
152
+ });
153
+ return {
154
+ appType: viteConfig.appType ?? "custom",
155
+ environments: {
156
+ [VITE_ENVIRONMENT_NAMES.client]: {
157
+ consumer: "client",
158
+ build: {
159
+ rollupOptions: { input: { main: ENTRY_POINTS.client } },
160
+ outDir: getClientOutputDirectory(viteConfig)
161
+ },
162
+ optimizeDeps: {
163
+ exclude: crawlFrameworkPkgsResult.optimizeDeps.exclude,
164
+ entries: [clientAlias, routerAlias].map((entry) => escapePath(entry))
165
+ }
166
+ },
167
+ [VITE_ENVIRONMENT_NAMES.server]: {
168
+ consumer: "server",
169
+ build: {
170
+ ssr: true,
171
+ rollupOptions: { input: viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]?.build?.rollupOptions?.input ?? serverAlias },
172
+ outDir: getServerOutputDirectory(viteConfig),
173
+ commonjsOptions: { include: [/node_modules/] },
174
+ copyPublicDir: viteConfig.environments?.[VITE_ENVIRONMENT_NAMES.server]?.build?.copyPublicDir ?? false
175
+ },
176
+ optimizeDeps: { entries: [
177
+ serverAlias,
178
+ startAlias,
179
+ routerAlias
180
+ ].map((entry) => escapePath(entry)) }
181
+ }
182
+ },
183
+ resolve: {
184
+ noExternal: [
185
+ "@tanstack/start**",
186
+ `@tanstack/${corePluginOpts.framework}-start**`,
187
+ ...crawlFrameworkPkgsResult.ssr.noExternal.sort()
188
+ ],
189
+ alias: { ...entryAliasConfiguration }
190
+ },
191
+ define: {
192
+ ...defineReplaceEnv("TSS_SERVER_FN_BASE", TSS_SERVER_FN_BASE),
193
+ ...defineReplaceEnv("TSS_CLIENT_OUTPUT_DIR", getClientOutputDirectory(viteConfig)),
194
+ ...defineReplaceEnv("TSS_ROUTER_BASEPATH", startConfig.router.basepath),
195
+ ...command === "serve" ? defineReplaceEnv("TSS_SHELL", startConfig.spa?.enabled ? "true" : "false") : {},
196
+ ...defineReplaceEnv("TSS_DEV_SERVER", command === "serve" ? "true" : "false"),
197
+ ...defineReplaceEnv("TSS_DEV_SSR_STYLES_ENABLED", startConfig.dev.ssrStyles.enabled ? "true" : "false"),
198
+ ...defineReplaceEnv("TSS_DEV_SSR_STYLES_BASEPATH", startConfig.dev.ssrStyles.basepath ?? resolvedStartConfig.viteAppBase),
199
+ ...command === "build" && startConfig.server.build.staticNodeEnv ? { "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || viteConfig.mode || "production") } : {}
200
+ },
201
+ builder: {
202
+ sharedPlugins: true,
203
+ async buildApp(builder) {
204
+ const client = builder.environments[VITE_ENVIRONMENT_NAMES.client];
205
+ const server = builder.environments[VITE_ENVIRONMENT_NAMES.server];
206
+ if (!client) throw new Error("Client environment not found");
207
+ if (!server) throw new Error("SSR environment not found");
208
+ if (!client.isBuilt) await builder.build(client);
209
+ if (!server.isBuilt) await builder.build(server);
210
+ if (!ssrIsProvider) {
211
+ const providerEnv = builder.environments[serverFnProviderEnv];
212
+ if (!providerEnv) throw new Error(`Provider environment "${serverFnProviderEnv}" not found`);
213
+ if (!providerEnv.isBuilt) await builder.build(providerEnv);
214
+ }
215
+ }
216
+ }
217
+ };
218
+ }
219
+ },
220
+ {
221
+ name: "tanstack-start-core:post-build",
222
+ enforce: "post",
223
+ buildApp: {
224
+ order: "post",
225
+ async handler(builder) {
226
+ const { startConfig } = getConfig();
227
+ await postServerBuild({
228
+ builder,
229
+ startConfig
230
+ });
231
+ }
232
+ }
233
+ },
234
+ startCompilerPlugin({
235
+ framework: corePluginOpts.framework,
236
+ environments,
237
+ generateFunctionId: startPluginOpts?.serverFns?.generateFunctionId,
238
+ providerEnvName: serverFnProviderEnv
239
+ }),
240
+ importProtectionPlugin({
241
+ getConfig,
242
+ framework: corePluginOpts.framework,
243
+ environments,
244
+ providerEnvName: serverFnProviderEnv
245
+ }),
246
+ tanStackStartRouter(startPluginOpts, getConfig, corePluginOpts),
247
+ loadEnvPlugin(),
248
+ startManifestPlugin({
249
+ getClientBundle: () => getBundle(VITE_ENVIRONMENT_NAMES.client),
250
+ getConfig
251
+ }),
252
+ {
253
+ name: "tanstack-start-core:dev-base-rewrite",
254
+ configureServer(server) {
255
+ if (!needsDevBaseRewrite) return;
256
+ const basePrefix = resolvedStartConfig.viteAppBase.replace(/\/$/, "");
257
+ server.middlewares.use((req, _res, next) => {
258
+ if (req.url && !req.url.startsWith(basePrefix)) req.url = basePrefix + req.url;
259
+ next();
260
+ });
261
+ }
262
+ },
263
+ devServerPlugin({
264
+ getConfig,
265
+ devSsrStylesEnabled: startPluginOpts?.dev?.ssrStyles?.enabled ?? true
266
+ }),
267
+ previewServerPlugin(),
268
+ {
269
+ name: "tanstack-start:core:capture-bundle",
270
+ applyToEnvironment(e) {
271
+ return e.name === VITE_ENVIRONMENT_NAMES.client || e.name === VITE_ENVIRONMENT_NAMES.server;
272
+ },
273
+ enforce: "post",
274
+ generateBundle(_options, bundle) {
275
+ const environment = this.environment.name;
276
+ if (!Object.values(VITE_ENVIRONMENT_NAMES).includes(environment)) throw new Error(`Unknown environment: ${environment}`);
277
+ capturedBundle[environment] = bundle;
278
+ }
279
+ }
280
+ ];
361
281
  }
362
282
  function defineReplaceEnv(key, value) {
363
- return {
364
- [`process.env.${key}`]: JSON.stringify(value),
365
- [`import.meta.env.${key}`]: JSON.stringify(value)
366
- };
283
+ return {
284
+ [`process.env.${key}`]: JSON.stringify(value),
285
+ [`import.meta.env.${key}`]: JSON.stringify(value)
286
+ };
367
287
  }
368
- export {
369
- TanStackStartVitePluginCore
370
- };
371
- //# sourceMappingURL=plugin.js.map
288
+ //#endregion
289
+ export { TanStackStartVitePluginCore };
290
+
291
+ //# sourceMappingURL=plugin.js.map