Skip to content

Commit cac61d5

Browse files
refactor: url option
1 parent a220b5d commit cac61d5

File tree

6 files changed

+70
-26
lines changed

6 files changed

+70
-26
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ module.exports = {
109109

110110
| Name | Type | Default | Description |
111111
| :-----------------------------------: | :-------------------------: | :----------------: | :--------------------------------------------------------------------- |
112-
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enables/Disables `url`/`image-set` functions handling |
112+
| **[`url`](#url)** | `{Boolean\|Object}` | `true` | Enables/Disables `url`/`image-set` functions handling |
113113
| **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enables/Disables `@import` at-rules handling |
114114
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Enables/Disables CSS Modules and their configuration |
115115
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
@@ -118,7 +118,7 @@ module.exports = {
118118

119119
### `url`
120120

121-
Type: `Boolean|Function`
121+
Type: `Boolean|Object`
122122
Default: `true`
123123

124124
Enables/Disables `url`/`image-set` functions handling.
@@ -165,7 +165,7 @@ module.exports = {
165165
};
166166
```
167167

168-
#### `Function`
168+
#### `Object`
169169

170170
Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
171171

@@ -179,15 +179,17 @@ module.exports = {
179179
test: /\.css$/i,
180180
loader: "css-loader",
181181
options: {
182-
url: (url, resourcePath) => {
183-
// resourcePath - path to css file
182+
url: {
183+
filter: (url, resourcePath) => {
184+
// resourcePath - path to css file
184185

185-
// Don't handle `img.png` urls
186-
if (url.includes("img.png")) {
187-
return false;
188-
}
186+
// Don't handle `img.png` urls
187+
if (url.includes("img.png")) {
188+
return false;
189+
}
189190

190-
return true;
191+
return true;
192+
},
191193
},
192194
},
193195
},

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default async function loader(content, map, meta) {
9494
replacements,
9595
context: this.context,
9696
rootContext: this.rootContext,
97-
filter: getFilter(options.url, this.resourcePath),
97+
filter: getFilter(options.url.filter, this.resourcePath),
9898
resolver: urlResolver,
9999
urlHandler: (url) => stringifyRequest(this, url),
100100
})

src/options.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
"type": "boolean"
1010
},
1111
{
12-
"instanceof": "Function"
12+
"type": "object",
13+
"properties": {
14+
"filter": {
15+
"instanceof": "Function"
16+
}
17+
},
18+
"additionalProperties": false
1319
}
1420
]
1521
},

test/__snapshots__/validate-options.test.js.snap

+36-2
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,46 @@ exports[`validate options should throw an error on the "unknown" option with "tr
278278
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
279279
`;
280280

281+
exports[`validate options should throw an error on the "url" option with "() => {}" value 1`] = `
282+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
283+
- options.url should be one of these:
284+
boolean | object { filter? }
285+
-> Enables/Disables 'url'/'image-set' functions handling (https://github.com/webpack-contrib/css-loader#url).
286+
Details:
287+
* options.url should be a boolean.
288+
* options.url should be an object:
289+
object { filter? }"
290+
`;
291+
292+
exports[`validate options should throw an error on the "url" option with "[]" value 1`] = `
293+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
294+
- options.url should be one of these:
295+
boolean | object { filter? }
296+
-> Enables/Disables 'url'/'image-set' functions handling (https://github.com/webpack-contrib/css-loader#url).
297+
Details:
298+
* options.url should be a boolean.
299+
* options.url should be an object:
300+
object { filter? }"
301+
`;
302+
303+
exports[`validate options should throw an error on the "url" option with "{"filter":true}" value 1`] = `
304+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
305+
- options.url.filter should be an instance of function."
306+
`;
307+
308+
exports[`validate options should throw an error on the "url" option with "{}" value 1`] = `
309+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
310+
- options.url has an unknown property 'unknown'. These properties are valid:
311+
object { filter? }"
312+
`;
313+
281314
exports[`validate options should throw an error on the "url" option with "true" value 1`] = `
282315
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
283316
- options.url should be one of these:
284-
boolean | function
317+
boolean | object { filter? }
285318
-> Enables/Disables 'url'/'image-set' functions handling (https://github.com/webpack-contrib/css-loader#url).
286319
Details:
287320
* options.url should be a boolean.
288-
* options.url should be an instance of function."
321+
* options.url should be an object:
322+
object { filter? }"
289323
`;

test/url-option.test.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,21 @@ describe('"url" option', () => {
5252

5353
it('should work with a value equal to "Function"', async () => {
5454
const compiler = getCompiler("./url/url.js", {
55-
url: (url, resourcePath) => {
56-
expect(typeof resourcePath === "string").toBe(true);
55+
url: {
56+
filter: (url, resourcePath) => {
57+
expect(typeof resourcePath === "string").toBe(true);
5758

58-
if (url.startsWith("/guide/img")) {
59-
return false;
60-
}
59+
if (url.startsWith("/guide/img")) {
60+
return false;
61+
}
6162

62-
// Don't handle `img.png`
63-
if (url.includes("img.png")) {
64-
return false;
65-
}
63+
// Don't handle `img.png`
64+
if (url.includes("img.png")) {
65+
return false;
66+
}
6667

67-
return true;
68+
return true;
69+
},
6870
},
6971
});
7072
const stats = await compile(compiler);

test/validate-options.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { getCompiler, compile } from "./helpers/index";
33
describe("validate options", () => {
44
const tests = {
55
url: {
6-
success: [true, false, () => {}],
7-
failure: ["true"],
6+
success: [true, false, { filter: () => true }],
7+
failure: ["true", [], () => {}, { filter: true }, { unknown: () => {} }],
88
},
99
import: {
1010
success: [true, false, () => {}],

0 commit comments

Comments
 (0)