diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 26668da8c0..b94b108f43 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -666,8 +666,17 @@ export default class Component { this.node_for_declaration.set(name, node); }); - globals.forEach((node, name) => { + // NOTE: add store variable first, then only $store value + // as `$store` will mark `store` variable as referenced and subscribable + const global_keys = Array.from(globals.keys()); + const sorted_globals = [ + ...global_keys.filter(key => key[0] !== '$'), + ...global_keys.filter(key => key[0] === '$') + ]; + + sorted_globals.forEach(name => { if (this.var_lookup.has(name)) return; + const node = globals.get(name); if (this.injected_reactive_declaration_vars.has(name)) { this.add_var({ diff --git a/test/runtime/samples/store-auto-subscribe-in-reactive-declaration-2/App.svelte b/test/runtime/samples/store-auto-subscribe-in-reactive-declaration-2/App.svelte new file mode 100644 index 0000000000..250305b62c --- /dev/null +++ b/test/runtime/samples/store-auto-subscribe-in-reactive-declaration-2/App.svelte @@ -0,0 +1,8 @@ + +