question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HOWTO for importing images doesn't work for SVGs

See original GitHub issue

Current Behavior

The HOWTO explaining how to support images, found here does not work for SVGs.

I have a tsdx.config.js file identical to the one in the HOWTO, but when I run a either yarn start or yarn build I see the following error:

✖ Failed to compile
(typescript) Error: /path/to/component/index.tsx(6,19): semantic error TS2307: Cannot find module '../icons/error.svg'.

I’ve also tried modified versions of that tsdx.config.js file that either just call images() or add in '**/*.svg' to the include list, to no avail.

This error relates to the following line in my component/index.tsx file:

import error from '../icons/error.svg';

Expected behavior

It should just work. 😉

Suggested solution(s)

Additional context

The component in question is using styled-components - it’s applying the image as a background, using: backgroundImage: url(${error}),

This component is part of a component library and I also have storybook set up in my repo - that is managing to successfully build and display this SVG file with this import.

Your environment

Software Version(s)
TSDX 0.12.3
TypeScript 3.7.5
Browser n/a
npm/Yarn yarn 1.22.4
Node 12.6.0
Operating System OS X 10.14.6

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:10

github_iconTop GitHub Comments

8reactions
MrNovadocommented, Jul 2, 2020

@stevesims @svgr/rollup (+ url) did the trick for me: https://react-svgr.com/docs/rollup/

const url = require('@rollup/plugin-url');
const svgr = require('@svgr/rollup').default;

module.exports = {
  rollup(config, options) {
    config.plugins = [
      url(),
      svgr({
        // configure however you like, this is just an example
        ref: true,
        memo: true,
        svgoConfig: {
          plugins: [
            { removeViewBox: false },
            { removeAttrs: { attrs: 'g:(stroke|fill):((?!^none$).)*' } }
          ],
        },
      }),
      ...config.plugins,
    ];

    return config;
  },
};
import SvgUrl, { ReactComponent as Svg } from './icon.svg'
4reactions
optimistikscommented, Mar 1, 2021

For Storybook users, if anyone stumbles on this, in addition to adding @svgr/rollup to tsdx.config.js as described above, I also had to install @svgr/webpack and add the following to .storybook/main.js to make it work with svgr.

  webpackFinal: async (config, { configType }) => {
    const assetRule = config.module.rules.find(({ test }) => test.test(".svg"));
    // exclude svg from the default storybook file-loader
    assetRule.exclude = /\.svg$/;

    // add svgr loader to handle svgs
    config.module.rules.push({
      test: /\.svg$/,
      use: ["@svgr/webpack"],
    });

    return config;
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't Import SVG image | OutSystems
Hi All,. I really need your help, why I can't import image with SVG format? It keeps telling me that the image is...
Read more >
Importing svg not working properly - Ask the community
by: Opening svg / vector file in Illustrator (*I haven't tried any other programs) Select “File” → “Save As” or “Save a Copy”...
Read more >
Unable to import svg files in typescript - Stack Overflow
If you use webpack, you can do this by creating a custom types file. Create a file named custom.d.ts with the following content:...
Read more >
How to fix SVG import size issues with Design Space 3
One of the big issues with Design Space 3 is when you import many SVG files they are sized VERY large and you...
Read more >
SVG file not showing up in Cricut Design Space? - YouTube
when importing a SVG file into Cricut Design Space the design is not showing up in the mat area but it has imported....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found