diff --git a/.changeset/font-data-subset-field.md b/.changeset/font-data-subset-field.md new file mode 100644 index 000000000000..9db6b23ea5f6 --- /dev/null +++ b/.changeset/font-data-subset-field.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Adds a `subset` field to the `FontData` type exposed via `fontData` from `astro:assets`. When using multiple font subsets (e.g., `subsets: ["latin", "korean"]`), each font data entry now includes the subset name, making it possible to distinguish between font entries for different subsets that share the same weight and style. diff --git a/packages/astro/src/assets/fonts/core/collect-font-data.ts b/packages/astro/src/assets/fonts/core/collect-font-data.ts index d78cb0824c32..6ecd189357a0 100644 --- a/packages/astro/src/assets/fonts/core/collect-font-data.ts +++ b/packages/astro/src/assets/fonts/core/collect-font-data.ts @@ -14,6 +14,7 @@ export function collectFontData( fontData.push({ weight: renderFontWeight(data.weight), style: data.style, + subset: data.meta?.subset, src: data.src .filter((src) => 'url' in src) .map((src) => ({ diff --git a/packages/astro/src/assets/fonts/types.ts b/packages/astro/src/assets/fonts/types.ts index 3b02b45348ae..b9241ca34a4c 100644 --- a/packages/astro/src/assets/fonts/types.ts +++ b/packages/astro/src/assets/fonts/types.ts @@ -247,6 +247,7 @@ export interface FontData { src: Array<{ url: string; format?: string; tech?: string }>; weight?: string; style?: string; + subset?: string; } /** diff --git a/packages/astro/test/units/assets/fonts/core.test.ts b/packages/astro/test/units/assets/fonts/core.test.ts index a0e204f3f12a..24ae741330d0 100644 --- a/packages/astro/test/units/assets/fonts/core.test.ts +++ b/packages/astro/test/units/assets/fonts/core.test.ts @@ -1182,6 +1182,7 @@ describe('fonts core', () => { }, ], style: 'normal', + subset: undefined, weight: '400', }, { @@ -1193,6 +1194,7 @@ describe('fonts core', () => { }, ], style: 'italic', + subset: undefined, weight: '500', }, ], @@ -1206,6 +1208,59 @@ describe('fonts core', () => { }, ], style: 'normal', + subset: undefined, + weight: '400', + }, + ], + }, + ); + }); + + it('collectFontData() includes subset from meta', () => { + assert.deepStrictEqual( + collectFontData([ + { + family: { cssVariable: '--font' }, + fonts: [ + { + weight: '400', + style: 'normal', + src: [{ url: 'latin-400.woff2', format: 'woff2' }], + meta: { subset: 'latin' }, + }, + { + weight: '400', + style: 'normal', + src: [{ url: 'korean-400.woff2', format: 'woff2' }], + meta: { subset: 'korean' }, + }, + ], + }, + ]), + { + '--font': [ + { + src: [ + { + format: 'woff2', + tech: undefined, + url: 'latin-400.woff2', + }, + ], + style: 'normal', + subset: 'latin', + weight: '400', + }, + { + src: [ + { + format: 'woff2', + tech: undefined, + url: 'korean-400.woff2', + }, + ], + style: 'normal', + subset: 'korean', weight: '400', }, ], diff --git a/packages/astro/test/units/assets/fonts/e2e.test.ts b/packages/astro/test/units/assets/fonts/e2e.test.ts index d877c1f47b9c..7440f29c697c 100644 --- a/packages/astro/test/units/assets/fonts/e2e.test.ts +++ b/packages/astro/test/units/assets/fonts/e2e.test.ts @@ -170,6 +170,7 @@ describe('Fonts E2E', () => { }, ], style: 'normal', + subset: 'latin', weight: '700', }, { @@ -181,6 +182,7 @@ describe('Fonts E2E', () => { }, ], style: 'italic', + subset: 'latin', weight: '700', }, ], @@ -194,6 +196,7 @@ describe('Fonts E2E', () => { }, ], style: 'normal', + subset: undefined, weight: '400', }, ], @@ -306,6 +309,7 @@ describe('Fonts E2E', () => { }, ], style: 'normal', + subset: 'latin', weight: '500', }, { @@ -317,6 +321,7 @@ describe('Fonts E2E', () => { }, ], style: 'italic', + subset: 'latin', weight: '700', }, ], @@ -330,6 +335,7 @@ describe('Fonts E2E', () => { }, ], style: 'normal', + subset: undefined, weight: '400', }, ],