Skip to content

Commit 16e5130

Browse files
feat(url): add url() filter support (options.url)
1 parent d9f6bba commit 16e5130

18 files changed

+233
-408
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export default `
7474
}
7575
`
7676
```
77+
7778
#### `{Boolean}`
7879

7980
To disable `url()` resolving by `css-loader` set the option to `false`
@@ -88,6 +89,32 @@ To disable `url()` resolving by `css-loader` set the option to `false`
8889
}
8990
```
9091

92+
#### `{RegExp}`
93+
94+
**webpack.config.js**
95+
```js
96+
{
97+
loader: 'css-loader',
98+
options: {
99+
url: /filter/
100+
}
101+
}
102+
```
103+
104+
#### `{Function}`
105+
106+
**webpack.config.js**
107+
```js
108+
{
109+
loader: 'css-loader',
110+
options: {
111+
url (url) {
112+
return /filter/.test(url)
113+
}
114+
}
115+
}
116+
```
117+
91118
### `import`
92119

93120
```css

src/index.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getOptions } from 'loader-utils';
99
import validateOptions from 'schema-utils';
1010

1111
import postcss from 'postcss';
12-
// TODO(michael-ciniawsky)
12+
// TODO(michael-ciniawsky)
1313
// replace with postcss-icss-{url, import}
1414
import urls from './plugins/url';
1515
import imports from './plugins/import';
@@ -26,13 +26,9 @@ const DEFAULTS = {
2626
sourceMap: false,
2727
};
2828

29-
export default function loader (css, map, meta) {
29+
export default function loader(css, map, meta) {
3030
// Loader Options
31-
const options = Object.assign(
32-
{},
33-
DEFAULTS,
34-
getOptions(this)
35-
);
31+
const options = Object.assign({}, DEFAULTS, getOptions(this));
3632

3733
validateOptions(schema, options, 'CSS Loader');
3834

@@ -52,9 +48,8 @@ export default function loader (css, map, meta) {
5248

5349
// URL Plugin
5450
if (options.url) {
55-
plugins.push(urls());
51+
plugins.push(urls(options));
5652
}
57-
5853

5954
// Import Plugin
6055
if (options.import) {
@@ -65,7 +60,7 @@ export default function loader (css, map, meta) {
6560
if (options.minimize) {
6661
plugins.push(minifier());
6762
}
68-
63+
6964
if (meta) {
7065
const { ast } = meta;
7166
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader')
@@ -74,34 +69,33 @@ export default function loader (css, map, meta) {
7469
css = ast.root;
7570
}
7671
}
77-
78-
map = options.sourceMap
72+
73+
map = options.sourceMap
7974
? {
80-
prev: map || false,
81-
inline: false,
82-
annotation: false,
83-
sourcesContent: true,
84-
}
85-
: false
86-
75+
prev: map || false,
76+
inline: false,
77+
annotation: false,
78+
sourcesContent: true,
79+
}
80+
: false;
81+
8782
return postcss(plugins)
8883
.process(css, {
8984
from: `/css-loader!${file}`,
9085
map,
9186
to: file,
92-
}).then(({ css, map, messages }) => {
87+
})
88+
.then(({ css, map, messages }) => {
9389
if (meta && meta.messages) {
94-
messages = messages.concat(meta.messages)
90+
messages = messages.concat(meta.messages);
9591
}
96-
92+
9793
// CSS Imports
9894
const imports = messages
99-
.filter((msg) => msg.type === 'import' ? msg : false)
95+
.filter((msg) => (msg.type === 'import' ? msg : false))
10096
.reduce((imports, msg) => {
10197
try {
102-
msg = typeof msg.import === 'function'
103-
? msg.import()
104-
: msg.import;
98+
msg = typeof msg.import === 'function' ? msg.import() : msg.import;
10599

106100
imports += msg;
107101
} catch (err) {
@@ -110,17 +104,15 @@ export default function loader (css, map, meta) {
110104
this.emitError(err);
111105
}
112106

113-
return imports
114-
}, '')
115-
107+
return imports;
108+
}, '');
109+
116110
// CSS Exports
117111
const exports = messages
118-
.filter((msg) => msg.type === 'export' ? msg : false)
119-
.reduce((exports, msg) => {
120-
try {
121-
msg = typeof msg.export === 'function'
122-
? msg.export()
123-
: msg.export;
112+
.filter((msg) => (msg.type === 'export' ? msg : false))
113+
.reduce((exports, msg) => {
114+
try {
115+
msg = typeof msg.export === 'function' ? msg.export() : msg.export;
124116

125117
exports += msg;
126118
} catch (err) {
@@ -130,23 +122,27 @@ export default function loader (css, map, meta) {
130122
}
131123

132124
return exports;
133-
}, '')
134-
135-
// TODO(michael-ciniawsky)
125+
}, '');
126+
127+
// TODO(michael-ciniawsky)
136128
// triage if and add CSS runtime back
137129
const result = [
138130
imports ? `// CSS Imports\n${imports}\n` : false,
139131
exports ? `// CSS Exports\n${exports}\n` : false,
140-
`// CSS\nexport default \`${css}\``
132+
`// CSS\nexport default \`${css}\``,
141133
]
142134
.filter(Boolean)
143135
.join('\n');
144-
136+
145137
cb(null, result, map ? map.toJSON() : null);
146138

147139
return null;
148140
})
149141
.catch((err) => {
150-
err.name === 'CssSyntaxError' ? cb(new SyntaxError(err)) : cb(err);
142+
err = err.name === 'CssSyntaxError' ? new SyntaxError(err) : err;
143+
144+
cb(err);
145+
146+
return null;
151147
});
152-
};
148+
}

src/lib/Error.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/lib/plugins/import.js

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)