Skip to content

Commit 4e9fb50

Browse files
refactor: removed importLoaders in favor import.loaders
1 parent 2f37484 commit 4e9fb50

10 files changed

+147
-113
lines changed

README.md

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,13 @@ module.exports = {
107107

108108
## Options
109109

110-
| Name | Type | Default | Description |
111-
| :-----------------------------------: | :-------------------------: | :----------------: | :--------------------------------------------------------------------- |
112-
| **[`url`](#url)** | `{Boolean\|Object}` | `true` | Enables/Disables `url`/`image-set` functions handling |
113-
| **[`import`](#import)** | `{Boolean\|Object}` | `true` | Enables/Disables `@import` at-rules handling |
114-
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Enables/Disables CSS Modules and their configuration |
115-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
116-
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
117-
| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
110+
| Name | Type | Default | Description |
111+
| :---------------------------: | :-------------------------: | :----------------: | :---------------------------------------------------- |
112+
| **[`url`](#url)** | `{Boolean\|Object}` | `true` | Enables/Disables `url`/`image-set` functions handling |
113+
| **[`import`](#import)** | `{Boolean\|Object}` | `true` | Enables/Disables `@import` at-rules handling |
114+
| **[`modules`](#modules)** | `{Boolean\|String\|Object}` | `{auto: true}` | Enables/Disables CSS Modules and their configuration |
115+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps |
116+
| **[`esModule`](#esmodule)** | `{Boolean}` | `true` | Use ES modules syntax |
118117

119118
### `url`
120119

@@ -250,6 +249,16 @@ module.exports = {
250249

251250
#### `Object`
252251

252+
| Name | Type | Default | Description |
253+
| :-----------------------: | :----------: | :---------: | :--------------------------------------------------------------------- |
254+
| **[`filter`](#filter)** | `{Function}` | `undefined` | Allow to filter `@import` |
255+
| **[`loaders`](#loaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |
256+
257+
##### `filter`
258+
259+
Type: `Function`
260+
Default: `undefined`
261+
253262
Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
254263

255264
**webpack.config.js**
@@ -281,6 +290,47 @@ module.exports = {
281290
};
282291
```
283292

293+
##### `loaders`
294+
295+
Type: `Number`
296+
Default: `0`
297+
298+
Enables/Disables or setups number of loaders applied before CSS loader.
299+
300+
The option `import.loaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
301+
302+
**webpack.config.js**
303+
304+
```js
305+
module.exports = {
306+
module: {
307+
rules: [
308+
{
309+
test: /\.css$/i,
310+
use: [
311+
"style-loader",
312+
{
313+
loader: "css-loader",
314+
options: {
315+
import: {
316+
loaders: 2,
317+
// 0 => no loaders (default);
318+
// 1 => postcss-loader;
319+
// 2 => postcss-loader, sass-loader
320+
},
321+
},
322+
},
323+
"postcss-loader",
324+
"sass-loader",
325+
],
326+
},
327+
],
328+
},
329+
};
330+
```
331+
332+
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
333+
284334
### `modules`
285335

286336
Type: `Boolean|String|Object`
@@ -1063,45 +1113,6 @@ module.exports = {
10631113
};
10641114
```
10651115

1066-
### `importLoaders`
1067-
1068-
Type: `Number`
1069-
Default: `0`
1070-
1071-
Enables/Disables or setups number of loaders applied before CSS loader.
1072-
1073-
The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.
1074-
1075-
**webpack.config.js**
1076-
1077-
```js
1078-
module.exports = {
1079-
module: {
1080-
rules: [
1081-
{
1082-
test: /\.css$/i,
1083-
use: [
1084-
"style-loader",
1085-
{
1086-
loader: "css-loader",
1087-
options: {
1088-
importLoaders: 2,
1089-
// 0 => no loaders (default);
1090-
// 1 => postcss-loader;
1091-
// 2 => postcss-loader, sass-loader
1092-
},
1093-
},
1094-
"postcss-loader",
1095-
"sass-loader",
1096-
],
1097-
},
1098-
],
1099-
},
1100-
};
1101-
```
1102-
1103-
This may change in the future when the module system (i. e. webpack) supports loader matching by origin.
1104-
11051116
### `esModule`
11061117

11071118
Type: `Boolean`

src/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
getPreRequester,
2121
getExportCode,
2222
getFilter,
23+
getImportLoaders,
2324
getImportCode,
2425
getModuleCode,
2526
getModulesPlugins,
@@ -72,7 +73,10 @@ export default async function loader(content, map, meta) {
7273
urlHandler: (url) =>
7374
stringifyRequest(
7475
this,
75-
combineRequests(getPreRequester(this)(options.importLoaders), url)
76+
combineRequests(
77+
getPreRequester(this)(getImportLoaders(options.import.loaders)),
78+
url
79+
)
7680
),
7781
})
7882
);
@@ -124,7 +128,10 @@ export default async function loader(content, map, meta) {
124128
urlHandler: (url) =>
125129
stringifyRequest(
126130
this,
127-
combineRequests(getPreRequester(this)(options.importLoaders), url)
131+
combineRequests(
132+
getPreRequester(this)(getImportLoaders(options.import.loaders)),
133+
url
134+
)
128135
),
129136
})
130137
);

src/options.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@
3030
"properties": {
3131
"filter": {
3232
"instanceof": "Function"
33+
},
34+
"loaders": {
35+
"description": "Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).",
36+
"anyOf": [
37+
{
38+
"type": "boolean"
39+
},
40+
{
41+
"type": "string"
42+
},
43+
{
44+
"type": "integer"
45+
}
46+
]
3347
}
3448
},
3549
"additionalProperties": false
@@ -139,20 +153,6 @@
139153
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
140154
"type": "boolean"
141155
},
142-
"importLoaders": {
143-
"description": "Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).",
144-
"anyOf": [
145-
{
146-
"type": "boolean"
147-
},
148-
{
149-
"type": "string"
150-
},
151-
{
152-
"type": "integer"
153-
}
154-
]
155-
},
156156
"esModule": {
157157
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
158158
"type": "boolean"

src/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ function getFilter(filter, resourcePath) {
256256
};
257257
}
258258

259+
function getImportLoaders(loaders) {
260+
return typeof loaders === "string" ? parseInt(loaders, 10) : loaders;
261+
}
262+
259263
function getValidLocalName(localName, exportLocalsConvention) {
260264
if (exportLocalsConvention === "dashesOnly") {
261265
return dashesCamelCase(localName);
@@ -390,10 +394,6 @@ function normalizeOptions(rawOptions, loaderContext) {
390394
typeof rawOptions.sourceMap === "boolean"
391395
? rawOptions.sourceMap
392396
: loaderContext.sourceMap,
393-
importLoaders:
394-
typeof rawOptions.importLoaders === "string"
395-
? parseInt(rawOptions.importLoaders, 10)
396-
: rawOptions.importLoaders,
397397
esModule:
398398
typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule,
399399
};
@@ -871,6 +871,7 @@ export {
871871
normalizeUrl,
872872
requestify,
873873
getFilter,
874+
getImportLoaders,
874875
getModulesOptions,
875876
getModulesPlugins,
876877
normalizeSourceMap,

test/__snapshots__/loader.test.js.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,9 @@ Array [
773773
774774
exports[`loader should work with none AST metadata: warnings 1`] = `Array []`;
775775
776-
exports[`loader should work with the "modules.auto" option and the "importLoaders" option: errors 1`] = `Array []`;
776+
exports[`loader should work with the "modules.auto" option and the "import.loaders" option: errors 1`] = `Array []`;
777777
778-
exports[`loader should work with the "modules.auto" option and the "importLoaders" option: result 1`] = `
778+
exports[`loader should work with the "modules.auto" option and the "import.loaders" option: result 1`] = `
779779
"/* Pure CSS */
780780
.imported-by-pure {
781781
overflow-x: hidden;
@@ -841,7 +841,7 @@ exports[`loader should work with the "modules.auto" option and the "importLoader
841841
"
842842
`;
843843
844-
exports[`loader should work with the "modules.auto" option and the "importLoaders" option: warnings 1`] = `Array []`;
844+
exports[`loader should work with the "modules.auto" option and the "import.loaders" option: warnings 1`] = `Array []`;
845845
846846
exports[`loader should work with webpackIgnore comment: errors 1`] = `Array []`;
847847

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,60 @@ exports[`validate options should throw an error on the "esModule" option with "t
99
exports[`validate options should throw an error on the "import" option with "() => {}" value 1`] = `
1010
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
1111
- options.import should be one of these:
12-
boolean | object { filter? }
12+
boolean | object { filter?, loaders? }
1313
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
1414
Details:
1515
* options.import should be a boolean.
1616
* options.import should be an object:
17-
object { filter? }"
17+
object { filter?, loaders? }"
1818
`;
1919

2020
exports[`validate options should throw an error on the "import" option with "[]" value 1`] = `
2121
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
2222
- options.import should be one of these:
23-
boolean | object { filter? }
23+
boolean | object { filter?, loaders? }
2424
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
2525
Details:
2626
* options.import should be a boolean.
2727
* options.import should be an object:
28-
object { filter? }"
28+
object { filter?, loaders? }"
2929
`;
3030

3131
exports[`validate options should throw an error on the "import" option with "{"filter":true}" value 1`] = `
3232
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
3333
- options.import.filter should be an instance of function."
3434
`;
3535

36+
exports[`validate options should throw an error on the "import" option with "{"loaders":2.5}" 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 should be one of these:
39+
boolean | object { filter?, loaders? }
40+
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
41+
Details:
42+
* options.import.loaders should be one of these:
43+
boolean | string | integer
44+
-> Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).
45+
Details:
46+
* options.import.loaders should be a boolean.
47+
* options.import.loaders should be a string.
48+
* options.import.loaders should be a integer."
49+
`;
50+
3651
exports[`validate options should throw an error on the "import" option with "{}" value 1`] = `
3752
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
3853
- options.import has an unknown property 'unknown'. These properties are valid:
39-
object { filter? }"
54+
object { filter?, loaders? }"
4055
`;
4156

4257
exports[`validate options should throw an error on the "import" option with "true" value 1`] = `
4358
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
4459
- options.import should be one of these:
45-
boolean | object { filter? }
60+
boolean | object { filter?, loaders? }
4661
-> Enables/Disables '@import' at-rules handling (https://github.com/webpack-contrib/css-loader#import).
4762
Details:
4863
* options.import should be a boolean.
4964
* options.import should be an object:
50-
object { filter? }"
51-
`;
52-
53-
exports[`validate options should throw an error on the "importLoaders" option with "2.5" value 1`] = `
54-
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
55-
- options.importLoaders should be one of these:
56-
boolean | string | integer
57-
-> Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).
58-
Details:
59-
* options.importLoaders should be a boolean.
60-
* options.importLoaders should be a string.
61-
* options.importLoaders should be a integer."
65+
object { filter?, loaders? }"
6266
`;
6367

6468
exports[`validate options should throw an error on the "modules" option with "{"auto":"invalid"}" value 1`] = `
@@ -267,49 +271,49 @@ exports[`validate options should throw an error on the "sourceMap" option with "
267271
exports[`validate options should throw an error on the "unknown" option with "/test/" value 1`] = `
268272
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
269273
- options has an unknown property 'unknown'. These properties are valid:
270-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
274+
object { url?, import?, modules?, sourceMap?, esModule? }"
271275
`;
272276

273277
exports[`validate options should throw an error on the "unknown" option with "[]" value 1`] = `
274278
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
275279
- options has an unknown property 'unknown'. These properties are valid:
276-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
280+
object { url?, import?, modules?, sourceMap?, esModule? }"
277281
`;
278282

279283
exports[`validate options should throw an error on the "unknown" option with "{"foo":"bar"}" value 1`] = `
280284
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
281285
- options has an unknown property 'unknown'. These properties are valid:
282-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
286+
object { url?, import?, modules?, sourceMap?, esModule? }"
283287
`;
284288

285289
exports[`validate options should throw an error on the "unknown" option with "{}" value 1`] = `
286290
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
287291
- options has an unknown property 'unknown'. These properties are valid:
288-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
292+
object { url?, import?, modules?, sourceMap?, esModule? }"
289293
`;
290294

291295
exports[`validate options should throw an error on the "unknown" option with "1" value 1`] = `
292296
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
293297
- options has an unknown property 'unknown'. These properties are valid:
294-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
298+
object { url?, import?, modules?, sourceMap?, esModule? }"
295299
`;
296300

297301
exports[`validate options should throw an error on the "unknown" option with "false" value 1`] = `
298302
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
299303
- options has an unknown property 'unknown'. These properties are valid:
300-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
304+
object { url?, import?, modules?, sourceMap?, esModule? }"
301305
`;
302306

303307
exports[`validate options should throw an error on the "unknown" option with "test" value 1`] = `
304308
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
305309
- options has an unknown property 'unknown'. These properties are valid:
306-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
310+
object { url?, import?, modules?, sourceMap?, esModule? }"
307311
`;
308312

309313
exports[`validate options should throw an error on the "unknown" option with "true" value 1`] = `
310314
"Invalid options object. CSS Loader has been initialized using an options object that does not match the API schema.
311315
- options has an unknown property 'unknown'. These properties are valid:
312-
object { url?, import?, modules?, sourceMap?, importLoaders?, esModule? }"
316+
object { url?, import?, modules?, sourceMap?, esModule? }"
313317
`;
314318

315319
exports[`validate options should throw an error on the "url" option with "() => {}" value 1`] = `

0 commit comments

Comments
 (0)