Skip to content

Commit 143e146

Browse files
feat(plugins/import): add @import filter support (options.import) (#656)
1 parent dad63b9 commit 143e146

File tree

12 files changed

+137
-14
lines changed

12 files changed

+137
-14
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ To disable `url()` resolving by `css-loader` set the option to `false`
103103

104104
#### `{Function}`
105105

106+
106107
**webpack.config.js**
107108
```js
108109
{
@@ -145,6 +146,32 @@ To disable `@import` resolving by `css-loader` set the option to `false`
145146
}
146147
```
147148

149+
#### `{RegExp}`
150+
151+
**webpack.config.js**
152+
```js
153+
{
154+
loader: 'css-loader',
155+
options: {
156+
import: /filter/
157+
}
158+
}
159+
```
160+
161+
#### `{Function}`
162+
163+
**webpack.config.js**
164+
```js
165+
{
166+
loader: 'css-loader',
167+
options: {
168+
import (url) {
169+
return /filter/.test(url)
170+
}
171+
}
172+
}
173+
```
174+
148175
### `minimize`
149176

150177
#### `{Boolean}`

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default function loader(css, map, meta) {
5353

5454
// Import Plugin
5555
if (options.import) {
56-
plugins.push(imports());
56+
plugins.push(imports(options));
5757
}
5858

5959
// Minifier

src/options.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
]
1111
},
1212
"import": {
13-
"type": "boolean"
13+
"anyOf": [
14+
{ "type": "string" },
15+
{ "type": "boolean" },
16+
{ "instanceof": "RegExp" },
17+
{ "instanceof": "Function" }
18+
]
1419
},
1520
"minimize": {
1621
"type": "boolean"

src/plugins/import.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable */
22
import postcss from 'postcss';
33
import valueParser from 'postcss-value-parser';
4-
// ICSS {String}
5-
// import { createICSSRules } from "icss-utils";
4+
5+
const plugin = 'postcss-icss-import';
66

77
const getArg = nodes =>
88
(nodes.length !== 0 && nodes[0].type === 'string'
@@ -38,19 +38,39 @@ const parseImport = (params) => {
3838
};
3939
};
4040

41-
const isExternalUrl = url => /^\w+:\/\//.test(url) || url.startsWith('//');
41+
const URL = /^\w+:\/\//;
42+
43+
const filter = (url, options) => {
44+
if (URL.test(url)) {
45+
return true;
46+
}
47+
48+
if (url.startsWith('//')) {
49+
return true;
50+
}
51+
52+
if (options.import instanceof RegExp) {
53+
return options.import.test(url);
54+
}
55+
56+
if (typeof options.import === 'function') {
57+
return options.import(url);
58+
}
4259

43-
const walkImports = (css, callback) => {
60+
return false;
61+
}
62+
63+
const walkImports = (css, cb) => {
4464
css.each((node) => {
4565
if (node.type === 'atrule' && node.name.toLowerCase() === 'import') {
46-
callback(node);
66+
cb(node);
4767
}
4868
});
4969
};
5070

51-
const plugin = 'postcss-icss-import';
71+
export default postcss.plugin(plugin, (options) => (css, result) => {
72+
let idx = 0;
5273

53-
export default postcss.plugin(plugin, () => (css, result) => {
5474
walkImports(css, (atrule) => {
5575
if (atrule.nodes) {
5676
return result.warn(
@@ -62,15 +82,15 @@ export default postcss.plugin(plugin, () => (css, result) => {
6282
const parsed = parseImport(atrule.params);
6383

6484
if (parsed === null) {
65-
return result.warn(`Unable to find uri in '${atrule.toString()}'`, {
85+
return result.warn(`Unable to find URI in '${atrule.toString()}'`, {
6686
node: atrule,
6787
});
6888
}
6989

7090
let idx = 0;
7191
const url = parsed.url;
7292

73-
if (!isExternalUrl(url)) {
93+
if (!filter(url, options)) {
7494
atrule.remove();
7595

7696
result.messages.push({
@@ -82,4 +102,4 @@ export default postcss.plugin(plugin, () => (css, result) => {
82102
idx++;
83103
}
84104
});
85-
});
105+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import './import.css';
2+
@import './filter/import.css';
3+
4+
.css {
5+
width: 100%;
6+
}
File renamed without changes.

test/fixtures/import/fixture.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import css from './fixture.css';
2+
3+
export default css;
File renamed without changes.

test/options/__snapshots__/import.test.js.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,31 @@ export default \`.fixture {
1111
}
1212
\`"
1313
`;
14+
15+
exports[`Options import {Function} 1`] = `
16+
"// CSS Imports
17+
import CSS__IMPORT__0 from './import.css';
18+
19+
20+
// CSS
21+
export default \`@import './filter/import.css';
22+
23+
.css {
24+
width: 100%;
25+
}
26+
\`"
27+
`;
28+
29+
exports[`Options import {RegExp} 1`] = `
30+
"// CSS Imports
31+
import CSS__IMPORT__0 from './import.css';
32+
33+
34+
// CSS
35+
export default \`@import './filter/import.css';
36+
37+
.css {
38+
width: 100%;
39+
}
40+
\`"
41+
`;

0 commit comments

Comments
 (0)