Skip to content

feat: deprecate svelte:component #12694

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

Merged
merged 5 commits into from
Aug 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/big-apes-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

feat: deprecate `svelte:component`
8 changes: 8 additions & 0 deletions packages/svelte/messages/compile-warnings/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ This code will work when the component is rendered on the client (which is why t

> Using `<slot>` to render parent content is deprecated. Use `{@render ...}` tags instead

## svelte_component_deprecated

> `<svelte:component>` is deprecated in runes mode — components are dynamic by default

In previous versions of Svelte, the component constructor was fixed when the component was rendered. In other words, if you wanted `<X>` to re-render when `X` changed, you would either have to use `<svelte:component this={X}>` or put the component inside a `{#key X}...{/key}` block.

In Svelte 5 this is no longer true — if `X` changes, `<X>` re-renders. For more complex expressions like `condition ? Y : Z` you can use a derived value instead.

## svelte_element_invalid_this

> `this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/** @import { SvelteComponent } from '#compiler' */
/** @import { Context } from '../types' */
import * as w from '../../../warnings.js';
import { visit_component } from './shared/component.js';

/**
* @param {SvelteComponent} node
* @param {Context} context
*/
export function SvelteComponent(node, context) {
if (context.state.analysis.runes) {
w.svelte_component_deprecated(node);
}

visit_component(node, context);
}
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const codes = [
"event_directive_deprecated",
"node_invalid_placement_ssr",
"slot_element_deprecated",
"svelte_component_deprecated",
"svelte_element_invalid_this"
];

Expand Down Expand Up @@ -767,6 +768,14 @@ export function slot_element_deprecated(node) {
w(node, "slot_element_deprecated", "Using `<slot>` to render parent content is deprecated. Use `{@render ...}` tags instead");
}

/**
* `<svelte:component>` is deprecated in runes mode — components are dynamic by default
* @param {null | NodeLike} node
*/
export function svelte_component_deprecated(node) {
w(node, "svelte_component_deprecated", "`<svelte:component>` is deprecated in runes mode — components are dynamic by default");
}

/**
* `this` should be an `{expression}`. Using a string attribute value will cause an error in future versions of Svelte
* @param {null | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"line": 13
}
},
{
"code": "svelte_component_deprecated",
"end": {
"column": 45,
"line": 15
},
"message": "`<svelte:component>` is deprecated in runes mode — components are dynamic by default",
"start": {
"column": 0,
"line": 15
}
},
{
"code": "attribute_quoted",
"message": "Quoted attributes on components and custom elements will be stringified in a future version of Svelte. If this isn't what you want, remove the quotes",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<svelte:options runes />

<script>
import A from './A.svelte';
</script>

<svelte:component this={A} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"code": "svelte_component_deprecated",
"message": "`<svelte:component>` is deprecated in runes mode — components are dynamic by default",
"start": {
"line": 7,
"column": 0
},
"end": {
"line": 7,
"column": 29
}
}
]
Loading