-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
103 lines (96 loc) · 2.95 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import vue from "@vitejs/plugin-vue";
import { readdir, rm } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import { ViteMinifyPlugin } from "vite-plugin-minify";
import { imageSet, processAllImages, processImage } from "./tools/image.js";
import { renderTwig } from "./tools/twig.js";
const CWD = process.cwd();
// Обрабатываем накопленные неоптимизированные изображения
processAllImages();
/** @type {import('vite').UserConfig} */
export default defineConfig(async ({ mode }) => {
const isDev = mode === "development";
const icons = (await readdir("src/icons"))
.filter((file) => file.endsWith(".svg"))
.map((file) => file.replace(".svg", ""))
.join('","');
return {
base: "",
build: {
emptyOutDir: true,
rollupOptions: {
input: {
// Добавляем нужные страницы
404: "404.html",
index: "index.html",
},
},
},
css: {
preprocessorOptions: {
scss: {
additionalData: `$icons: "${icons}"; @use "@/scss/env" as *;`,
api: "modern-compiler",
silenceDeprecations: ["import", "mixed-decls"],
},
},
},
plugins: [
vue(),
{
configureServer(server) {
const handle = (eventName) => (rawPath) => {
const filePath = path.relative(CWD, rawPath).replaceAll("\\", "/");
const extname = path.extname(filePath);
if (
eventName !== "unlink" &&
(extname === ".twig" || filePath.includes("public/"))
) {
server.ws.send({ type: "full-reload" });
} else if (filePath.includes("src/icons") && extname === ".svg") {
server.restart();
} else if (
eventName === "add" &&
filePath.includes("raw/") &&
imageSet.has(extname)
) {
processImage(filePath);
}
};
server.watcher.on("add", handle("add"));
server.watcher.on("change", handle("change"));
server.watcher.on("unlink", handle("unlink"));
},
name: "watcher",
},
{
name: "twig",
transformIndexHtml: {
async handler(_html, { originalUrl, path }) {
return await renderTwig({ isDev, url: originalUrl || path });
},
order: "pre",
},
},
{
apply: "build",
name: "clean",
async renderStart() {
await Promise.all(
["dist/pixelperfect", "dist/images/README.md"].map((pathName) =>
rm(pathName, { force: true, recursive: true }),
),
);
},
},
ViteMinifyPlugin(),
],
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
};
});