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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export default class Component {

this.walk_instance_js_post_template();

this.stylesheet.recompute_stylesheet_hash(this);

if (!compile_options.customElement) this.stylesheet.reify();

this.stylesheet.warn_on_unused_selectors(this);
Expand Down Expand Up @@ -1450,4 +1452,4 @@ function get_relative_path(from: string, to: string) {
}

return from_parts.concat(to_parts).join('/');
}
}
29 changes: 29 additions & 0 deletions src/compiler/compile/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,35 @@ export default class Stylesheet {
}
}

recompute_stylesheet_hash(component: Component) {
const collectIf = (test_function) => (acc, child) => {
if (child instanceof Atrule) {
child.children.forEach(rule => {
if (test_function(rule)) {
acc.push(rule);
}
});
} else {
if (test_function(child)) {
acc.push(child);
}
}
return acc;
};

const used_rules = this.children.reduce(collectIf(rule => rule.is_used(component.compile_options.dev)), []);
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?

acc += component.source.charAt(i);
}
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.

}
}

reify() {
this.nodes_with_css_class.forEach((node: Element) => {
node.add_css_class();
Expand Down
10 changes: 10 additions & 0 deletions test/runtime/samples/css-style-hashing/Component1.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="component-1 red"> Red </div>

<style>
.red {
background: red;
}
.yellow {
background: yellow;
}
</style>
11 changes: 11 additions & 0 deletions test/runtime/samples/css-style-hashing/Component2.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="component-2 red"> Red </div>
<div class="component-2 yellow"> Yellow </div>

<style>
.red {
background: red;
}
.yellow {
background: yellow;
}
</style>
10 changes: 10 additions & 0 deletions test/runtime/samples/css-style-hashing/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
test({ assert, component, target, window }) {
const [component_one_red] = target.querySelectorAll('.component-1');
const [component_two_red, component_two_yellow] = target.querySelectorAll('.component-2');

assert.equal(window.getComputedStyle(component_one_red).background, 'red');
assert.equal(window.getComputedStyle(component_two_red).background, 'red');
assert.equal(window.getComputedStyle(component_two_yellow).background, 'yellow');
}
};
7 changes: 7 additions & 0 deletions test/runtime/samples/css-style-hashing/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Component1 from './Component1.svelte'
import Component2 from './Component2.svelte'
</script>

<Component1 />
<Component2 />