-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
76 lines (71 loc) · 2.11 KB
/
vite.config.js
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
import path from "node:path";
import fs from "node:fs/promises";
import prependFile from "prepend-file";
/* ========================= */
/* = Copyright (c) NullDev = */
/* ========================= */
const navigator = global.navigator || {};
if (!navigator.userAgent) navigator.userAgent = "";
/**
* Prepends a header to the generated files
*
* @returns {Promise<void>}
*/
const writeHeaderToFiles = async function(){
const files = await fs.readdir(path.resolve("snippets"));
const matches = files.filter(file => file.endsWith(".js") || file.endsWith(".css"));
matches.forEach(async file => {
await prependFile(
path.resolve("snippets", file),
"/* - COPY & PASTE THIS INTO THE DISCORD DEVTOOLS CONSOLE (Ctrl + Shift + i) - */\n\n",
);
});
};
/**
* Vite plugin to inline utils.js into the snippets
*
* @return {import("vite").Plugin}
*/
const inlineUtilsPlugin = () => {
return {
name: "inline-utils",
async transform(code, id){
if (id.endsWith(".js")){
const utilsCode = await fs.readFile(path.resolve("src/util/utils.js"), "utf8");
return {
code: utilsCode + "\n" + code.replace(/import\s*{[^}]*}\s*from\s*['"][^'"]+['"];\s*/g, ""),
map: null,
};
}
return null;
},
};
};
export default {
build: {
target: "esnext",
outDir: "snippets",
emptyOutDir: true,
minify: true,
polyfill: false,
rollupOptions: {
input: {
...Object.fromEntries((await fs.readdir("src/snippets")).map((snippet) => [snippet.replace(".js", ""), path.resolve("src/snippets", snippet)])),
},
output: {
entryFileNames: "[name].js",
chunkFileNames: "[name].js",
format: "es",
},
plugins: [
inlineUtilsPlugin(),
],
},
},
plugins: [{
name: "post-build",
async closeBundle(){
await writeHeaderToFiles();
},
}],
};