Help us understand the problem. What is going on with this article?

ReactでSVGを読み込んでdata:で埋め込む

More than 1 year has passed since last update.

いくつかローダーあったけど、これが素直でした。

npm i svg-url-loader -D

でWebpackの設定は

const path = require('path');

module.exports = {
    entry: {
        console: './src/console/index.js'
    },
    output: {
        filename: './static/dist/[name]/a.js'
    },
    module: {
        rules: [
        {
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['env','es2015', 'react']
                }
            }
        },
        {
            test: /\.svg$/,
            use: {
                loader: 'svg-url-loader',
                options: {
                    noquotes: true
                }
            }
        }
        ]
    }
}

※上記はこの記述だけではない。「test: /.svg$/」あたりのオブジェクトが該当箇所です。

ポイントはオプションにnoquotestrueで渡すこと。
最初なにもつけずにやったらみたいにダブルクォーテーションがかぶった。

SVGの読み込みはとくに特別なことせず

import Logo from '../assets/svg/logo.svg';

こういう感じでimportすればOKです。

書きながら「React v15.0」からSVGの云々がサポートされるように云々というのを目にしてしまったけど、
Webpackの設定が悪かったのかコンパイル通らなかったから見なかったことにしよう。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away