From 35da0f5a6bd1163dfa67047487576cc408d80d2d Mon Sep 17 00:00:00 2001 From: qinmu Date: Fri, 29 Apr 2022 16:08:38 +0800 Subject: [PATCH] fix: fragment property of Empty Component is set as true in dev mode, inconsistent with production mode --- src/compiler/compile/render_dom/Block.ts | 5 ++++- src/compiler/compile/render_dom/index.ts | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/compiler/compile/render_dom/Block.ts b/src/compiler/compile/render_dom/Block.ts index 34c4774804..2d930c0a11 100644 --- a/src/compiler/compile/render_dom/Block.ts +++ b/src/compiler/compile/render_dom/Block.ts @@ -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'); 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 || diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 9d9699bdbf..6c1590d4d1 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -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(); if (has_create_fragment) { body.push(b` function create_fragment(#ctx) { @@ -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'} });`} ${dev_props_check} }