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

vite.ts

84 lines2.8 KB

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',
18const defaultEntryPaths = {
19 client: path.resolve(defaultEntryDir, 'client.tsx'),
20 server: path.resolve(defaultEntryDir, 'server.ts'),
21 start: path.resolve(defaultEntryDir, 'start.ts'),
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 ],
59 include: [
60 'react',
61 'react/jsx-runtime',
62 'react/jsx-dev-runtime',
63 'react-dom',
64 ...(environmentName === VITE_ENVIRONMENT_NAMES.client
65 ? ['react-dom/client']
66 : ['react-dom/server']),
67 // `@tanstack/react-store` has a dependency on `use-sync-external-store`, which is CJS.
68 // It therefore needs to be included so that it is converted to ESM.
69 '@tanstack/react-router > @tanstack/react-store',
70 ],
71 }
72 : undefined,
73 }
74 },
75 },
76 TanStackStartVitePluginCore(
77 {
78 framework: 'react',
79 defaultEntryPaths,
80 },
81 options,
82 ),
83 ]