Skip to content

Commit 5e702e7

Browse files
feat: allow filtering urls (#856)
1 parent 9642aa5 commit 5e702e7

8 files changed

+383
-30
lines changed

README.md

+49-22
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,41 @@ module.exports = {
114114

115115
## Options
116116

117-
| Name | Type | Default | Description |
118-
| :-----------------------------------------: | :-----------------: | :-------------: | :------------------------------------------ |
119-
| **[`url`](#url)** | `{Boolean}` | `true` | Enable/Disable `url()` handling |
120-
| **[`import`](#import)** | `{Boolean}` | `true` | Enable/Disable @import handling |
121-
| **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
122-
| **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
123-
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
124-
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
125-
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
126-
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
117+
| Name | Type | Default | Description |
118+
| :-----------------------------------------: | :-------------------: | :-------------: | :------------------------------------------ |
119+
| **[`url`](#url)** | `{Boolean\|Function}` | `true` | Enable/Disable `url()` handling |
120+
| **[`import`](#import)** | `{Boolean}` | `true` | Enable/Disable @import handling |
121+
| **[`modules`](#modules)** | `{Boolean\|String}` | `false` | Enable/Disable CSS Modules and setup mode |
122+
| **[`localIdentName`](#localidentname)** | `{String}` | `[hash:base64]` | Configure the generated ident |
123+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `false` | Enable/Disable Sourcemaps |
124+
| **[`camelCase`](#camelcase)** | `{Boolean\|String}` | `false` | Export Classnames in CamelCase |
125+
| **[`importLoaders`](#importloaders)** | `{Number}` | `0` | Number of loaders applied before CSS loader |
126+
| **[`exportOnlyLocals`](#exportonlylocals)** | `{Boolean}` | `false` | Export only locals |
127127

128128
### `url`
129129

130-
Type: `Boolean`
130+
Type: `Boolean|Function`
131131
Default: `true`
132132

133-
Enable/disable `url()` resolving. Absolute `urls` are not resolving by default.
133+
Control `url()` resolving. Absolute `urls` are not resolving by default.
134+
135+
Examples resolutions:
136+
137+
```
138+
url(image.png) => require('./image.png')
139+
url(./image.png) => require('./image.png')
140+
```
141+
142+
To import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
143+
144+
```
145+
url(~module/image.png) => require('module/image.png')
146+
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
147+
```
148+
149+
#### `Boolean`
150+
151+
Enable/disable `url()` resolving.
134152

135153
**webpack.config.js**
136154

@@ -150,18 +168,27 @@ module.exports = {
150168
};
151169
```
152170

153-
Examples resolutions:
171+
#### `Function`
154172

155-
```
156-
url(image.png) => require('./image.png')
157-
url(./image.png) => require('./image.png')
158-
```
159-
160-
To import assets from a `node_modules` path (include `resolve.modules`) and for `alias`, prefix it with a `~`:
173+
Allow to filter `url()`. All filtered `url()` will not be resolved.
161174

162-
```
163-
url(~module/image.png) => require('module/image.png')
164-
url(~aliasDirectory/image.png) => require('otherDirectory/image.png')
175+
```js
176+
module.exports = {
177+
module: {
178+
rules: [
179+
{
180+
test: /\.css$/,
181+
loader: 'css-loader',
182+
options: {
183+
url: (url, resourcePath) => {
184+
// `url()` with `img.png` stay untouched
185+
return url.includes('img.png');
186+
},
187+
},
188+
},
189+
],
190+
},
191+
};
165192
```
166193

167194
### `import`

src/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getImportPrefix,
2727
placholderRegExps,
2828
dashesCamelCase,
29+
getFilter,
2930
} from './utils';
3031
import Warning from './Warning';
3132
import CssSyntaxError from './CssSyntaxError';
@@ -66,9 +67,6 @@ export default function loader(content, map, meta) {
6667
}
6768
}
6869

69-
const resolveImport = options.import !== false;
70-
const resolveUrl = options.url !== false;
71-
7270
const plugins = [];
7371

7472
if (options.modules) {
@@ -100,14 +98,16 @@ export default function loader(content, map, meta) {
10098
);
10199
}
102100

103-
if (resolveImport) {
101+
if (options.import !== false) {
104102
plugins.push(importParser());
105103
}
106104

107-
if (resolveUrl) {
105+
if (options.url !== false) {
108106
plugins.push(
109107
urlParser({
110-
filter: (value) => isUrlRequest(value),
108+
filter: getFilter(options.url, this.resourcePath, (value) =>
109+
isUrlRequest(value)
110+
),
111111
})
112112
);
113113
}

src/options.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
"additionalProperties": false,
33
"properties": {
44
"url": {
5-
"type": "boolean"
5+
"anyOf": [
6+
{
7+
"type": "boolean"
8+
},
9+
{
10+
"instanceof": "Function"
11+
}
12+
]
613
},
714
"import": {
815
"type": "boolean"

src/utils.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,24 @@ function getLocalIdent(loaderContext, localIdentName, localName, options) {
6161
.replace(/^((-?[0-9])|--)/, '_$1');
6262
}
6363

64-
export { getImportPrefix, getLocalIdent, placholderRegExps, dashesCamelCase };
64+
function getFilter(filter, resourcePath, defaultFilter = null) {
65+
return (content) => {
66+
if (defaultFilter && !defaultFilter(content)) {
67+
return false;
68+
}
69+
70+
if (typeof filter === 'function') {
71+
return !filter(content, resourcePath);
72+
}
73+
74+
return true;
75+
};
76+
}
77+
78+
export {
79+
getImportPrefix,
80+
getLocalIdent,
81+
placholderRegExps,
82+
dashesCamelCase,
83+
getFilter,
84+
};

test/__snapshots__/errors.test.js.snap

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ exports[`validation 1`] = `
44
"CSS Loader Invalid Options
55
66
options.url should be boolean
7+
options.url should pass \\"instanceof\\" keyword validation
8+
options.url should match some schema in anyOf
79
"
810
`;
911

0 commit comments

Comments
 (0)