import { readFileSync } from 'node:fs'
import { basename, relative } from 'node:path'
import { defineConfig } from 'vite'
const siteHeaderFile = new URL('./src/site-header.html', import.meta.url)
const siteRoot = new URL('./', import.meta.url).pathname
// Adds a footer Source link to each page's own code on Branchwell. src/main.js updates it from
// location.pathname, so the 404 page and any other URL a page answers point at the right path.
function addSourceLink(html, filename) {
const path = '/' + relative(siteRoot, filename).replace(/\.html$/, '').replace(/(^|\/)index$/, '')
return html.replace(/(class="footer-links"[^>]*>[\s\S]*?\n( *))/, (_, links, indent, close) =>
`${links}\n${indent}Source${close}`)
}
// Keep fonts as separate files. Vite inlines small assets as data: URLs, and the
// site's Content-Security-Policy (font-src 'self') blocks data: fonts.
export default defineConfig({
plugins: [{
name: 'opentalon-site-header',
transformIndexHtml: {
order: 'pre',
handler(html, { filename }) {
const page = basename(filename, '.html')
const href = `href="/${page}"`
const header = readFileSync(siteHeaderFile, 'utf8').trimEnd()
.replace(href, `${href} aria-current="page"`)
return addSourceLink(html.replace(/^ {6}$/m, header), filename)
},
},
}],
build: {
rolldownOptions: {
input: ['index.html', 'download.html', 'help.html', 'compare.html', 'privacy.html', 'app-notices.html', 'research.html', '404.html'],
},
assetsInlineLimit: (file) => (/\.(woff2?|ttf|otf)$/i.test(file) ? false : undefined),
},
})