@@ -12,22 +12,24 @@ const providerContext = {
12
12
}
13
13
14
14
export default {
15
- async setup ( _options , nuxt ) {
16
- // Scan for all font files in public directories
17
- for ( const layer of nuxt . options . _layers ) {
18
- const publicDir = join ( layer . config . srcDir || layer . cwd , layer . config . dir ?. public || 'public' )
19
- const possibleFontFiles = await globby ( '**/*.{ttf,woff,woff2,eot,otf}' , {
20
- absolute : true ,
21
- cwd : publicDir ,
22
- } )
23
- providerContext . rootPaths . push ( withTrailingSlash ( publicDir ) )
24
- for ( const file of possibleFontFiles ) {
25
- registerFont ( file )
15
+ setup ( _options , nuxt ) {
16
+ // TODO: rework when providers can respond with font metric data
17
+ // Scan for all font files in public asset directories
18
+ nuxt . hook ( 'nitro:init' , async ( nitro ) => {
19
+ for ( const assetsDir of nitro . options . publicAssets ) {
20
+ const possibleFontFiles = await globby ( '**/*.{ttf,woff,woff2,eot,otf}' , {
21
+ absolute : true ,
22
+ cwd : assetsDir . dir ,
23
+ } )
24
+ providerContext . rootPaths . push ( withTrailingSlash ( assetsDir . dir ) )
25
+ for ( const
8000
file of possibleFontFiles ) {
26
+ registerFont ( file . replace ( assetsDir . dir , join ( assetsDir . dir , assetsDir . baseURL || '/' ) ) )
27
+ }
26
28
}
27
- }
28
29
29
- // Sort rootPaths so we resolve to most specific path first
30
- providerContext . rootPaths = providerContext . rootPaths . sort ( ( a , b ) => b . length - a . length )
30
+ // Sort rootPaths so we resolve to most specific path first
31
+ providerContext . rootPaths = providerContext . rootPaths . sort ( ( a , b ) => b . length - a . length )
32
+ } )
31
33
32
34
// Update registry when files change
33
35
nuxt . hook ( 'builder:watch' , ( event , relativePath ) => {
@@ -73,6 +75,8 @@ const NON_WORD_RE = /[^\w\d]+/g
73
75
74
76
export const isFontFile = ( id : string ) => FONT_RE . test ( id )
75
77
78
+ // TODO: support without hyphen
79
+ // TODO: support reading font metrics
76
80
const weightMap : Record < string , string > = {
77
81
100 : 'thin' ,
78
82
200 : 'extra-light' ,
@@ -86,7 +90,7 @@ const weightMap: Record<string, string> = {
86
90
}
87
91
88
92
const weights = Object . entries ( weightMap ) . flatMap ( e => e ) . filter ( r => r !== 'normal' )
89
- const WEIGHT_RE = createRegExp ( anyOf ( ...weights ) . groupedAs ( 'weight' ) . after ( not . digit ) . before ( not . digit . or ( wordBoundary ) ) , [ 'i' ] )
93
+ const WEIGHT_RE = createRegExp ( anyOf ( ...new Set ( [ ... weights , ... weights . map ( w => w . replace ( '-' , '' ) ) ] ) ) . groupedAs ( 'weight' ) . after ( not . digit ) . before ( not . digit . or ( wordBoundary ) ) , [ 'i' ] )
90
94
91
95
const styles = [ 'italic' , 'oblique' ] as const
92
96
const STYLE_RE = createRegExp ( anyOf ( ...styles ) . groupedAs ( 'style' ) . before ( not . wordChar . or ( wordBoundary ) ) , [ 'i' ] )
0 commit comments