'Webpack how to negative match resource query
How can I add a resource query to my Webpack configuration so that if a require statement contains a query string, Webpack skips the loader I've specified in the configuration file. Consider the following configuration:
test: /\.(jpe?g|png|svg|gif|ico|webp)$/,
resourceQuery: /(?!ni-ignore)/i,
use: [
{
loader: "url-loader",
options: {
limit: 8192,
fallback: "file-loader",
publicPath: `/static/images/`,
outputPath: `/static/images/`,
name: "[name]-[hash].[ext]"
}
}
]
I want Webpack to skip the rule if a require statement contains ni-ignore
query.
// should skip the rule
const image = require("./image.jpg?ni-ignore");
// should follow the rule
const image = require("./image.jpg");
Is there any way to add a negative resource query matcher?
Solution 1:[1]
Try thisresourceQuery: /^((?!ni-ignore).)*$/i
It seems that a part was missing in regex.
Solution 2:[2]
I know this is a bit old, but in case someone still needs this, resourceQuery now accepts negative rules in the form of:
resourceQuery: { not: [/raw/] },
See https://webpack.js.org/guides/asset-modules/#replacing-inline-loader-syntax for more details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Semiaddict |