-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
69 lines (66 loc) · 1.71 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
67
68
69
/* eslint-disable node/no-extraneous-import */
import {defineConfig, Plugin} from 'vite';
import fg from 'fast-glob';
import path from 'node:path';
import jsBeautify from 'js-beautify';
// Plugin that auto-injects the index.js file into every html page in examples.
const examplesHtmlPlugin = () => {
const plugin: Plugin = {
name: 'examples-html',
transformIndexHtml: {
order: 'pre',
handler: (html) => html.replace(
'</body>',
'</body><script src="scripts/index.js" type="module"></script>'
),
},
};
return plugin;
};
// Plugin that pretty prints the html output.
const prettyHtmlPlugin = () => {
function prettyHtml(html: string): string {
try {
const output = jsBeautify.html(html, {
indent_size: 0,
end_with_newline: true,
max_preserve_newlines: 0,
extra_liners: [],
});
return output.trimStart();
} catch (e) {
console.error('failed to pretty html:', e);
return html;
}
}
const plugin: Plugin = {
name: 'pretty-html',
transformIndexHtml: {
order: 'post',
handler: (html) => prettyHtml(html),
},
};
return plugin;
};
export default defineConfig(() => {
const inputs: Record<string, string> = {};
const files = fg.sync('./examples/*.html');
files.forEach(relpath => {
const basename = path.basename(relpath);
const filename = basename.slice(0, -5);
inputs[filename] = relpath;
});
inputs.main = './examples/index.html';
return {
root: './examples',
base: '/degu/examples/',
appType: 'mpa',
build: {
minify: true,
rollupOptions: {
input: inputs,
},
},
plugins: [examplesHtmlPlugin(), prettyHtmlPlugin()],
};
});