-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
77 lines (69 loc) · 3.29 KB
/
Copy pathvite.config.js
File metadata and controls
77 lines (69 loc) · 3.29 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
import { readdirSync, readFileSync } from 'fs';
import { join } from 'path';
import { defineConfig } from 'vite';
const THEME_SCRIPT = `<script>const t=localStorage.theme;document.documentElement.dataset.theme=t||(matchMedia('(prefers-color-scheme:dark)').matches?'dark':'light')</script>`;
const THEME_BUTTON = `<button id="theme-toggle" aria-label="Toggle theme">
<svg class="icon-moon" viewBox="0 0 16 16" fill="currentColor"><path d="M6 .278a.77.77 0 0 1 .08.858 7.2 7.2 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277q.792-.001 1.533-.16a.79.79 0 0 1 .81.316.73.73 0 0 1-.031.893A8.35 8.35 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.75.75 0 0 1 6 .278"/></svg>
<svg class="icon-sun" viewBox="0 0 16 16" fill="currentColor"><path d="M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8M8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0m0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13m8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5M3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8m10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0m-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0m9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707M4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708"/></svg>
</button>`;
const THEME_MODULE = `<script type="module" src="/src/theme.js"></script>`;
const EXAMPLE_DIRS = ['src/less-javascript', 'src/css-new-features'];
function themePlugin() {
return {
name: 'theme',
transformIndexHtml: {
order: 'pre',
handler(html, ctx) {
if (!EXAMPLE_DIRS.some((d) => ctx.filename.includes(d))) return html;
return html
.replace('</head>', ` ${THEME_SCRIPT}\n </head>`)
.replace(/(<body[^>]*>)/, `$1\n ${THEME_BUTTON}\n`)
.replace('</body>', ` ${THEME_MODULE}\n </body>`);
},
},
};
}
function readCards(dirRel) {
const dir = join(process.cwd(), dirRel);
let entries;
try {
entries = readdirSync(dir, { withFileTypes: true });
} catch {
return '';
}
return entries
.filter((e) => e.isDirectory())
.map((e) => {
const file = join(dir, e.name, `${e.name}.html`);
let src = '';
try {
src = readFileSync(file, 'utf-8');
} catch {
return '';
}
const title = src.match(/<title>(.+?)(?:\s*—[^<]*)?\s*<\/title>/i)?.[1] ?? e.name;
const desc = src.match(/<meta\s+name="description"\s+content="([^"]+)"/i)?.[1] ?? '';
const href = `/${dirRel}/${e.name}/${e.name}.html`;
return `
<a href="${href}" class="demo-card">
<span class="demo-card__title">${title}</span>
<span class="demo-card__desc">${desc}</span>
<span class="demo-card__arrow">→</span>
</a>`;
})
.join('');
}
function demosPlugin() {
return {
name: 'demos',
transformIndexHtml(html) {
return html.replace(
/<div class="demos-list" data-source="([^"]+)"><\/div>/g,
(_, source) => `<div class="demos-list">${readCards(`src/${source}`)}\n </div>`,
);
},
};
}
export default defineConfig({
plugins: [themePlugin(), demosPlugin()],
});