@@ -11,67 +11,45 @@ const __filename = fileURLToPath(import.meta.url);
1111const __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 */
1818export 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}
0 commit comments