Skip to content

Commit ccdf3a7

Browse files
committed
fix: @attach opt's you into runes mode
1 parent 42e7e81 commit ccdf3a7

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

.changeset/famous-rocks-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: `@attach` opt's you into runes mode

documentation/docs/03-template-syntax/[email protected]

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Attachments are functions that run when an element is mounted to the DOM. Option
77
> [!NOTE]
88
> Attachments are available in Svelte 5.29 and newer.
99
10+
> [!NOTE]
11+
> Attachments also opt you in for runes mode so if you use them in a legacy component you would have to also migrate that component.
12+
1013
```svelte
1114
<!--- file: App.svelte --->
1215
<script>

documentation/docs/99-legacy/00-legacy-overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ The following pages document these features for
99
- people still using Svelte 3/4
1010
- people using Svelte 5, but with components that haven't yet been migrated
1111

12-
Since Svelte 3/4 syntax still works in Svelte 5, we will distinguish between _legacy mode_ and _runes mode_. Once a component is in runes mode (which you can opt into by using runes, or by explicitly setting the `runes: true` compiler option), legacy mode features are no longer available.
12+
Since Svelte 3/4 syntax still works in Svelte 5, we will distinguish between _legacy mode_ and _runes mode_. Once a component is in runes mode (which you can opt into by using runes or attachments, or by explicitly setting the `runes: true` compiler option), legacy mode features are no longer available.
1313

1414
If you're exclusively interested in the Svelte 3/4 syntax, you can browse its documentation at [v4.svelte.dev](https://v4.svelte.dev).

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,23 @@ export function analyze_component(root, source, options) {
403403

404404
const component_name = get_component_name(options.filename);
405405

406-
const runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune);
406+
let runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune);
407+
408+
// if we are not in runes mode by the bindings we need to check if there's any attachment
409+
if (!runes) {
410+
// we need to do it beforehand otherwise we would not be in runes mode until we hit an attachment
411+
walk(
412+
/** @type {AST.SvelteNode} */ (template.ast),
413+
{},
414+
{
415+
AttachTag(_, context) {
416+
runes = true;
417+
// once we found one we can stop the traversal
418+
context.stop();
419+
}
420+
}
421+
);
422+
}
407423

408424
if (!runes) {
409425
for (let check of synthetic_stores_legacy_check) {

0 commit comments

Comments
 (0)