Skip to content

Commit 5ad396b

Browse files
committed
refactor add styles method call to reduce generated output
1 parent bf528d1 commit 5ad396b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/compiler/compile/render_dom/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,10 @@ export default function dom(
528528
class ${name} extends ${superclass} {
529529
constructor(options) {
530530
super(${options.dev && 'options'});
531-
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, ${should_add_css ? add_css : x`@noop`}, ${dirty});
531+
532+
${should_add_css && b`@addCssToComponent(this, add_css, options);`}
533+
534+
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, ${dirty});
532535
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}
533536
534537
${dev_props_check}

src/runtime/internal/Component.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface T$$ {
3434
on_mount: any[];
3535
on_destroy: any[];
3636
skip_bound: boolean;
37-
customStyleTag: HTMLElement;
37+
customStyleTag?: HTMLElement;
3838
}
3939

4040
export function bind(component, name, callback) {
@@ -97,18 +97,26 @@ function make_dirty(component, i) {
9797
component.$$.dirty[(i / 31) | 0] |= (1 << (i % 31));
9898
}
9999

100-
export function init(component, options, instance, create_fragment, not_equal, props, add_css, dirty = [-1]) {
100+
101+
export function addCssToComponent(that, add_css, options) {
102+
that.$$ = {
103+
customStyleTag: options.customStyleTag || current_component && current_component.$$.customStyleTag
104+
};
105+
add_css(that.$$.customStyleTag);
106+
}
107+
108+
export function init(component, options, instance, create_fragment, not_equal, props, dirty = [-1]) {
101109
const parent_component = current_component;
102110
set_current_component(component);
103111

104112
const prop_values = options.props || {};
105113

106114
const $$: T$$ = component.$$ = {
115+
...component.$$,
116+
107117
fragment: null,
108118
ctx: null,
109119

110-
customStyleTag: options.customStyleTag || parent_component && parent_component.$$.customStyleTag,
111-
112120
// state
113121
props,
114122
update: noop,
@@ -130,8 +138,6 @@ export function init(component, options, instance, create_fragment, not_equal, p
130138

131139
let ready = false;
132140

133-
add_css($$.customStyleTag);
134-
135141
$$.ctx = instance
136142
? instance(component, prop_values, (i, ret, ...rest) => {
137143
const value = rest.length ? rest[0] : ret;

0 commit comments

Comments
 (0)