Repository navigation
feat(docs): update docs about fonts + proguard rules #1479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@callstack/repack": patch | ||
| --- | ||
|
|
||
| Ship consumer ProGuard rules for the Android library so release builds with R8 (`minifyEnabled true`) no longer fail with missing `com.google.errorprone.annotations` classes referenced by `nimbus-jose-jwt`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Applied to apps that depend on Re.Pack when R8/ProGuard is enabled (minifyEnabled true). | ||
|
|
||
| # nimbus-jose-jwt (used by code signing) bundles a shaded Gson that references | ||
| # Error Prone annotations. They are compile-time only and not on the runtime classpath. | ||
| -dontwarn com.google.errorprone.annotations.** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,6 @@ | |
| "inline-assets", | ||
| "remote-assets", | ||
| "svg", | ||
| "fonts", | ||
| "deploy" | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Using custom fonts | ||
|
|
||
| Font files (`ttf` and `otf`) are processed by the [Assets loader](/api/loaders/assets-loader) like any other asset, so you can `require` or `import` them from JavaScript. How you load the font afterwards depends on whether it is used by a JavaScript library or by native `<Text>` components. | ||
|
|
||
| ## Loading fonts from JavaScript | ||
|
|
||
| Libraries that load fonts at runtime from an asset reference, such as [React Native Skia](https://shopify.github.io/react-native-skia/), work without any additional configuration: | ||
|
|
||
| ```jsx | ||
| import { useFont } from '@shopify/react-native-skia'; | ||
|
|
||
| const font = useFont(require('./assets/fonts/Inter.ttf'), 24); | ||
| ``` | ||
|
|
||
| In development, the font is served by the development server. In release builds on Android, Re.Pack emits the font to both the `font` and `raw` resource directories and generates a `raw/keep.xml` file, so the font can be found by name and is kept when resource shrinking (`shrinkResources`) is enabled. See [Android resource directories](/api/loaders/assets-loader#android-resource-directories) for details. | ||
|
|
||
| ## Using fonts in native `<Text>` components | ||
|
|
||
| Requiring a font file doesn't register it with the platform, so it can't be used with the `fontFamily` style yet. This is the same as with Metro. Use one of the following approaches. | ||
|
|
||
| ### Link fonts natively (recommended) | ||
|
|
||
| Add the font files to the native projects so they are available in both debug and release builds: | ||
|
|
||
| - **Android**: place the files in `android/app/src/main/assets/fonts/`. The file name (without extension) becomes the `fontFamily` value. | ||
| - **iOS**: add the files to your app target in Xcode and list them under `UIAppFonts` in `Info.plist`. The font's family name becomes the `fontFamily` value. | ||
|
|
||
| ```jsx | ||
| <Text style={{ fontFamily: 'Inter' }}>Hello</Text> | ||
| ``` | ||
|
|
||
| ### Register fonts emitted by Re.Pack (Android release builds) | ||
|
|
||
| In Android release builds, fonts bundled by Re.Pack are available as `font` resources. A font is only bundled if it is `require`d or `import`ed somewhere in your JavaScript code. The resource name comes from the font's path relative to the project root, not to the file that requires it (e.g. `assets/fonts/Inter.ttf` becomes `assets_fonts_inter`). You can register these fonts in `MainApplication` with `ReactFontManager`: | ||
|
|
||
| ```js title="index.js" | ||
| // makes Re.Pack bundle the font, even if it's only used by native <Text> | ||
| require('./assets/fonts/Inter.ttf'); | ||
| ``` | ||
|
|
||
| ```kotlin title="MainApplication.kt" | ||
| import com.facebook.react.common.assets.ReactFontManager | ||
|
|
||
| override fun onCreate() { | ||
| super.onCreate() | ||
| loadReactNative(this) | ||
|
|
||
| val fontId = resources.getIdentifier("assets_fonts_inter", "font", packageName) | ||
| if (fontId != 0) { | ||
| ReactFontManager.getInstance().addCustomFont(this, "Inter", fontId) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| :::warning Release builds only | ||
| When using the development server, fonts are served over HTTP and are not part of the Android resources, so the resource lookup above returns `0` and the font falls back to the system font. Link the font natively if you need it in debug builds as well. | ||
|
|
||
| The lookup also returns `0` in release builds if the font isn't required from JavaScript. Because of the `fontId != 0` check, this fails silently and the text uses the system font. | ||
| ::: | ||
|
|
||
| ## Font collections (`.ttc`) | ||
|
|
||
| `ttc` files are not included in the default [asset extensions](/api/utils/constants#asset_extensions). To use them, add a rule that processes them with the Assets loader: | ||
|
|
||
| ```js title="rspack.config.cjs" | ||
| const Repack = require('@callstack/repack'); | ||
|
|
||
| module.exports = { | ||
| module: { | ||
| rules: [ | ||
| ...Repack.getAssetTransformRules(), | ||
| { | ||
| test: /\.ttc$/, | ||
| use: '@callstack/repack/assets-loader', | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| This is enough for libraries that load fonts from JavaScript, such as React Native Skia. Native `<Text>` on Android only looks up `.ttf` and `.otf` files in `assets/fonts/`, so a `.ttc` file can't be linked natively there. In Android release builds, you can still register the `font` resource emitted by Re.Pack with [`addCustomFont`](#register-fonts-emitted-by-repack-android-release-builds). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.