Skip to content

Commit 5a99783

Browse files
0xcaffRyanZim
authored andcommitted
Add Filter Parameter (#327)
This allows for conditionally processing imports based on their URLs. Closes #279, #254, #299, #259
1 parent 7c863ea commit 5a99783

File tree

9 files changed

+122
-0
lines changed

9 files changed

+122
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ If this behavior is not what you want, look at `skipDuplicates` option
3535
please look at
3636
[postcss-easy-import](https://github.com/trysound/postcss-easy-import)
3737
(which use this plugin under the hood).
38+
- Imports which are not modified (by `options.filter` or because they are remote
39+
imports) are moved to the top of the output.
3840
- **This plugin attempts to follow the CSS `@import` spec**; `@import`
3941
statements must precede all other statements (besides `@charset`).
4042

@@ -110,6 +112,14 @@ Checkout the [tests](test) for more examples.
110112

111113
### Options
112114

115+
### `filter`
116+
Type: `Function`
117+
Default: `() => true`
118+
119+
Only transform imports for which the test function returns `true`. Imports for
120+
which the test function returns `false` will be left as is. The function gets
121+
the path to import as an argument and should return a boolean.
122+
113123
#### `root`
114124

115125
Type: `String`

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ function applyMedia(bundle) {
109109
function applyStyles(bundle, styles) {
110110
styles.nodes = []
111111

112+
// Strip additional statements.
112113
bundle.forEach(stmt => {
113114
if (stmt.type === "import") {
114115
stmt.node.parent = undefined
@@ -140,6 +141,11 @@ function parseStyles(result, styles, options, state, media) {
140141
return
141142
}
142143

144+
if (options.filter && !options.filter(stmt.uri)) {
145+
// rejected by filter
146+
return
147+
}
148+
143149
return resolveImportId(result, stmt, options, state)
144150
})
145151
}, Promise.resolve())

test/filter.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// external tooling
2+
import test from "ava"
3+
4+
// internal tooling
5+
import checkFixture from "./helpers/check-fixture"
6+
7+
test("should filter all imported stylesheets", checkFixture, "filter-all", {
8+
filter: () => false,
9+
})
10+
11+
test("should filter some stylesheets", checkFixture, "filter-some", {
12+
filter: url => url !== "foobar.css",
13+
})
14+
15+
test("shouldn't accept ignored stylesheets", checkFixture, "filter-ignore", {
16+
filter: () => true,
17+
})

test/fixtures/filter-all.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import "foo.css";
2+
@import "foo.css" screen;
3+
@import 'bar.css';
4+
@import 'bar.css' screen;
5+
@import url(baz.css);
6+
@import url(baz.css) screen;
7+
@import url("foobar.css");
8+
@import url("foobar.css") screen and (min-width: 25em);
9+
@import url('foobarbaz.css');
10+
@import url('foobarbaz.css') print, screen and (min-width: 25em);
11+
12+
content{}

test/fixtures/filter-all.expected.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import "foo.css";
2+
@import "foo.css" screen;
3+
@import 'bar.css';
4+
@import 'bar.css' screen;
5+
@import url(baz.css);
6+
@import url(baz.css) screen;
7+
@import url("foobar.css");
8+
@import url("foobar.css") screen and (min-width: 25em);
9+
@import url('foobarbaz.css');
10+
@import url('foobarbaz.css') print, screen and (min-width: 25em);
11+
content{}

test/fixtures/filter-ignore.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import "ignore.css" (min-width: 25em);
2+
@import "http://css";
3+
@import "https://css";
4+
@import 'http://css';
5+
@import 'https://css';
6+
@import url(http://css);
7+
@import url(https://css);
8+
@import url("http://css");
9+
@import url("https://css");
10+
@import url('http://css');
11+
@import url('https://css');
12+
@import url("//css");
13+
@import url('//css');
14+
@import url(//css);
15+
content{}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@import "http://css" (min-width: 25em);
2+
@import "http://css-screen" (min-width: 25em) and screen;
3+
@import "http://css";
4+
@import "https://css";
5+
@import 'http://css';
6+
@import 'https://css';
7+
@import url(http://css);
8+
@import url(https://css);
9+
@import url("http://css");
10+
@import url("https://css");
11+
@import url('http://css');
12+
@import url('https://css');
13+
@import url("//css");
14+
@import url('//css');
15+
@import url(//css);
16+
@media (min-width: 25em){
17+
ignore{}
18+
}
19+
content{}

test/fixtures/filter-some.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import "foo.css";
2+
@import "foo.css" screen;
3+
@import 'bar.css';
4+
@import 'bar.css' screen;
5+
@import url(baz.css);
6+
@import url(baz.css) screen;
7+
@import url("foobar.css");
8+
@import url("foobar.css") screen and (min-width: 25em);
9+
@import url('foobarbaz.css');
10+
@import url('foobarbaz.css') print, screen and (min-width: 25em);
11+
12+
content{}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
@import url("foobar.css");
3+
@import url("foobar.css") screen and (min-width: 25em);
4+
foo{}
5+
@media screen{
6+
foo{}
7+
}
8+
bar{}
9+
@media screen{
10+
bar{}
11+
}
12+
baz{}
13+
@media screen{
14+
baz{}
15+
}
16+
foobarbaz{}
17+
@media print, screen and (min-width: 25em){
18+
foobarbaz{}
19+
}
20+
content{}

0 commit comments

Comments
 (0)