Rewrites url for vite preview server paths

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2022-12-12 17:18:35 +01:00
parent 7be0a366b3
commit df680677b5
+29 -5
View File
@@ -1,8 +1,10 @@
/// <reference types="vitest" />
import type { ViteDevServer } from "vite";
import type { Connect, ViteDevServer } from "vite";
import type http from "node:http";
import path from "path";
import fs from "node:fs";
import path from "node:path";
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
@@ -41,7 +43,8 @@ export default defineConfig({
dev: process.env.NODE_ENV !== "production",
},
}),
pluginRewriteAll(),
configureDevServer(),
configurePreviewServer(),
],
server: {
// We have to set host here, otherwise CI binds to the ipv6 address and
@@ -73,9 +76,9 @@ export default defineConfig({
define: defineConstants(),
});
function pluginRewriteAll() {
function configureDevServer() {
return {
name: "rewrite-all",
name: "configure-dev-server",
configureServer(server: ViteDevServer) {
return () => {
server.middlewares.use((req, _res, next) => {
@@ -86,3 +89,24 @@ function pluginRewriteAll() {
},
};
}
function configurePreviewServer() {
return {
name: "configure-preview-server",
configurePreviewServer(server: {
middlewares: Connect.Server;
httpServer: http.Server;
}) {
server.middlewares.use((req, _res, next) => {
if (
fs.existsSync(`./public${req.url}`) ||
req.url?.startsWith("/assets")
) {
return next();
}
req.url = "/index.html";
next();
});
},
};
}