Files
slp-modular-cms/frontend/vite.config.ts
T

103 lines
4.1 KiB
TypeScript

/// <reference types="vitest/config" />
import path from 'node:path';
import fs from 'node:fs';
import { defineConfig, loadEnv, type Plugin } from 'vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import { version } from './package.json';
import { resolveTheme } from './themes/resolveTheme';
/** VITE_THEME selects themes/<name>/theme.css at build time (unset = today's admin, unchanged). */
function themeFor(mode: string) {
return resolveTheme({ ...loadEnv(mode, process.cwd(), 'VITE_'), ...process.env }.VITE_THEME);
}
/**
* Per-site favicon (VITE_THEME): the page links the theme favicon instead of the default one.
* Build emits it as an asset; the dev server serves it from the theme folder.
*/
function themeFavicon(faviconPath: string | null): Plugin {
const publicName = 'favicon-theme.png';
return {
name: 'cms-theme-favicon',
// 'post': Vite has already prefixed the base path (/admin/) by then, so match any prefix.
transformIndexHtml: {
order: 'post',
handler(html) {
return faviconPath === null ? html : html.replace(/<link rel="icon"[^>]*favicon\.svg"[^>]*>/, (tag) =>
tag.replace(/type="[^"]*"/, 'type="image/png"').replace(/href="([^"]*)favicon\.svg"/, `href="$1${publicName}"`));
},
},
configureServer(server) {
if (faviconPath === null) return;
server.middlewares.use(`/${publicName}`, (_req, res) => {
res.setHeader('Content-Type', 'image/png');
res.end(fs.readFileSync(faviconPath));
});
},
generateBundle() {
if (faviconPath === null) return;
this.emitFile({ type: 'asset', fileName: publicName, source: fs.readFileSync(faviconPath) });
},
};
}
// https://vite.dev/config/
export default defineConfig(({ command, mode }) => ({
// Production builds are deployed under /admin (see Program.cs); the dev server keeps serving from '/'.
base: command === 'build' ? '/admin/' : '/',
plugins: [react(), tailwindcss(), themeFavicon(themeFor(mode).favicon)],
define: {
// Injected at build time, used as the Sentry `release` tag (see src/lib/sentry.ts).
__APP_VERSION__: JSON.stringify(version),
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
// The selected theme's CSS (or an empty file); imported once from main.tsx.
'@theme': themeFor(mode).css,
},
},
server: {
port: 5173,
proxy: {
// Mirrors the tunnel path the API serves in test and production, so the same
// Sentry `tunnel` option also works during `pnpm dev` — where this SPA runs on
// 5173 and the API on 7221.
//
// Note this targets our own API rather than Sentry's ingest host directly. The
// reference project proxies straight to Sentry, which forces the DSN's project id
// into this file and requires keeping it in sync by hand. The API derives the
// destination from its own DSN, so there is nothing to synchronise and no project
// id in a committed file.
'/sentry-tunnel': {
target: 'https://localhost:7221',
changeOrigin: true,
secure: false,
},
},
},
test: {
globals: true,
environment: 'jsdom',
setupFiles: ['./src/test/setup.ts'],
testTimeout: 20000,
css: true,
coverage: {
provider: 'v8',
reporter: ['text', 'html'],
include: ['src/**/*.{ts,tsx}'],
exclude: [
'src/**/*.test.{ts,tsx}',
'src/test/**',
'src/mocks/**',
'src/main.tsx',
'src/vite-env.d.ts',
// Calls Sentry.init, which cannot run meaningfully under jsdom; its skip-without-
// a-DSN behaviour is covered by a test that asserts init was not called.
'src/lib/sentry.ts',
],
},
},
}));