rsbuild.config.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { readFileSync } from "node:fs";
  2. import { fileURLToPath } from "node:url";
  3. import { defineConfig } from "@rsbuild/core";
  4. import { pluginReact } from "@rsbuild/plugin-react";
  5. import { pluginTailwindcss } from "@rsbuild/plugin-tailwindcss";
  6. const isDevelopment = process.env.NODE_ENV === "development";
  7. const DEV_PORT = Number(process.env.OSS_DEV_PORT ?? 5180);
  8. const DEV_PROXY_PATH = "/api-gateway";
  9. const gatewayTarget = process.env.OSS_GATEWAY_BASE;
  10. const apiBase = process.env.ENV_KLT_API_BASE_URL;
  11. /**
  12. * 项目空间(`end_user` 档)登录上下文:`auth/password@v1` 除 `scope_type/scope_id` 外
  13. * 还须带 `project_id/tenant_id/env/stage`,缺一即 11103;且 `challenge/verify@v1` 必须带
  14. * **同一份**上下文,否则 12006「challenge_token 与登录上下文不匹配」。
  15. *
  16. * 本地开发时,未配置的标识可从建号台账 temp/seoh_provision.json 读取。
  17. * ⚠️ 该台账同时含 `auth_key`/`auth_code` 两枚凭证,这里按字段白名单只读 tenant_id/
  18. * project_id,⛔ 整体读入或内联进产物。可用 OSS_SEOH_* 覆盖。
  19. */
  20. function seoCtx() {
  21. let j: { tenant_id?: string; project_id?: string } = {};
  22. if (isDevelopment) {
  23. try {
  24. j = JSON.parse(readFileSync(fileURLToPath(new URL("../temp/seoh_provision.json", import.meta.url)), "utf-8"));
  25. } catch {
  26. /* 台账缺失时留空,由登录页给出提示。 */
  27. }
  28. }
  29. return {
  30. tenant_id: process.env.OSS_SEOH_TENANT_ID ?? String(j.tenant_id ?? ""),
  31. project_id: process.env.OSS_SEOH_PROJECT_ID ?? String(j.project_id ?? ""),
  32. env: process.env.OSS_SEOH_ENV ?? (isDevelopment ? "nonprod" : "prod"),
  33. stage: process.env.OSS_SEOH_STAGE ?? (isDevelopment ? "dev" : "prod"),
  34. };
  35. }
  36. export default defineConfig({
  37. plugins: [pluginReact(), pluginTailwindcss()],
  38. source: {
  39. entry: {
  40. index: "./src/main.tsx",
  41. },
  42. define: {
  43. __SEO_CTX__: JSON.stringify(seoCtx()),
  44. "import.meta.env.ENV_KLT_API_BASE_URL": JSON.stringify(apiBase ?? ""),
  45. },
  46. },
  47. server: {
  48. port: DEV_PORT,
  49. ...(isDevelopment && gatewayTarget && apiBase === DEV_PROXY_PATH
  50. ? {
  51. host: "127.0.0.1",
  52. proxy: {
  53. [DEV_PROXY_PATH]: {
  54. target: gatewayTarget,
  55. changeOrigin: true,
  56. // 仅本地代理到开发网关的 HTTPS 请求跳过证书校验。
  57. secure: false,
  58. pathRewrite: { "^/api-gateway": "" },
  59. },
  60. },
  61. }
  62. : {}),
  63. },
  64. });