10000 fix(local): scan for fonts in all public assets dirs · nuxt/fonts@6e7ae2b · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 6e7ae2b

Browse files
committed
fix(local): scan for fonts in all public assets dirs
1 parent d95da7b commit 6e7ae2b

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/providers/local.ts

+19-15
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@ const providerContext = {
1212
}
1313

1414
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+
}
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

7476
export const isFontFile = (id: string) => FONT_RE.test(id)
7577

78+
// TODO: support without hyphen
79+
// TODO: support reading font metrics
7680
const weightMap: Record<string, string> = {
7781
100: 'thin',
7882
200: 'extra-light',
@@ -86,7 +90,7 @@ const weightMap: Record<string, string> = {
8690
}
8791

8892
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'])
9094

9195
const styles = ['italic', 'oblique'] as const
9296
const STYLE_RE = createRegExp(anyOf(...styles).groupedAs('style').before(not.wordChar.or(wordBoundary)), ['i'])

test/providers/local.test.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { fileURLToPath } from 'node:url'
22
import fsp from 'node:fs/promises'
33

44
import type { Nuxt } from '@nuxt/schema'
5+
import type { Nitro, NitroOptions } from 'nitropack'
56
import { describe, expect, it } from 'vitest'
67
import { dirname, join } from 'pathe'
78

@@ -17,7 +18,7 @@ describe('local font provider', () => {
1718
'font.otf',
1819
'font.txt',
1920
].flatMap(l => [`public/${l}`, `layer/public/${l}`]))
20-
const provider = await setupFixture(['scanning', 'scanning/layer'])
21+
const provider = await setupFixture(['scanning/public', 'scanning/layer/public'])
2122
const faces = provider.resolveFontFaces('font', {
2223
fallbacks: [],
2324
weights: ['normal'],
@@ -55,7 +56,7 @@ describe('local font provider', () => {
5556
'public/MyFontbold-latin.ttf',
5657
'public/MyFontbold-latin.woff',
5758
])
58-
const provider = await setupFixture(['resolve-weights'])
59+
const provider = await setupFixture(['resolve-weights/public'])
5960
expect(provider.resolveFontFaces('MyFont', {
6061
fallbacks: [],
6162
weights: ['normal'],
@@ -130,13 +131,20 @@ type DeepPartial<T> = {
130131
[P in keyof T]?: DeepPartial<T[P]>
131132
}
132133

133-
async function setupFixture(layers: string[]) {
134+
async function setupFixture(publicAssetDirs: string[]) {
135+
let promise: Promise<unknown>
134136
const mockNuxt = {
135-
options: {
136-
_layers: layers.map(l => ({ cwd: join(fixturePath, l), config: { srcDir: join(fixturePath, l) } })),
137+
hook: (event: string, callback: (nitro: Nitro) => Promise<unknown>) => {
138+
if (event === 'nitro:init') {
139+
promise = callback({
140+
options: {
141+
publicAssets: publicAssetDirs.map(l => ({ dir: join(fixturePath, l), baseURL: '/', maxAge: 1 })) satisfies NitroOptions['publicAssets'],
142+
},
143+
} as Partial<Nitro> as Nitro)
144+
}
137145
},
138-
hook: () => {},
139146
} satisfies DeepPartial<Nuxt> as unknown as Nuxt
140147
await localProvider.setup({}, mockNuxt)
148+
await promise!
141149
return localProvider
142150
}

0 commit comments

Comments
 (0)
0