-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.mts
44 lines (40 loc) · 1.14 KB
/
vite.config.mts
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
import react from "@vitejs/plugin-react";
import crypto from "node:crypto";
import path from "node:path";
import UnoCSS from "unocss/vite";
import { defineConfig } from "vite";
// https://vite.dev/config/
export default defineConfig({
base: "./",
css: {
modules: { generateScopedName: createGenerateScopedName() },
preprocessorOptions: {
scss: {
api: "modern-compiler",
},
},
},
plugins: [react(), UnoCSS()],
});
/**
* Generate scoped name for CSS modules
*/
export function createGenerateScopedName() {
/**
* Cached CSS file hash
*/
const cachedCSSHash = new Map<string, string>();
function getCSSHash(fileName: string, css: string): string {
let hash = cachedCSSHash.get(fileName);
if (!hash) {
hash = crypto.createHash("md5").update(css).digest("hex").slice(0, 5);
cachedCSSHash.set(fileName, hash);
}
return hash;
}
return function generateScopedName(className: string, fileName: string, css: string): string {
const moduleName = path.basename(fileName).split(".")[0];
const hash = getCSSHash(fileName, css);
return `${moduleName}_${className}_${hash}`;
};
}