-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
46 lines (42 loc) · 1.42 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
import { createHash } from "node:crypto";
import { readFileSync, renameSync, writeFileSync } from "node:fs";
import { basename,resolve } from "node:path";
import { glob } from "glob";
import { defineConfig } from "vite";
type FileMapping = Record<string, {file: string}>;
export default defineConfig({
build: {
minify: "terser",
lib: {
// https://gist.github.com/jasenmichael/58dd3a4f8e7ec1d003e88907bba392d7
entry: glob.sync(resolve(__dirname, "wpn-utils/*.ts")),
formats: ["es"],
},
outDir: resolve(__dirname, "html/js/wpn_utils"),
rollupOptions: {
output: {
entryFileNames: "[name].[hash].js",
},
},
manifest: "manifest.json",
},
plugins: [
{
name: "hashCSS",
closeBundle() {
const CSSManifestFile = "css_manifest.json";
const CSSFiles = glob.sync(resolve(__dirname, "html/css/*.css"), { withFileTypes: true });
writeFileSync(CSSManifestFile, '');
const CSSMetaData: FileMapping = {};
for (const CSSFile of CSSFiles) {
const data = readFileSync(CSSFile.fullpath());
const hash = createHash("sha256").update(data).digest("hex").substring(0, 8);
const fileName = [basename(CSSFile.name, ".css"), hash, "css"].join(".");
CSSMetaData[basename(CSSFile.name, ".css")] = {file: fileName}
renameSync(CSSFile.fullpath(), resolve(__dirname, "html/css/", fileName));
}
writeFileSync(CSSManifestFile, JSON.stringify(CSSMetaData), { flag: 'a+' })
},
},
],
});