Skip to content

Commit 853b074

Browse files
refactor: removed importLoaders option in favor import.loaders option (#1300)
1 parent 2f37484 commit 853b074

13 files changed

+662
-641
lines changed

README.md

+58-47
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

+9-2
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

+14-14
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

+5-4
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,

0 commit comments

Comments
 (0)