-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathvite.config.ts
504 lines (461 loc) · 17.1 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
* Copyright (C) Online-Go.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Plugin, defineConfig, ResolvedConfig, ViteDevServer, ProxyOptions } from "vite";
import react from "@vitejs/plugin-react";
import fixReactVirtualized from "esbuild-plugin-react-virtualized";
import path from "path";
import { promises as fs } from "fs";
import { IncomingMessage } from "http";
import http from "http";
import checker from "vite-plugin-checker";
import atImport from "postcss-import";
import inline_svg from "postcss-inline-svg";
import autoprefixer from "autoprefixer";
import { nodePolyfills } from "vite-plugin-node-polyfills";
const OGS_I18N_BUILD_MODE = (process.env.OGS_I18N_BUILD_MODE || "false").toLowerCase() === "true";
let OGS_BACKEND = process.env.OGS_BACKEND || "BETA";
OGS_BACKEND = OGS_BACKEND.toUpperCase();
//OGS_BACKEND = "BETA";
//OGS_BACKEND = "PRODUCTION";
//OGS_BACKEND = "LOCAL";
const backend_url =
OGS_BACKEND === "BETA"
? "https://beta.online-go.com"
: OGS_BACKEND === "PRODUCTION"
? "https://online-go.com"
: //: "http://localhost:1080"; // local
"http://127.0.0.1:1080"; // local
const proxy: Record<string, ProxyOptions> = {};
// REST api proxies
for (const base_path of [
"/api",
"/termination-api",
"/merchant",
"/billing",
"/sso",
"/oauth2",
"/complete",
"/disconnect",
"/OGSScoreEstimator",
"/oje",
"/firewall",
"/__debug__",
]) {
proxy[base_path] = {
target: backend_url,
changeOrigin: true,
rewrite: (path: string) => {
return backend_url + path;
},
};
}
// termination-server websocket proxy
proxy["^/$"] = {
target: backend_url + "/",
changeOrigin: true,
ws: true,
rewriteWsOrigin: true,
rewrite: (path: string) => {
//console.info("termination-server websocket -> ", backend_url + "/");
return backend_url + path;
},
bypass: (req, res, _options) => {
if (res) {
// res is undefined for websockets, which is the only thing we want
// to proxy. For other requests, namely serving index.html, we want
// vite to handle it, so returning the url here does that.
return req.url;
}
return undefined;
},
};
export default defineConfig({
root: "src",
build: !OGS_I18N_BUILD_MODE
? {
// This is our production build
outDir: "../dist",
sourcemap: true,
minify: "terser",
chunkSizeWarningLimit: 1024 * 1024 * 1.5,
rollupOptions: {
input: {
ogs: "src/main.tsx",
},
output: {
assetFileNames: "[name].[ext]",
entryFileNames: "[name].js",
},
},
}
: {
// This build section is for our i18n system which run xgettext
// on the non-minified bundle
outDir: "../i18n/build/",
sourcemap: true,
minify: false,
target: "es2015",
chunkSizeWarningLimit: 1024 * 1024 * 99,
rollupOptions: {
input: {
ogs: "src/main.tsx",
},
output: {
format: "commonjs",
assetFileNames: "[name].strings.[ext]",
entryFileNames: "[name].strings.js",
manualChunks: (id: string) => {
if (id.includes("node_modules")) {
return "vendor";
}
return;
},
},
},
},
/*
* NOTE: We don't use vite css processing for our production builds because
* it doesn't support generating sourcemaps in production as of 2025-01-07
*
* For production, see compile-css.js, which should always kept in sync
* with this config.
*/
css: {
postcss: {
plugins: [atImport(), inline_svg(), autoprefixer()],
},
preprocessorMaxWorkers: true,
devSourcemap: true,
},
define: {
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
},
plugins: [
ogs_vite_middleware(),
react(),
process.env.NODE_ENV !== "production" ? nodePolyfills() : null,
// checker relative directory is src/
//
!OGS_I18N_BUILD_MODE
? checker({
typescript: {
tsconfigPath:
process.env.NODE_ENV === "production"
? "tsconfig.json"
: "../tsconfig.json",
},
eslint: {
useFlatConfig: true,
lintCommand: "eslint .",
},
overlay: {
initialIsOpen: true,
},
enableBuild: true,
})
: null,
],
resolve: {
alias: Object.assign(
{
"@": path.resolve(__dirname, "src"),
},
process.env.NODE_ENV !== "production"
? {
"source-map-js": "source-map",
}
: ({} as any),
),
},
optimizeDeps: {
esbuildOptions: {
plugins: [fixReactVirtualized],
},
},
server: {
port: 8080,
host: true,
proxy,
allowedHosts: true,
hmr: {
path: "/__vite_hmr",
overlay: true,
},
},
});
/*
* For historical reasons, OGS uses a custom index.html template system
*/
function ogs_vite_middleware(): Plugin {
let config: ResolvedConfig;
//let command: "build" | "serve" = "build";
return {
name: "ogs-process-index-template",
/*
config(_config, env) {
command = env.command;
},
*/
configResolved(resolvedConfig) {
config = resolvedConfig;
},
/**
* for dev
* if SPA, just use template and write script main.{js,ts} for /{entry}.html
* if MPA, check pageName(default is index) and write /${pagesDir}/{pageName}/${entry}.html
*/
configureServer(server: ViteDevServer) {
return () => {
/* Handle our custom index template, serve it for anything that doesn't look like a file */
server.middlewares.use(async (req, res, next) => {
const url = req.originalUrl || "";
// if not html, next it.
const should_serve_index =
url.endsWith(".html") || url === "/" || /^.*\/[^.]*$/.test(url);
if (!should_serve_index) {
return next();
}
let content = await fs.readFile(
path.resolve(config.root, "index.html"),
"utf-8",
);
content = await ogs_process_template(content, req);
// using vite's transform html function to add basic html support
//content = await server.transformIndexHtml?.(req.url, content, req.originalUrl);
content = await server.transformIndexHtml?.(url, content, req.originalUrl);
res.end(content);
});
/* Handle some non vite static files */
server.middlewares.use(async (req, res, next) => {
const url = req.originalUrl;
function send_response(
body: string,
content_type: string = "application/javascript",
) {
res.setHeader("Content-Type", `${content_type}; charset=utf-8`);
res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
res.setHeader("Pragma", "no-cache");
res.setHeader("Expires", "0");
res.setHeader("Content-Length", Buffer.byteLength(body));
res.statusCode = 200;
res.end(body);
return;
}
if (url === "/goban.js") {
console.info(`GET ${url} -> node_modules/goban/build/goban.js`);
send_response(
await fs.readFile("node_modules/goban/build/goban.js", {
encoding: "utf-8",
}),
);
return;
}
if (url === "/goban.js.map") {
console.info(`GET ${url} -> node_modules/goban/build/goban.js.map`);
send_response(
await fs.readFile("node_modules/goban/build/goban.js.map", {
encoding: "utf-8",
}),
);
return;
}
if (url === "/manifest.json") {
const manifest = {
short_name: "OGS",
name: "Online-Go.com",
icons: [
{
src: "https://cdn.online-go.com/icons/android-chrome-192x192.png",
type: "image/png",
sizes: "192x192",
},
],
start_url: "/",
display: "standalone",
scope: "/",
background_color: "#eeeeee",
theme_color: "#000000",
};
send_response(JSON.stringify(manifest), "application/json");
return;
}
if (url?.endsWith("ogs.css")) {
// blank, vite deals with css stuff until production
send_response("", "text/css");
return;
}
if (url?.endsWith("vendor.js")) {
console.info(`GET ${url} -> node_modules/vendor.js`);
send_response("");
return;
}
if (url?.startsWith("/locale")) {
/*
console.info(
`GET ${url} -> http://storage.googleapis.com/ogs-site-files/dev${url}`,
);
*/
const options = {
hostname: "storage.googleapis.com",
port: 80,
path: "/ogs-site-files/dev" + url,
method: "GET",
};
const outgoing_request = http.request(options, (outgoing_response) => {
outgoing_response.setEncoding("utf8");
let data = "";
outgoing_response.on("data", (chunk) => {
data += chunk.toString();
});
outgoing_response.on("end", () => {
send_response(data);
});
});
outgoing_request.on("error", (e) => {
res.statusCode = 500;
res.end(e.message);
});
outgoing_request.end();
return;
}
return next();
});
};
},
};
}
async function ogs_process_template(content: string, req: IncomingMessage): Promise<string> {
const use_local_goban = (await fs.lstat("node_modules/goban")).isSymbolicLink();
const url = new URL(req.url || "", `http://${req.headers.host}`);
const port = url.port;
const supported_languages = JSON.parse(
await fs.readFile("i18n/languages.json", { encoding: "utf-8" }),
);
const package_json = JSON.parse(await fs.readFile("package.json", { encoding: "utf-8" }));
const replaced = content.replace(/[{][{]\s*(\w+)\s*[}][}]/g, (_, parameter) => {
switch (parameter) {
case "CDN_SERVICE": {
// We run within a docker container on 8080 but are served out of 443 so no
// need to specify a port, just use the same hostname.
if (url.hostname?.indexOf("uffizzi") >= 0) {
return `//${url.hostname}/`;
}
return `//${url.hostname}:${port}/`;
}
case "LIVE_RELOAD": {
if (url.hostname?.indexOf("uffizzi") >= 0) {
// no need for live reloading on uffizzi
return ``;
}
//return `<script async src="//${url.hostname}:35701/livereload.js"></script>`;
return ``;
}
case "MIN":
return "";
case "PAGE_TITLE":
return "Play Go at online-go.com!";
case "PAGE_DESCRIPTION":
return "Online-Go.com is the best place to play the game of Go online. Our community supported site is friendly, easy to use, and free, so come join us and play some Go!";
case "PAGE_KEYWORDS":
return "Go, Baduk, Weiqi, OGS, Online-Go.com";
case "PAGE_LANGUAGE":
return getPreferredLanguage(req, supported_languages);
case "OG_TITLE":
return "";
case "OG_URL":
return "";
case "OG_IMAGE":
return "";
case "OG_DESCRIPTION":
return "";
case "SUPPORTED_LANGUAGES":
return JSON.stringify(supported_languages);
case "AMEX_CLIENT_ID":
/* cspell: disable-next-line */
return "kvEB9qXE6jpNUv3fPkdbWcPaZ7nQAXyg";
case "AMEX_ENV":
return "qa";
case "RELEASE":
return "";
case "VERSION":
return "";
case "LANGUAGE_VERSION":
return "";
case "VENDOR_HASH_DOTJS":
return "js";
case "VERSION_DOTJS":
//return "js";
return "";
case "OGS_VERSION_HASH_DOTJS":
return "js";
case "VERSION_DOTCSS":
return "css";
case "LANGUAGE_VERSION_DOTJS":
return "js";
case "GOBAN_JS": {
if (use_local_goban) {
return `/goban.js`;
} else {
return `https://cdn.online-go.com/goban/${package_json.devDependencies.goban.substr(
1,
)}/goban.js`;
}
}
case "EXTRA_CONFIG":
//return `<script>window['websocket_host'] = "${server_url}";</script>`;
return ``;
}
return "{{" + parameter + "}}";
});
return replaced;
}
/* Detect preferred language */
function isSupportedLanguage(lang: string, supported_languages: any) {
if (!lang) {
return null;
}
lang = lang.toLowerCase();
if (lang in supported_languages) {
return lang;
}
lang = lang.replace(/-[a-z]+/, "");
if (lang in supported_languages) {
return lang;
}
return null;
}
function getPreferredLanguage(req: IncomingMessage, supported_languages: any) {
let languages = ["en"];
try {
languages = (req.headers?.["accept-language"] || "")
.split(",")
.map((s) => s.replace(/;q=.*/, "").trim().toLowerCase());
} catch (e) {
console.trace(e);
}
try {
for (let i = 0; i < languages.length; ++i) {
const lang = isSupportedLanguage(languages[i], supported_languages);
if (lang) {
return lang;
}
}
} catch (e) {
console.trace(e);
}
return "en";
}