@@ -12,22 +12,24 @@ const providerContext = {
1212}
1313
1414export 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 file of possibleFontFiles ) {
26+ registerFont ( file . replace ( assetsDir . dir , join ( assetsDir . dir , assetsDir . baseURL || '/' ) ) )
27+ }
2628 }
27- }
2829
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+ } )
3133
3234 // Update registry when files change
3335 nuxt . hook ( 'builder:watch' , ( event , relativePath ) => {
@@ -73,6 +75,8 @@ const NON_WORD_RE = /[^\w\d]+/g
7375
7476export const isFontFile = ( id : string ) => FONT_RE . test ( id )
7577
78+ // TODO: support without hyphen
79+ // TODO: support reading font metrics
7680const weightMap : Record < string , string > = {
7781 100 : 'thin' ,
7882 200 : 'extra-light' ,
@@ -86,7 +90,7 @@ const weightMap: Record<string, string> = {
8690}
8791
8892const 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' ] )
9094
9195const styles = [ 'italic' , 'oblique' ] as const
9296const STYLE_RE = createRegExp ( anyOf ( ...styles ) . groupedAs ( 'style' ) . before ( not . wordChar . or ( wordBoundary ) ) , [ 'i' ] )
0 commit comments