Skip to content

refactor: removed importLoaders option in favor import.loaders option #1300

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

Merged
merged 2 commits into from
May 1, 2021
Merged
Show file tree
Hide file tree
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
105 changes: 58 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ module.exports = {

## Options

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

### `url`

Expand Down Expand Up @@ -250,6 +249,16 @@ module.exports = {

#### `Object`

| Name | Type | Default | Description |
| :-----------------------: | :----------: | :---------: | :--------------------------------------------------------------------- |
| **[`filter`](#filter)** | `{Function}` | `undefined` | Allow to filter `@import` |
| **[`loaders`](#loaders)** | `{Number}` | `0` | Enables/Disables or setups number of loaders applied before CSS loader |

##### `filter`

Type: `Function`
Default: `undefined`

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

**webpack.config.js**
Expand Down Expand Up @@ -281,6 +290,47 @@ module.exports = {
};
```

##### `loaders`

Type: `Number`
Default: `0`

Enables/Disables or setups number of loaders applied before CSS loader.

The option `import.loaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
"style-loader",
{
loader: "css-loader",
options: {
import: {
loaders: 2,
// 0 => no loaders (default);
// 1 => postcss-loader;
// 2 => postcss-loader, sass-loader
},
},
},
"postcss-loader",
"sass-loader",
],
},
],
},
};
```

This may change in the future when the module system (i. e. webpack) supports loader matching by origin.

### `modules`

Type: `Boolean|String|Object`
Expand Down Expand Up @@ -1063,45 +1113,6 @@ module.exports = {
};
```

### `importLoaders`

Type: `Number`
Default: `0`

Enables/Disables or setups number of loaders applied before CSS loader.

The option `importLoaders` allows you to configure how many loaders before `css-loader` should be applied to `@import`ed resources.

**webpack.config.js**

```js
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: [
"style-loader",
{
loader: "css-loader",
options: {
importLoaders: 2,
// 0 => no loaders (default);
// 1 => postcss-loader;
// 2 => postcss-loader, sass-loader
},
},
"postcss-loader",
"sass-loader",
],
},
],
},
};
```

This may change in the future when the module system (i. e. webpack) supports loader matching by origin.

### `esModule`

Type: `Boolean`
Expand Down
11 changes: 9 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getPreRequester,
getExportCode,
getFilter,
getImportLoaders,
getImportCode,
getModuleCode,
getModulesPlugins,
Expand Down Expand Up @@ -72,7 +73,10 @@ export default async function loader(content, map, meta) {
urlHandler: (url) =>
stringifyRequest(
this,
combineRequests(getPreRequester(this)(options.importLoaders), url)
combineRequests(
getPreRequester(this)(getImportLoaders(options.import.loaders)),
url
)
),
})
);
Expand Down Expand Up @@ -124,7 +128,10 @@ export default async function loader(content, map, meta) {
urlHandler: (url) =>
stringifyRequest(
this,
combineRequests(getPreRequester(this)(options.importLoaders), url)
combineRequests(
getPreRequester(this)(getImportLoaders(options.import.loaders)),
url
)
),
})
);
Expand Down
28 changes: 14 additions & 14 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
"properties": {
"filter": {
"instanceof": "Function"
},
"loaders": {
"description": "Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).",
"anyOf": [
{
"type": "boolean"
},
{
"type": "string"
},
{
"type": "integer"
}
]
}
},
"additionalProperties": false
Expand Down Expand Up @@ -139,20 +153,6 @@
"description": "Enables/Disables generation of source maps (https://github.com/webpack-contrib/css-loader#sourcemap).",
"type": "boolean"
},
"importLoaders": {
"description": "Enables/Disables or setups number of loaders applied before CSS loader (https://github.com/webpack-contrib/css-loader#importloaders).",
"anyOf": [
{
"type": "boolean"
},
{
"type": "string"
},
{
"type": "integer"
}
]
},
"esModule": {
"description": "Use the ES modules syntax (https://github.com/webpack-contrib/css-loader#esmodule).",
"type": "boolean"
Expand Down
9 changes: 5 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ function getFilter(filter, resourcePath) {
};
}

function getImportLoaders(loaders) {
return typeof loaders === "string" ? parseInt(loaders, 10) : loaders;
}

function getValidLocalName(localName, exportLocalsConvention) {
if (exportLocalsConvention === "dashesOnly") {
return dashesCamelCase(localName);
Expand Down Expand Up @@ -390,10 +394,6 @@ function normalizeOptions(rawOptions, loaderContext) {
typeof rawOptions.sourceMap === "boolean"
? rawOptions.sourceMap
: loaderContext.sourceMap,
importLoaders:
typeof rawOptions.importLoaders === "string"
? parseInt(rawOptions.importLoaders, 10)
: rawOptions.importLoaders,
esModule:
typeof rawOptions.esModule === "undefined" ? true : rawOptions.esModule,
};
Expand Down Expand Up @@ -871,6 +871,7 @@ export {
normalizeUrl,
requestify,
getFilter,
getImportLoaders,
getModulesOptions,
getModulesPlugins,
normalizeSourceMap,
Expand Down
Loading