Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/svelte-meta-component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

feat: include the `component` instance an element was rendered in in the dev `__svelte_meta`
Comment thread
dummdidumm marked this conversation as resolved.
Resolved
5 changes: 3 additions & 2 deletions packages/svelte/src/internal/client/dev/elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { COMMENT_NODE, DOCUMENT_FRAGMENT_NODE, ELEMENT_NODE } from '#client/constants';
import { HYDRATION_END, HYDRATION_START } from '../../../constants.js';
import { hydrating } from '../dom/hydration.js';
import { dev_stack } from '../context.js';
import { component_context, dev_stack } from '../context.js';

/**
* @param {any} fn
Expand Down Expand Up @@ -30,7 +30,8 @@ function assign_location(element, filename, location) {
// @ts-expect-error
element.__svelte_meta = {
parent: dev_stack,
loc: { file: filename, line: location[0], column: location[1] }
loc: { file: filename, line: location[0], column: location[1] },
component: component_context
Comment thread
vercel[bot] marked this conversation as resolved.
Resolved
};

if (location[2]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export function element(node, get_tag, is_svg, render_fn, get_namespace, locatio
file: filename,
line: location[0],
column: location[1]
}
},
component: component_context
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
mode: ['client', 'hydrate'],
compileOptions: {
dev: true
},
html: `
<p>main</p>
<button>toggle</button>
<p>main if</p>
<p>child</p>
<p>child each</p>
<p>child</p>
<p>child each</p>
<p>passthrough</p>
<p>main snippet</p>
<p>main dynamic</p>
`,

test({ target, assert }) {
/** @param {Element} element */
const component = (element) => /** @type {any} */ (element).__svelte_meta.component;

const check = () => {
const [
main,
main_if,
child1,
child1_each,
child2,
child2_each,
passthrough,
snippet,
dynamic
] = target.querySelectorAll('p');

assert.ok(component(main));
// the instance that rendered the element, also inside blocks
assert.equal(component(main_if), component(main));
assert.equal(component(dynamic), component(main));

// each instance of a component is its own context
assert.ok(component(child1));
assert.notEqual(component(child1), component(main));
assert.notEqual(component(child1), component(child2));
assert.equal(component(child1_each), component(child1));
assert.equal(component(child2_each), component(child2));

// a snippet is rendered in the component that renders it, like the `parent` stack
assert.equal(component(snippet), component(passthrough));
};

check();

// re-created elements belong to the same instance
const button = /** @type {HTMLButtonElement} */ (target.querySelector('button'));
flushSync(() => button.click());
flushSync(() => button.click());
check();
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>child</p>

{#each [1]}
<p>child each</p>
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script lang="ts">
import Child from './child.svelte';
import Passthrough from './passthrough.svelte';
let show = $state(true);
</script>

<p>main</p>
<button onclick={() => (show = !show)}>toggle</button>

{#if show}
<p>main if</p>
{/if}

<Child />
<Child />

<Passthrough>
<p>main snippet</p>
</Passthrough>

<svelte:element this={'p'}>main dynamic</svelte:element>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts">
let { children } = $props();
</script>

<p>passthrough</p>
{@render children()}
Loading