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

Recomputes css hash id when unused selectors are present #4326

Conversation

colincasey
Copy link
Contributor

This is an attempt to fix #4313 which demonstrates that incorrect css can be produced when two components have the same style rules and the first component to be included has unused selectors.

This is an attempt to fix sveltejs#4313 which demonstrates that incorrect css can be produced when two components have the same style rules and the first component to be included has unused selectors.
const unused_rules = this.children.reduce(collectIf(rule => !rule.is_used(component.compile_options.dev)), []);
if (unused_rules.length > 0) {
const new_hash = hash(used_rules.reduce((acc, rule: Rule) => {
for (let i = rule.node.start; i < rule.node.end; i++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there an easier way to get the raw text of a node?

}
return acc;
}, ''));
this.id = `svelte-${new_hash}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to me, reassigning the stylesheet id here seems clunky even though it does fix the reported bug. i would've preferred to set the id accounting for the unused selectors in the Stylesheet constructor but, at this stage, Rule.is_used() only returns false. it isn't until after the html Fragment is created that the dom nodes are paired with the css selectors and Rule.is_used() can accurately report which are unused. any suggestions on how to improve this flow would be appreciated.

@Conduitry
Copy link
Member

The id being recomputed doesn't just feel clunky, I think it indicates that there's a situation this isn't handling. If I comment out the first place where this.id is assigned, the test suite has lots of errors and exceptions, which means that something is attempting to use the old id value.

A proper solution for this sounds like it will probably involve multiple sweeps through the template AST: one to note which elements need the scoping class added to them and to determine which selectors are needed, and another to apply the scoping class whose id will depend on which selectors were kept.

@tanhauhau
Copy link
Member

or another solution is to include the template into the computation of the hash too.

the template indirectly have the information of used/unused selectors, which is part of the recomputing of hash.

@Conduitry
Copy link
Member

Using the template in the hash would probably be the simplest. That's how it used to work before #1091. I'm not completely clear on the reasoning for the switch, and unfortunately the gitter chat room has been deleted so we can't see what i18n thing it was also intended to help with.

@Conduitry Conduitry closed this Feb 23, 2020
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.

CSS isolation bug when components styles is equals and have not used css rule
3 participants