Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

vite.ts

123 lines4.4 KB

1import {
2 START_ENVIRONMENT_NAMES,
3 tanStackStartVite,
4} from '@tanstack/start-plugin-core/vite'
5import type {
6 TanStackStartViteInputConfig,
7 TanStackStartVitePluginCoreOptions,
8} from '@tanstack/start-plugin-core/vite'
9import {
10 configureRsc,
11 reactStartRscVitePlugin,
12} from '@tanstack/react-start-rsc/plugin/vite'
13import path from 'pathe'
14import { reactStartDefaultEntryPaths, reactStartPluginDir } from './shared'
15import type { PluginOption } from 'vite'
16​
17const isInsideRouterMonoRepo =
18 path.basename(path.resolve(reactStartPluginDir, '../../../../')) ===
19 'packages'
20​
21export function tanstackStart(
22 options?: TanStackStartViteInputConfig & { rsc?: { enabled?: boolean } },
23): Array<PluginOption> {
24 const rscEnabled = options?.rsc?.enabled ?? false
25 const rscConfig = rscEnabled ? configureRsc() : undefined
26 let corePluginOpts: TanStackStartVitePluginCoreOptions = {
27 framework: 'react',
28 defaultEntryPaths: reactStartDefaultEntryPaths,
29 providerEnvironmentName: START_ENVIRONMENT_NAMES.server,
30 ssrIsProvider: true,
31 ssrResolverStrategy: {
32 type: 'default',
33 },
34 }
35​
36 const serverEnvironments: Array<string> = [
37 START_ENVIRONMENT_NAMES.server,
38 ...(rscConfig ? [rscConfig.envName] : []),
39 ]
40​
41 if (rscConfig) {
42 corePluginOpts = {
43 ...corePluginOpts,
44 providerEnvironmentName: rscConfig.providerEnvironmentName,
45 ssrIsProvider: false,
46 ssrResolverStrategy: rscConfig.ssrResolverStrategy,
47 serializationAdapters: rscConfig.serializationAdapters,
48 compilerTransforms: rscConfig.compilerTransforms,
49 }
50 }
51 return [
52 {
53 name: 'tanstack-react-start:config',
54 configEnvironment(environmentName, options) {
55 const needsOptimizeDeps =
56 environmentName === START_ENVIRONMENT_NAMES.client ||
57 (serverEnvironments.includes(environmentName) &&
58 // This indicates that the server environment has opted in to dependency optimization
59 options.optimizeDeps?.noDiscovery === false)
60​
61 // Check if @tanstack/react-router is in noExternal (as array)
62 const reactRouterInNoExternal =
63 Array.isArray(options.resolve?.noExternal) &&
64 options.resolve.noExternal.some(
65 (pattern) =>
66 pattern === '@tanstack/react-router' ||
67 (typeof pattern === 'string' && pattern.includes('react-router')),
68 )
69​
70 return {
71 resolve: {
72 dedupe: [
73 'react',
74 'react-dom',
75 '@tanstack/react-start',
76 '@tanstack/react-router',
77 ],
78 // Don't mark react-router as external if:
79 // 1. noExternal is true (bundle everything)
80 // 2. We're not in the router monorepo
81 // 3. react-router is explicitly in noExternal for this environment
82 external:
83 options.resolve?.noExternal === true ||
84 !isInsideRouterMonoRepo ||
85 reactRouterInNoExternal
86 ? undefined
87 : ['@tanstack/react-router', '@tanstack/react-router-devtools'],
88 },
89 optimizeDeps: needsOptimizeDeps
90 ? {
91 // As `@tanstack/react-start` depends on `@tanstack/react-router`, we should exclude both.
92 exclude: [
93 '@tanstack/react-start',
94 '@tanstack/react-router',
95 '@tanstack/react-router-devtools',
96 '@tanstack/start-static-server-functions',
97 ],
98 include: [
99 'react',
100 'react/jsx-runtime',
101 'react/jsx-dev-runtime',
102 'react-dom',
103 ...(environmentName === START_ENVIRONMENT_NAMES.client
104 ? ['react-dom/client']
105 : ['react-dom/server']),
106 // `@tanstack/react-store` has a dependency on `use-sync-external-store`, which is CJS.
107 // It therefore needs to be included so that it is converted to ESM.
108 '@tanstack/react-router > @tanstack/react-store',
109 ...(options.optimizeDeps?.exclude?.find(
110 (x) => x === '@tanstack/react-form',
111 )
112 ? ['@tanstack/react-form > @tanstack/react-store']
113 : []),
114 ],
115 }
116 : undefined,
117 }
118 },
119 },
120 rscConfig ? reactStartRscVitePlugin() : null,
121 tanStackStartVite(corePluginOpts, options),
122 ]