Skip to content

Commit fc16c52

Browse files
committed
fix: treat imports as potentially reactive
1 parent d9369d8 commit fc16c52

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

.changeset/fuzzy-dancers-buy.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: treat imports as potentially reactive

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export function Identifier(node, context) {
9090
if (binding) {
9191
if (context.state.expression) {
9292
context.state.expression.dependencies.add(binding);
93-
context.state.expression.has_state ||= binding.kind !== 'normal';
93+
context.state.expression.has_state ||=
94+
binding.kind !== 'normal' || binding.declaration_kind === 'import';
9495
}
9596

9697
if (
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
html: '<button>1</button> <button>2</button> <p>1</p>',
6+
7+
test({ assert, target }) {
8+
const [btn1, btn2] = target.querySelectorAll('button');
9+
10+
flushSync(() => btn2.click());
11+
assert.htmlEqual(target.innerHTML, `<button>1</button> <button>2</button> <p>2</p>`);
12+
13+
// reset state for next variant
14+
flushSync(() => btn1.click());
15+
assert.htmlEqual(target.innerHTML, `<button>1</button> <button>2</button> <p>1</p>`);
16+
}
17+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { stuff } from './stuff.svelte.js';
3+
</script>
4+
5+
<button onclick={() => Object.assign(stuff, { x: 1 })}>1</button>
6+
<button onclick={() => Object.assign(stuff, { x: 2 })}>2</button>
7+
<p>{Object.values(stuff)}</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const stuff = $state({ x: 1 });

0 commit comments

Comments
 (0)