-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
53 lines (47 loc) · 1.62 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
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { resolve } from 'path'
import { colorSuitePlugin } from 'tailwindcss-color-suite'
import MagicString from 'magic-string';
import { readFileSync } from 'fs'
let bytes_saved:number = 0
export default defineConfig({
plugins: [
vue(),
colorSuitePlugin(),
createSvgIconsPlugin({
iconDirs: [resolve(process.cwd(), 'src/assets/icons')]
}),
],
build: {
rollupOptions: {
plugins: [
{
name: 'postprocess',
closeBundle() {
console.log(`Saved ${bytes_saved/1000} kB by compressing class names.`)
},
renderChunk(code, chunk, { sourcemap }) {
let src = new MagicString(code)
const transforms = readFileSync('./dist/main-classname-transforms.json', 'utf8')
const replacements = Object.entries<string>(JSON.parse(transforms))
for (let [key, replace=''] of replacements) {
let find = new RegExp(`(?<=^|\\s+|"+|'+|\`+)${key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?=\\s+|$|"+|'+|\`+)`, 'g')
let token:RegExpExecArray
while (token = find.exec(code)) {
bytes_saved += key.length - replace.length
src.overwrite(token.index, token.index + token[0].length, replace)
}
}
return {
code: src.toString(),
map: sourcemap === false ? null : src.generateMap({ hires: true })
}
}
}
]
}
},
envPrefix: 'WLED'
})