fix(lib/assets): make RN resolve custom assets properly

This commit is contained in:
PalmDevs 2025-05-07 00:51:43 +07:00
parent 72bd9c00fb
commit 73dbf1aa2e
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
2 changed files with 12 additions and 3 deletions

View file

@ -77,12 +77,11 @@ export function setPreferredAssetType(type: Asset['type']) {
* @throws An error if an asset with the same name and type already exists and `override` is not set to true.
*/
export function registerAsset(asset: RegisterableAsset, override?: boolean) {
const { name } = asset
const { name, type } = asset
if (override) _overrides.set(name, asset)
// Make sure to not override the asset if it already exists, unless explicitly set to override
else if (_assets.get(name)?.[1]?.[asset.type])
throw new Error(`Asset "${name}" with type "${asset.type}" already exists`)
else if (_assets.get(name)?.[1]?.[type]) throw new Error(`Asset "${name}" with type "${type}" already exists`)
// @ts-expect-error
return AssetRegistry.registerAsset(asset)

View file

@ -57,6 +57,16 @@ const unsubForResolveAssetSource = waitForModules(
(_, resolveAssetSource) => {
unsubForResolveAssetSource()
// Custom assets
// Why do we need to do this? Because RN will attempt to resolve the asset via its path, which we don't provide via custom assets.
// This will result in a crash, because it tries to read the path and run checks on it, but the path is undefined.
// @ts-expect-error
resolveAssetSource.addCustomSourceTransformer(({ asset }) => {
// @ts-expect-error
if (!asset.__packager_asset) return asset
})
// Asset overrides
// @ts-expect-error
resolveAssetSource.addCustomSourceTransformer(({ asset }) => _overrides.get(asset.name))
},