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

images

Open klimczk1 opened this issue 5 years ago • 16 comments
trafficstars

In the newest version of library is a problem with using images. image There is something wrong with import path. import google from './assets/google.jpg' but using import google from '/../assets/google.jpg' is all right. This behaviour is bug for me, but maybe I am wrong ;)

klimczk1 avatar Aug 20 '20 21:08 klimczk1

Just spent the last few days going crazy over this too. Images were working for months, then just recently stopped. Changing their path worked for me too. Removing the "./" on the import worked for me.

GarrettSYHampton avatar Aug 25 '20 01:08 GarrettSYHampton

same error :-(

xfishernet avatar Aug 30 '20 04:08 xfishernet

This is also broken for me :(. Removing ./ worked for me as well, but this isn't great considering I'd like to keep my assets in a single folder. What about fonts as well?

bobbyflowstate avatar Sep 02 '20 14:09 bobbyflowstate

By the way I also had to add declare module '*.png'; to my typings.d.ts file as I'm using Typescript.

bobbyflowstate avatar Sep 02 '20 14:09 bobbyflowstate

Sry don't mean to spam. But I've now changed it to import logo from '/../assets/logo.png'; and it works. Basically the path can't start with a dot? Seems like a bug to me. Or it should be strongly documented so people don't spend hours dealing with this.

bobbyflowstate avatar Sep 02 '20 14:09 bobbyflowstate

Any solution for this issue ? the /../ works but breaks storybook giving an error like : Module not found: Error: You attempted to import /../../assets/image.png which falls outside of the project src/ directory. Relative imports outside of src/ are not supported

azimqordoba avatar Oct 02 '20 02:10 azimqordoba

Same here, taking away the dot works. This does seem like a bug though, at least it should have been highlighted as a breaking change.

For me (create-create-lib) however, even when removing the dot my unit tests fail as they want the dot to be there...Presumably because they work via react-scripts and not micro-bundle. Therefore I have had to disable my unit test for that component.

Duncan-Alexander-Coutts avatar Nov 16 '20 17:11 Duncan-Alexander-Coutts

Same here. Spent a couple of hours looking at what went wrong. But, paths starting with ./ does not work!

It seems like a bug!

sudobhat avatar Dec 03 '20 13:12 sudobhat

facing same issue

kishan-tocca avatar Jan 18 '21 10:01 kishan-tocca

any workaround ?

SivaGanesh56 avatar Mar 13 '21 07:03 SivaGanesh56

@transitive-bullshit need ur input on this

SivaGanesh56 avatar Mar 13 '21 07:03 SivaGanesh56

Hi @SivaGanesh56, I have followed one approach

  1. Created 'types/images.d.ts' file with following content.

    /// declare module '*.png'{ const src: string; export default src; }

  2. Go to tsconfig.json" paste inside compiler options "typeRoots" : ["node_modules/@types", "src/types"]

  3. Go to component file where you want load image ex: Header import brandLogo from "/../assets/logo.png"; "<\ img src={brandLogo} alt="logo" />"

Thanks, Durga Prasad.

prasadrails avatar Mar 13 '21 15:03 prasadrails

mark

woowalker avatar Mar 22 '21 07:03 woowalker

Sry don't mean to spam. But I've now changed it to import logo from '/../assets/logo.png'; and it works. Basically the path can't start with a dot? Seems like a bug to me. Or it should be strongly documented so people don't spend hours dealing with this.

Just used this and it worked. My only problem was with Jest, which wasn't finding the assets correctly.

In my case for SVGs I just appended this to my package.json to make it work:

"jest": {
    "moduleNameMapper": {
      "/../(.*).svg$": "<rootDir>/path/to/$1.svg",
      "/./(.*).svg$": "<rootDir>/another/path/to/$1.svg"
    }
  }

Still a monkeypatch waiting for the final fix on the bundler side, but I hope helpful for others who want to tests. :)

dej611 avatar Mar 28 '21 09:03 dej611

Jest fails to recognise the path like import logo from assets/logo.png where logo.png is located at src/assets.

@dej611 solution works fine. Thanks!

theIYD avatar Jun 21 '21 04:06 theIYD

What is the status on this? I can't import any image with any of the solutions mentioned above. create-react-library version 3.1.1 EDIT it works for me if I import like this import vector from 'assets/vector.svg'

Lionardo avatar Nov 02 '21 09:11 Lionardo