Skip to content

Commit 5115545

Browse files
authored
Add ignoreRule option (kristerkari#232)
* Add ignoreRule option * Add new option to README
1 parent 28f0bec commit 5115545

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@ transform(`
6969
}
7070
```
7171

72+
### `ignoreRule` option
73+
74+
```js
75+
transform(
76+
`
77+
.foo {
78+
color: red;
79+
}
80+
.bar {
81+
font-size: 12px;
82+
}
83+
`,
84+
{
85+
ignoreRule: (selector) => {
86+
if (selector === ".foo") {
87+
return true;
88+
}
89+
},
90+
},
91+
);
92+
```
93+
94+
↓ ↓ ↓ ↓ ↓ ↓
95+
96+
```js
97+
{
98+
bar: {
99+
fontSize: 12;
100+
}
101+
}
102+
```
103+
72104
### CSS Modules :export block
73105

74106
Parsing the [CSS Modules (ICSS) :export](https://github.com/css-modules/icss#export) is supported. The `:export` is often used to share variables from CSS or from a preprocessor like Sass/Less/Stylus to Javascript:

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export default function transform(
22
css: string,
33
options?: {
4+
ignoreRule?: (selector: string) => boolean;
45
parseMediaQueries?: boolean;
56
},
67
): { [selector: string]: unknown };

src/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ const transform = (css, options) => {
111111
continue;
112112
}
113113

114+
if (
115+
typeof options?.ignoreRule === "function" &&
116+
options.ignoreRule(rule.selectors[s]) === true
117+
) {
118+
continue;
119+
}
120+
114121
const selector = rule.selectors[s].replace(/^\./, "");
115122
const styles = (result[selector] = result[selector] || {});
116123
transformDecls(styles, rule.declarations, result);
@@ -155,6 +162,13 @@ const transform = (css, options) => {
155162
for (const r in rule.rules) {
156163
const ruleRule = rule.rules[r];
157164
for (const s in ruleRule.selectors) {
165+
if (
166+
typeof options?.ignoreRule === "function" &&
167+
options.ignoreRule(ruleRule.selectors[s]) === true
168+
) {
169+
continue;
170+
}
171+
158172
result[media] = result[media] || {};
159173
const selector = ruleRule.selectors[s].replace(/^\./, "");
160174
const mediaStyles = (result[media][selector] =

src/index.spec.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,81 @@ describe("rem unit", () => {
26702670
});
26712671
});
26722672

2673+
describe("ignoreRule option", () => {
2674+
it("should allow to ignore a selector completely", () => {
2675+
expect(
2676+
transform(
2677+
`
2678+
.foo {
2679+
color: red;
2680+
}
2681+
.bar {
2682+
font-size: 12px;
2683+
}
2684+
`,
2685+
{
2686+
ignoreRule: (selector) => {
2687+
if (selector === ".foo") {
2688+
return true;
2689+
}
2690+
},
2691+
},
2692+
),
2693+
).toEqual({
2694+
bar: { fontSize: 12 },
2695+
});
2696+
});
2697+
2698+
it("should do nothing when returing false", () => {
2699+
expect(
2700+
transform(
2701+
`
2702+
.foo {
2703+
color: red;
2704+
}
2705+
.bar {
2706+
font-size: 12px;
2707+
}
2708+
`,
2709+
{
2710+
ignoreRule: (selector) => {
2711+
if (selector === ".bar") {
2712+
return false;
2713+
}
2714+
if (selector === ".foo") {
2715+
return false;
2716+
}
2717+
},
2718+
},
2719+
),
2720+
).toEqual({
2721+
bar: { fontSize: 12 },
2722+
foo: { color: "red" },
2723+
});
2724+
});
2725+
2726+
it("should not error out even if ignoreRule is not a function", () => {
2727+
expect(
2728+
transform(
2729+
`
2730+
.foo {
2731+
color: red;
2732+
}
2733+
.bar {
2734+
font-size: 12px;
2735+
}
2736+
`,
2737+
{
2738+
ignoreRule: true,
2739+
},
2740+
),
2741+
).toEqual({
2742+
bar: { fontSize: 12 },
2743+
foo: { color: "red" },
2744+
});
2745+
});
2746+
});
2747+
26732748
describe("viewport units", () => {
26742749
it("should transform viewport units", () => {
26752750
expect(
@@ -3195,6 +3270,48 @@ describe("media queries", () => {
31953270
});
31963271
});
31973272

3273+
it("should support media queries + ignoreRule option", () => {
3274+
expect(
3275+
transform(
3276+
`
3277+
.container {
3278+
background-color: #f00;
3279+
}
3280+
3281+
@media (orientation: landscape) {
3282+
.container {
3283+
background-color: #00f;
3284+
}
3285+
}
3286+
`,
3287+
{
3288+
ignoreRule: (selector) => {
3289+
if (selector === ".container") {
3290+
return true;
3291+
}
3292+
},
3293+
parseMediaQueries: true,
3294+
},
3295+
),
3296+
).toEqual({
3297+
__mediaQueries: {
3298+
"@media (orientation: landscape)": [
3299+
{
3300+
expressions: [
3301+
{
3302+
feature: "orientation",
3303+
modifier: undefined,
3304+
value: "landscape",
3305+
},
3306+
],
3307+
inverse: false,
3308+
type: "all",
3309+
},
3310+
],
3311+
},
3312+
});
3313+
});
3314+
31983315
it("should throw for invalid types", () => {
31993316
expect(() =>
32003317
transform(

0 commit comments

Comments
 (0)