-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathvite.config.ts
More file actions
301 lines (269 loc) · 7.39 KB
/
vite.config.ts
File metadata and controls
301 lines (269 loc) · 7.39 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { execSync } from "child_process";
import * as http from "http";
import * as https from "https";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { visualizer } from "rollup-plugin-visualizer";
import { ConfigEnv, defineConfig, loadEnv, PluginOption, UserConfig } from "vite";
import viteCompression from "vite-plugin-compression";
import eslintPlugin from "vite-plugin-eslint";
import { createHtmlPlugin } from "vite-plugin-html";
import { createStyleImportPlugin } from "vite-plugin-style-import";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { wrapperEnv } from "./src/utils/getEnv";
const DEFAULT_BACKEND_ORIGIN = "http://127.0.0.1:8080";
const BACKEND_PROBE_PATHS = ["/api/admin/probe", "/api/admin/isLogined"];
const BACKEND_PROBE_SIGNATURE = "paicoding-port-for-admin";
const BACKEND_PROBE_TIMEOUT = 1200;
const WELL_KNOWN_BACKEND_PORTS = [8080, 9201, 8081, 8082, 8083, 8084, 8090, 8888, 9999];
function normalizeLocalOrigin(value?: string) {
if (!value) return "";
try {
const url = new URL(value);
if (!["127.0.0.1", "localhost", "0.0.0.0"].includes(url.hostname)) return "";
const hostname = url.hostname === "0.0.0.0" ? "127.0.0.1" : url.hostname;
return `${url.protocol}//${hostname}${url.port ? `:${url.port}` : ""}`;
} catch {
return "";
}
}
function getListeningPorts() {
try {
const output = execSync("lsof -nP -iTCP -sTCP:LISTEN", {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"]
});
const javaPorts: number[] = [];
const otherPorts: number[] = [];
output
.split("\n")
.slice(1)
.forEach(line => {
const trimmedLine = line.trim();
if (!trimmedLine) return;
const matchedPort = trimmedLine.match(/:(\d+)\s+\(LISTEN\)$/);
if (!matchedPort) return;
const port = Number(matchedPort[1]);
if (!Number.isInteger(port)) return;
const command = trimmedLine.split(/\s+/)[0]?.toLowerCase();
if (command === "java") {
javaPorts.push(port);
return;
}
otherPorts.push(port);
});
return [...new Set([...javaPorts, ...otherPorts])];
} catch {
return [];
}
}
function parseProbeResponse(body: string, fallbackOrigin: string) {
try {
const payload = JSON.parse(body);
if (
payload?.status?.code === 0 &&
payload?.result?.signature === BACKEND_PROBE_SIGNATURE &&
payload?.result?.app === "paicoding" &&
payload?.result?.module === "admin"
) {
return normalizeLocalOrigin(payload?.result?.host) || fallbackOrigin;
}
if (payload?.status?.code === 0 && typeof payload?.result === "boolean") {
return fallbackOrigin;
}
} catch {
return "";
}
return "";
}
function requestProbe(url: URL, fallbackOrigin: string): Promise<string> {
const requestClient = url.protocol === "https:" ? https : http;
return new Promise(resolve => {
let settled = false;
const finish = (matchedOrigin: string) => {
if (settled) return;
settled = true;
resolve(matchedOrigin);
};
const req = requestClient.request(
url,
{
method: "GET",
timeout: BACKEND_PROBE_TIMEOUT,
headers: {
Accept: "application/json"
}
},
res => {
let body = "";
res.setEncoding("utf8");
res.on("data", chunk => {
if (body.length < 4096) {
body += chunk;
}
});
res.on("end", () => {
finish(parseProbeResponse(body, fallbackOrigin));
});
}
);
req.on("timeout", () => {
req.destroy();
finish("");
});
req.on("error", () => finish(""));
req.end();
});
}
async function probeBackendOrigin(origin: string) {
for (const probePath of BACKEND_PROBE_PATHS) {
const matchedOrigin = await requestProbe(new URL(probePath, origin), origin);
if (matchedOrigin) {
return matchedOrigin;
}
}
return "";
}
async function resolveBackendOrigin(configuredDomain: string) {
const configuredOrigin = normalizeLocalOrigin(configuredDomain);
const candidateOrigins = new Set<string>();
if (configuredOrigin) {
candidateOrigins.add(configuredOrigin);
}
getListeningPorts().forEach(port => {
candidateOrigins.add(`http://127.0.0.1:${port}`);
});
WELL_KNOWN_BACKEND_PORTS.forEach(port => {
candidateOrigins.add(`http://127.0.0.1:${port}`);
});
candidateOrigins.add(DEFAULT_BACKEND_ORIGIN);
for (const origin of candidateOrigins) {
const matchedOrigin = await probeBackendOrigin(origin);
if (matchedOrigin) {
return matchedOrigin;
}
}
return configuredOrigin || DEFAULT_BACKEND_ORIGIN;
}
// @see: https://vitejs.dev/config/
/** @type {import('vite').UserConfig} */
export default defineConfig(async (mode: ConfigEnv): Promise<UserConfig> => {
const env = loadEnv(mode.mode, process.cwd());
const viteEnv = wrapperEnv(env);
const shouldAutoDiscoverBackend = ["development", "test"].includes(mode.mode) && viteEnv.VITE_API_URL.startsWith("/");
const configuredDomain = viteEnv.VITE_DOMAIN;
const backendOrigin = shouldAutoDiscoverBackend ? await resolveBackendOrigin(configuredDomain) : configuredDomain;
if (shouldAutoDiscoverBackend) {
viteEnv.VITE_DOMAIN = backendOrigin;
console.info(`[vite] PaiCoding backend target: ${backendOrigin}`);
}
const plugins: PluginOption[] = [
react(),
createHtmlPlugin({
inject: {
data: {
title: viteEnv.VITE_GLOB_APP_TITLE
}
}
}),
createSvgIconsPlugin({
iconDirs: [resolve(process.cwd(), "src/assets/icons")],
symbolId: "icon-[dir]-[name]"
}),
createStyleImportPlugin({
libs: [
{
libraryName: "antd",
esModule: true,
resolveStyle: (name: any) => {
return `antd/es/${name}/style/index`;
}
}
]
}),
eslintPlugin()
];
if (viteEnv.VITE_REPORT) {
plugins.push(visualizer() as unknown as PluginOption);
}
if (viteEnv.VITE_BUILD_GZIP) {
plugins.push(
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: "gzip",
ext: ".gz"
}) as unknown as PluginOption
);
}
return {
define: shouldAutoDiscoverBackend
? {
"import.meta.env.VITE_DOMAIN": JSON.stringify(backendOrigin)
}
: undefined,
// base: "/",
// alias config
resolve: {
alias: {
"@": resolve(__dirname, "./src")
}
},
// global css
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
additionalData: `@import "@/styles/var.less";`
}
}
},
// server config
server: {
host: "127.0.0.1", // 服务器主机名,如果允许外部访问,可设置为"0.0.0.0"
port: viteEnv.VITE_PORT,
open: viteEnv.VITE_OPEN,
cors: true,
// https: false,
// 代理跨域(mock 不需要配置,这里只是个示例)
proxy: {
"/admin": {
target: backendOrigin,
changeOrigin: true,
rewrite: path => path.replace(/^\/api/, "")
},
"/api/admin": {
target: backendOrigin,
changeOrigin: true
}
}
},
plugins,
esbuild: {
pure: viteEnv.VITE_DROP_CONSOLE ? ["console.log", "debugger"] : []
},
base: "./",
// build configure
build: {
outDir: "dist",
// esbuild 打包更快,但是不能去除 console.log,去除 console 使用 terser 模式
minify: "esbuild",
// minify: "terser",
// terserOptions: {
// compress: {
// drop_console: viteEnv.VITE_DROP_CONSOLE,
// drop_debugger: true
// }
// },
rollupOptions: {
output: {
// Static resource classification and packaging
chunkFileNames: "assets/js/[name]-[hash].js",
entryFileNames: "assets/js/[name]-[hash].js",
assetFileNames: "assets/[name]-[hash].[ext]"
}
}
}
};
});