KBaba7/llama.cpp
0
1import { defineConfig, PluginOption } from 'vite';2import react from '@vitejs/plugin-react';3import { viteSingleFile } from 'vite-plugin-singlefile';4import path from 'node:path';5import fs from 'node:fs';6import zlib from 'node:zlib';7 8/* eslint-disable */9 10const MAX_BUNDLE_SIZE = 1.5 * 1024 * 1024; // only increase when absolutely necessary11 12const GUIDE_FOR_FRONTEND = `13<!--14 This is a single file build of the frontend.15 It is automatically generated by the build process.16 Do not edit this file directly.17 To make changes, refer to the "Web UI" section in the README.18-->19`.trim();20 21const FRONTEND_PLUGINS = [react()];22 23const BUILD_PLUGINS = [24 ...FRONTEND_PLUGINS,25 viteSingleFile(),26 (function llamaCppPlugin() {27 let config: any;28 return {29 name: 'llamacpp:build',30 apply: 'build',31 async configResolved(_config: any) {32 config = _config;33 },34 writeBundle() {35 const outputIndexHtml = path.join(config.build.outDir, 'index.html');36 const content =37 GUIDE_FOR_FRONTEND + '\n' + fs.readFileSync(outputIndexHtml, 'utf-8');38 const compressed = zlib.gzipSync(Buffer.from(content, 'utf-8'), {39 level: 9,40 });41 42 // because gzip header contains machine-specific info, we must remove these data from the header43 // timestamp44 compressed[0x4] = 0;45 compressed[0x5] = 0;46 compressed[0x6] = 0;47 compressed[0x7] = 0;48 // OS49 compressed[0x9] = 0;50 51 if (compressed.byteLength > MAX_BUNDLE_SIZE) {52 throw new Error(53 `Bundle size is too large (${Math.ceil(compressed.byteLength / 1024)} KB).\n` +54 `Please reduce the size of the frontend or increase MAX_BUNDLE_SIZE in vite.config.js.\n`55 );56 }57 58 const targetOutputFile = path.join(59 config.build.outDir,60 '../../public/index.html.gz'61 );62 fs.writeFileSync(targetOutputFile, compressed);63 },64 } satisfies PluginOption;65 })(),66];67 68export default defineConfig({69 // @ts-ignore70 plugins: process.env.ANALYZE ? FRONTEND_PLUGINS : BUILD_PLUGINS,71 server: {72 proxy: {73 '/v1': 'http://localhost:8080',74 },75 },76});77 