简体   繁体   English

SVG 图标在捆绑后不呈现(webpack)

[英]SVG icons do not render after bundling (webpack)

I'm using the file-loader plugin, to load my images, well, all images are displayed correctly after the webpack build, but my SVG's icons don't appear on the page.我正在使用文件加载器插件来加载我的图像,好吧,在 webpack 构建之后,所有图像都正确显示,但我的 SVG 图标没有出现在页面上。 The folder structure is correct, being the same for all images.文件夹结构正确,所有图像都相同。 And no type of error is returned并且没有返回任何类型的错误

 const HtmlWebpackPlugin = require('html-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const path = require('path'); module.exports = { entry: './src/js/controller.js', output: { filename: 'index.js', path: path.resolve(__dirname, 'dist'), clean: true, }, module: { rules: [ //Parsing Styles { test: /\.s[ac]ss$/i, use: [ // Creates `style` nodes from JS strings MiniCssExtractPlugin.loader, // Translates CSS into CommonJS "css-loader", // Compiles Sass to CSS "sass-loader", ], }, //Parsing Images { test: /\.(png|jpe?g|gif|svg)$/i, use: [ { loader: 'file-loader', options: { name: '[path][name].[ext]' }, }, ] }, ], }, plugins: [ new HtmlWebpackPlugin({ template: './index.html', filename: 'index.html' }), new MiniCssExtractPlugin({ filename: 'style.css' }) ] }

My icons are embedded in html directly.我的图标直接嵌入在 html 中。

 <button class="btn search__btn"> <svg class="search__icon"> <use href="src/img/icons.svg#icon-search"></use> </svg> <span>Search</span> </button>

The structure of my folders is as follows:我的文件夹结构如下:

Develop

src/
├── img/
│   ├── ...
│   └──  icons.svg
├───js/
│    └──index.js
├───sass/
│   ├──...
│   └──main.scss
index.html

Build

dist/
├── src/
│   └── img/
│       ├──...
│       └──icons.svg
style.css
index.js
index.html

Try installing npm install @svgr/webpack and then add the following rule in webpack尝试安装npm install @svgr/webpack然后在 webpack 中添加以下规则

{
        test: /\.svg$/,
        use: ['@svgr/webpack'],
      },
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM