-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.mjs
More file actions
68 lines (59 loc) · 1.88 KB
/
Copy pathvite.config.mjs
File metadata and controls
68 lines (59 loc) · 1.88 KB
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
import { defineConfig, loadEnv } from "vite";
import { viteSingleFile } from "vite-plugin-singlefile";
import fs from "fs";
import path from "path";
function inlineSvgFaviconPlugin(options) {
return {
name: "inline-svg-favicon",
enforce: "post",
transformIndexHtml(html) {
if (!fs.existsSync(options.svg)) return html;
const ext = path.extname(options.svg).toLowerCase();
let faviconTag = "";
try {
if (ext === ".svg") {
let svgContent = fs.readFileSync(options.svg, "utf8");
svgContent = svgContent
.replace(/<\?xml[^>]*>\s*/g, "")
.replace(/\s+/g, " ");
const base64 = Buffer.from(svgContent).toString("base64");
faviconTag = `<link rel="icon" type="image/svg+xml" href="data:image/svg+xml;base64,${base64}"/>\n`;
} else {
const buf = fs.readFileSync(options.svg);
const base64 = buf.toString("base64");
const mime = ext === ".png" ? "image/png" : ext === ".ico" ? "image/x-icon" : "application/octet-stream";
faviconTag = `<link rel="icon" type="${mime}" href="data:${mime};base64,${base64}"/>\n`;
}
} catch (e) {
return html;
}
return html.replace(/(<head[^>]*>)/i, `$1\n ${faviconTag}`);
},
};
}
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), "");
const isSingleFile = env.SINGLE_FILE === "true";
return {
base: "./",
plugins: [
isSingleFile && viteSingleFile(),
isSingleFile && inlineSvgFaviconPlugin({ svg: "public/favicon.png" }),
].filter(Boolean),
define: {
global: "globalThis",
},
worker: {
format: "es",
},
build: {
sourcemap: !isSingleFile,
outDir: "./dist",
emptyOutDir: true,
chunkSizeWarningLimit: 1500,
},
optimizeDeps: {
exclude: ["wasm-git"],
},
};
});