Skip to content

Commit cc1f2c6

Browse files
authored
remove unneeded fragment block (#4568)
1 parent a0749f6 commit cc1f2c6

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

src/compiler/compile/render_dom/Block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ export default class Block {
419419
return body;
420420
}
421421

422-
has_content() {
423-
return this.first ||
422+
has_content(): boolean {
423+
return !!this.first ||
424424
this.event_listeners.length > 0 ||
425425
this.chunks.intro.length > 0 ||
426426
this.chunks.outro.length > 0 ||

src/compiler/compile/render_dom/Renderer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,8 @@ export default class Renderer {
284284

285285
return node;
286286
}
287+
288+
remove_block(block: Block | Node | Node[]) {
289+
this.blocks.splice(this.blocks.indexOf(block), 1);
290+
}
287291
}

src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export default class InlineComponentWrapper extends Wrapper {
155155
// removing empty slot
156156
for (const slot of this.slots.keys()) {
157157
if (!this.slots.get(slot).block.has_content()) {
158+
this.renderer.remove_block(this.slots.get(slot).block);
158159
this.slots.delete(slot);
159160
}
160161
}

src/compiler/compile/render_dom/wrappers/Slot.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,23 @@ export default class SlotWrapper extends Wrapper {
114114
get_slot_context_fn = 'null';
115115
}
116116

117+
let has_fallback = !!this.fallback;
117118
if (this.fallback) {
118119
this.fragment.render(this.fallback, null, x`#nodes` as Identifier);
120+
has_fallback = this.fallback.has_content();
121+
if (!has_fallback) {
122+
renderer.remove_block(this.fallback);
123+
}
119124
}
120125

121126
const slot = block.get_unique_name(`${sanitize(slot_name)}_slot`);
122127
const slot_definition = block.get_unique_name(`${sanitize(slot_name)}_slot_template`);
123-
const slot_or_fallback = this.fallback ? block.get_unique_name(`${sanitize(slot_name)}_slot_or_fallback`) : slot;
128+
const slot_or_fallback = has_fallback ? block.get_unique_name(`${sanitize(slot_name)}_slot_or_fallback`) : slot;
124129

125130
block.chunks.init.push(b`
126131
const ${slot_definition} = ${renderer.reference('$$slots')}.${slot_name};
127132
const ${slot} = @create_slot(${slot_definition}, #ctx, ${renderer.reference('$$scope')}, ${get_slot_context_fn});
128-
${this.fallback ? b`const ${slot_or_fallback} = ${slot} || ${this.fallback.name}(#ctx);` : null}
133+
${has_fallback ? b`const ${slot_or_fallback} = ${slot} || ${this.fallback.name}(#ctx);` : null}
129134
`);
130135

131136
block.chunks.create.push(
@@ -161,7 +166,7 @@ export default class SlotWrapper extends Wrapper {
161166

162167
const dynamic_dependencies = Array.from(this.dependencies).filter(is_dependency_dynamic);
163168

164-
const fallback_dynamic_dependencies = this.fallback
169+
const fallback_dynamic_dependencies = has_fallback
165170
? Array.from(this.fallback.dependencies).filter(is_dependency_dynamic)
166171
: [];
167172

@@ -173,7 +178,7 @@ export default class SlotWrapper extends Wrapper {
173178
);
174179
}
175180
`;
176-
const fallback_update = this.fallback && fallback_dynamic_dependencies.length > 0 && b`
181+
const fallback_update = has_fallback && fallback_dynamic_dependencies.length > 0 && b`
177182
if (${slot_or_fallback} && ${slot_or_fallback}.p && ${renderer.dirty(fallback_dynamic_dependencies)}) {
178183
${slot_or_fallback}.p(#ctx, #dirty);
179184
}

0 commit comments

Comments
 (0)