A
A
Alexey Nikolaev2020-10-13 18:23:41
React
Alexey Nikolaev, 2020-10-13 18:23:41

How to exclude webpack loader based on resource import form?

Good evening.
There is an application built via create-react-app. As you know, CRA offers a way to import SVGs as components.

import { ReactComponent as LogoIcon } from '@/assets/logo.svg';

This is provided by the following section of code in the config:
plugins: [
    [
      require.resolve('babel-plugin-named-asset-import'),
      {
        loaderMap: {
          svg: {
            ReactComponent:
              '@svgr/webpack?-svgo,+titleProp,+ref![path]',
          },
        },
      },
    ],
  ],

However, I want to add the ability to handle svgs as character sprites, while still being able to import svgs as components. To do this, I added svg-sprite-loader.
{
  test: /\.svg$/,
  use: [
    {
      loader: 'svg-sprite-loader',
      options: {
        symbolId: 'icon-[name]'
      }
    }
  ]
},

Actually sprites work with a bang, as they should, but here the import as a component falls off - the loaders conflict with each other.

Question: is it possible to somehow exclude the loader from the chain if the import is in any format? I.e
import icon from 'icon.svg'; // svg-sprite-loader
import { ReactComponent as icon } from './icon.svg'; // cra-loader


Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xenonhammer, 2020-10-13
@xenonhammer

It is possible to write a function that will return svg paths as jsx. You don't need webpack for this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question