Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
86 lines (68 loc) · 2.69 KB
/
Copy pathvite.config.js
File metadata and controls
86 lines (68 loc) · 2.69 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// SPDX-FileCopyrightText: 2026 The Khronos Group, Inc.
// SPDX-License-Identifier: Apache-2.0 OR MIT
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import tailwindcss from '@tailwindcss/vite';
import nunjucks from 'nunjucks';
import { defineConfig } from 'vite';
import { loadConfig } from './scripts/lib/config.mjs';
import { loadData } from './scripts/lib/data.mjs';
import { buildSite } from './scripts/lib/score.mjs';
const root = fileURLToPath(new URL('.', import.meta.url));
const templates = `${root}src/templates`;
function compact(value) {
if (value < 1000) {
return String(value);
}
const thousands = value / 1000;
return `${thousands >= 100 ? Math.round(thousands) : thousands.toFixed(1).replace(/\.0$/, '')}k`;
}
function formatDate(value, options) {
return value ? new Intl.DateTimeFormat('en-US', { timeZone: 'UTC', ...options }).format(new Date(value)) : '';
}
function createEnvironment() {
const env = new nunjucks.Environment(new nunjucks.FileSystemLoader(templates, { noCache: true }), {
autoescape: true,
throwOnUndefined: false,
});
env.addFilter('compact', compact);
env.addFilter('plural', (count, noun) => `${count} ${noun}${count === 1 ? '' : 's'}`);
env.addFilter('monthYear', (value) => formatDate(value, { month: 'short', year: 'numeric' }));
env.addFilter('longDate', (value) => formatDate(value, { day: 'numeric', month: 'long', year: 'numeric' }));
return env;
}
/** Renders index.html from the Nunjucks templates and the scored repository data. */
function khronosSite() {
let site;
return {
name: 'khronos-site',
transformIndexHtml: {
order: 'pre',
handler(html) {
const { data, source } = loadData();
site = buildSite(data, loadConfig());
this.info?.(`${site.stats.listed} repositories from ${source}`);
return createEnvironment().renderString(html, { site, year: new Date().getUTCFullYear() });
},
},
configureServer(server) {
server.watcher.add([`${root}config`, `${root}data`, templates]);
server.watcher.on('change', (file) => {
if (/\.(njk|yml|json)$/.test(file)) {
server.ws.send({ type: 'full-reload' });
}
});
},
generateBundle() {
const report = { generatedAt: site.generatedAt, repositories: site.report };
this.emitFile({ type: 'asset', fileName: 'report.json', source: `${JSON.stringify(report, null, 2)}\n` });
this.emitFile({ type: 'asset', fileName: 'CNAME', source: readFileSync(`${root}CNAME`) });
},
};
}
export default defineConfig({
base: './',
appType: 'mpa',
plugins: [khronosSite(), tailwindcss()],
build: { outDir: 'dist', emptyOutDir: true },
});