Skip to content

Commit 61f5a0e

Browse files
committed
webui: vite embeds assets
1 parent 7f48486 commit 61f5a0e

File tree

1 file changed

+22
-46
lines changed

1 file changed

+22
-46
lines changed

tools/server/webui/vite.config.ts

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ const GUIDE_FOR_FRONTEND = `
1818

1919
const MAX_BUNDLE_SIZE = 2 * 1024 * 1024;
2020

21-
// true: embed KaTeX-fonts into index.html, false: don't embed fonts.
22-
const DO_EMBED_FONTS = true;
21+
/**
22+
* the maximum size of an embedded asset in bytes,
23+
* e.g. maximum size of embedded font (see node_modules/katex/dist/fonts/*.woff2)
24+
*/
25+
const MAX_ASSET_SIZE = 32000;
26+
27+
/** public/index.html.gz minified flag */
28+
const ENABLE_JS_MINIFICATION = true;
2329

2430
function llamaCppBuildPlugin() {
2531
return {
@@ -38,49 +44,18 @@ function llamaCppBuildPlugin() {
3844

3945
let content = readFileSync(indexPath, 'utf-8');
4046

41-
// Inline KaTeX fonts directly from node_modules
42-
const katexFonts = [
43-
'KaTeX_AMS-Regular',
44-
'KaTeX_Caligraphic-Bold',
45-
'KaTeX_Caligraphic-Regular',
46-
'KaTeX_Fraktur-Bold',
47-
'KaTeX_Fraktur-Regular',
48-
'KaTeX_Main-BoldItalic',
49-
'KaTeX_Main-Bold',
50-
'KaTeX_Main-Italic',
51-
'KaTeX_Main-Regular',
52-
'KaTeX_Math-BoldItalic',
53-
'KaTeX_Math-Italic',
54-
'KaTeX_SansSerif-Bold',
55-
'KaTeX_SansSerif-Italic',
56-
'KaTeX_SansSerif-Regular',
57-
'KaTeX_Script-Regular',
58-
'KaTeX_Size1-Regular',
59-
'KaTeX_Size2-Regular',
60-
'KaTeX_Size3-Regular',
61-
'KaTeX_Size4-Regular',
62-
'KaTeX_Typewriter-Regular'
63-
];
64-
65-
katexFonts.forEach((font) => {
66-
const fontPath = resolve('node_modules/katex/dist/fonts', font + '.woff2');
67-
let srcUrl = '';
68-
if (existsSync(fontPath)) {
69-
const fontContent = readFileSync(fontPath);
70-
if (DO_EMBED_FONTS) {
71-
// Replace both the original and hashed references
72-
// e.g. url(./KaTeX_Math-Italic.flOr_0UB.ttf) has to be replaces by a base64 data url.
73-
const bufData = Buffer.from(fontContent).toString('base64');
74-
srcUrl = `src:url(data:application/font-woff2;base64,${bufData}) format("woff2")`;
75-
}
76-
} else {
77-
console.log('Missing font file', fontPath);
78-
}
79-
content = content.replace(
80-
new RegExp(`src:(?:,?url\\(./${font}[.][^)]+\\) format\\("[a-z0-9]+"\\))+`),
81-
srcUrl
82-
);
83-
});
47+
// Remove non-embedded font-URLs of KaTeX.
48+
content = content.replace(
49+
new RegExp(/src:(?:,?url\(.\/KaTeX_[^)]+\) format\("[a-z0-9]+"\))+/, 'g'),
50+
''
51+
);
52+
53+
// Remove embedded ttf- and woff-fonts.
54+
// See ./node_modules/katex/src/styles/fonts.scss.
55+
content = content.replace(
56+
new RegExp(/,?url\(data:font\/(?:ttf|woff);base64,[^)]+\) format\("[a-z0-9]+"\)/, 'g'),
57+
''
58+
);
8459

8560
const faviconPath = resolve('static/favicon.svg');
8661
if (existsSync(faviconPath)) {
@@ -124,7 +99,8 @@ function llamaCppBuildPlugin() {
12499
export default defineConfig({
125100
plugins: [tailwindcss(), sveltekit(), devtoolsJson(), llamaCppBuildPlugin()],
126101
build: {
127-
minify: true // enable JS minification
102+
assetsInlineLimit: MAX_ASSET_SIZE,
103+
minify: ENABLE_JS_MINIFICATION
128104
},
129105
test: {
130106
projects: [

0 commit comments

Comments
 (0)