Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Add note about JSX usage #35

Closed
kswope opened this issue Aug 19, 2015 · 5 comments
Closed

Add note about JSX usage #35

kswope opened this issue Aug 19, 2015 · 5 comments

Comments

@kswope
Copy link

kswope commented Aug 19, 2015

Just spent the last few hours (more than a few?) on this problem and it was entirely avoidable but I haven't seen any docs on it.

If you are using JSX (and therefor probably babel-loader) you do not need to use file-loader or url-loader for img tags in the JSX, its somehow already handled by babel. I somehow knew JSX was handled by babel-loader, but I have no idea how that came about, but as a bonus it handles the stuff of file-loader.

I eventually figured this out because I was doing this, which seems to be the right way to do it

<img src={require('../../assets/images/image.png')} />

But having png files handled by both babel-loader and file-loader meant the browser was displaying a broken image, which I eventually downloaded and opened, and instead of ugly binary, it was this :)

module.exports = __webpack_public_path__ + "265a6261bff86f8c7bc3c98c5eba3583.png"

@legendary-mich
Copy link

@kswope
I had the same issue. I'm using ts-loader, though. It would be great if title of this issue was more to the point. For example: "require () creates two files instead of one". Then, I would have a chance to find this answer knowingly, and not like it is right now, by accident.

Thanks for Sharing, though. :)

@tobycox
Copy link

tobycox commented Jul 13, 2016

Ah thank god I found this. I agree it would be great to change the title of the issue for future people who are banging their heads against this.
I was searching for things like "broken images webpack" and "broken images file-loader".
Hopefully these breadcrumbs will lead people here...

@SpaceK33z
Copy link
Contributor

Anyone wants to update the readme with PR?

@michael-ciniawsky michael-ciniawsky changed the title Document request [JSX] require('path/to/img.png) creates two files instead of one Jun 2, 2017
@michael-ciniawsky michael-ciniawsky self-assigned this Jun 2, 2017
@michael-ciniawsky michael-ciniawsky changed the title [JSX] require('path/to/img.png) creates two files instead of one [JSX] require('path/to/img.png') creates two files instead of one Jun 2, 2017
@michael-ciniawsky michael-ciniawsky changed the title [JSX] require('path/to/img.png') creates two files instead of one Add note about JSX usage Jul 28, 2017
@oieduardorabelo
Copy link

oieduardorabelo commented Sep 11, 2017

I was having this problem today and, downgrading to file-loader": "0.11.2", worked as expected for me.

"babel-core": "6.26.0",
"babel-loader": "7.1.2",
"babel-preset-react-app": "3.0.2",
"file-loader": "0.11.2" // using 1.0.0, doesn't work 

in my case, I was importing the file in the top:

import IntroImage from '../../images/1@2x.png';
import Slide0Image from '../../images/2@2x.png';

... code

<img src={IntroImage} />

... etc

Using 1.0.0, the image was corrupted (in the webpack logs, we could see it being emitted, but, opening it via browser didn't work)

I managed to isolate the scenario in this repo: https://github.com/oieduardorabelo/file-loader-error

@alexander-akait
Copy link
Member

@oieduardorabelo known issue, should be fixed after #183

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

No branches or pull requests

7 participants