File size: 3,978 Bytes
c023610
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import viteBasicSSLPlugin from "@vitejs/plugin-basic-ssl";
import viteReactPlugin from "@vitejs/plugin-react";
import dotenv from "dotenv";
import getGitCommitHash from "helper-git-hash";
import { visualizer } from "rollup-plugin-visualizer";
import { defineConfig } from "vite";
import { cacheServerHook } from "./server/cacheServerHook";
import { compressionServerHook } from "./server/compressionServerHook";
import { crossOriginServerHook } from "./server/crossOriginServerHook";
import { internalApiEndpointServerHook } from "./server/internalApiEndpointServerHook";
import { searchEndpointServerHook } from "./server/searchEndpointServerHook";
import { getSearchToken, regenerateSearchToken } from "./server/searchToken";
import { statusEndpointServerHook } from "./server/statusEndpointServerHook";
import { validateAccessKeyServerHook } from "./server/validateAccessKeyServerHook";

dotenv.config({ path: [".env", ".env.example"] });

export default defineConfig(({ command }) => {
  if (command === "build") regenerateSearchToken();

  return {
    root: "./client",
    define: {
      VITE_SEARCH_TOKEN: JSON.stringify(getSearchToken()),
      VITE_BUILD_DATE_TIME: Date.now(),
      VITE_COMMIT_SHORT_HASH: JSON.stringify(getGitCommitHash({ short: true })),
      VITE_ACCESS_KEYS_ENABLED: JSON.stringify(
        Boolean(process.env.ACCESS_KEYS),
      ),
      VITE_ACCESS_KEY_TIMEOUT_HOURS: JSON.stringify(
        Number(process.env.ACCESS_KEY_TIMEOUT_HOURS) || 0,
      ),
      VITE_WEBLLM_DEFAULT_F16_MODEL_ID: JSON.stringify(
        process.env.WEBLLM_DEFAULT_F16_MODEL_ID,
      ),
      VITE_WEBLLM_DEFAULT_F32_MODEL_ID: JSON.stringify(
        process.env.WEBLLM_DEFAULT_F32_MODEL_ID,
      ),
      VITE_WLLAMA_DEFAULT_MODEL_ID: JSON.stringify(
        process.env.WLLAMA_DEFAULT_MODEL_ID,
      ),
      VITE_INTERNAL_API_ENABLED: JSON.stringify(
        Boolean(process.env.INTERNAL_OPENAI_COMPATIBLE_API_BASE_URL),
      ),
      VITE_INTERNAL_API_NAME: JSON.stringify(
        process.env.INTERNAL_OPENAI_COMPATIBLE_API_NAME,
      ),
      VITE_DEFAULT_INFERENCE_TYPE: JSON.stringify(
        process.env.DEFAULT_INFERENCE_TYPE,
      ),
    },
    server: {
      host: process.env.HOST,
      port: process.env.PORT ? Number(process.env.PORT) : undefined,
      hmr: {
        port: process.env.HMR_PORT ? Number(process.env.HMR_PORT) : undefined,
      },
    },
    preview: {
      host: process.env.HOST,
      port: process.env.PORT ? Number(process.env.PORT) : undefined,
    },
    build: {
      target: "esnext",
      chunkSizeWarningLimit: 5000,
    },
    plugins: [
      process.env.BASIC_SSL === "true" ? viteBasicSSLPlugin() : undefined,
      viteReactPlugin(),
      {
        name: "configure-server-compression",
        configureServer: compressionServerHook,
        configurePreviewServer: compressionServerHook,
      },
      {
        name: "configure-server-cross-origin-isolation",
        configureServer: crossOriginServerHook,
        configurePreviewServer: crossOriginServerHook,
      },
      {
        name: "configure-server-search-endpoint",
        configureServer: searchEndpointServerHook,
        configurePreviewServer: searchEndpointServerHook,
      },
      {
        name: "configure-server-status-endpoint",
        configureServer: statusEndpointServerHook,
        configurePreviewServer: statusEndpointServerHook,
      },
      {
        name: "configure-server-cache",
        configurePreviewServer: cacheServerHook,
      },
      {
        name: "configure-server-validate-access-key",
        configureServer: validateAccessKeyServerHook,
        configurePreviewServer: validateAccessKeyServerHook,
      },
      {
        name: "configure-server-internal-api-endpoint",
        configureServer: internalApiEndpointServerHook,
        configurePreviewServer: internalApiEndpointServerHook,
      },
      visualizer({
        filename: "vite-build-stats.html",
      }),
    ],
  };
});