Skip to content
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

Make asset registry globally available #2720

Closed
wants to merge 1 commit into from

Conversation

nandorojo
Copy link
Contributor

What

Exposes the asset registry array as a global variable so that libraries can access the assets without needing to install react-native-web.

Why

I build many open source libraries for React Native & React Native Web, many of which render an image under the hood.

In particular, I often want to do this in my libraries:

<img src={require('./local-image.png')} />

However, for libraries using Metro for Web, this doesn't work because ther result of require is a number.

I'd love to be able to do this:

function MyLibrarysComponent({ src }) {
  const asset = typeof src === 'number' ? globalThis.__react_native_web_assets[src] : src
}

I am explicitly not interested in importing Image from react-native-web, because I want to support web-only apps without adding requiring additional dependencies or configuration.

Workaround

The only way to fix this currently would be to import resolveAssetSource from react-native-web's internals. However, this would require adding react-native-web as a dependency of the library, just to support Metro Web users.

With this PR, I can confidently pull from the global variable for users which certainly have react-native-web installed, without requiring Next.js/Vite/Remix users to install it.

Copy link

codesandbox-ci bot commented Oct 3, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit cf6ed6c:

Sandbox Source
react-native-web-examples Configuration

@necolas
Copy link
Owner

necolas commented Oct 15, 2024

Hi, I don't think this makes sense for RNfWeb. Inline requires are a React Native pattern, that otherwise should be supported by bundlers, especially if your use case doesn't involve depending on RNfWeb. Thanks

@necolas necolas closed this Oct 15, 2024
@nandorojo
Copy link
Contributor Author

Yeah I think we’re going to upstream this in expo’s metro config instead expo/expo#31955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants