-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvite.config.ts
88 lines (83 loc) · 3.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import childProcess from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { defineConfig, Plugin, TransformResult } from 'vite';
import { createHtmlPlugin } from 'vite-plugin-html';
import { visualizer } from 'rollup-plugin-visualizer';
// https://vitejs.dev/config/
export default defineConfig({
base: '',
plugins: [
(() => {
// Download shader_minifier executable from:
// https://github.com/laurentlb/shader-minifier
if (process.env.NODE_ENV !== 'production') return;
if (childProcess.spawnSync('shader_minifier', ['--help']).error) {
console.warn('Shader minifier is not available.');
return;
}
return ({
name: 'minify-shader',
async transform(code, id) {
if (id.endsWith('.glsl?raw')) {
const temp = path.join(os.tmpdir(), Math.random().toString(36).substring(2, 10));
const result: TransformResult = {
code: '',
map: null,
};
try {
await new Promise((resolve, reject) => {
const p = childProcess.spawn('shader_minifier', [
'-o', temp,
'--format', 'text',
'--aggressive-inlining',
'--preserve-externals',
id.replace(/\?raw$/g, ''),
]);
p.on('close', resolve);
p.on('error', reject);
});
result.code = `export default ${JSON.stringify(await fs.promises.readFile(temp, { encoding: 'utf-8' }))}`;
} finally {
await fs.promises.unlink(temp).catch(() => {});
}
return result;
}
}
} as Plugin);
})(),
createHtmlPlugin({
minify: {
collapseWhitespace: true,
collapseBooleanAttributes: true,
decodeEntities: true,
removeComments: true,
removeAttributeQuotes: false,
// removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeEmptyAttributes: true,
useShortDoctype: true,
processConditionalComments: true,
sortAttributes: true,
sortClassName: true,
minifyCSS: true,
minifyJS: true,
minifyURLs: false,
},
}),
],
build: {
chunkSizeWarningLimit: Infinity,
target: 'esnext',
rollupOptions: {
plugins: [
visualizer({
gzipSize: true,
brotliSize: true,
}),
],
},
},
});