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

Ensure we can apply classes that are grouped with non-class selectors #6922

Merged
merged 2 commits into from Jan 6, 2022

Conversation

RobinMalfait
Copy link
Contributor

@RobinMalfait RobinMalfait commented Jan 6, 2022

This PR fixes a bug where you can't apply .foo if you defined it as span, .foo {} in your css. This used to work in older AOT mode, but didn't work in JIT mode yet.

When we have a css rule that is defined as .foo, .bar {}, then we will crawl each selector and link it to the same node. This is useful because now our Map looks something like this:

Map(2) { 'foo' => Node {}, 'bar' => Node {} }

This allows us to later on @apply foo or @apply bar and we can do a direct lookup for this "candidate".

When we have css defined as span {}, then we consider this "non-ondemandable". This means that we will always inject these rules into the * section and call it a day.

However, it could happen that you have something like this: span, .foo {} up until now this was totally fine. It contains a non-ondemandable selector (span) and therefore we injected this into that * section.

However, the issue occurs if you now try to @apply foo. Since we had an early return for this use case it didn't endup in our Map from above and now you get an error like:

The `foo` class does not exist. If `foo` is a custom class, make sure it is defined within a `@layer` directive.

So instead what we will do is keep track whether or not a css rule contains any on-demandable classes. If this is the case then we still generate it always by putting it in that * section. However, we will still register all on-demandable classes in our Map (in this case .foo).

This allows us to @apply foo again!

Fixes: #6882

When we have a css rule that is defined as `.foo, .bar {}`, then we will
crawl each selector and link it to the same node. This is useful because
now our Map looks something like this:

```js
Map(2) { 'foo' => Node {}, 'bar' => Node {} }
```

This allows us to later on `@apply foo` or `@apply bar` and we can do a
direct lookup for this "candidate".

When we have css defined as `span {}`, then we consider this
"non-ondemandable". This means that we will _always_ inject these rules
into the `*` section and call it a day.

However, it could happen that you have something like this: `span, .foo
{}` up until now this was totally fine. It contains a non-ondemandable
selector (`span`) and therefore we injected this into that `*` section.

However, the issue occurs if you now try to `@apply foo`. Since we had
an early return for this use case it didn't endup in our Map from above
and now you get an error like:

```
The `foo` class does not exist. If `foo` is a custom class, make sure it
is defined within a `@layer` directive."
```

So instead what we will do is keep track whether or not a css rule
contains any on-demandable classes. If this is the case then we still
generate it always by putting it in that `*` section. However, we will
still register all on-demandable classes in our Map (in this case `.foo`).

This allows us to `@apply foo` again!
@RobinMalfait RobinMalfait merged commit 21fe083 into master Jan 6, 2022
@RobinMalfait RobinMalfait deleted the fix/6882 branch January 6, 2022 12:27
@RobinMalfait RobinMalfait changed the title Ensure we can apply classes defined with non-"on-demandable" selectors Ensure we can apply classes defined with non-class selectors Jan 6, 2022
@RobinMalfait RobinMalfait changed the title Ensure we can apply classes defined with non-class selectors Ensure we can apply classes that are grouped with non-class selectors Jan 6, 2022
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.

@layer declarations not found when using grouped selectors
1 participant