@callstack/react-native-asset

中文文档
3.1.0发布于 2024/03/12ISCCommonJS近 30 天下载 2.4万

Linking and unlinking of assets in your react-native app, works for fonts and sounds

原始使用文档
文档目录10 节
  1. Link and unlink assets to your react-native project with ea…
  2. Advantages
  3. Usage
  4. Explanation
  5. Parameters
  6. Font assets linking and usage
  7. Android
  8. iOS
  9. Migrating from 2.x
  10. Backward compatability

react-native-asset

Advantages

  • react-native link only supports font files, this tool supports all assets.
  • Unlinking is automatic when you delete an asset, with react-native link, you need to unlink the files manually.
  • Proper link (and unlink) for mp3 (to use with react-native-sound) and ttf files.

Usage

  • Install

    npm install -g @callstack/react-native-asset
    # or yarn
    yarn global add @callstack/react-native-asset
    
  • Add assets to your react-native.config.js as you would with react-native link

    ...
     assets: [
        "./src/font",
        "./src/mp3",
      ];
    
  • Add platform-specific assets to your react-native.config.js like so:

    ...
    assets: [
      "./src/mp3",
    ],
    iosAssets: [
      "./src/font/ios",
    ],
    androidAssets: [
      "./src/font/android",
    ],
    
  • Run the command and linking + unlinking is automatic!

    react-native-asset
    

Explanation

With react-native link you have to unlink the files manually, which is hard work.
Instead this library writes link-assets-manifest.json to the root of android and ios folders to keep track of the files which it added, for later removing it for you if missing from your assets!

Parameters

  • -p, --path - path to project, defaults to cwd.
  • -a, --assets - assets paths, for example react-native-asset -a ./src/font ./src/mp3.
  • -ios-a, --ios-assets - ios assets paths, will disable android linking.
  • -android-a, --android-assets - android assets paths, will disable ios linking.
  • -n-u, --no-unlink - won't unlink assets which no longer exists, not recommended.

Font assets linking and usage

Android

Font assets are linked in Android by using XML resources. For instance, if you add the Lato font to your project, it will generate a lato.xml file in android/app/src/main/res/font/ folder with all the font variants that you added. It will also add a method call in MainApplication.kt or MainApplication.java file in order to register the custom font during the app initialization. It will look something like this:

// other imports

import com.facebook.react.common.assets.ReactFontManager // <- imports ReactFontManager.

class MainApplication : Application(), ReactApplication {

  // other methods

  override fun onCreate() {
    super.onCreate()
    ReactFontManager.getInstance().addCustomFont(this, "Lato", R.font.lato) // <- registers the custom font.
    // ...
  }
}

In this case, Lato is what you have to set in the fontFamily style of your Text component. To select the font variant e.g. weight and style, use fontWeight and fontStyle styles respectively.

<Text style={{ fontFamily: 'Lato', fontWeight: '700', fontStyle: 'italic' }}>Lato Bold Italic</Text>

iOS

Font assets are linked in iOS by editing project.pbxproj and Info.plist files. To use the font in your app, you can a combination of fontFamily, fontWeight and fontStyle styles in the same way you would use for Android. In case you didn't link your font assets in Android and you are not sure which value you have to set in fontFamily style, you can use Font Book app in your Mac to find out the correct value by looking the Family Name property.

Migrating from 2.x

If you have already linked font assets in your Android project, when running this tool it will relink your fonts to use XML resources for them. This migration will allow you to use your fonts in the code the same way you would use it for iOS. Please update your code to use fontFamily, fontWeight and fontStyle styles correctly.

Backward compatability

  • to use react-native 0.59 and below, use version 1.1.4

版本与发布记录

@callstack/react-native-asset 共发布 2 个版本,首次发布于 2023/11/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.0latest2024/03/122 年前
3.0.02023/11/222 年前

关于 @callstack/react-native-asset 的常见问题

@callstack/react-native-asset 是什么?

Linking and unlinking of assets in your react-native app, works for fonts and sounds

如何安装 @callstack/react-native-asset?

在项目目录运行 npm install @callstack/react-native-asset,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react-native(>=0.60.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 react-native-asset,可以用 npx react-native-asset 直接运行。

@callstack/react-native-asset 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/callstack__react-native-asset 是否存在。

@callstack/react-native-asset 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@callstack/react-native-asset 可以商用吗?

@callstack/react-native-asset 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@callstack/react-native-asset 的下载量有多少?

根据 npm 官方下载统计,@callstack/react-native-asset 在 2026/10/09 – 2026/11/07 这 30 天内被下载 24,426 次,日均约 814 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@callstack/react-native-asset 还在维护吗?

最新版本 3.1.0 发布于 2024/03/12(2 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/11/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。