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

Images with @2x or @3x in their file names are not resolved (breaks static image resources) #43

Closed
mo-solnet opened this issue Aug 21, 2017 · 5 comments
Assignees

Comments

@mo-solnet
Copy link

mo-solnet commented Aug 21, 2017

Do you want to request a feature or report a bug?

Report a bug

What is the current behavior?

Images with @2x or @3x in the file name are not resolved when doing require('../myImage@2x.png'). If I remove the @ then it loads the file without an issue.

image

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

Others have reproduced this behavior as you can see 👉 facebook/react-native#15030

What is the expected behavior?

I expect the files to load and be resolved without an issue. This problem breaks static image resources 👉 https://facebook.github.io/react-native/docs/images.html#static-image-resources

Please provide your exact metro-bundler configuration and mention your metro-bundler, node, yarn/npm version and operating system.

I've used the stock version that comes with react-native 0.47.1.
I've also tried --reset-cache and finally I've also updated metro-bundler to version 0.11.0.
npm 5.0.3
yarn 0.27.5
Mac OSX 10.12.6

The only workaround I've found is to remove the @ when requiring the file

@cpojer
Copy link
Contributor

cpojer commented Oct 9, 2017

@jeanlauliac knows about assets.

@jeanlauliac
Copy link

Sorry I've been lagging behind quite a lot.

About this issue: in Metro, assets are not meant to be required with a specific scale, since different devices might use different scales. In that example, requiring ./myImage.png would cause either myImage@2x.png or myImage@3x.png to be used depending on the device screen pixel ratio (virtual-pixel to physical-pixel ratio, so to speak).

@mo-solnet can you explain what is the benefit of using require('../myImage@2x.png') over require('../myImage.png')?

@jasperkuperus
Copy link

@jeanlauliac Is it an idea to add a better error if someone does use @2x and then refer to this issue?

@jeanlauliac
Copy link

jeanlauliac commented Dec 11, 2017

Yes, a specific error message sounds good to me. I would not refer to this issue though, I'd just mention that removing the scale specifier solves the problem.

@jeanlauliac
Copy link

I haven't got the time to try to fit a hint message somewhere in the stack, but I'll close this issue for now, as this is the expected behaviour, and scale specifiers should not be included in the name when requiring assets. Do feel free to send a PR to tweak error messages however.

The documentation mentions having different densities and does have the example without the density specifier, so I don't think it needs updating? (https://facebook.github.io/react-native/docs/images.html#static-image-resources)

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

No branches or pull requests

4 participants