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.

SVGs referenced by style using `url()` syntax result in javascript in compiled css

See original GitHub issue

Hello, I have a tricky situation: we are requiring SVG files and would like them to be converted to react components which this library is working for, but then we also use some styles from a third party library which requires an SVG file, and when that conversion is taking place this loader injects JS into our compiled CSS file.

Our loaders config looks like this:

      {
        test: /\.styl$/,
        loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=[path][name]---[local]---[hash:base64:5]&-autoprefixer!postcss!stylus'),
      },
      {
        test: /\.svg$/,
        loader: 'babel!svg-react',
      }

The result looks something like this:

background-image:url(function (props, context, updater) {
	      // This constructor gets overridden by mocks. The argument is used
	      // by mocks to assert on what gets mounted.

	      if (false) {
	        process.env.NODE_ENV !== 'production' ? warning(this instanceof Constructor, 'Something is calling a React component directly. Use a factory or ' + 'JSX instead. See: https://fb.me/react-legacyfactory') : void 0;
	      }

	      // Wire up auto-binding
	      if (this.__reactAutoBindPairs.length) {
	        bindAutoBindMethods(this);
	      }

	      this.props = props;
	      this.context = context;
	      this.refs = emptyObject;
	      this.updater = updater || ReactNoopUpdateQueue;

	      this.state = null;

	      // ReactClasses doesn't have constructors. Instead, they use the
	      // getInitialState and componentWillMount methods for initialization.

	      var initialState = this.getInitialState ? this.getInitialState() : null;
	      if (false) {
	        // We allow auto-mocks to proceed as if they're returning null.
	        if (initialState === undefined && this.getInitialState._isMockFunction) {
	          // This is probably bad practice. Consider warning here and
	          // deprecating this convenience.
	          initialState = null;
	        }
	      }
	      !(typeof initialState === 'object' && !Array.isArray(initialState)) ?  false ? invariant(false, '%s.getInitialState(): must return an object or null', Constructor.displayName || 'ReactCompositeComponent') : _prodInvariant('82', Constructor.displayName || 'ReactCompositeComponent') : void 0;

	      this.state = initialState;
	    })

which obviously isn’t valid CSS 😩

Is there a way of differentiating between SVGs that are loaded via require() and ones that are loaded via url() inside CSS? If there is then I might be able to set a separate loader for those SVGs. Thank you in advance.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
nemzescommented, Nov 28, 2016

Can confirm; having the same problem.

0reactions
Vyprichenkocommented, Dec 8, 2021

This problem can be quite easily resolved with Webpack Rule.issuer and (optionally) Rule.oneOf for more precise configuration. No images duplication is necessary.

Example config:

{
    test: /\.(png|jpg|gif|svg)$/,
    // As opposed to Webpack loaders order (from last to first),
    // only the first matching Rule will be used here
    oneOf: [
        {
            test: /\.svg$/,
            // Use this loader for SVG-into-JavaScript imports only
            issuer: /\.(js|jsx)$/,
            loader: 'svg-react-loader'
        },
        {
            // Use file-loader for all other kinds of images
            loader: 'file-loader'
        }
    ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

SVG - Parcel
SVG can be referenced from CSS files using the url() function. As with the <img> element, SVGs in background images do not support...
Read more >
SVG/CSS woes, to embed or reference the file - Stack Overflow
I cant seem to wrap my head around a way to access the paths inside a reference svg file. I've started reading on...
Read more >
SVG Properties and CSS
Presentation attributes are used to style SVG elements and can be used as CSS properties. Some of these attributes are SVG-only while others ......
Read more >
How to use SVGs in React - LogRocket Blog
Learn how to use or render an SVG image on a React webpage, the pros and cons, and using components ... You can...
Read more >
Can't use relative paths in url() in scss files #12797 - GitHub
src/styles.scss) Module Error (from ./node_modules/postcss-loader/lib/index.js): (Emitted value instead of an instance of Error) ...
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