-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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++) { | ||
acc += component.source.charAt(i); | ||
} | ||
return acc; | ||
}, '')); | ||
this.id = `svelte-${new_hash}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to me, reassigning the stylesheet |
||
} | ||
} | ||
|
||
reify() { | ||
this.nodes_with_css_class.forEach((node: Element) => { | ||
node.add_css_class(); | ||
|
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> |
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> |
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'); | ||
} | ||
}; |
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 /> |
There was a problem hiding this comment.
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?