perf: lower traffic usage through font subsetting in pixel-mode

This commit is contained in:
Erzbir
2025-12-03 14:20:59 +08:00
parent c6d4cf809c
commit b04b407f7f
60 changed files with 533 additions and 6 deletions
+26
View File
@@ -2,6 +2,7 @@ import esbuild from 'esbuild'
import {sassPlugin} from 'esbuild-sass-plugin'; import {sassPlugin} from 'esbuild-sass-plugin';
import {readdirSync} from 'fs'; import {readdirSync} from 'fs';
import {join} from 'path'; import {join} from 'path';
import * as fs from "node:fs";
async function build_main() { async function build_main() {
const srcfile = 'src/main.js'; const srcfile = 'src/main.js';
@@ -25,6 +26,31 @@ async function build_main() {
minify: true, minify: true,
sourcemap: false sourcemap: false
}) })
const tempEntry = "src/pixel.js"
const tempOutfile = "templates/assets/dist/pixel.js";
await esbuild.build({
entryPoints: [tempEntry],
bundle: true,
outfile: tempOutfile,
assetNames: '[name]',
plugins: [
sassPlugin(),
],
loader: {
'.woff': 'file',
'.woff2': 'file',
'.ttf': 'file',
},
format: 'esm',
target: 'es2020',
minify: true,
sourcemap: false
})
if (fs.existsSync(tempOutfile)) {
fs.unlinkSync(tempOutfile);
}
} }
+1
View File
@@ -0,0 +1 @@
import './styles/pixel.scss';
-6
View File
@@ -11,9 +11,3 @@
font-display: swap; font-display: swap;
font-style: normal; font-style: normal;
} }
@font-face {
font-family: 'Fusion Pixel 12px P zh_hans';
font-display: swap;
src: url("../fonts/fusion-pixel-12px-proportional-zh_hans.woff2") format('woff2');
}
File diff suppressed because one or more lines are too long
+21
View File
@@ -120,6 +120,27 @@
</style> </style>
/*/--> /*/-->
</th:block> </th:block>
<script no-pjax th:inline="javascript">
function appendPixelFont() {
const pixel_style = document.documentElement.getAttribute('pixel_style');
const link = document.createElement('link');
link.rel = 'stylesheet';
if (pixel_style === 'true') {
link.href = /*[[@{/assets/dist/pixel.css?v={version}(version=${theme.spec.version})}]]*/;
}
document.head.appendChild(link);
}
document.addEventListener('DOMContentLoaded', () => {
appendPixelFont();
document.getElementById('pixel-toggle').addEventListener('click', () => {
appendPixelFont();
})
});
</script>
</head> </head>
<body> <body>