Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
38 lines (36 loc) · 1.5 KB
/
Copy pathvite.config.ts
File metadata and controls
38 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { resolve } from 'node:path'
import { defineConfig, type Plugin } from 'vite'
import { libraryPlugin } from './src/library/plugin.ts'
import { globalNavigation } from './src/navigation.ts'
import { pages } from './src/pages.ts'
import { head, toolPage } from './src/shell.ts'
// Each page's <head>, and a tool page's whole document, from src/pages.ts
// (src/shell.ts): the HTML files in the repository are placeholders for them.
function pageShells(): Plugin {
return {
name: 'page-shells',
transformIndexHtml: {
order: 'pre',
handler(html, { filename }) {
const page = pages.find((p) => resolve(import.meta.dirname, p.file) === filename)
if (!page) return html
if (page.content) return toolPage(page)
if (!/<head>[\s\S]*?<\/head>/.test(html)) throw new Error(`${page.file}: no <head> to fill`)
if (page.key === 'main') html = html.replace(/<nav aria-label="(?:Main navigation|Navigation principale)">[\s\S]*?<\/nav>/, globalNavigation(page.lang))
return html.replace(/<head>[\s\S]*?<\/head>/, head(page))
},
},
}
}
// One entry per page of src/pages.ts.
export default defineConfig(({ isSsrBuild }) => ({
base: '/',
plugins: [pageShells(), libraryPlugin()],
build: {
chunkSizeWarningLimit: 800,
// the SSR build (src/prerender.ts) takes its entry from the command line
rollupOptions: isSsrBuild ? {} : {
input: Object.fromEntries(pages.map((page) => [page.name, resolve(import.meta.dirname, page.file)])),
},
},
}))