webpackでsvgを使用するためにwebpack.config.jsを設定しているのですが、上手く表示されません。ターミナルではエラーは発生しておらず、原因がわかりません。
webpack.config.jsのコードは公式レファレンスから拝借してます。
"devDependencies": { "babel-core": "^6.26.3", "babel-loader": "^7.1.5", "babel-preset-env": "^1.7.0", "css-loader": "^2.1.1", "html-webpack-plugin": "^3.2.0", "node-sass": "^4.12.0", "path": "^0.12.7", "sass-loader": "^7.1.0", "style-loader": "^0.23.1", "svg-sprite-webpack-plugin": "^1.0.4", "webpack": "^4.31.0", "webpack-cli": "^3.3.2", "webpack-dev-server": "^3.3.1", "webpack-isomorphic-tools": "^3.0.6" }
html
1<svg class="user_icon icon__small"> 2 <use xlink:href="./img/icons-defs.svg#icon-user"></use> 3</svg>
Javascript
1const path = require('path'); 2const HtmlWebpackPlugin = require('html-webpack-plugin'); 3const IconPlugin = require('svg-sprite-webpack-plugin').plugin; 4const iconPlugin = new IconPlugin('icons-[defs].svg'); 5const iconParser = require('svg-sprite-webpack-plugin').webpackIsomorphicParser; 6 7module.exports = { 8 entry: './src/js/index.js', 9 output: { 10 filename: 'bundle.js' 11 }, 12 module: { 13 rules: [ 14 { 15 test: /.scss$/, 16 use: [ 17 "style-loader", // creates style nodes from JS strings 18 "css-loader", // translates CSS into CommonJS 19 "sass-loader" // compiles Sass to CSS, using Node Sass by default 20 ] 21 }, 22 { 23 test: /.js$/, 24 exclude: /node_modules/, 25 use: { 26 loader: 'babel-loader' 27 } 28 }, 29 { 30 test: /.svg$/, 31 loader: iconPlugin.extract(), 32 } 33 ] 34 }, 35 devServer: { 36 contentBase: './dist' 37 }, 38 plugins: [ 39 new HtmlWebpackPlugin({ 40 filename: 'index.html', 41 template: './src/index.html' 42 }), 43 new IconPlugin('icons-[defs].svg', { 44 options: { 45 assets: { 46 svg: { 47 extension: 'svg', 48 parser: iconParser 49 } 50 } 51 } 52 }) 53 ] 54}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。