Branchwell

OpenTalon / website · opentalon.ai / vite.config.js

1import { readFileSync } from 'node:fs'
2import { basename, relative } from 'node:path'
3import { defineConfig } from 'vite'
4
5const siteHeaderFile = new URL('./src/site-header.html', import.meta.url)
6const siteRoot = new URL('./', import.meta.url).pathname
7
8// Adds a footer Source link to each page's own code on Branchwell. src/main.js updates it from
9// location.pathname, so the 404 page and any other URL a page answers point at the right path.
10function addSourceLink(html, filename) {
11 const path = '/' + relative(siteRoot, filename).replace(/\.html$/, '').replace(/(^|\/)index$/, '')
12 return html.replace(/(class="footer-links"[^>]*>[\s\S]*?\n( *)<a [^\n]*)(\s*\n *<\/(?:nav|div)>)/, (_, links, indent, close) =>
13 `${links}\n${indent}<a href="https://branchwell.ai/opentalon/website/for${path}" rel="noopener" data-source-link>Source</a>${close}`)
14}
15
16// Keep fonts as separate files. Vite inlines small assets as data: URLs, and the
17// site's Content-Security-Policy (font-src 'self') blocks data: fonts.
18export default defineConfig({
19 plugins: [{
20 name: 'opentalon-site-header',
21 transformIndexHtml: {
22 order: 'pre',
23 handler(html, { filename }) {
24 const page = basename(filename, '.html')
25 const href = `href="/${page}"`
26 const header = readFileSync(siteHeaderFile, 'utf8').trimEnd()
27 .replace(href, `${href} aria-current="page"`)
28 return addSourceLink(html.replace(/^ {6}<!-- site-header -->$/m, header), filename)
29 },
30 },
31 }],
32 build: {
33 rolldownOptions: {
34 input: ['index.html', 'download.html', 'help.html', 'compare.html', 'privacy.html', 'app-notices.html', 'research.html', '404.html'],
35 },
36 assetsInlineLimit: (file) => (/\.(woff2?|ttf|otf)$/i.test(file) ? false : undefined),
37 },
38})