Skip to content

Commit 6c60287

Browse files
committed
fix: no inlining for conditionals inside legacy template expressions
1 parent 32a1453 commit 6c60287

File tree

9 files changed

+40
-16
lines changed

9 files changed

+40
-16
lines changed

.changeset/eight-brooms-exist.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: no inlining for conditionals inside legacy template expressions

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import { TransitionDirective } from './visitors/TransitionDirective.js';
6767
import { UpdateExpression } from './visitors/UpdateExpression.js';
6868
import { UseDirective } from './visitors/UseDirective.js';
6969
import { VariableDeclarator } from './visitors/VariableDeclarator.js';
70+
import { ConditionalExpression } from './visitors/ConditionalExpression.js';
7071
import is_reference from 'is-reference';
7172
import { mark_subtree_dynamic } from './visitors/shared/fragment.js';
7273

@@ -177,7 +178,8 @@ const visitors = {
177178
TitleElement,
178179
UpdateExpression,
179180
UseDirective,
180-
VariableDeclarator
181+
VariableDeclarator,
182+
ConditionalExpression
181183
};
182184

183185
/**

packages/svelte/src/compiler/phases/2-analyze/visitors/Attribute.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export function Attribute(node, context) {
3030
}
3131
}
3232

33+
if (node.name === 'autofocus' || node.name === 'muted') {
34+
mark_subtree_dynamic(context.path);
35+
}
36+
3337
if (node.name.startsWith('on')) {
3438
mark_subtree_dynamic(context.path);
3539
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @import { ConditionalExpression } from 'estree' */
2+
/** @import { Context } from '../types' */
3+
4+
import { mark_subtree_dynamic } from './shared/fragment';
5+
6+
/**
7+
* @param {ConditionalExpression} node
8+
* @param {Context} context
9+
*/
10+
export function ConditionalExpression(node, context) {
11+
// In legacy mode, we treat conditionals inside the template as not inlinable so patterns
12+
// such as BROWSER ? foo : bar, continue to work during hydration
13+
if (context.state.expression && !context.state.analysis.runes) {
14+
context.state.expression.can_inline = false;
15+
mark_subtree_dynamic(context.path);
16+
}
17+
}

packages/svelte/src/compiler/phases/2-analyze/visitors/RegularElement.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@ export function RegularElement(node, context) {
7575
node.attributes.push(create_attribute('value', child.start, child.end, [child]));
7676
}
7777

78-
if (
79-
node.attributes.some(
80-
(attribute) =>
81-
attribute.type === 'Attribute' &&
82-
(attribute.name === 'autofocus' || attribute.name === 'muted')
83-
)
84-
) {
85-
mark_subtree_dynamic(context.path);
86-
}
87-
8878
const binding = context.state.scope.get(node.name);
8979
if (
9080
binding !== null &&

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/** @import { ComponentClientTransformState, ComponentContext } from '../../types' */
44
import { normalize_attribute } from '../../../../../../utils.js';
55
import { is_ignored } from '../../../../../state.js';
6-
import { get_attribute_expression, is_event_attribute } from '../../../../../utils/ast.js';
6+
import { is_event_attribute } from '../../../../../utils/ast.js';
77
import * as b from '../../../../../utils/builders.js';
88
import { build_getter, create_derived } from '../../utils.js';
99
import { build_template_chunk, build_update } from './utils.js';

packages/svelte/src/compiler/phases/3-transform/client/visitors/shared/fragment.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ function is_static_element(node) {
142142
return false;
143143
}
144144

145-
if (attribute.name === 'autofocus' || attribute.name === 'muted') {
146-
return false;
147-
}
148-
149145
if (node.name === 'option' && attribute.name === 'value') {
150146
return false;
151147
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
html: `<div title="client">div</div>`
5+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts">
2+
const browser = typeof window !== 'undefined';
3+
</script>
4+
5+
<div title={browser ? "client": "server"}>div</div>

0 commit comments

Comments
 (0)