Skip to content

Resolve platform specific files if general file was not found #1699

Description

@stephan-wolf-ais

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch metro@0.83.6 for the project I'm working on.

We used a package which contains only platform specific resources. This means metro was not able to find files specified by i.e. icon@3x without an platform specifier. This patch solved the issue.

Here is the diff that solved my problem:

diff --git a/node_modules/metro/src/Assets.js b/node_modules/metro/src/Assets.js
index ac3302c..10f4ae1 100644
--- a/node_modules/metro/src/Assets.js
+++ b/node_modules/metro/src/Assets.js
@@ -223,9 +223,22 @@ async function getAsset(
   }
   if (fileExistsInFileMap != null) {
     if (!fileExistsInFileMap(absolutePath)) {
-      throw new Error(
-        `'${relativePath}' could not be found, because it is not within the projectRoot or watchFolders, or it is blocked via the resolver.blockList config`,
-      );
+      // When the exact file doesn't exist in the file map, check for a
+      // platform-specific variant (e.g. icon@3x.android.png instead of
+      // icon@3x.png). Some packages only ship platform-specific assets.
+      let foundPlatformVariant = false;
+      if (platform != null) {
+        const ext = _path.default.extname(absolutePath);
+        const platformPath = absolutePath.slice(0, -ext.length) + '.' + platform + ext;
+        if (fileExistsInFileMap(platformPath)) {
+          foundPlatformVariant = true;
+        }
+      }
+      if (!foundPlatformVariant) {
+        throw new Error(
+          `'${relativePath}' could not be found, because it is not within the projectRoot or watchFolders, or it is blocked via the resolver.blockList config`,
+        );
+      }
     }
   } else {
     if (!pathBelongsToRoots(absolutePath, [projectRoot, ...watchFolders])) {

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions