【问题标题】:url() in SCSS won't resolve correctly with Webpack 4SCSS 中的 url() 无法使用 Webpack 4 正确解析
【发布时间】:2019-04-01 00:09:12
【问题描述】:

我在下面发布了用于生产环境的 Webpack 配置。我试图在我的一个 SCSS 文件中使用background-image: url(../img/chevron-thin-right.svg);,但它被解析为background-image: url([object Module]),因此无法正常工作。我正在尝试将纯 SCSS 和 HTML 项目移植到 Webpack 捆绑的反应应用程序中,因此上述方法曾经有效。任何修复将不胜感激。

webpack.common.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const outputDirectory = 'dist';

module.exports = {
  entry: './src/client/index.js',
  output: {
    path: path.join(__dirname, outputDirectory),
    filename: 'bundle.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Production',
      template: './public/index.html',
      favicon: './public/favicon.ico',
      hash: true,
      filename: 'index.html'
    })
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.(jpg|png|woff|woff2|eot|ttf)$/,
        use: {
          loader: 'file-loader?limit=100000&name=images/[name].[ext]'
        }
      },
      {
        test: /\.svg$/,
        use: [
          "babel-loader",
          {
            loader: "react-svg-loader",
            options: {
              svgo: {
                plugins: [
                  { 
                    removeTitle: true,
                    removeComments: true
                  }
                ],
                floatPrecision: 2
              }
            }
          }
        ]
      }
    ]
  }
};

webpack.prod.js

const merge = require('webpack-merge');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const common = require('./webpack.common.js');
const path = require('path');
const autoprefixer = require('autoprefixer');

module.exports = {};

module.exports = merge(common, {
  mode: 'production',
  optimization: {
    splitChunks: {
      cacheGroups: {
        styles: {
          name: 'styles',
          test: /\.css$/,
          chunks: 'all',
          enforce: true
        }
      }
    }
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new MiniCssExtractPlugin({
      filename: '[name].css'
    })
  ],
  module: {
    rules: [
      {
        test: /\.scss$/,
        use: [
          MiniCssExtractPlugin.loader, 
          {
            loader: 'css-loader',
            options: {
              sourceMap: true,
              root: path.resolve(__dirname),
            },
          },
          {
            loader: 'postcss-loader',
            options: {
              ident: 'postcss',
              plugins: () => [
                autoprefixer({
                  'browsers': ['last 2 versions', 'ie >= 10'],
                }),
              ],
              sourceMap: true,
            },
          },
          {
            loader: 'sass-loader',
            options: {
              outputStyle: 'compressed',
              sourceMap: true,
              includePaths: [
                './src/client/style/scss',
              ],
            },
          }
        ]
      }
    ]
  }
});

【问题讨论】:

    标签: javascript html css webpack


    【解决方案1】:

    如果其他人正在寻找答案,我能够解决它。这是由于没有通过 url-loader 加载关联的 SVG。重新添加 svg 时出现了另一个问题,因为我想用作 React 组件的内联 SVG 遇到错误,因为它们现在正在通过 url-loader 而不是我的 react-svg-loader。

    下面是工作的 webpack.common.js 和 webpack.prod.js 保持不变。解决方案是通过将所有内联扩展从 .svg 转换为 .inline.svg 并相应地调整 webpack 配置来区分我的内联和外部 SVG

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    
    const outputDirectory = 'dist';
    
    module.exports = {
      entry: './src/client/index.js',
      output: {
        path: path.join(__dirname, outputDirectory),
        filename: 'bundle.js'
      },
      plugins: [
        new HtmlWebpackPlugin({
          title: 'Production',
          template: './public/index.html',
          favicon: './public/favicon.ico',
          hash: true,
          filename: 'index.html'
        })
      ],
      module: {
        rules: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
              loader: 'babel-loader'
            }
          },
          {
            test: /\.(jpg|png|woff|woff2|eot|ttf|svg)$/,
            exclude: /\.inline.svg$/,
            use: {
              loader: 'url-loader?limit=1000&name=images/[name].[ext]'
            }
          },
          {
            test: /\.inline.svg$/,
            use: [
              "babel-loader",
              {
                loader: "react-svg-loader",
                options: {
                  svgo: {
                    plugins: [
                      { 
                        removeTitle: true,
                        removeComments: true
                      }
                    ],
                    floatPrecision: 2
                  }
                }
              }
            ]
          }
        ]
      }
    };

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 2021-04-25
      • 2020-12-21
      • 2019-05-11
      • 2016-07-21
      • 2014-12-02
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      相关资源
      最近更新 更多