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.
UPDATE (thanks to the user in the comments):
There is a new option now, you can exclude specific url from Webpack resolving with a /* webpackIgnore: true */
comment:
.class {
/* webpackIgnore: true */
background: url("./url/img.png");
}
I think Webpack 5 css-loader
resolves this url()
calls by default, and you need to disable it manually:
loader: "css-loader",
options: {
// Add this option
url: false,
},
Maybe there are same options for your other loaders, but I'm pretty sure you just need to disable it for css-loader
only.