create-react-library icon indicating copy to clipboard operation
create-react-library copied to clipboard

Unable to Load Image assets

Open NishithHM opened this issue 5 years ago • 7 comments

I have several .svg assets in my assets folder. When I see the dist folder I see images used in the application appearing there, when I use the particular component in the project the assets are not loading I see broken image image

but when I see index.modern.js

var leftArrow = "arrowLeft~iCDoygWo.svg";

this is wrong way of importing images when I convert this to import or requires it works fine

var leftArrow = require("./arrowLeft~iCDoygWo.svg";)

image

NishithHM avatar Nov 09 '20 04:11 NishithHM

@NishithHM I have the same problem. But it seems that a walkaround was found in issue #285 May be you can try : var leftArrow = require("/./arrowLeft~iCDoygWo.svg";)

jbournonville avatar Nov 09 '20 15:11 jbournonville

I was able to fix this issue importing the SVG as ReactComponent

import { ReactComponent as YourFileName } from './your-file.svg'

and then, use it like:

<YourFileName />

saraahso avatar Mar 11 '21 09:03 saraahso

@jbournonville yup, same here! thanks 😉

madzadev avatar Mar 22 '21 16:03 madzadev

I just wrote a custom function on my utils file, just pass the asset it work, just a hack

.//usage <img aria-hidden src={loadAsset(calenderIcon)} alt="calender" className={styles.calendarClass} onClick={disabled ? null : toggleCalendar} />

// implementation export const loadAsset = (asset) => { try { if (asset.includes('static')) return asset; return require(./${asset}); } catch (error) { return asset; } };

NishithHM avatar Apr 05 '21 12:04 NishithHM

I have the same problem. Any proper solution yet?

sudobhat avatar May 06 '21 08:05 sudobhat

@sudobhat see above comment, use loadAsset custom function

nishithMobinius avatar May 06 '21 11:05 nishithMobinius

@NishithHM hello, when i use loadAsset custom function, i got a error after bundle ../dist/index.d.ts Module not found: Can't resolve './components/BatchOperationBar' in '/Users/firewing/study_project/sm-antd/dist' when i remover loadAsset, it can works excuse me , What do you think might be the reason

ZellerDorjee avatar Aug 07 '21 08:08 ZellerDorjee