Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-terms-post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a case where font providers provided as class instances may not work when using the experimental Fonts API. It affected the local provider
9 changes: 8 additions & 1 deletion packages/astro/src/assets/fonts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const requiredFamilyAttributesSchema = z.object({
cssVariable: z.string(),
});

export const fontProviderSchema = z
const _fontProviderSchema = z
.object({
name: z.string(),
config: z.record(z.string(), z.any()).optional(),
Expand All @@ -36,6 +36,13 @@ export const fontProviderSchema = z
})
.strict();

// Using z.object directly makes zod remap the input, preventing
// the usage of class instances. Instead, we check if it matches
// the right shape and pass the original
export const fontProviderSchema = z.custom<FontProvider>((v) => {
return _fontProviderSchema.safeParse(v).success;
}, 'Invalid FontProvider object');

export const fontFamilySchema = z
.object({
...requiredFamilyAttributesSchema.shape,
Expand Down
17 changes: 17 additions & 0 deletions packages/astro/test/units/config/config-validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, it } from 'node:test';
import { stripVTControlCharacters } from 'node:util';
import { z } from 'zod';
import { fontProviders } from '../../../dist/assets/fonts/providers/index.js';
import { LocalFontProvider } from '../../../dist/assets/fonts/providers/local.js';
import { validateConfig as _validateConfig } from '../../../dist/core/config/validate.js';
import { formatConfigErrorMessage } from '../../../dist/core/messages.js';
import { envField } from '../../../dist/env/config.js';
Expand Down Expand Up @@ -534,6 +535,22 @@ describe('Config Validation', () => {
}),
);
});

it('should preserve FontProvider as a class instance', async () => {
const config = await validateConfig({
experimental: {
fonts: [
{
provider: fontProviders.local(),
name: 'Roboto',
cssVariable: '--font-roboto',
options: {},
},
],
},
});
assert.equal(config.experimental.fonts?.[0].provider instanceof LocalFontProvider, true);
});
});

describe('csp', () => {
Expand Down