Wednesday 21 July 2021

Disable resolving for CSS/SASS/SCSS/LESS URLs in Webpack 5

It's hard to believe, but I can't find a clear answer.

In SASS, I have a CSS rule with a url that looks like:

.eSearchFAQsAccord-q {
  &::after {
    content: url("./images/caret.svg");
  }
}

By default, Webpack will resolve this (though it chokes on my SVG). I want it to NOT resolve. I want the browser to load this image separately, from my static directory.

I'm not sure what to say about what I've tried so far. I've tried a lot of settings from around the web in my Webpack config that didn't work...

Here's my Webpack config, which I think is pretty out-of-the-box:

const CopyWebpackPlugin = require("copy-webpack-plugin");

module.exports = {
  mode: "development",
  entry: {
    index: `./src/index.js`,
  },
  target: "web",
  output: {
    path: `${__dirname}/dist`,
    filename: "[name].js",
  },
  plugins: [
    new CopyWebpackPlugin({
      patterns: [{ from: "static" }],
    }),
  ],
  devServer: {
    hot: true,
    inline: true,
    host: "localhost",
    port: 8080,
    watchOptions: {
      poll: true,
    },
  },
  module: {
    rules: [
      {
        
        test: /\.s[ac]ss$/i,
        use: [
          "style-loader",
          {
            loader: "css-loader",
            options: {
              sourceMap: true,
            },
          },
          {
            loader: "sass-loader",
            options: {
              sourceMap: true,
            },
          },
        ],
      },
      {
        test: /\.js$/,
        enforce: "pre",
        use: ["source-map-loader"],
      },
    ],
  },
};

I'm sure my issue is because I don't have intimate knowledge of Webpack, but the documentation I don't think is particularly intuitive at a glance.



from Disable resolving for CSS/SASS/SCSS/LESS URLs in Webpack 5

No comments:

Post a Comment