Skip to content

Commit 2dd2821

Browse files
committed
fix: revert splitting templates and generate deriveds instead
1 parent 839f8d3 commit 2dd2821

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,7 @@ export function RegularElement(node, context) {
212212
node,
213213
node_id,
214214
// If value binding exists, that one takes care of calling $.init_select
215-
value_binding === null && node.name === 'select',
216-
class_directives.some((cd) => cd.metadata.expression.has_call) ||
217-
style_directives.some((sd) => sd.metadata.expression.has_call)
215+
value_binding === null && node.name === 'select'
218216
);
219217
is_attributes_reactive = true;
220218
} else {
@@ -456,9 +454,10 @@ function build_element_spread_attributes(
456454
context,
457455
element,
458456
element_id,
459-
needs_select_handling,
460-
needs_isolation = false
457+
needs_select_handling
461458
) {
459+
let needs_isolation = false;
460+
462461
/** @type {ObjectExpression['properties']} */
463462
const values = [];
464463

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/** @import { ComponentContext } from '../../types' */
44
import { normalize_attribute } from '../../../../../../utils.js';
55
import * as b from '../../../../../utils/builders.js';
6-
import { build_getter } from '../../utils.js';
6+
import { build_getter, create_derived } from '../../utils.js';
77
import { build_template_literal, build_update } from './utils.js';
88

99
/**
@@ -29,21 +29,29 @@ export function build_style_directives(
2929
directive.value === true
3030
? build_getter({ name: directive.name, type: 'Identifier' }, context.state)
3131
: build_attribute_value(directive.value, context).value;
32+
const { has_state, has_call } = directive.metadata.expression;
33+
34+
let final_value = value;
35+
36+
if (has_call) {
37+
const id = b.id(state.scope.generate('style_directive'));
38+
39+
state.init.push(b.const(id, create_derived(state, b.thunk(value))));
40+
final_value = b.call('$.get', id);
41+
}
3242

3343
const update = b.stmt(
3444
b.call(
3545
'$.set_style',
3646
element_id,
3747
b.literal(directive.name),
38-
value,
48+
final_value,
3949
/** @type {Expression} */ (directive.modifiers.includes('important') ? b.true : undefined),
4050
force_check ? b.true : undefined
4151
)
4252
);
4353

44-
const { has_state, has_call } = directive.metadata.expression;
45-
46-
if (!is_attributes_reactive || has_call) {
54+
if (!is_attributes_reactive && has_call) {
4755
state.init.push(build_update(update));
4856
} else if (is_attributes_reactive || has_state || has_call) {
4957
state.update.push(update);
@@ -70,11 +78,22 @@ export function build_class_directives(
7078
const state = context.state;
7179
for (const directive of class_directives) {
7280
const value = /** @type {Expression} */ (context.visit(directive.expression));
73-
const update = b.stmt(b.call('$.toggle_class', element_id, b.literal(directive.name), value));
74-
7581
const { has_state, has_call } = directive.metadata.expression;
7682

77-
if (!is_attributes_reactive || has_call) {
83+
let final_value = value;
84+
85+
if (has_call) {
86+
const id = b.id(state.scope.generate('class_directive'));
87+
88+
state.init.push(b.const(id, create_derived(state, b.thunk(value))));
89+
final_value = b.call('$.get', id);
90+
}
91+
92+
const update = b.stmt(
93+
b.call('$.toggle_class', element_id, b.literal(directive.name), final_value)
94+
);
95+
96+
if (!is_attributes_reactive && has_call) {
7897
state.init.push(build_update(update));
7998
} else if (is_attributes_reactive || has_state || has_call) {
8099
state.update.push(update);

0 commit comments

Comments
 (0)