|
8 | 8 | import { resolve } from 'path' |
9 | 9 | import basicSsl from '@vitejs/plugin-basic-ssl' |
10 | 10 | import react from '@vitejs/plugin-react-swc' |
11 | | -import { defineConfig } from 'vite' |
| 11 | +import { defineConfig, type Plugin } from 'vite' |
12 | 12 | import { createHtmlPlugin } from 'vite-plugin-html' |
13 | 13 | import { z } from 'zod' |
14 | 14 |
|
15 | | -import { dotPathFixPlugin } from './libs/vite-plugin-dot-path-fix' |
16 | 15 | import tsConfig from './tsconfig.json' |
17 | 16 |
|
18 | 17 | const ApiMode = z.enum(['msw', 'dogfood', 'nexus']) |
@@ -149,3 +148,34 @@ export default defineConfig(({ mode }) => ({ |
149 | 148 | includeSource: ['app/**/*.ts', 'libs/**/*.ts'], |
150 | 149 | }, |
151 | 150 | })) |
| 151 | + |
| 152 | +/** |
| 153 | + * Configure a safelist of path patterns that can be redirected to `/` despite |
| 154 | + * having a dot in them. |
| 155 | + * |
| 156 | + * Vite does not rewrite paths with dots in them to serve `/index.html`, likely |
| 157 | + * because it wants to assume they are static files that should be served |
| 158 | + * directly. See https://github.com/vitejs/vite/issues/2415. |
| 159 | + * |
| 160 | + * We have a few non-file console paths that we expect to contain a dot. Names |
| 161 | + * cannot contain dots, but semver versions always will. So we safelist some |
| 162 | + * paths that we expect to have dots so they will work in the dev server. |
| 163 | + * |
| 164 | + * If a path needs to be added to this safelist, it will show up as a blank page |
| 165 | + * in local dev and the Vite `--debug` output will say: |
| 166 | + * |
| 167 | + * "Not rewriting GET /has.dot because the path includes a dot (.) character." |
| 168 | + */ |
| 169 | +function dotPathFixPlugin(safeDotPaths: RegExp[]): Plugin { |
| 170 | + return { |
| 171 | + name: 'dot-path-fix', |
| 172 | + configureServer: (server) => { |
| 173 | + server.middlewares.use((req, _, next) => { |
| 174 | + if (req.url && safeDotPaths.some((p) => req.url?.match(p))) { |
| 175 | + req.url = '/' |
| 176 | + } |
| 177 | + next() |
| 178 | + }) |
| 179 | + }, |
| 180 | + } |
| 181 | +} |
0 commit comments