-
Notifications
You must be signed in to change notification settings - Fork 1.2k
import images #12
Comments
This may be related:
|
All you have to do is add declare module '*.png' to a |
There is two things that must be done. The first one got identified by pelotom which is to specify to TypeScript how to handle the PNG file. However, the original post contains a syntax that was not working for me. I had to use the star/import syntax. To summarize |
Even i tried to export default as |
@midori0507 How are you passing the src to your image? It should be similar to :
|
Having : declare module "*.png" {
const content: string;
export default content;
} 2: import * as logo from "./logo.png";
<img src={logo} /> 4: I sill get |
import logo from "./logo.png"; |
@pelotom spoke too soon, not working like that. I got |
ok got rid of typescript warning by removing {
const content: string;
export default content;
} Just having |
I tried everything but it is not working. Is it because of my loaders?
|
I've fixed this problem in this way:
|
Thanks @pelotom |
@pelotom or @damiangreen |
I created an answer on SO outlining a couple of options: https://stackoverflow.com/a/49715468/4216035 |
Do not work for me. Have i missed something? Have i messed up with folders structure/absolute|relative paths? webpack does see this particular image during build process Built at: 4/14/2018 5:43:07 PM
Asset Size Chunks Chunk Names
d2281ac61e357b25714ab2a6d357291e.jpg 947 KiB [emitted]
main.bundle.js 2.82 MiB main [emitted] main
index.html 178 bytes [emitted]
Entrypoint main = main.bundle.js import image from './large-image.jpg';
console.log('----------');
console.log('image');
console.log(image); //undefined here
console.log('----------');
export default class CssModules extends React.Component<{}, {}> {
render() {
return (
<section className={styles.root}>
<div className={styles.largeImageBg} />
<img className={styles.largeImageImage} src={image} alt="" />
</section>
);
}
} i'm using webpack-url-loader and have next project structure:
|
solved:
|
For anyone using Parcel, this works:
|
One problem I am having with the approach @pelotom suggested is that TypeScript will not check if the file imported actually exists.
For some reason tsc only checks the path in |
the following worked for me:
|
@pelotom @midori0507 @redredredredredred So....
Have a file it's meant to be in and a location for that file? thanks. |
this work for me. But add project architecture
tsconfig.json
typings/index.d.ts
|
@ww-k I had the same question. Here's what worked for me. Inside your
|
@kabua thank you. It seems like |
I have too many ".d.ts" files in my project. Which one to use ? |
@AnshulBasia pretty much any of it. But better some root Btw, how did I solve it:
So imports like this works perfectly:
P.S. I feel like this is still just a workaround, these png/etc files should be supported out of the box |
This works with stock create-react-app:
import logo from "./ss-logo-transparent.png";
The text was updated successfully, but these errors were encountered: