1
+ import type { CssNode , StyleSheet } from 'css-tree' ;
2
+ import { parse , walk } from 'css-tree' ;
1
3
import { transform } from 'esbuild' ;
2
4
import { createJiti } from 'jiti' ;
3
5
import MagicString from 'magic-string' ;
4
- import { parse , walk } from 'css-tree' ;
5
- import { dirname , extname } from 'pathe' ;
6
- import { hasProtocol , withLeadingSlash } from 'ufo' ;
6
+ import { dirname } from 'pathe' ;
7
+ import { withLeadingSlash } from 'ufo' ;
7
8
import {
8
9
createUnifont ,
9
10
type FontFaceData ,
10
11
type Provider ,
11
12
type RemoteFontSource ,
12
13
} from 'unifont' ;
14
+ import type {
15
+ FontFaceResolution ,
16
+ FontFamilyManualOverride ,
17
+ FontFamilyProviderOverride ,
18
+ ModuleOptions ,
19
+ Options ,
20
+ } from '../types' ;
21
+ import { setupPublicAssetStrategy } from './assets' ;
13
22
import {
14
- type GenericCSSFamily ,
23
+ addLocalFallbacks ,
24
+ extractEndOfFirstChild ,
15
25
extractFontFamilies ,
16
26
extractGeneric ,
17
- extractEndOfFirstChild ,
18
- addLocalFallbacks ,
27
+ type GenericCSSFamily ,
19
28
} from './parse' ;
20
29
import {
21
- formatToExtension ,
22
30
generateFontFace ,
23
31
generateFontFallbacks ,
24
- parseFont ,
25
32
relativiseFontSources ,
26
33
} from './render' ;
27
- import { hash } from 'ohash' ;
28
- import { filename } from 'pathe/utils' ;
29
- import type {
30
- ModuleOptions ,
31
- RawFontFaceData ,
32
- FontFamilyManualOverride ,
33
- FontFamilyProviderOverride ,
34
- Options ,
35
- FontFaceResolution ,
36
- } from '../types' ;
37
34
38
35
const defaultValues = {
39
36
weights : [ 400 ] ,
@@ -76,6 +73,9 @@ async function defaultResolveFontFace(
76
73
fallbackOptions ,
77
74
) {
78
75
const { module } = options ;
76
+
77
+ const { normalizeFontData } = await setupPublicAssetStrategy ( options ) ;
78
+
79
79
const override = module . families ?. find ( ( f ) => f . name === fontFamily ) ;
80
80
81
81
// This CSS will be injected in a separate location
@@ -90,46 +90,6 @@ async function defaultResolveFontFace(
90
90
return addLocalFallbacks ( fontFamily , font ) ;
91
91
}
92
92
93
- function normalizeFontData (
94
- faces : RawFontFaceData | FontFaceData [ ] ,
95
- ) : FontFaceData [ ] {
96
- // const assetsBaseURL = module.assets.prefix || "/fonts"; //TODO: Review this if it's necessary?
97
- // const renderedFontURLs = new Map<string, string>(); //TODO: Review this if it's necessary?
98
- const data : FontFaceData [ ] = [ ] ;
99
- for ( const face of Array . isArray ( faces ) ? faces : [ faces ] ) {
100
- data . push ( {
101
- ...face ,
102
- unicodeRange :
103
- face . unicodeRange === undefined || Array . isArray ( face . unicodeRange )
104
- ? face . unicodeRange
105
- : [ face . unicodeRange ] ,
106
- src : ( Array . isArray ( face . src ) ? face . src : [ face . src ] ) . map ( ( src ) => {
107
- const source = typeof src === 'string' ? parseFont ( src ) : src ;
108
- if (
109
- 'url' in source &&
110
- hasProtocol ( source . url , { acceptRelative : true } )
111
- ) {
112
- source . url = source . url . replace ( / ^ \/ \/ / , 'https://' ) ;
113
- const file = [
114
- // TODO: investigate why negative ignore pattern below is being ignored
115
- filename ( source . url . replace ( / \? .* / , '' ) ) . replace ( / ^ - + / , '' ) ,
116
- hash ( source ) +
117
- ( extname ( source . url ) || formatToExtension ( source . format ) || '' ) ,
118
- ]
119
- . filter ( Boolean )
120
- . join ( '-' ) ;
121
-
122
- // renderedFontURLs.set(file, source.url); //TODO: Review this if it's necessary?
123
- source . originalURL = source . url ;
124
- // source.url = joinURL(assetsBaseURL, file); //TODO: Review this if it's necessary?
125
- }
126
- return source ;
127
- } ) ,
128
- } ) ;
129
- }
130
- return data ;
131
- }
132
-
133
93
async function resolveFontFaceWithOverride (
134
94
fontFamily : string ,
135
95
override ?: FontFamilyManualOverride | FontFamilyProviderOverride ,
@@ -155,19 +115,16 @@ async function defaultResolveFontFace(
155
115
normalizedDefaults . fallbacks [ fallbackOptions ?. generic || 'sans-serif' ] ;
156
116
157
117
if ( override && 'src' in override ) {
158
- const fonts = addFallbacks (
159
- fontFamily ,
160
- normalizeFontData ( {
161
- src : override . src ,
162
- display : override . display ,
163
- weight : override . weight ,
164
- style : override . style ,
165
- } ) ,
166
- ) ;
118
+ const fonts = normalizeFontData ( {
119
+ src : override . src ,
120
+ display : override . display ,
121
+ weight : override . weight ,
122
+ style : override . style ,
123
+ } ) ;
167
124
168
125
return {
169
126
fallbacks,
170
- fonts,
127
+ fonts : addFallbacks ( fontFamily , fonts ) ,
171
128
} ;
172
129
}
173
130
@@ -267,7 +224,7 @@ export async function transformCSS(
267
224
opts : { relative ?: boolean } = { } ,
268
225
) {
269
226
const { fontless } = options ;
270
- const s = new MagicString ( code ) ;
227
+ const string = new MagicString ( code ) ;
271
228
272
229
const injectedDeclarations = new Set < string > ( ) ;
273
230
@@ -356,11 +313,11 @@ export async function transformCSS(
356
313
}
357
314
}
358
315
359
- s . prepend ( prefaces . join ( '' ) ) ;
316
+ string . prepend ( prefaces . join ( '' ) ) ;
360
317
361
318
if ( fallbackOptions && insertFontFamilies ) {
362
319
const insertedFamilies = fallbackMap . map ( ( f ) => `"${ f . name } "` ) . join ( ', ' ) ;
363
- s . prependLeft ( fallbackOptions . index , `, ${ insertedFamilies } ` ) ;
320
+ string . prependLeft ( fallbackOptions . index , `, ${ insertedFamilies } ` ) ;
364
321
}
365
322
}
366
323
@@ -433,7 +390,7 @@ export async function transformCSS(
433
390
434
391
await Promise . all ( promises ) ;
435
392
436
- return s ;
393
+ return string ;
437
394
}
438
395
439
396
async function resolveProviders ( _providers : ModuleOptions [ 'providers' ] = { } ) {
@@ -452,5 +409,6 @@ async function resolveProviders(_providers: ModuleOptions['providers'] = {}) {
452
409
} ) ;
453
410
}
454
411
}
412
+
455
413
return providers as Record < string , ( options : any ) => Provider > ;
456
414
}
0 commit comments