diff --git a/CHANGELOG.md b/CHANGELOG.md index f9b3bb8b9a2b..3fceb5f2fc16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte changelog +## Unreleased + +* Fix reactivity with imported values that are then mutated ([#4555](https://github.com/sveltejs/svelte/issues/4555)) + ## 3.21.0 * Support dimension bindings in cross-origin environments ([#2147](https://github.com/sveltejs/svelte/issues/2147)) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index bb1546ceee9c..c83f8153a01e 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -632,7 +632,6 @@ export default class Component { this.add_var({ name, initialised: instance_scope.initialised_declarations.has(name), - hoistable: /^Import/.test(node.type), writable }); @@ -986,6 +985,7 @@ export default class Component { hoistable_nodes, var_lookup, injected_reactive_declaration_vars, + imports, } = this; const top_level_function_declarations = new Map(); @@ -1137,6 +1137,14 @@ export default class Component { this.fully_hoisted.push(node); } } + + for (const { specifiers } of imports) { + for (const specifier of specifiers) { + const variable = var_lookup.get(specifier.local.name); + + if (!variable.mutated) variable.hoistable = true; + } + } } extract_reactive_declarations() { diff --git a/test/runtime/samples/reactive-import-statement-2/_config.js b/test/runtime/samples/reactive-import-statement-2/_config.js new file mode 100644 index 000000000000..ccc97cd076b2 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/_config.js @@ -0,0 +1,3 @@ +export default { + html: `
prop value
` +}; diff --git a/test/runtime/samples/reactive-import-statement-2/data.js b/test/runtime/samples/reactive-import-statement-2/data.js new file mode 100644 index 000000000000..56fb86982c77 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/data.js @@ -0,0 +1,3 @@ +export const obj = { + prop: 'prop value' +}; diff --git a/test/runtime/samples/reactive-import-statement-2/main.svelte b/test/runtime/samples/reactive-import-statement-2/main.svelte new file mode 100644 index 000000000000..2582974b71b3 --- /dev/null +++ b/test/runtime/samples/reactive-import-statement-2/main.svelte @@ -0,0 +1,8 @@ + + +{prop}
diff --git a/test/runtime/samples/reactive-import-statement/_config.js b/test/runtime/samples/reactive-import-statement/_config.js new file mode 100644 index 000000000000..7fb8097ca58a --- /dev/null +++ b/test/runtime/samples/reactive-import-statement/_config.js @@ -0,0 +1,38 @@ +import * as path from 'path'; + +export default { + html: ` + import +1 + 2 + 3 + 4 = 10
+ local +1 + 2 + 3 + 4 = 10
+ + `, + before_test() { + delete require.cache[path.resolve(__dirname, 'data.js')]; + }, + async test({ assert, target, window, }) { + const btn = target.querySelector('button'); + const clickEvent = new window.MouseEvent('click'); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual(target.innerHTML, ` + import +1 + 2 + 3 + 4 + 5 = 15
+ local +1 + 2 + 3 + 4 + 5 = 15
+ + `); + + await btn.dispatchEvent(clickEvent); + + assert.htmlEqual(target.innerHTML, ` + import +1 + 2 + 3 + 4 + 5 + 6 = 21
+ local +1 + 2 + 3 + 4 + 5 + 6 = 21
+ + `); + } +}; diff --git a/test/runtime/samples/reactive-import-statement/data.js b/test/runtime/samples/reactive-import-statement/data.js new file mode 100644 index 000000000000..ad494c7cb04b --- /dev/null +++ b/test/runtime/samples/reactive-import-statement/data.js @@ -0,0 +1 @@ +export const numbers = [1, 2, 3, 4]; \ No newline at end of file diff --git a/test/runtime/samples/reactive-import-statement/main.svelte b/test/runtime/samples/reactive-import-statement/main.svelte new file mode 100644 index 000000000000..daded0494f4b --- /dev/null +++ b/test/runtime/samples/reactive-import-statement/main.svelte @@ -0,0 +1,19 @@ + + +import{numbers.join(' + ')} = {sum}
+local{local_numbers.join(' + ')} = {local_sum}
+ + \ No newline at end of file