Skip to content

[google_fonts] Add google_fonts_lite file to allow tree-shaking of the other huge files#11433

Open
TheCarpetMerchant wants to merge 3 commits intoflutter:mainfrom
TheCarpetMerchant:main
Open

[google_fonts] Add google_fonts_lite file to allow tree-shaking of the other huge files#11433
TheCarpetMerchant wants to merge 3 commits intoflutter:mainfrom
TheCarpetMerchant:main

Conversation

@TheCarpetMerchant
Copy link
Copy Markdown

Adds a google_fonts_lite.dart generated file, which contains a map of fontFamily to fonts variants and a getFont function that simply forwards the call to the internal googleFontsTextStyle function which does the downloading and loading of the font.

List which issues are fixed by this PR. You must list at least one issue.
flutter/flutter#184337

I have not done documentation, testing etc because this PR is for discussion purposes. I'd like approval of the concept before doing the paperwork.

The idea is to provide a lite version of the GoogleFonts class. This lite version does not depend on any of the other generated code, which is absolutely huge and takes up a massive amount of space (8MB on android for v8.0.0). By using this lite version only, the compiler is able to tree-shake all that additionnal code while you retain the ability to invoke any font from the package via getFont.

For me this brings the package's footprint from 8MB/target platform to less than a 1MB/target when building a release apk.

@flutter-dashboard
Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@github-actions github-actions bot added p: google_fonts triage-framework Should be looked at in framework triage labels Apr 4, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a 'lite' version of the Google Fonts generator to support tree-shaking by creating a GoogleFontsLite class. Key changes include updating the generator to process a new template, making the GoogleFontsFile constructor const, and adding the google_fonts_lite.tmpl file. Review feedback recommends fixing a typo in the template comments, declaring the GoogleFontsLite class as abstract with a static const map, using display names for font keys, and implementing safer error handling in the getFont method to replace the current null-assertion operator.

// That is used to allow tree-shaking to remove all of the _parts files.
// If you only call GoogleFontsLite.fontsMap or GoogleFontsLite.loadFont(),
// the code in the GoogleFonts class and its parts classes is never called.
// WHat is called is googleFontsTextStyle, which attempts to load a font if not already loaded.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Typo: "WHat" should be "What".

// What is called is googleFontsTextStyle, which attempts to load a font if not already loaded.

Comment on lines +12 to +21
class GoogleFontsLite {
static Map<String, Map<GoogleFontsVariant, GoogleFontsFile>> fontsMap = {
{{#fontEntries}}
'{{fontFamily}}': {
{{#fontUrls}}
const GoogleFontsVariant(fontWeight: FontWeight.w{{variantWeight}}, fontStyle: FontStyle.{{variantStyle}},) : const GoogleFontsFile('{{hash}}', {{length}},),
{{/fontUrls}}
},
{{/fontEntries}}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Consider making GoogleFontsLite an abstract class to prevent instantiation, as it only contains static members. Additionally, the fontsMap should be declared as static const for better efficiency. Using fontFamilyDisplay (the actual font name with spaces) as the map key is also recommended for better compatibility with font fallback and consistency with the main GoogleFonts API.

abstract class GoogleFontsLite {
  static const Map<String, Map<GoogleFontsVariant, GoogleFontsFile>> fontsMap = {
    {{#fontEntries}}
      '{{fontFamilyDisplay}}': {
      {{#fontUrls}}
      const GoogleFontsVariant(fontWeight: FontWeight.w{{variantWeight}}, fontStyle: FontStyle.{{variantStyle}},) : const GoogleFontsFile('{{hash}}', {{length}},),
      {{/fontUrls}}
      },
    {{/fontEntries}}
  };

Comment on lines +23 to +25
static TextStyle getFont(String fontFamily) {
return googleFontsTextStyle(fontFamily: fontFamily, fonts: fontsMap[fontFamily]!);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The getFont method uses the null-assertion operator !, which will cause a runtime crash if an unknown font family is requested. It is safer to check for the existence of the font and throw a more descriptive error.

  static TextStyle getFont(String fontFamily) {
    final fonts = fontsMap[fontFamily];
    if (fonts == null) {
      throw ArgumentError('No font family with name $fontFamily was found.');
    }
    return googleFontsTextStyle(fontFamily: fontFamily, fonts: fonts);
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: google_fonts triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant