Open History to see the last commit
1import { fileURLToPath } from 'node:url'
2import {
3 TanStackStartVitePluginCore,
4 VITE_ENVIRONMENT_NAMES,
5} from '@tanstack/start-plugin-core'
6import path from 'pathe'
7import type { TanStackStartInputConfig } from '@tanstack/start-plugin-core'
8import type { PluginOption } from 'vite'
9β
10const currentDir = path.dirname(fileURLToPath(import.meta.url))
11const defaultEntryDir = path.resolve(
12 currentDir,
13 '..',
14 '..',
15 'plugin',
16 'default-entry',
17)
18const defaultEntryPaths = {
19 client: path.resolve(defaultEntryDir, 'client.tsx'),
20 server: path.resolve(defaultEntryDir, 'server.ts'),
21 start: path.resolve(defaultEntryDir, 'start.ts'),
22}
23β
24const isInsideRouterMonoRepo =
25 path.basename(path.resolve(currentDir, '../../../../')) === 'packages'
26β
27export function tanstackStart(
28 options?: TanStackStartInputConfig,
29): Array<PluginOption> {
30 return [
31 {
32 name: 'tanstack-react-start:config',
33 configEnvironment(environmentName, options) {
34 return {
35 resolve: {
36 dedupe: [
37 'react',
38 'react-dom',
39 '@tanstack/react-start',
40 '@tanstack/react-router',
41 ],
42 external:
43 options.resolve?.noExternal === true || !isInsideRouterMonoRepo
44 ? undefined
45 : ['@tanstack/react-router', '@tanstack/react-router-devtools'],
46 },
47 optimizeDeps:
48 environmentName === VITE_ENVIRONMENT_NAMES.client ||
49 (environmentName === VITE_ENVIRONMENT_NAMES.server &&
50 // This indicates that the server environment has opted in to dependency optimization
51 options.optimizeDeps?.noDiscovery === false)
52 ? {
53 // As `@tanstack/react-start` depends on `@tanstack/react-router`, we should exclude both.
54 exclude: [
55 '@tanstack/react-start',
56 '@tanstack/react-router',
57 '@tanstack/react-router-devtools',
58 '@tanstack/start-static-server-functions',
59 ],
60 include: [
61 'react',
62 'react/jsx-runtime',
63 'react/jsx-dev-runtime',
64 'react-dom',
65 ...(environmentName === VITE_ENVIRONMENT_NAMES.client
66 ? ['react-dom/client']
67 : ['react-dom/server']),
68 // `@tanstack/react-store` has a dependency on `use-sync-external-store`, which is CJS.
69 // It therefore needs to be included so that it is converted to ESM.
70 '@tanstack/react-router > @tanstack/react-store',
71 ...(options.optimizeDeps?.exclude?.find(
72 (x) => x === '@tanstack/react-form',
73 )
74 ? ['@tanstack/react-form > @tanstack/react-store']
75 : []),
76 ],
77 }
78 : undefined,
79 }
80 },
81 },
82 TanStackStartVitePluginCore(
83 {
84 framework: 'react',
85 defaultEntryPaths,
86 },
87 options,
88 ),
89 ]
90}