css-modules / postcss-modules-local-by-default

PostCSS plugin for css modules to local-scope classes and ids
MIT License
25 stars 13 forks source link

The combination of a global and a local selector should be a local selector #73

Open jantimon opened 1 month ago

jantimon commented 1 month ago

html { a_value: some-value; } is not local - and therefore css-modules/postcss-modules-local-by-default marks them as not pure.

However if only local selectors are inside that non local selector, it is still fine as all declarations are wrapped by a local selector. The following examples are pure:

html { 
  .foo { 
     a_value: some-value; 
  } 
}
html { 
 .foo { a_value: some-value; } 
 .bar { a_value: some-value; }
}

As soon as a non local declaration gets added it still be marked as non pure. The following examples are NOT pure:

html { 
 .foo { a_value: some-value; } 
 button { a_value: some-value; } 
}
html { 
  .foo { 
     a_value: some-value; 
  } 
  color: red;
}
html { 
 .foo { a_value: some-value; } 
 button { 
   color: red;
   .bar { a_value: some-value; }
  } 
}

Especially the second case will be tricky to implement in the current code as by now only the selectors are analyzed to find out wether the code is pure or not.

In future we would have to iterate over every declaration and check for its surrounding selector wether it is pure or not.

jantimon commented 1 month ago

I created a small next.js demo project with the following css code:

.page {
  button {
    color: green;
  }
}

Stackblitz without postcss-nesting without postcss-nesting .page { button { .. } } is not pure:

shot-MPwBLLsD@2x

however the same css code works once you add postcss-nesting

Stackblitz with postcss-nesting shot-mTFxVBKN@2x

to align both cases I created #72 - and to proof that .page button I created a pr with a test case in #74