// `npm run dev`: vite serves site/, rebuilding the docs and reloading the page whenever // docs/, docs.css or docs-lake.js change. A failed build shows in vite's error overlay. import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { defineConfig } from 'vite'; import { build } from './build-docs.mjs'; const here = import.meta.dirname; const sources = [ join(here, '..', 'docs'), join(here, 'docs.css'), join(here, 'docs-lake.js'), ]; function docs() { let queue = Promise.resolve(); const rebuild = (server) => (queue = queue.then(async () => { try { await build(); // vite ignores changes under site/docs, so it would keep serving the old docs-lake.js. server.moduleGraph.invalidateAll(); server.ws.send({ type: 'full-reload' }); } catch (error) { server.config.logger.error(error.stack ?? String(error)); server.ws.send({ type: 'error', err: { message: error.message, stack: error.stack ?? '' }, }); } })); return { name: 'albedo-docs', async configureServer(server) { server.watcher.add(sources); server.watcher.on( 'all', (_, file) => sources.some((source) => file.startsWith(source)) && rebuild(server), ); // A folder's address without its slash redirects to the folder. The static host serves the // page at both addresses instead, which is why the docs carry absolute addresses. server.middlewares.use((request, response, next) => { const path = request.url.split('?')[0]; if ( path.endsWith('/') || path.includes('.') || !existsSync(join(here, path, 'index.html')) ) return next(); response .writeHead(301, { location: request.url.replace(path, `${path}/`) }) .end(); }); server.httpServer?.once('listening', () => setTimeout(() => { const urls = [ ...(server.resolvedUrls?.local ?? []), ...(server.resolvedUrls?.network ?? []), ]; urls.forEach((url) => server.config.logger.info(` docs: ${url}docs/`), ); }), ); await rebuild(server); }, }; } export default defineConfig({ plugins: [docs()], // Separate static pages, not an app: an unknown address is a 404, not the landing page. appType: 'mpa', // The built pages change on every rebuild; the plugin reloads once the whole build is written. server: { watch: { ignored: [join(here, 'docs', '**')] } }, });