Files
dynamicbrain.neuroinf.jp/vite.config.ts
T
orrisroot 87c0274bc1 feat: migrate from Create React App to Vite
Rebuild the site on the stack the other converted sites share: Vite 8,
React 19, TypeScript 7, react-router 8, Biome and sanitize.css, with the
site data moved out of the bundle into static-contents/.

- base the database, credits and pico code on cerebellum.neuroinf.jp,
  keeping this site's item types, rankings and menus
- port the MediaWiki pages to function components
- serve all module data from modules/, redirecting the old download and
  /database/file URLs
- tile the plate texture and keep form controls on white
- track page views with GA4
2026-09-14 16:57:14 +09:00

63 lines
2.5 KiB
TypeScript

import fs from 'node:fs';
import path from 'node:path';
import react from '@vitejs/plugin-react';
import sirv from 'sirv';
import type { Connect, Plugin, ResolvedConfig } from 'vite';
import { defineConfig } from 'vite';
/**
* `static-contents/` holds the dumped site data (`modules/`, `rss.xml`, and the static `conferences/` and `hetero/` sites).
* It sits next to the repository and is served from the site root next to
* `public/`, but it is deliberately kept out of the bundle: it runs to
* gigabytes of frozen data that the web server maps in directly. The dev and
* preview servers mount it so that both behave like production.
*/
const staticContents = (): Plugin => {
let dir: string;
const mount = (middlewares: { use: (fn: Connect.NextHandleFunction) => void }, dev: boolean) => {
if (fs.existsSync(dir)) {
// maxAge 0 makes sirv send `Cache-Control: public, max-age=0`, so the
// browser revalidates against the ETag instead of falling back to
// heuristic caching. Without it a re-dump, or another site served
// from the same localhost port, is answered from a stale cache.
middlewares.use(sirv(dir, { dev, etag: true, maxAge: 0 }));
}
};
return {
name: 'dynamicbrain:static-contents',
configResolved(config: ResolvedConfig) {
dir = path.resolve(config.root, '../static-contents');
},
configureServer(server) {
mount(server.middlewares, true);
},
configurePreviewServer(server) {
mount(server.middlewares, false);
},
};
};
// https://vite.dev/config/
export default defineConfig({
plugins: [react(), staticContents()],
optimizeDeps: { exclude: ['fs'] },
build: {
sourcemap: true,
rolldownOptions: {
external: ['fs'],
output: {
// react and react-dom are over 200 kB together, which would
// push a single dependency chunk past the 500 kB warning
// threshold. They also change far less often than the rest,
// so split them off and let the two cache independently.
codeSplitting: {
groups: [
{ name: 'vendor-react', test: /node_modules\/(react|react-dom|scheduler)\// },
{ name: 'vendor-misc', test: /node_modules\// },
],
},
},
},
},
});