GarthDB / postcss-inherit

A PostCSS port of https://github.com/reworkcss/rework-inherit
Apache License 2.0
16 stars 7 forks source link

Output just the new rules #161

Closed dphochman closed 6 years ago

dphochman commented 7 years ago

How to output just the new rules, and not the rules that did not change, and not the selectors that were already present in the source CSS?

For example, I want

.yellow {color: yellow;}
.gray {color: gray;}
.black {color: black;}
.button {@inherit: .gray, .black;}

to yield:

.button {color: gray;}
.button {color: black;}

postcss-inherit is helping me leverage an unruly stylesheet collection that I inherited, and the option to separate and apply the changes makes it easier to integrate and makes the result much cleaner and easier to understand. Thank you!

GarthDB commented 7 years ago

@dphochman I'm glad it's been useful.

You could use placeholder classes:

%yellow {color: yellow;}
%gray {color: gray;}
%black {color: black;}
.button {@inherit: %gray, %black;}

That should give you the result you're looking for.

dphochman commented 6 years ago

Hi @GarthDB, inserting the % and inheriting from placeholders is definitely helping.

Doing this uncovered some undesirable behavior in postcss-inherit:

  1. @inherit: selector does not inherit selector inside a @media query block, and may throw Could not find rule that matched selector in the same atRule.. Is there an option or a technique to inherit an atRule container if there's a matching selector inside?
  2. @inherit: selector inherits selector and selector:pseudoclass and selector[attr="value"], but does not inherit selector.classname. Is there an option or a pattern or a technique to inherit all the selector.classname variations?

I can probably code my way out of these issues for the project in front of me, but if there's a general solution or approach, perhaps that could be documented or folded back into postcss-inherit.

GarthDB commented 6 years ago

These are both intended behaviors.

  1. In the README it says there is a limitation with media queries:

When in a media query, you can only inherit rules from root, or rules contained in a media query with the same parameters.

This is because media queries mess with cascading, and inheriting across them causes issues. The README explains how you can use this with media queries.

  1. This will get variations of a selector but not descendant or sibling selectors, you'll have to inherit those individually.
JavoByte commented 6 years ago

Is there anyway to get a set of rules of another file and just yield the ones defined in my file? For example

@import "tachyons";

html {
  @inherit: .sans-serif; 
}

To output just

html {
  font-family: /* all the stuff defined in tachyons */
}

Or any suggestion to achieve this behaviour?