Team Ai
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 21d agoView on Hugging Face
0likes1.2kdownloads
vite-plugin-relativize-base.ts67 linesDownload Raw Back to scripts
1import { BUILD_CONFIG } from '../src/lib/constants/pwa.constants';2import { existsSync, readFileSync, writeFileSync } from 'node:fs';3import { resolve } from 'path';4import type { Plugin } from 'vite';5 6let processed = false;7 8const OUTPUT_DIR = process.env.LLAMA_UI_OUT_DIR ?? BUILD_CONFIG.OUTPUT_DIR;9 10function rewrite(path: string, pairs: [string, string][]): void {11	if (!existsSync(path)) {12		return;13	}14 15	const text = readFileSync(path, 'utf-8');16 17	let out = text;18 19	for (const [from, to] of pairs) {20		out = out.split(from).join(to);21	}22 23	if (out !== text) {24		writeFileSync(path, out, 'utf-8');25	}26}27 28/**29 * Relativize SvelteKit absolute base refs so the build is relocatable under any subpath.30 *31 * SvelteKit bakes root absolute /_app/ paths into the SPA fallback because paths.relative32 * does not apply to a depth agnostic fallback page. Rewriting to ./_app/ lets a plain33 * recursive copy of the output into /any/subdir/ resolve assets against the document URL.34 * Runs after adapter-static writes index.html and the PWA plugin writes sw.js, deferred the35 * same way as buildInfoPlugin so the emitted files exist.36 */37export function relativizeBasePlugin(): Plugin {38	return {39		apply: 'build',40		closeBundle() {41			setTimeout(() => {42				try {43					if (processed) return;44 45					processed = true;46 47					const outDir = resolve(OUTPUT_DIR);48 49					// index.html: modulepreload, stylesheet and bootstrap import reference "/_app/50					rewrite(resolve(outDir, 'index.html'), [['"/_app/', '"./_app/']]);51 52					// sw.js: the only absolute entries are the navigate fallback precache key and handler53					rewrite(resolve(outDir, 'sw.js'), [54						['{url:"/"', '{url:"./"'],55						['createHandlerBoundToURL("/"', 'createHandlerBoundToURL("./"']56					]);57 58					console.log('Relativized base refs in index.html and sw.js');59				} catch (error) {60					console.error('Failed to relativize base refs:', error);61				}62			}, 100);63		},64		name: 'llamacpp:relativize-base'65	};66}67