diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 9822529ece..9f16b3c7b9 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -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); @@ -1450,4 +1452,4 @@ function get_relative_path(from: string, to: string) { } return from_parts.concat(to_parts).join('/'); -} \ No newline at end of file +} diff --git a/src/compiler/compile/css/Stylesheet.ts b/src/compiler/compile/css/Stylesheet.ts index 246dab0f12..4d1003a385 100644 --- a/src/compiler/compile/css/Stylesheet.ts +++ b/src/compiler/compile/css/Stylesheet.ts @@ -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}`; + } + } + reify() { this.nodes_with_css_class.forEach((node: Element) => { node.add_css_class(); diff --git a/test/runtime/samples/css-style-hashing/Component1.svelte b/test/runtime/samples/css-style-hashing/Component1.svelte new file mode 100644 index 0000000000..6d8a8ceb76 --- /dev/null +++ b/test/runtime/samples/css-style-hashing/Component1.svelte @@ -0,0 +1,10 @@ +