Skip to content

Commit 5e6034c

Browse files
feat: allow to filter import at-rules (#857)
1 parent 5e702e7 commit 5e6034c

File tree

7 files changed

+375
-19
lines changed

7 files changed

+375
-19
lines changed

README.md

+61-17
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ module.exports = {
117117
| Name | Type | Default | Description |
118118
| :-----------------------------------------: | :-------------------: | :-------------: | :------------------------------------------ |
119119
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
120-
| **[`import`](#import)** | `{Boolean}` | `true` | Enable/Disable @import handling |
120+
| **[`import`](#import)** | `{Boolean\/Function}` | `true` | Enable/Disable @import handling |
121121
| **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
122122
| **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
123123
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
@@ -130,19 +130,24 @@ module.exports = {
130130
Type: `Boolean|Function`
131131
Default: `true`
132132

133-
Control `url()` resolving. Absolute `urls` are not resolving by default.
133+
Control `url()` resolving. Absolute urls are not resolving.
134134

135135
Examples resolutions:
136136

137137
```
138138
url(image.png) => require('./image.png')
139+
url('image.png') => require('./image.png')
139140
url(./image.png) => require('./image.png')
141+
url('./image.png') => require('./image.png')
142+
url('http://dontwritehorriblecode.com/2112.png') => require('http://dontwritehorriblecode.com/2112.png')
143+
image-set(url('image2x.png') 1x, url('image1x.png') 2x) => require('./image1x.png') and require('./image2x.png')
140144
```
141145

142146
To import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
143147

144148
```
145149
url(~module/image.png) => require('module/image.png')
150+
url('~module/image.png') => require('module/image.png')
146151
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
147152
```
148153

@@ -170,7 +175,9 @@ module.exports = {
170175

171176
#### `Function`
172177

173-
Allow to filter `url()`. All filtered `url()` will not be resolved.
178+
Allow to filter `url()`. All filtered `url()` will not be resolved (left in the code as they were written).
179+
180+
**webpack.config.js**
174181

175182
```js
176183
module.exports = {
@@ -181,6 +188,8 @@ module.exports = {
181188
loader: 'css-loader',
182189
options: {
183190
url: (url, resourcePath) => {
191+
// resourcePath - path to css file
192+
184193
// `url()` with `img.png` stay untouched
185194
return url.includes('img.png');
186195
},
@@ -196,7 +205,31 @@ module.exports = {
196205
Type: `Boolean`
197206
Default: `true`
198207

199-
Enable/disable `@import` resolving. Absolute urls are not resolving.
208+
Control `@import` resolving. Absolute urls in `@import` will be moved in runtime code.
209+
210+
Examples resolutions:
211+
212+
```
213+
@import 'style.css' => require('./style.css')
214+
@import url(style.css) => require('./style.css')
215+
@import url('style.css') => require('./style.css')
216+
@import './style.css' => require('./style.css')
217+
@import url(./style.css) => require('./style.css')
218+
@import url('./style.css') => require('./style.css')
219+
@import url('http://dontwritehorriblecode.com/style.css') => @import url('http://dontwritehorriblecode.com/style.css') in runtime
220+
```
221+
222+
To import styles from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
223+
224+
```
225+
@import url(~module/style.css) => require('module/style.css')
226+
@import url('~module/style.css') => require('module/style.css')
227+
@import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')
228+
```
229+
230+
#### `Boolean`
231+
232+
Enable/disable `@import` resolving.
200233

201234
**webpack.config.js**
202235

@@ -216,22 +249,33 @@ module.exports = {
216249
};
217250
```
218251

219-
Examples resolutions:
252+
#### `Function`
220253

221-
```
222-
@import 'style.css' => require('./style.css')
223-
@import url(style.css) => require('./style.css')
224-
@import url('style.css') => require('./style.css')
225-
@import './style.css' => require('./style.css')
226-
@import url(./style.css) => require('./style.css')
227-
@import url('./style.css') => require('./style.css')
228-
```
254+
Allow to filter `@import`. All filtered `@import` will not be resolved (left in the code as they were written).
229255

230-
To import styles from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
256+
**webpack.config.js**
231257

232-
```
233-
@import url(~module/style.css) => require('module/style.css')
234-
@import url(~aliasDirectory/style.css) => require('otherDirectory/style.css')
258+
```js
259+
module.exports = {
260+
module: {
261+
rules: [
262+
{
263+
test: /\.css$/,
264+
loader: 'css-loader',
265+
options: {
266+
import: (parsedImport, resourcePath) => {
267+
// parsedImport.url - url of `@import`
268+
// parsedImport.media - media query of `@import`
269+
// resourcePath - path to css file
270+
271+
// `@import` with `style.css` stay untouched
272+
return parsedImport.url.includes('style.css');
273+
},
274+
},
275+
},
276+
],
277+
},
278+
};
235279
```
236280

237281
### [`modules`](https://github.com/css-modules/css-modules)

src/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ export default function loader(content, map, meta) {
9999
}
100100

101101
if (options.import !== false) {
102-
plugins.push(importParser());
102+
plugins.push(
103+
importParser({
104+
filter: getFilter(options.import, this.resourcePath),
105+
})
106+
);
103107
}
104108

105109
if (options.url !== false) {

src/options.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212
]
1313
},
1414
"import": {
15-
"type": "boolean"
15+
"anyOf": [
16+
{
17+
"type": "boolean"
18+
},
19+
{
20+
"instanceof": "Function"
21+
}
22+
]
1623
},
1724
"modules": {
1825
"anyOf": [

test/__snapshots__/errors.test.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ exports[`validation 2`] = `
1313
"CSS Loader Invalid Options
1414
1515
options.import should be boolean
16+
options.import should pass \\"instanceof\\" keyword validation
17+
options.import should match some schema in anyOf
1618
"
1719
`;
1820

0 commit comments

Comments
 (0)