Skip to content
  • Sponsor webpack-contrib/css-loader

  • Notifications You must be signed in to change notification settings
  • Fork 609

feat: support regex in filter #1555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/options.json
Original file line number Diff line number Diff line change
@@ -13,7 +13,16 @@
"type": "object",
"properties": {
"filter": {
"instanceof": "Function"
"description": "Allows to custom `url()`/`image-set()` functions handling via regexp or function.",
"link": "https://github.com/webpack-contrib/css-loader#url",
"anyOf": [
{
"instanceof": "RegExp"
},
{
"instanceof": "Function"
}
]
}
},
"additionalProperties": false
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -529,7 +529,9 @@ function getFilter(filter, resourcePath) {
if (typeof filter === "function") {
return filter(...args, resourcePath);
}

if (filter instanceof RegExp) {
return filter.exec(...args);
}
return true;
};
}
8,619 changes: 5,321 additions & 3,298 deletions test/__snapshots__/url-option.test.js.snap

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion test/__snapshots__/validate-options.test.js.snap
Original file line number Diff line number Diff line change
@@ -386,7 +386,18 @@ exports[`validate options should throw an error on the "url" option with "[]" va

exports[`validate options should throw an error on the "url" option with "{"filter":true}" value 1`] = `
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
- options.url.filter should be an instance of function."
- options.url should be one of these:
boolean | object { filter? }
-> Allows to enables/disables \`url()\`/\`image-set()\` functions handling.
-> Read more at https://github.com/webpack-contrib/css-loader#url
Details:
* options.url.filter should be one of these:
RegExp | function
-> Allows to custom \`url()\`/\`image-set()\` functions handling via regexp or function.
-> Read more at https://github.com/webpack-contrib/css-loader#url
Details:
* options.url.filter should be an instance of RegExp.
* options.url.filter should be an instance of function."
`;

exports[`validate options should throw an error on the "url" option with "{}" value 1`] = `
32 changes: 32 additions & 0 deletions test/url-option.test.js
Original file line number Diff line number Diff line change
@@ -629,4 +629,36 @@ describe('"url" option', () => {
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work with url.filter", async () => {
const compiler = getCompiler("./url/url.js", {
url: {
filter: /img/gm,
},
});
const stats = await compile(compiler);

expect(getModuleSource("./url/url.css", stats)).toMatchSnapshot("module");
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
"result"
);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should not work with url.filter", async () => {
const compiler = getCompiler("./url/url.js", {
url: {
filter: /broken/gm,
},
});
const stats = await compile(compiler);

expect(getModuleSource("./url/url.css", stats)).toMatchSnapshot("module");
expect(getExecutedCode("main.bundle.js", compiler, stats)).toMatchSnapshot(
"result"
);
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});