-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.mts
286 lines (250 loc) · 7.67 KB
/
vite.config.mts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import fs from "fs";
import path from "path";
import YAML from "yaml";
import { globSync } from "glob";
import hash from "hash.js";
import deepmerge from "deepmerge";
import { defineConfig, loadEnv } from "vite";
import preact from "@preact/preset-vite";
import pixiUrlPatch from "./plugins/pixi-url-patch";
console.log("building...");
export default ({ mode }) => {
const viteEnv = loadEnv(mode, process.cwd());
const isProd = mode === "production";
const isDev = !isProd;
// buildtime
console.log("buildtime updating...");
const dest = path.resolve(__dirname, "src", "buildtime.ts");
const destYaml = path.resolve(__dirname, "external", "yaml", "buildtime.yml");
const code = fs.readFileSync(dest, { encoding: "utf-8" })
.toString()
.replace("export default ", "return ");
const prev = new Function(code)();
const buildNo = prev.build + 1;
fs.writeFileSync(
dest,
(() => {
const dt = new Date();
return `// eslint-disable-next-line\nexport default ${JSON.stringify({
time: dt.getTime(),
build: buildNo,
})}`;
})(),
"utf-8",
);
fs.writeFileSync(
destYaml,
YAML.stringify(buildNo),
"utf-8",
);
// yaml hash
if (isProd) {
(() => {
console.log("yaml hash updating...");
interface DBHashType {
[K: string]: string | DBHashType;
}
const yamlDir = path.resolve(__dirname, "external", "yaml");
const list = (() => {
const baseDir = path.resolve(__dirname, "external", "yaml");
const globPath = path.join(baseDir, "**", "*.yml");
return globSync(globPath.replace(/\\/g, "/"))
.filter(f => !/[/\\]buildtime.yml$/.test(f))
.map(f => {
const rel = path.relative(baseDir, f).replace(/\\/g, "/");
return `!/${rel.substring(0, rel.length - 4)}`;
});
})();
let outs: DBHashType = {};
list.forEach(item => {
const _item = item.substring(2);
const file = path.resolve(yamlDir, `${_item}.yml`);
if (!fs.existsSync(file)) return;
const tree = ((value: string) => {
const parts = _item.split("/");
const root: DBHashType = {};
let target = root;
for (let i = 0; i < parts.length; i++) {
const p = parts[i];
if (i === parts.length - 1) // end of parts
target[p] = value;
else
target = target[p] = {};
}
return root;
})(
hash.sha1()
.update(fs.readFileSync(file, "utf-8"))
.digest("hex")
.substring(0, 8),
);
outs = deepmerge(outs, tree);
});
const keys: string[] = [];
JSON.stringify(outs, (k, v) => (keys.push(k), v), "\t");
keys.sort();
const output = [
"// Content automatically generated",
"export interface DBHashType { [K: string]: string | DBHashType; }",
`export default ${JSON.stringify(outs, keys, "\t")} as DBHashType;`,
].join("\n");
fs.writeFileSync(
path.resolve(__dirname, "src", "libs", "Loader", "hash.ts"),
output,
"utf-8",
);
})();
} else
console.log(`skip hash update - on ${mode}`);
const prependData = `${[
"@charset \"UTF-8\";",
"@use \"sass:color\";",
"@use \"sass:math\";",
"@use \"sass:list\";",
"@use \"sass:map\";",
`$NODE_ENV: "${mode}";`,
`$LOCALHOST: "${viteEnv.VITE_LOCALHOST || ""}:${viteEnv.VITE_ASSET_PORT}";`,
`@import "${path.resolve(__dirname, "src", "themes", "base").replace(/\\/g, "/")}";`,
].join("\n")}\n`;
console.log("vite building...");
return defineConfig({
esbuild: {
jsxFactory: "h",
jsxFragment: "Fragment",
// jsxInject: `import { h, Fragment } from "preact";`,
logOverride: {
"this-is-undefined-in-esm": "silent",
},
},
optimizeDeps: {
include: [],
},
build: {
assetsDir: "build",
reportCompressedSize: false,
modulePreload: { polyfill: false },
minify: isProd,
sourcemap: isDev,
rollupOptions: {
output: {
inlineDynamicImports: false,
manualChunks (id) {
// entry
if (
id.includes("/src/index.") ||
id.includes("/src/app/")
) return undefined;
// vendor
if (id.includes("/node_modules/bootstrap")) return "vendor.bootstrap";
if (id.includes("/node_modules/react")) return "vendor.react";
if (id.includes("/node_modules/@esotericsoftware/")) return "vendor.spine";
if (id.includes("/node_modules/@popperjs/")) return "vendor.popperjs";
if (id.includes("/node_modules/graphlib/")) return "vendor.graphlib";
if (id.includes("/node_modules/lodash/")) return "vendor.lodash";
// not used anymore
if (id.includes("/node_modules/swiper/")) return "vendor.swiper";
if (
id.includes("/node_modules/pixi") ||
id.includes("/node_modules/@pixi/") ||
id.includes("/node_modules/@turf/")
) return "vendor.pixi";
if (
id.includes("/node_modules/@reactflow/") ||
id.includes("/node_modules/@tisoap/") ||
id.includes("/node_modules/@dagrejs/") ||
id.includes("/node_modules/d3-") ||
id.includes("/node_modules/pathfinding/")
) return "vendor.flow";
if (id.includes("/node_modules/")) return "vendor";
// components/bootstrap-icon/
if (id.includes("/src/components/bootstrap-icon/")) return "components.icon";
// types & libs & loader hash -> base
if (id.includes("/src/types/")) return "base";
if (id.includes("/src/libs/")) return "base";
// external -> each file
if (id.includes("/src/external/")) {
const _ = "/src/external/";
const idx = id.indexOf(_) + _.length;
const _name = id.substring(idx);
if (_name.indexOf("/") >= 0)
return `external.${_name.substring(0, _name.indexOf("/"))}`;
else
return `external.${_name.substring(0, _name.lastIndexOf("."))}`;
}
// components
if (
id.includes("/src/components/locale/") ||
id.includes("/src/components/Loader/") ||
id.includes("/src/components/redirect/")
) return "components.base";
if (id.includes("/src/components/bootstrap-")) return "components.bootstrap";
if (id.includes("/src/components/buff-")) return "components.buff";
if (id.includes("/src/components/drop-")) return "components.drop";
if (id.includes("/src/components/equip-")) return "components.equip";
if (id.includes("/src/components/roguelike-")) return "components.roguelike";
if (id.includes("/src/components/skill-")) return "components.skill";
if (id.includes("/src/components/unit-")) return "components.unit";
if (id.includes("/src/components/popup/")) return "components.popup";
if (id.includes("/src/components/")) return "components";
// // routes
if (id.includes("/src/routes/changelog/changelog/")) {
const y = id.replace(/.*\/src\/routes\/changelog\/changelog\/([0-9]+).*/g, "$1");
return `routes.changelog.${y}`;
}
if (id.includes("/src/routes/changelog/")) return "routes.changelog";
if (id.includes("/src/routes/")) {
const y = id.replace(/.*\/src\/routes\/([^/]+)\/?.*/g, "$1");
return `routes.${y}`;
}
return "chunk";
},
},
},
},
server: {
fs: {
allow: [__dirname],
},
},
css: {
preprocessorOptions: {
// @ts-ignore
css: {
charset: false,
},
sass: {
charset: false,
additionalData: prependData,
},
scss: {
charset: false,
additionalData: prependData,
},
},
},
plugins: [
preact(),
pixiUrlPatch(),
],
resolve: {
alias: [
{
find: "@/",
replacement: `${path.resolve(__dirname, "src")}/`,
},
{
find: "!/",
replacement: `${path.resolve(__dirname, "node_modules")}/`,
},
{
find: "react",
replacement: "preact/compat",
},
{
find: "react-dom",
replacement: "preact/compat",
},
],
},
});
};