Skip to content

Commit b9c1892

Browse files
authored
Move vite dot path plugin into vite.config.ts (#1974)
move vite dot path plugin into vite.config.ts
1 parent e2e56d8 commit b9c1892

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

libs/vite-plugin-dot-path-fix/index.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

vite.config.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import { resolve } from 'path'
99
import basicSsl from '@vitejs/plugin-basic-ssl'
1010
import react from '@vitejs/plugin-react-swc'
11-
import { defineConfig } from 'vite'
11+
import { defineConfig, type Plugin } from 'vite'
1212
import { createHtmlPlugin } from 'vite-plugin-html'
1313
import { z } from 'zod'
1414

15-
import { dotPathFixPlugin } from './libs/vite-plugin-dot-path-fix'
1615
import tsConfig from './tsconfig.json'
1716

1817
const ApiMode = z.enum(['msw', 'dogfood', 'nexus'])
@@ -149,3 +148,34 @@ export default defineConfig(({ mode }) => ({
149148
includeSource: ['app/**/*.ts', 'libs/**/*.ts'],
150149
},
151150
}))
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

Comments
 (0)