Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pure ignore comment for CSS Modules #80

Merged
merged 3 commits into from
Nov 5, 2024

Conversation

jantimon
Copy link
Contributor

Add support for selectively opting out of pure CSS Module requirements via /* cssmodules-pure-ignore */ comments

  • Comments only affect the immediately following rule or declaration
  • Parent selectors with ignore comments still localize their children
  • Properties following an ignored property are re-localized
  • The implementation uses PostCSS's comment tracking capabilities for minimal performance impact

Initially I started with the @ prefix (@cssmodules-pure-ignore), but removed based on feedback from @jaggli for cleaner syntax.

fixes #78

Examples

/* cssmodules-pure-ignore */
#third-party-modal-backdrop {
  background: rgba(0, 0, 0, 0.5);
}

.component {
  /* cssmodules-pure-ignore */
  animation-name: fade-out;
  &.isActive { /* <-- still locally scoped */
     animation-name: disabled;  /*  <-- still locally scoped */
  }
}

The comment can include additional explanation text:

/* cssmodules-pure-ignore Modal styling injected into body */
#payment-modal { ... }

Test Cases

  • Basic ignore functionality for selectors
  • Animation property handling
  • Nested selector behavior
  • Comment scope boundaries
  • Comments with additional explanation text
  • Mixed local/global rules

Please let me know if you have more ideas for tests

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, today/tomorrow I plan to finish everything here and make a release

@alexander-akait
Copy link
Member

What about just ignoring the error, i.e. don't fall and don't mix localized logic and error logic, otherwise it leads to confusion in syntax

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
@jantimon
Copy link
Contributor Author

Ah you mean - you still would have to write :global?

I like the idea as it is simpler to implement and more explicit 👍

@alexander-akait
Copy link
Member

Ah you mean - you still would have to write :global?

we basically just need to suppress errors on these comments, they should not have any effect on the generated code

@jantimon jantimon force-pushed the add-pure-ignore branch 2 times, most recently from 1374f5e to c812ff4 Compare October 31, 2024 20:34
@jantimon
Copy link
Contributor Author

jantimon commented Oct 31, 2024

Sounds good - I changed it so now the comment:

  • only affects the next rule
  • doesn't modify the generated code
  • only suppresses the pure mode error

@alexander-akait can you please take another look? :)

@alexander-akait
Copy link
Member

Good job

@jantimon
Copy link
Contributor Author

jantimon commented Nov 1, 2024

postcss-modules-local-by-default removes :global during processing

should we also remove the cssmodules-pure-ignore comment?

I guess the generated css would be more readable without those extra comments

@alexander-akait
Copy link
Member

@jantimon Yeah, sounds good for me If it doesn't hurt performance

@alexander-akait
Copy link
Member

@jantimon can you rebase?

@jantimon
Copy link
Contributor Author

jantimon commented Nov 2, 2024

@alexander-akait done - the only conflict was the readme

@@ -615,13 +651,17 @@ module.exports = (options = {}) => {
context.options = options;
context.localAliasMap = localAliasMap;

if (pureMode && context.hasPureGlobals) {
const ignoreComment = pureMode ? getIgnoreComment(rule) : undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid extra loops when mode is not pure - better perf

@alexander-akait alexander-akait merged commit 9d07eeb into css-modules:master Nov 5, 2024
19 checks passed
@jantimon jantimon deleted the add-pure-ignore branch November 6, 2024 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

opt-out mechanism for unavoidable global CSS scenarios
2 participants