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

[fix]destroy empty component #7492

Merged
merged 13 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/compile/render_dom/Block.ts
Expand Up @@ -428,13 +428,16 @@ export default class Block {
}

has_content(): boolean {
// exclude "ThrowStatment" type node
// as in dev mode, 'ThrowStatement' type node will always be added, which will make has_content() always return true in dev mode
const validClaims = this.chunks.claim.filter(i => Array.isArray(i) && i[0] && i[0].type !== 'ThrowStatement');
tanhauhau marked this conversation as resolved.
Show resolved Hide resolved
return !!this.first ||
this.event_listeners.length > 0 ||
this.chunks.intro.length > 0 ||
this.chunks.outro.length > 0 ||
this.chunks.create.length > 0 ||
this.chunks.hydrate.length > 0 ||
this.chunks.claim.length > 0 ||
validClaims.length > 0 ||
this.chunks.mount.length > 0 ||
this.chunks.update.length > 0 ||
this.chunks.destroy.length > 0 ||
Expand Down
8 changes: 5 additions & 3 deletions src/compiler/compile/render_dom/index.ts
Expand Up @@ -333,8 +333,10 @@ export default function dom(
// $$props arg is still needed for unknown prop check
args.push(x`$$props`);
}

const has_create_fragment = component.compile_options.dev || block.has_content();
// fix: remove "component.compile_options.dev" condition, which
// will set has_create_fragment always be true in dev mode,
// inconsistent with the behavior in production mode.
const has_create_fragment = block.has_content();
magentaqin marked this conversation as resolved.
Show resolved Hide resolved
if (has_create_fragment) {
body.push(b`
function create_fragment(#ctx) {
Expand Down Expand Up @@ -593,7 +595,7 @@ export default function dom(
constructor(options) {
super(${options.dev && 'options'});
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, ${optional_parameters});
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: ${has_create_fragment ? 'create_fragment.name' : 'this.name'} });`}
tanhauhau marked this conversation as resolved.
Show resolved Hide resolved

${dev_props_check}
}
Expand Down
5 changes: 4 additions & 1 deletion src/runtime/internal/transitions.ts
Expand Up @@ -79,6 +79,9 @@ export function transition_out(block: Fragment, local: 0 | 1, detach?: 0 | 1, ca
});

block.o(local);
// fix: destroy non-fragment element such as empty components.
tanhauhau marked this conversation as resolved.
Show resolved Hide resolved
} else if (typeof callback === 'function') {
tanhauhau marked this conversation as resolved.
Show resolved Hide resolved
callback();
}
}

Expand Down Expand Up @@ -143,7 +146,7 @@ export function create_in_transition(node: Element & ElementCSSInlineStyle, fn:
return {
start() {
if (started) return;

started = true;
delete_rule(node);

Expand Down