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.

[v5] Use webpack 5 asset modules also for svgr

See original GitHub issue

Is your proposal related to a problem?

This issue is related to Webpack 5 and the svg loader part of the configuration. Webpack 5 now use Asset modules to replace raw/url/file loaders.

Most of file loading is already converted into asset modules in the new CRA webpack 5 config but theres on outstanding regarding loading svg.

Describe the solution you’d like

When converting to webpack 5 I didn’t manage to convert the svgr loader setup to be using asset modules, for now it’s using file-loader still. It would be great if it could be converted into the asset module pattern, ideally by setting the asset type instead of using other loaders to be webpack 5 native.

Describe alternatives you’ve considered

@marella mentioned that he did a new-url-loader as an alternative see comment for more details

Additional context

Reading the webpack 5 documentation I would imagine that something like this would have worked: (Also note that we might have to think about url assets maybe thats out of scope for now?)

  test: /\.svg$/,
  use: [
    {
      loader: '@svgr/webpack',
      options: {
        prettier: false,
        svgo: false,
        svgoConfig: {
          plugins: [{ removeViewBox: false }],
        },
        titleProp: true,
        ref: true,
      },
    },
    type: 'asset/resource',
    generator: {
      filename: 'static/media/[name].[hash].[ext]',
    }
/* replacing:
    {
      loader: 'file-loader',
      options: {
        name: 'static/media/[name].[hash].[ext]',
      },
    },
*/
  ],

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
alexander-akaitcommented, Jul 20, 2021

Both example doesn’t violate ECMA modules spec

2reactions
alexander-akaitcommented, Jul 20, 2021

No need to use dirty solution like https://github.com/gregberge/svgr/issues/551#issuecomment-864531501, just register loader on resourceQuery: /svgr/ and use import foo from './my.svg?svgr' to import, new-url-loader is wrong solution too, i.e.

module: {
    rules: [
      {
        test: /\.svg$/,
        resourceQuery: /svgr/,
        use: [
          {
            loader: "@svgr/webpack",
          },
        ],
      },
    ],
  },

and use code:

import result from './image.svg?svgr';

console.log(typeof result)

if (module.hot) {
  module.hot.accept();
}

In future we will improve this and will be possible to use:

import result from "./image.svg" assert { type: "@svgr/webpack" };

console.log(typeof result)

if (module.hot) {
  module.hot.accept();
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Asset Modules - webpack
Asset Modules is a type of module that allows one to use asset files (fonts, icons, etc) without configuring additional loaders. Prior to...
Read more >
Webpack - SVGR
SVGR provides an official webpack.js loader to import SVG as React ... url-loader and file-loader are deprecated over Asset Modules in webpack v5....
Read more >
Webpack 5 - Asset Modules - DEV Community ‍ ‍
In this article, I want to tell you about the Asset Modules - an experimental feature of webpack 5, which makes it possible...
Read more >
Webpack 5: Asset Modules - Medium
I upgraded all dev dependencies to their latest version and found some good replacements for deprecated plugins. It was obvious that I still...
Read more >
Webpack 5 Asset Modules: How to resolve and import assets
This will be a full demo on how to use all 4 types of Asset Modules Loaders which allow importing all kinds of...
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