Open History to see the last commit
1import fs from 'node:fs'
2import path from 'node:path'
3import { tanstackStart } from '@tanstack/react-start/plugin/vite'
4import { defineConfig } from 'vite'
5import tsConfigPaths from 'vite-tsconfig-paths'
6import viteReact from '@vitejs/plugin-react'
7import tailwindcss from '@tailwindcss/vite'
8import chokidar from 'chokidar'
9
10function watchWorkspacePackages() {
11 return {
12 name: `watch-workspace-packages`,
13 configureServer(server: any) {
14 const watchPaths = [
15 path.resolve(__dirname, `../../../packages/db/dist`),
16 path.resolve(__dirname, `../../../packages/offline-transactions/dist`),
17 path.resolve(
18 __dirname,
19 `../../../packages/browser-db-sqlite-persistence/dist`,
20 ),
21 path.resolve(
22 __dirname,
23 `../../../packages/db-sqlite-persistence-core/dist`,
24 ),
25 ]
26
27 console.log(`[watch-workspace] Starting to watch paths:`)
28 watchPaths.forEach((p) => console.log(` - ${p}`))
29 console.log(`[watch-workspace] Current directory: ${__dirname}`)
30 console.log(`[watch-workspace] Resolved paths:`)
31 watchPaths.forEach((p) => console.log(` - ${path.resolve(p)}`))
32
33 let ready = false
34
35 const watcher = chokidar.watch(watchPaths, {
36 ignored: /node_modules/,
37 persistent: true,
38 })
39
40 watcher.on(`ready`, () => {
41 ready = true
42 console.log(
43 `[watch-workspace] Initial scan complete. Watching for changes...`,
44 )
45 })
46
47 watcher.on(`add`, (filePath) => {
48 if (!ready) return
49 console.log(`[watch-workspace] File added: ${filePath}`)
50 server.ws.send({
51 type: `full-reload`,
52 })
53 })
54
55 watcher.on(`change`, (filePath) => {
56 if (!ready) return
57 console.log(`[watch-workspace] File changed: ${filePath}`)
58 server.ws.send({
59 type: `full-reload`,
60 })
61 })
62
63 watcher.on(`error`, (error) => {
64 console.error(`[watch-workspace] Watcher error:`, error)
65 })
66 },
67 }
68}
69
70export default defineConfig({
71 server: {
72 port: 3000,
73 watch: {
74 ignored: [`!**/node_modules/@tanstack/**`],
75 },
76 },
77 optimizeDeps: {
78 exclude: [
79 `@tanstack/db`,
80 `@tanstack/offline-transactions`,
81 `@tanstack/browser-db-sqlite-persistence`,
82 `@tanstack/db-sqlite-persistence-core`,
83 `@journeyapps/wa-sqlite`,
84 ],
85 },
86 plugins: [
87 // Serve .wasm files before TanStack Start's catch-all handler intercepts them.
88 // We use configureServer returning a function (post-hook) and unshift onto the
89 // stack so this runs before any other middleware including TanStack Start.
90 {
91 name: `serve-wasm-files`,
92 configureServer(server: any) {
93 const wasmHandler = (req: any, res: any, next: () => void) => {
94 // Strip query string before checking extension
95 const urlWithoutQuery = (req.url ?? ``).split(`?`)[0]
96 if (!urlWithoutQuery.endsWith(`.wasm`)) {
97 return next()
98 }
99
100 // Handle /@fs/ paths used by Vite for serving node_modules files
101 const fsPrefix = `/@fs`
102 let filePath: string | undefined
103 if (urlWithoutQuery.startsWith(fsPrefix)) {
104 filePath = urlWithoutQuery.slice(fsPrefix.length)
105 }
106
107 if (!filePath || !fs.existsSync(filePath)) {
108 return next()
109 }
110
111 const content = fs.readFileSync(filePath)
112 res.writeHead(200, {
113 'Content-Type': `application/wasm`,
114 'Content-Length': content.byteLength,
115 'Cache-Control': `no-cache`,
116 })
117 res.end(content)
118 }
119
120 // Prepend to the middleware stack so it runs before TanStack Start
121 server.middlewares.stack.unshift({
122 route: ``,
123 handle: wasmHandler,
124 })
125 },
126 },
127 watchWorkspacePackages(),
128 tsConfigPaths({
129 projects: [`./tsconfig.json`],
130 }),
131 tanstackStart({
132 spa: { enabled: true },
133 }),
134 viteReact(),
135 tailwindcss(),
136 ],
137})