forked from hanzi-chai/hanzi-chai.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
126 lines (122 loc) · 3.18 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
/// <reference types="vitest" />
import path from "node:path";
import { defineConfig, type UserConfig, type PluginOption } from "vite";
import react from "@vitejs/plugin-react-swc";
import yaml from "@modyfi/vite-plugin-yaml";
import Pages from "vite-plugin-pages";
import wasm from "vite-plugin-wasm";
import { visualizer } from "rollup-plugin-visualizer";
import { chunkSplitPlugin } from "vite-plugin-chunk-split";
import cdn from "vite-plugin-cdn-import";
// vite.config.js
const wasmContentTypePlugin = {
name: "wasm-content-type-plugin",
configureServer(server: any) {
server.middlewares.use((req: any, res: any, next: any) => {
if (req.url.endsWith(".wasm")) {
res.setHeader("Content-Type", "application/wasm");
}
next();
});
},
};
export default defineConfig(({ mode }) => {
// https://vitejs.dev/config/
const sharedConfig: UserConfig = {
resolve: {
alias: {
"~/": `${path.resolve(__dirname, "src")}/`,
},
},
build: {
chunkSizeWarningLimit: 700,
outDir: `dist/${mode.toLowerCase()}`,
emptyOutDir: true,
reportCompressedSize: false,
},
define: {
APP_VERSION: JSON.stringify(process.env.npm_package_version),
},
optimizeDeps: {
exclude: ["libchai"],
},
plugins: [
wasmContentTypePlugin,
react({
// plugins: [["@swc-jotai/react-refresh", {}]],
}),
wasm(),
yaml(),
Pages({
importMode: "async",
}),
chunkSplitPlugin({
customSplitting: {
antd: [/node_modules\/antd/],
router: [/node_modules\/react-router/],
react: [/node_modules\/react(-dom)?\//],
immer: [/node_modules\/immer/],
mathjs: [/node_modules\/mathjs/],
yaml: [/node_modules\/js-yaml/],
reactflow: [/node_modules\/@reactflow/],
tools: [/node_modules\/styled-components/, /node_modules\/js-md5/],
},
}),
],
test: {
globals: true,
coverage: {
enabled: true,
provider: "v8",
reporter: ["text", "html"],
},
},
};
switch (mode) {
case "CF":
break;
case "BEX":
sharedConfig.plugins?.push(
visualizer({
brotliSize: true,
title: "打包产物分析",
filename: "dist/visualizer.html",
}) as PluginOption,
);
break;
case "PAGES":
sharedConfig.plugins?.push(
cdn({
prodUrl:
"https://registry.npmmirror.com/{name}/{version}/files/{path}",
modules: [
"react",
"react-dom",
"dayjs",
{
name: "js-yaml",
var: "jsyaml",
path: "dist/js-yaml.min.js",
},
{
name: "decimal.js",
var: "Decimal",
path: "decimal.js",
},
{
name: "mathjs",
var: "math",
path: "lib/browser/math.js",
},
{
name: "antd",
var: "antd",
path: "dist/antd-with-locales.js",
},
],
}),
);
break;
}
return sharedConfig;
});