Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat(fonts): vite plugin #13093

Merged
merged 56 commits into from
Feb 7, 2025
Merged

feat(fonts): vite plugin #13093

merged 56 commits into from
Feb 7, 2025

Conversation

florian-lefebvre
Copy link
Member

@florian-lefebvre florian-lefebvre commented Jan 30, 2025

Changes

  • Closes PLT-2756
  • Adds a basic Fonts component
  • Adds the virtual module and most of the "core features"
    • resolving providers
    • resolving fonts data
    • caching
    • dev/build handling
  • Updates the config

Testing

Manual + automated

Docs

N/A

@github-actions github-actions bot removed the pkg: example Related to an example package (scope) label Feb 3, 2025
@florian-lefebvre florian-lefebvre marked this pull request as ready for review February 4, 2025 15:19
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Florian to start this. I left a bunch of questions, the majority of them aren't blockers, but there's one thing that requires attention: the use of ModuleLoader in dev, which should be in line with how the other features work.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more nits, and I probably spot a bug. Nothing is a blocker, great work @florian-lefebvre !


export const GOOGLE_PROVIDER_NAME = 'google';

// TODO: https://github.com/unjs/unifont/issues/108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider, that the issue has been stuck for two weeks, maybe we should consider adding a warning or something for users.

*/
type PreloadData = Array<{
/**
* Absolute link to a font file, eg. /_astro/fonts/abc.woff
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example doesn't point to an absolute file path. Usually it's something that starts with /Users or C:\

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean an absolute link, not an absolute filepath. That was to emphasize the fact it starts with /, not ./ or anything else. if you have a better name, I'll take it!

Comment on lines +42 to +45
/**
* A font type, eg. woff2, woff, ttf...
*/
type: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extension? Or, if it's really a type, maybe we could consider an enum

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to call it type as it's used in the type prop of the link element in the Fonts component. And I think it's not a 1-1 match, I'm using a naive approach atm i'll add a todo to investigate

subsets: family.subsets ?? DEFAULTS.subsets,
fallbacks: family.fallbacks ?? DEFAULTS.fallbacks,
};
const { fonts: fontsData, fallbacks } = await resolveFont(family.name, resolvedOptions, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some variable here is unused

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll use fallbacks later

// 1. local and has a name
// 2. remote and has an url
// Once we have the key, it's safe to access the related source property
const key = 'name' in source ? 'name' : 'url';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: explain why name has precedence over url

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't matter which would should be accessed first, it's purely arbitrary

if (!hashToUrlMap.has(hash)) {
hashToUrlMap.set(hash, value);
const segments = hash.split('.');
// It's safe, there's at least 1 member in the array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// It's safe, there's at least 1 member in the array
// It's safe, there's at least 1 member in the array because the hash has a pattern <HASH>.<EXTENSION>

Comment on lines +197 to +198
const type = segments.at(-1)!;
if (segments.length === 1 || !FONT_TYPES.includes(type)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment on line 196 is in contradiction with segments.length === 1. The check will always be false because segments will always have two items: the hash and the extension/type. This is a bug?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested manually this part and basically consider those cases (assuming a split on dots):

  • "" => [""] (length 1 invalid)
  • "abc" => ["abc"] (length 1 invalid)
  • "abc.def" => ["abc", "def"] (length 2 valid, last member not matching font type)
  • "abc.woff" => ["abc", "woff"] (length 2 valid, last member matching font type)
  • "abc.def.ghi" => ["abc", "def", "ghi"] (length 3 valid, last member not matching font type)
  • "abc.def.ttf" => ["abc", "def", "ttf"] (length 3 valid, last member matching font type)

@florian-lefebvre florian-lefebvre merged commit 3c18360 into feat/fonts Feb 7, 2025
15 checks passed
@florian-lefebvre florian-lefebvre deleted the feat/fonts-plugin branch February 7, 2025 13:16
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants