// @ts-check import { existsSync, readdirSync, readFileSync, rmSync } from 'node:fs'; import { resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { defineConfig } from 'astro/config'; /** * Astro copies the original photos into _astro/ even though pages only use * the resized versions. The originals still carry their EXIF (GPS, camera, * exact time), so delete every image no page references. * @returns {import('astro').AstroIntegration} */ const dropUnusedOriginals = () => ({ name: 'drop-unused-originals', hooks: { 'astro:build:done': ({ dir, pages, logger }) => { const root = fileURLToPath(dir); if (!existsSync(`${root}_astro`)) return; const html = pages .map(({ pathname }) => { const page = pathname === '' ? 'index' : pathname.replace(/\/$/, ''); return readFileSync(`${root}${page}.html`, 'utf8'); }) .join('\n'); for (const file of readdirSync(`${root}_astro`)) { if (/\.(jpe?g|png|webp|avif)$/i.test(file) && !html.includes(`/_astro/${file}`)) { rmSync(`${root}_astro/${file}`); logger.info(`removed unreferenced ${file}`); } } }, }, }); // photos/ and profile/ to build from; the example content by default, a site // repo's checkout when building a real site (FOLIO_CONTENT=../my-site) const content = resolve(process.env.FOLIO_CONTENT ?? 'content'); export default defineConfig({ // p/.html instead of p//index.html: static-server's nginx // serves $uri.html, so /p/ works without a trailing-slash redirect build: { format: 'file' }, trailingSlash: 'never', integrations: [dropUnusedOriginals()], vite: { define: { 'import.meta.env.FOLIO_CONTENT': JSON.stringify(content) }, resolve: { alias: { '@content': content } }, server: { fs: { allow: ['.', content] } }, }, });