Open History to see the last commit
1import { normalizePath } from 'vite'
2import { DEV_CLIENT_ENTRY } from '../constants'
3import { createVirtualModule } from './createVirtualModule'
4import type {
5 CompileStartFrameworkOptions,
6 GetConfigFn,
7 ResolvedStartConfig,
8} from '../types'
9import type { PluginOption, ViteBuilder } from 'vite'
10β
11export function createDevClientEntryPlugin(opts: {
12 framework: CompileStartFrameworkOptions
13 getClientEntry: () => string
14}): PluginOption {
15 return createVirtualModule({
16 name: 'tanstack-start-core:dev-client-entry',
17 moduleId: DEV_CLIENT_ENTRY,
18 enforce: 'pre',
19 async load() {
20 const clientEntry = JSON.stringify(
21 normalizePath(opts.getClientEntry()).replaceAll('\\', '/'),
22 )
23β
24 if (shouldInjectReactRefreshPreamble(this.environment, opts.framework)) {
25 const reactRefresh = await this.resolve?.('/@react-refresh')
26β
27 if (!reactRefresh) {
28 throw new Error(
29 'TanStack Start React dev mode requires the React Refresh runtime, but /@react-refresh could not be resolved. Add @vitejs/plugin-react or another compatible React Refresh Vite plugin to your Vite config.',
30 )
31 }
32β
33 return (
34 getReactRefreshPreambleCode(reactRefresh.id) +
35 `\nawait import(${clientEntry})`
36 )
37 }
38β
39 return `import ${clientEntry}`
40 },
41 })
42}
43β
44function shouldInjectReactRefreshPreamble(
45 environment: ReactRefreshEnvironment | undefined,
46 framework: CompileStartFrameworkOptions,
47) {
48 const config = environment?.config
49β
50 return (
51 framework === 'react' &&
52 config?.command === 'serve' &&
53 config.consumer === 'client' &&
54 config.server?.hmr !== false
55 )
56}
57β
58interface ReactRefreshEnvironment {
59 config?: {
60 command?: string
61 consumer?: string
62 server?: {
63 hmr?: boolean
64 }
65 }
66}
67β
68function getReactRefreshPreambleCode(reactRefreshId: string) {
69 return `import { injectIntoGlobalHook } from ${JSON.stringify(reactRefreshId)};
70injectIntoGlobalHook(window);
71window.$RefreshReg$ = () => {};
72window.$RefreshSig$ = () => (type) => type;
73window.__vite_plugin_react_preamble_installed__ = true;`
74}
75β
76export function createPostBuildPlugin(opts: {
77 getConfig: GetConfigFn
78 postServerBuild: (opts: {
79 startConfig: ReturnType<GetConfigFn>['startConfig']
80 builder: ViteBuilder
81 }) => Promise<void>
82}): PluginOption {
83 return {
84 name: 'tanstack-start-core:post-build',
85 enforce: 'post',
86 buildApp: {
87 order: 'post',
88 async handler(builder) {
89 const { startConfig } = opts.getConfig()
90 await opts.postServerBuild({ builder, startConfig })
91 },
92 },
93 }
94}
95β
96export function createDevBaseRewritePlugin(opts: {
97 shouldRewriteDevBase: () => boolean
98 resolvedStartConfig: ResolvedStartConfig
99}): PluginOption {
100 return {
101 name: 'tanstack-start-core:dev-base-rewrite',
102 configureServer(server) {
103 if (!opts.shouldRewriteDevBase()) {
104 return
105 }
106β
107 const basePrefix = opts.resolvedStartConfig.basePaths.publicBase.replace(
108 /\/$/,
109 '',
110 )
111β
112 server.middlewares.use((req, _res, next) => {
113 if (req.url && !req.url.startsWith(basePrefix)) {
114 req.url = basePrefix + req.url
115 }
116β
117 next()
118 })
119 },
120 }
121}