为什么背景网址不能与webpack中的svg文件一起使用?

时间:2018-11-16 08:37:09

标签: webpack webpack-4

webpack.base.config.js:

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');

const resolve = dir => path.join(__dirname, '..', dir);

module.exports = {
  entry: {
    app: './src/index.js',
  },
  output: {
    path: resolve('dist'),
    filename: 'js/[name].[hash].js',
    publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  },
  optimization: {
    splitChunks: {
      cacheGroups: {
        vendor: {
          chunks:'initial',
          name: 'vendor',
          test: 'vendor',
          enforce: true,
        },
      },
    },
  },
  resolve: {
    extensions: ['.js', '.json'],
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        enforce: 'pre',
        use: {
          loader: 'eslint-loader',
        },
      },
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /\.html$/,
        exclude: /node_modules/,
        use: {
          loader: 'html-loader',
        },
      },
      {
        test: /\.(sa|sc|c)ss$/,
        exclude: /node_modules/,
        use: [
          MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
            },
          },
          {
            loader: 'postcss-loader',
            options: {
              sourceMap: true,
            },
          },
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true,
            },
          },
        ],
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        exclude: /node_modules/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: '[hash]-[name].[ext]',
        },
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        exclude: /node_modules/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: '[hash]-[name].[ext]'
        },
      },
      {
        test: /\.(woff2?|eot|ttf|otf|svg)(\?.*)?$/,
        exclude: /node_modules/,
        loader: 'file-loader',
        options: {
          limit: 10000,
          name: '[name]-[hash].[ext]',
        },
      },
    ],
  },
};

我在CSS中这样写:

.icon {
  width: 4.2rem;
  height: 5.3rem;
  background: url('../img/icon.svg') left top no-repeat;
  background-size: contain;
}

这是图标,而不是图标:

background: url(data:image/svg+xml;base64,bW9kdWxlLmV4cG9ydHMgPSBfX3dlYnBhY2tfcHVibGljX3BhdGhfXyArICJpY29uLWRvYy1hNjk0NTdkY2IzZWQ1OWVhZmZhZjE4MDA3MTkwNWI1Yi5zdmciOw==) left top no-repeat

项目结构如下:

project
--public
----index.html
--src
----assets
------img
--------icon.svg
------scss
--------icons.scss (тут и пишу)
----components
----views
----...

问题::使用此配置,无论在开发模式还是销售中,jsx内的导入都可以正常工作。但是背景网址不起作用,也就是说,SVG图片不可见,使用PNG一切都很好。

1 个答案:

答案 0 :(得分:0)

在此代码中,您告诉Webpack使用systemProp.sonar.tests=src/test systemProp.sonar.jacoco.reportPath=${project.buildDir}/jacoco/test.exec模块将 svg 图像转换为 base64

url-loader

那应该真的有用...

我建议您尝试以下操作:

尝试将svg直接插入您的html中,图标应该在其中。这样做是为了可以重用它,例如在此表单的末尾在DOM中创建一个图标组件:

例如

{
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        exclude: /node_modules/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: '[hash]-[name].[ext]',
        },
      }

如果将其插入为html,它将起作用