-
Notifications
You must be signed in to change notification settings - Fork 3
/
vite.config.ts
66 lines (64 loc) · 2.4 KB
/
vite.config.ts
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
import {cwd} from 'node:process';
import {join} from 'node:path';
import {defineConfig} from 'vite';
import {readdir, readFile} from 'node:fs/promises';
import type {InputOptions} from 'rollup';
import type {ViteDevServer} from 'vite';
export default defineConfig(async () => {
const examplesDir = join(cwd(), 'examples')
const entries = await readdir(examplesDir);
const examples: string[] = [];
for (const entry of entries) {
if (entry.endsWith('.html') || entry.endsWith('.htm')) {
examples.push(join(examplesDir, entry));
}
}
return {
plugins: [
{
name: 'slidev-iframes',
options(options: InputOptions) {
/**
* This makes it so the build step includes all example sources.
* This could be removed if Slidev were to treat the <iframe> URLs
* like Vite does (bundling their resources and serving them).
* See https://github.com/slidevjs/slidev/issues/1921.
*/
if (!options.input) {
options.input = [...examples];
} else if (typeof options.input === 'string') {
options.input = [options.input, ...examples];
} else if (Array.isArray(options.input)) {
options.input = [...options.input, ...examples];
} else {
console.error('unexpected options.input: ', options.input);
}
return options;
},
configureServer(server: ViteDevServer) {
/**
* This is a workaround for https://github.com/slidevjs/slidev/issues/1923.
* If that issue is closed, this `configureServer` function can be removed.
* If this stops working, another workaround is to use `.htm` files instead
* of `.html`.
*
* See https://github.com/slidevjs/slidev/pull/1926 for a fix that could
* make this unnecessary.
*/
server.middlewares.use(async (req, res, next) => {
if (req.url) {
const url = new URL(req.url, 'http://' + req.headers.host);
const filePath = join(cwd(), url.pathname);
if (examples.includes(filePath)) {
const content = await readFile(filePath, {encoding: 'utf8'});
const end = res.end.bind(res);
res.end = () => end(content);
}
}
next();
});
}
}
]
};
});