1
- import fsp from " node:fs/promises" ;
2
- import { hash } from " ohash" ;
3
- import { extname , join } from " pathe" ;
4
- import { filename } from " pathe/utils" ;
5
- import { hasProtocol , joinRelativeURL , joinURL } from " ufo" ;
6
- import type { FontFaceData } from " unifont" ;
7
- import { storage } from " ../cache" ;
8
- import type { Options , RawFontFaceData } from " ../types" ;
9
- import { formatToExtension , parseFont } from " ./render" ;
1
+ import fsp from ' node:fs/promises' ;
2
+ import { hash } from ' ohash' ;
3
+ import { extname , join } from ' pathe' ;
4
+ import { filename } from ' pathe/utils' ;
5
+ import { hasProtocol , joinRelativeURL , joinURL } from ' ufo' ;
6
+ import type { FontFaceData } from ' unifont' ;
7
+ import { storage } from ' ../cache' ;
8
+ import type { Options , RawFontFaceData } from ' ../types' ;
9
+ import { formatToExtension , parseFont } from ' ./render' ;
10
10
11
11
const renderedFontURLs = new Map < string , string > ( ) ;
12
12
13
13
export async function setupPublicAssetStrategy ( options : Options ) {
14
14
const { module } = options ;
15
15
16
- const assetsBaseURL = module . assets . prefix || " /fonts" ;
16
+ const assetsBaseURL = module . assets . prefix || ' /fonts' ;
17
17
18
18
function normalizeFontData (
19
- faces : RawFontFaceData | FontFaceData [ ]
19
+ faces : RawFontFaceData | FontFaceData [ ] ,
20
20
) : FontFaceData [ ] {
21
21
const data : FontFaceData [ ] = [ ] ;
22
22
for ( const face of Array . isArray ( faces ) ? faces : [ faces ] ) {
@@ -27,20 +27,20 @@ export async function setupPublicAssetStrategy(options: Options) {
27
27
? face . unicodeRange
28
28
: [ face . unicodeRange ] ,
29
29
src : ( Array . isArray ( face . src ) ? face . src : [ face . src ] ) . map ( ( src ) => {
30
- const source = typeof src === " string" ? parseFont ( src ) : src ;
30
+ const source = typeof src === ' string' ? parseFont ( src ) : src ;
31
31
if (
32
- " url" in source &&
32
+ ' url' in source &&
33
33
hasProtocol ( source . url , { acceptRelative : true } )
34
34
) {
35
- source . url = source . url . replace ( / ^ \/ \/ / , " https://" ) ;
35
+ source . url = source . url . replace ( / ^ \/ \/ / , ' https://' ) ;
36
36
const file = [
37
37
// TODO: investigate why negative ignore pattern below is being ignored
38
- filename ( source . url . replace ( / \? .* / , "" ) ) . replace ( / ^ - + / , "" ) ,
38
+ filename ( source . url . replace ( / \? .* / , '' ) ) . replace ( / ^ - + / , '' ) ,
39
39
hash ( source ) +
40
- ( extname ( source . url ) || formatToExtension ( source . format ) || "" ) ,
40
+ ( extname ( source . url ) || formatToExtension ( source . format ) || '' ) ,
41
41
]
42
42
. filter ( Boolean )
43
- . join ( "-" ) ;
43
+ . join ( '-' ) ;
44
44
45
45
renderedFontURLs . set ( file , source . url ) ;
46
46
source . originalURL = source . url ;
@@ -59,7 +59,7 @@ export async function setupPublicAssetStrategy(options: Options) {
59
59
60
60
const rollupBefore = async ( ) => {
61
61
for ( const [ filename , url ] of renderedFontURLs ) {
62
- const key = " data:fonts:" + filename ;
62
+ const key = ' data:fonts:' + filename ;
63
63
// Use storage to cache the font data between builds
64
64
let res = await storage . getItemRaw ( key ) ;
65
65
if ( ! res ) {
@@ -77,12 +77,12 @@ export async function setupPublicAssetStrategy(options: Options) {
77
77
78
78
await fsp . writeFile (
79
79
joinRelativeURL ( options . fontless . baseURL , assetsBaseURL , filename ) ,
80
- res
80
+ res ,
81
81
) ;
82
82
}
83
83
} ;
84
84
85
- options . hooks [ " rollup:before" ] = rollupBefore ;
85
+ options . hooks [ ' rollup:before' ] = rollupBefore ;
86
86
87
87
return {
88
88
normalizeFontData,
0 commit comments