Skip to content

Commit 24278f8

Browse files
committed
refactor font-preview
1 parent 882b12c commit 24278f8

File tree

3 files changed

+25
-48
lines changed

3 files changed

+25
-48
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ copyAnticheatToDev.sh
126126

127127
# ignore generated fonts
128128
frontend/src/webfonts-generated
129-
frontend/static/webfonts-preview
130129

131130
.turbo
132131
frontend/.env.sentry-build-plugin

frontend/vite-plugins/font-preview.ts

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,45 @@ const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = path.dirname(__filename);
1212

1313
/**
14-
* Generate a preview file from each font in `static/webfonts` into `static/webfonts-preview`.
14+
* Generate a preview file from each font in `static/webfonts` into `dist/webfonts-preview`.
1515
* A preview file only contains the characters needed to show the preview.
1616
* @returns
1717
*/
1818
export function fontPreview(): Plugin {
1919
return {
2020
name: "vite-plugin-webfonts-preview",
2121
apply: "build",
22-
async buildStart() {
22+
23+
async generateBundle() {
24+
const srcDir = __dirname + "/../static/webfonts";
2325
const start = performance.now();
2426
console.log("\nCreating webfonts preview...");
2527

26-
await generatePreviewFonts();
27-
28-
const end = performance.now();
29-
console.log(
30-
`Creating webfonts preview took ${Math.round(end - start)} ms`,
31-
);
32-
},
33-
};
34-
}
28+
for (const name of Object.keys(Fonts)) {
29+
const font = Fonts[name as KnownFontName];
30+
if (font.systemFont) continue;
3531

36-
async function generatePreviewFonts(debug: boolean = false): Promise<void> {
37-
const srcDir = __dirname + "/../static/webfonts";
38-
const targetDir = __dirname + "/../static/webfonts-preview";
39-
fs.mkdirSync(targetDir, { recursive: true });
32+
const includedCharacters =
33+
(font.display ?? name.replaceAll("_", " ")) + "Fontfamily";
4034

41-
for (const name of Object.keys(Fonts)) {
42-
const font = Fonts[name as KnownFontName];
43-
if (font.systemFont) continue;
35+
const fileName = font.fileName;
4436

45-
const includedCharacters =
46-
(font.display ?? name.replaceAll("_", " ")) + "Fontfamily";
37+
const fontFile = fs.readFileSync(srcDir + "/" + fileName);
38+
const subset = await subsetFont(fontFile, includedCharacters, {
39+
targetFormat: "woff2",
40+
});
4741

48-
const fileName = font.fileName;
42+
this.emitFile({
43+
type: "asset",
44+
fileName: `webfonts-preview/${fileName}`,
45+
source: subset,
46+
});
47+
}
4948

50-
await generateSubset(
51-
srcDir + "/" + fileName,
52-
targetDir + "/" + fileName,
53-
includedCharacters,
54-
);
55-
if (debug) {
49+
const end = performance.now();
5650
console.log(
57-
`Processing ${name} with file ${fileName} to display "${includedCharacters}".`,
51+
`Creating webfonts preview took ${Math.round(end - start)} ms`,
5852
);
59-
}
60-
}
61-
}
62-
63-
async function generateSubset(
64-
source: string,
65-
target: string,
66-
includedCharacters: string,
67-
): Promise<void> {
68-
const font = fs.readFileSync(source);
69-
const subset = await subsetFont(font, includedCharacters, {
70-
targetFormat: "woff2",
71-
});
72-
fs.writeFileSync(target, subset);
73-
}
74-
//detect if we run this as a main
75-
if (import.meta.url.endsWith(process.argv[1] as string)) {
76-
void generatePreviewFonts(true);
53+
},
54+
};
7755
}

frontend/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function getPlugins({
8181
const plugins: PluginOption[] = [
8282
envConfig({ isDevelopment, clientVersion, env }),
8383
languageHashes({ skip: isDevelopment }),
84+
fontPreview(),
8485
versionFile({ clientVersion }),
8586
checker({
8687
oxlint: {
@@ -97,7 +98,6 @@ function getPlugins({
9798
const devPlugins: PluginOption[] = [Inspect()];
9899

99100
const prodPlugins: PluginOption[] = [
100-
fontPreview(),
101101
fontawesomeSubset(),
102102
ViteMinifyPlugin(),
103103
VitePWA({

0 commit comments

Comments
 (0)