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

Dont create a fragment at all for DOM-less component #3831

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/compiler/compile/render_dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,21 @@ export default class Block {
return body;
}

has_content() {
return this.renderer.options.dev ||
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 ||
this.chunks.mount.length > 0 ||
this.chunks.update.length > 0 ||
this.chunks.destroy.length > 0 ||
this.has_animation;
}

render() {
const key = this.key && this.get_unique_name('key');

Expand Down
17 changes: 11 additions & 6 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,16 @@ export default function dom(
args.push(x`$$props`, x`$$invalidate`);
}

body.push(b`
function create_fragment(#ctx) {
${block.get_contents()}
}
const has_create_fragment = block.has_content();
if (has_create_fragment) {
body.push(b`
function create_fragment(#ctx) {
${block.get_contents()}
}
`);
}

body.push(b`
${component.extract_javascript(component.ast.module)}

${component.fully_hoisted}
Expand Down Expand Up @@ -437,7 +442,7 @@ export default function dom(

${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}

@init(this, { target: this.shadowRoot }, ${definition}, create_fragment, ${not_equal}, ${prop_names});
@init(this, { target: this.shadowRoot }, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_names});

${dev_props_check}

Expand Down Expand Up @@ -489,7 +494,7 @@ export default function dom(
constructor(options) {
super(${options.dev && `options`});
${should_add_css && b`if (!@_document.getElementById("${component.stylesheet.id}-style")) ${add_css}();`}
@init(this, options, ${definition}, create_fragment, ${not_equal}, ${prop_names});
@init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment': 'null'}, ${not_equal}, ${prop_names});
${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`}

${dev_props_check}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,12 @@ export default class InlineComponentWrapper extends Wrapper {
`);

block.chunks.create.push(
b`if (${name}) ${name}.$$.fragment.c();`
b`if (${name}) @create_component(${name}.$$.fragment);`
);

if (parent_nodes && this.renderer.options.hydratable) {
block.chunks.claim.push(
b`if (${name}) ${name}.$$.fragment.l(${parent_nodes});`
b`if (${name}) @claim_component(${name}.$$.fragment, ${parent_nodes});`
);
}

Expand Down Expand Up @@ -437,7 +437,7 @@ export default class InlineComponentWrapper extends Wrapper {
${munged_bindings}
${munged_handlers}

${name}.$$.fragment.c();
@create_component(${name}.$$.fragment);
@transition_in(${name}.$$.fragment, 1);
@mount_component(${name}, ${update_mount_node}, ${anchor});
} else {
Expand Down Expand Up @@ -472,11 +472,11 @@ export default class InlineComponentWrapper extends Wrapper {
${munged_handlers}
`);

block.chunks.create.push(b`${name}.$$.fragment.c();`);
block.chunks.create.push(b`@create_component(${name}.$$.fragment);`);

if (parent_nodes && this.renderer.options.hydratable) {
block.chunks.claim.push(
b`${name}.$$.fragment.l(${parent_nodes});`
b`@claim_component(${name}.$$.fragment, ${parent_nodes});`
);
}

Expand Down
46 changes: 36 additions & 10 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ import { blank_object, is_function, run, run_all, noop, has_prop } from './utils
import { children } from './dom';
import { transition_in } from './transitions';

interface Fragment {
key: string|null;
first: null;
/* create */ c: () => void;
/* claim */ l: (nodes: any) => void;
/* hydrate */ h: () => void;
/* mount */ m: (target: HTMLElement, anchor: any) => void;
/* update */ p: (changed: any, ctx: any) => void;
/* measure */ r: () => void;
/* fix */ f: () => void;
/* animate */ a: () => void;
/* intro */ i: (local: any) => void;
/* outro */ o: (local: any) => void;
/* destroy */ d: (detaching: 0|1) => void;
}
// eslint-disable-next-line @typescript-eslint/class-name-casing
interface T$$ {
dirty: null;
Expand All @@ -13,7 +28,7 @@ interface T$$ {
callbacks: any;
after_update: any[];
props: Record<string, 0 | string>;
fragment: null|any;
fragment: null|false|Fragment;
not_equal: any;
before_update: any[];
context: Map<any, any>;
Expand All @@ -29,10 +44,18 @@ export function bind(component, name, callback) {
}
}

export function create_component(block) {
block && block.c();
}

export function claim_component(block, parent_nodes) {
block && block.l(parent_nodes);
}

export function mount_component(component, target, anchor) {
const { fragment, on_mount, on_destroy, after_update } = component.$$;

fragment.m(target, anchor);
fragment && fragment.m(target, anchor);

// onMount happens before the initial afterUpdate
add_render_callback(() => {
Expand All @@ -51,15 +74,16 @@ export function mount_component(component, target, anchor) {
}

export function destroy_component(component, detaching) {
if (component.$$.fragment) {
run_all(component.$$.on_destroy);
const $$ = component.$$;
if ($$.fragment !== null) {
run_all($$.on_destroy);

component.$$.fragment.d(detaching);
$$.fragment && $$.fragment.d(detaching);

// TODO null out other refs, including component.$$ (but need to
// preserve final state?)
component.$$.on_destroy = component.$$.fragment = null;
component.$$.ctx = {};
$$.on_destroy = $$.fragment = null;
$$.ctx = {};
}
}

Expand Down Expand Up @@ -115,15 +139,17 @@ export function init(component, options, instance, create_fragment, not_equal, p
$$.update();
ready = true;
run_all($$.before_update);
$$.fragment = create_fragment($$.ctx);

// `false` as a special case of no DOM component
$$.fragment = create_fragment ? create_fragment($$.ctx) : false;

if (options.target) {
if (options.hydrate) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment!.l(children(options.target));
$$.fragment && $$.fragment!.l(children(options.target));
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
$$.fragment!.c();
$$.fragment && $$.fragment!.c();
}

if (options.intro) transition_in(component.$$.fragment);
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/internal/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ export function flush() {
}

function update($$) {
if ($$.fragment) {
if ($$.fragment !== null) {
$$.update($$.dirty);
run_all($$.before_update);
$$.fragment.p($$.dirty, $$.ctx);
$$.fragment && $$.fragment.p($$.dirty, $$.ctx);
$$.dirty = null;

$$.after_update.forEach(add_render_callback);
Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/component-static-array/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
Expand All @@ -15,7 +16,7 @@ function create_fragment(ctx) {

return {
c() {
nested.$$.fragment.c();
create_component(nested.$$.fragment);
},
m(target, anchor) {
mount_component(nested, target, anchor);
Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/component-static-immutable/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
Expand All @@ -15,7 +16,7 @@ function create_fragment(ctx) {

return {
c() {
nested.$$.fragment.c();
create_component(nested.$$.fragment);
},
m(target, anchor) {
mount_component(nested, target, anchor);
Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/component-static-immutable2/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
Expand All @@ -15,7 +16,7 @@ function create_fragment(ctx) {

return {
c() {
nested.$$.fragment.c();
create_component(nested.$$.fragment);
},
m(target, anchor) {
mount_component(nested, target, anchor);
Expand Down
5 changes: 3 additions & 2 deletions test/js/samples/component-static-var/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SvelteComponent,
create_component,
destroy_component,
detach,
element,
Expand Down Expand Up @@ -28,9 +29,9 @@ function create_fragment(ctx) {

return {
c() {
foo.$$.fragment.c();
create_component(foo.$$.fragment);
t0 = space();
bar.$$.fragment.c();
create_component(bar.$$.fragment);
t1 = space();
input = element("input");
dispose = listen(input, "input", ctx.input_input_handler);
Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/component-static/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
Expand All @@ -15,7 +16,7 @@ function create_fragment(ctx) {

return {
c() {
nested.$$.fragment.c();
create_component(nested.$$.fragment);
},
m(target, anchor) {
mount_component(nested, target, anchor);
Expand Down
14 changes: 1 addition & 13 deletions test/js/samples/component-store-file-invalidate/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@ import {
SvelteComponent,
component_subscribe,
init,
noop,
safe_not_equal,
set_store_value
} from "svelte/internal";

import { count } from "./store.js";

function create_fragment(ctx) {
return {
c: noop,
m: noop,
p: noop,
i: noop,
o: noop,
d: noop
};
}

function instance($$self, $$props, $$invalidate) {
let $count;
component_subscribe($$self, count, $$value => $$invalidate("$count", $count = $$value));
Expand All @@ -34,7 +22,7 @@ function instance($$self, $$props, $$invalidate) {
class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, { increment: 0 });
init(this, options, instance, null, safe_not_equal, { increment: 0 });
}

get increment() {
Expand Down
15 changes: 2 additions & 13 deletions test/js/samples/computed-collapsed-if/expected.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import { SvelteComponent, init, noop, safe_not_equal } from "svelte/internal";

function create_fragment(ctx) {
return {
c: noop,
m: noop,
p: noop,
i: noop,
o: noop,
d: noop
};
}
import { SvelteComponent, init, safe_not_equal } from "svelte/internal";

function instance($$self, $$props, $$invalidate) {
let { x } = $$props;
Expand All @@ -32,7 +21,7 @@ function instance($$self, $$props, $$invalidate) {
class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, { x: 0, a: 0, b: 0 });
init(this, options, instance, null, safe_not_equal, { x: 0, a: 0, b: 0 });
}

get a() {
Expand Down
15 changes: 2 additions & 13 deletions test/js/samples/deconflict-globals/expected.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { SvelteComponent, init, noop, safe_not_equal } from "svelte/internal";
import { SvelteComponent, init, safe_not_equal } from "svelte/internal";
import { onMount } from "svelte";

function create_fragment(ctx) {
return {
c: noop,
m: noop,
p: noop,
i: noop,
o: noop,
d: noop
};
}

function instance($$self, $$props, $$invalidate) {
let { foo = "bar" } = $$props;

Expand All @@ -29,7 +18,7 @@ function instance($$self, $$props, $$invalidate) {
class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, { foo: 0 });
init(this, options, instance, null, safe_not_equal, { foo: 0 });
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/dynamic-import/expected.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
SvelteComponent,
create_component,
destroy_component,
init,
mount_component,
Expand All @@ -17,7 +18,7 @@ function create_fragment(ctx) {

return {
c() {
lazyload.$$.fragment.c();
create_component(lazyload.$$.fragment);
},
m(target, anchor) {
mount_component(lazyload, target, anchor);
Expand Down
Loading