-
Notifications
You must be signed in to change notification settings - Fork 7
/
vite.config.ts
148 lines (130 loc) · 4.02 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import fs from "fs";
import path from "path";
import react from "@vitejs/plugin-react";
import copy from "rollup-plugin-copy";
import { defineConfig } from "vite";
import stylelint from "vite-plugin-stylelint";
import svgr from "vite-plugin-svgr";
import tsconfigPaths from "vite-tsconfig-paths";
import type { Config } from "@/constants/config.ts";
import type { Image } from "@/constants/image.ts";
import config from "./app.config.json";
// https://vitejs.dev/config/
export default defineConfig({
base: "./",
plugins: [
react(),
tsconfigPaths(),
svgr(),
stylelint({
fix: true,
}),
copyContributeGuide(),
copyBackgroundsForFileSystem(),
],
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "./src/styles/_mixin.scss" as *;`,
},
},
},
assetsInclude: ["**/*.md"],
});
function copyContributeGuide() {
const { contributeGuide } = config as unknown as Config;
const targetDirectory = "dist";
return copy({
targets: [
{
src: contributeGuide,
dest: targetDirectory,
},
],
hook: "writeBundle",
});
}
/**
* If config has `filesystem` type for `backgroundsUri`, copy local backgrounds path to dist folder
*/
function copyBackgroundsForFileSystem() {
const { backgroundsUri, themes } = config as unknown as Config;
if (
backgroundsUri &&
backgroundsUri.type === "filesystem" &&
backgroundsUri.path
) {
const dir = path.resolve();
const sourceDirectory = backgroundsUri.path;
const targetDirectory = "dist";
const order = themes.map(({ name }) => name);
const filteredOrder = themes
.filter(({ isHidden }) => !isHidden)
.map(({ name }) => name);
const fileContentsArray = readFilesRecursively(sourceDirectory);
order.map((theme, index) => {
const isThemeHidden =
themes.filter(({ name }) => name === theme)[0]?.isHidden ?? false;
const filteredFileContentsArray = fileContentsArray.filter(
({ theme: fileTheme }) => fileTheme === theme,
);
themes[index]["backgrounds"] = isThemeHidden
? []
: filteredFileContentsArray.map(({ src, fontColor }) =>
fontColor ? { src, fontColor } : { src },
);
});
const configJsonPath = path.join(dir, "output.config.json");
fs.writeFileSync(configJsonPath, JSON.stringify(config, null, 2));
return copy({
targets: filteredOrder.map((theme) => ({
src: path.join(sourceDirectory, theme),
dest: path.join(targetDirectory, backgroundsUri.path),
})),
hook: "writeBundle",
});
} else {
const dir = path.resolve();
const configJsonPath = path.join(dir, "output.config.json");
fs.writeFileSync(configJsonPath, JSON.stringify(config, null, 2));
}
}
function readFilesRecursively(dir: string) {
const { backgroundsUri, themes } = config as unknown as Config;
const fileContentsArray: Image[] = [];
const readFiles = (dir: string) => {
const files = fs.readdirSync(dir);
const order = themes
.filter(({ isHidden = false }) => !isHidden)
.map(({ name }) => name);
const sortedFiles = files.sort(
(a, b) => order.indexOf(a) - order.indexOf(b),
);
sortedFiles.forEach((file) => {
const filePath = path.join(dir, file);
const theme = dir.replace(path.join(backgroundsUri.path, path.sep), "");
if (fs.statSync(filePath).isDirectory()) {
readFiles(filePath);
} else if (isImageFile(filePath)) {
const item = file.split(".")?.[2]
? {
theme,
src: filePath,
fontColor: file.split(".")[1],
}
: {
theme,
src: filePath,
};
fileContentsArray.push(item);
}
});
};
readFiles(dir);
return fileContentsArray;
}
function isImageFile(filePath: string) {
const allowedExtensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"];
const extension = path.extname(filePath).toLowerCase();
return allowedExtensions.includes(extension);
}