how to set up an inline svg with webpack

后端 未结 9 1927
感动是毒
感动是毒 2020-12-02 16:49

I am wondering how to set up an inline svg with webpack?

I am following the react-webpack-cookbook.

I have my webpack.config set up correc

9条回答
  •  一个人的身影
    2020-12-02 17:17

    I hope my late answer will still be useful for someone, because I don't like any of abovementioned options.

    The react-svg-loader webpack loader allows you to import SVG icons like JSX components:

    import Logo from './logo.svg';
    
    class App extends Component {
      render() {
        return (
          
    ); } }

    and minimum config looks like this:

    {
      test: /\.svg$/,
      use: [
        {
          loader: "babel-loader"
        },
        {
          loader: "react-svg-loader",
          options: {
            jsx: true // true outputs JSX tags
          }
        }
      ]
    }
    

    The best part is that it just outputs the svg file contents, without any extra wrappers and dangerouslySetInnerHTML in your code.

提交回复
热议问题