Skip to content

Commit 6c8eba3

Browse files
refactor: import option
1 parent 22976d1 commit 6c8eba3

9 files changed

+2226
-2182
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ module.exports = {
110110
| Name | Type | Default | Description |
111111
| :-----------------------------------: | :-------------------------: | :----------------: | :--------------------------------------------------------------------- |
112112
| **[`url`](#url)** | `{Boolean\|Object}` | `true` | Enables/Disables `url`/`image-set` functions handling |
113-
| **[`import`](#import)** | `{Boolean\|Function}` | `true` | Enables/Disables `@import` at-rules handling |
113+
| **[`import`](#import)** | `{Boolean\|Object}` | `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 |
116116
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
@@ -200,7 +200,7 @@ module.exports = {
200200

201201
### `import`
202202

203-
Type: `Boolean|Function`
203+
Type: `Boolean|Object`
204204
Default: `true`
205205

206206
Enables/Disables `@import` at-rules handling.
@@ -248,7 +248,7 @@ module.exports = {
248248
};
249249
```
250250

251-
#### `Function`
251+
#### `Object`
252252

253253
Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
254254

@@ -262,15 +262,17 @@ module.exports = {
262262
test: /\.css$/i,
263263
loader: "css-loader",
264264
options: {
265-
import: (url, media, resourcePath) => {
266-
// resourcePath - path to css file
265+
import: {
266+
filter: (url, media, resourcePath) => {
267+
// resourcePath - path to css file
267268

268-
// Don't handle `style.css` import
269-
if (url.includes("style.css")) {
270-
return false;
271-
}
269+
// Don't handle `style.css` import
270+
if (url.includes("style.css")) {
271+
return false;
272+
}
272273

273-
return true;
274+
return true;
275+
},
274276
},
275277
},
276278
},

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export default async function loader(content, map, meta) {
6767
api: importPluginApi,
6868
context: this.context,
6969
rootContext: this.rootContext,
70-
filter: getFilter(options.import, this.resourcePath),
70+
filter: getFilter(options.import.filter, this.resourcePath),
7171
resolver,
7272
urlHandler: (url) =>
7373
stringifyRequest(

src/options.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
"type": "boolean"
2727
},
2828
{
29-
"instanceof": "Function"
29+
"type": "object",
30+
"properties": {
31+
"filter": {
32+
"instanceof": "Function"
33+
}
34+
},
35+
"additionalProperties": false
3036
}
3137
]
3238
},

test/__snapshots__/import-option.test.js.snap

+994-994
Large diffs are not rendered by default.

test/__snapshots__/url-option.test.js.snap

+1,159-1,159
Large diffs are not rendered by default.

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

+36-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,48 @@ exports[`validate options should throw an error on the "esModule" option with "t
66
-> Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule)."
77
`;
88

9+
exports[`validate options should throw an error on the "import" option with "() => {}" value 1`] = `
10+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
11+
- options.import should be one of these:
12+
boolean | object { filter? }
13+
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
14+
Details:
15+
* options.import should be a boolean.
16+
* options.import should be an object:
17+
object { filter? }"
18+
`;
19+
20+
exports[`validate options should throw an error on the "import" option with "[]" value 1`] = `
21+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
22+
- options.import should be one of these:
23+
boolean | object { filter? }
24+
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
25+
Details:
26+
* options.import should be a boolean.
27+
* options.import should be an object:
28+
object { filter? }"
29+
`;
30+
31+
exports[`validate options should throw an error on the "import" option with "{"filter":true}" value 1`] = `
32+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
33+
- options.import.filter should be an instance of function."
34+
`;
35+
36+
exports[`validate options should throw an error on the "import" option with "{}" value 1`] = `
37+
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
38+
- options.import has an unknown property 'unknown'. These properties are valid:
39+
object { filter? }"
40+
`;
41+
942
exports[`validate options should throw an error on the "import" option with "true" value 1`] = `
1043
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
1144
- options.import should be one of these:
12-
boolean | function
45+
boolean | object { filter? }
1346
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
1447
Details:
1548
* options.import should be a boolean.
16-
* options.import should be an instance of function."
49+
* options.import should be an object:
50+
object { filter? }"
1751
`;
1852

1953
exports[`validate options should throw an error on the "importLoaders" option with "2.5" value 1`] = `

test/import-option.test.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,25 @@ describe('"import" option', () => {
5353
expect(getErrors(stats)).toMatchSnapshot("errors");
5454
});
5555

56-
it('should work when "Function"', async () => {
56+
it("should work with import.filter", async () => {
5757
const compiler = getCompiler("./import/import.js", {
58-
import: (url, media, resourcePath) => {
59-
expect(url).toBeDefined();
58+
import: {
59+
filter: (url, media, resourcePath) => {
60+
expect(url).toBeDefined();
6061

61-
if (url === "test-nested-media.css") {
62-
expect(media).toBeDefined();
63-
}
62+
if (url === "test-nested-media.css") {
63+
expect(media).toBeDefined();
64+
}
6465

65-
expect(resourcePath).toBeDefined();
66+
expect(resourcePath).toBeDefined();
6667

67-
// Don't handle `test.css`
68-
if (url.includes("test.css")) {
69-
return false;
70-
}
68+
// Don't handle `test.css`
69+
if (url.includes("test.css")) {
70+
return false;
71+
}
7172

72-
return true;
73+
return true;
74+
},
7375
},
7476
});
7577
const stats = await compile(compiler);

test/url-option.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('"url" option', () => {
5050
expect(getErrors(stats)).toMatchSnapshot("errors");
5151
});
5252

53-
it('should work with a value equal to "Function"', async () => {
53+
it("should work with url.filter", async () => {
5454
const compiler = getCompiler("./url/url.js", {
5555
url: {
5656
filter: (url, resourcePath) => {

test/validate-options.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ describe("validate options", () => {
77
failure: ["true", [], () => {}, { filter: true }, { unknown: () => {} }],
88
},
99
import: {
10-
success: [true, false, () => {}],
11-
failure: ["true"],
10+
success: [true, false, { filter: () => true }],
11+
failure: ["true", [], () => {}, { filter: true }, { unknown: () => {} }],
1212
},
1313
modules: {
1414
success: [

0 commit comments

Comments
 (0)