Files
Find-Word/vite.config.ts
2026-10-14 10:01:46 +00:00

53 lines
1.8 KiB
TypeScript

import { statSync } from "node:fs";
import { resolve, sep } from "node:path";
import type { IncomingMessage, ServerResponse } from "node:http";
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
// 테스트는 DOM을 흉내 내되 오디오 장치를 열지 않는다. 실제 브라우저 검증은
// 별도의 Playwright 경로에서 수행하여 순수 로직과 장치 의존 실패를 구분한다.
// Vite의 SPA fallback은 Accept: text/html인 누락 WAV까지 200 HTML로 바꾼다.
// 개발/preview에서도 배포 Nginx의 try_files =404와 같은 정적 자산 계약을 적용한다.
function audioFilesOnly(directory: string) {
const root = resolve(directory);
return (
request: IncomingMessage,
response: ServerResponse,
next: () => void,
) => {
const pathname = new URL(request.url ?? "/", "http://localhost").pathname;
if (!/^\/(audio|ambience)\//.test(pathname)) return next();
try {
const file = resolve(root, `.${decodeURIComponent(pathname)}`);
if (!file.startsWith(root + sep) || !statSync(file).isFile())
throw new Error("missing");
next();
} catch {
response.statusCode = 404;
response.setHeader("Content-Type", "text/plain; charset=utf-8");
response.end("Audio asset not found");
}
};
}
export default defineConfig({
plugins: [
react(),
{
name: "static-audio-404",
configureServer(server) {
server.middlewares.use(audioFilesOnly(server.config.publicDir));
},
configurePreviewServer(server) {
server.middlewares.use(audioFilesOnly(server.config.build.outDir));
},
},
],
test: {
environment: "jsdom",
setupFiles: ["./tests/setup.ts"],
include: ["tests/**/*.test.{ts,tsx}"],
restoreMocks: true,
},
});