Skip to content

Vite optimization treats re-exported stores as different references #124

@mattjennings

Description

@mattjennings

Describe the bug

If you have a library that re-exports stores (say, in an index.js from ./store.js), the exported store from index.js is a different reference than the same one from ./store.js.

This is not the case if the package is excluded from optimizeDeps.

Reproduction

URL: https://github.com/mattjennings/vite-svelte-store-bug

add-numbers package:

// index.js
export { total, add } from './store'
export { default as Total } from './Total.svelte'

// store.js
import { writable } from "svelte/store";

export const total = writable(0);

export function add(number) {
  total.update((prev) => prev + number);
}
<!-- Total.svelte -->
<script>
  import { total } from "./store";
</script>

{$total}

and in your App, you use it like so:

<script>
  import { add, total, Total } from "add-numbers";
</script>

<button on:click={() => add(1)}>add</button>

Store: {$total}

Component: <Total />

$total updates correctly, <Total /> does not.

If you import the stores from store.js:

<script>
  import { add, total } from "add-numbers/store.js"
  import { Total } from 'add-numbers'
</script>

<button on:click={() => add(1)}>add</button>

Store: {$total}

Component: <Total />

then it updates as expected. Otherwise, you need to pass the library into optimizeDeps.exclude in vite config.

Logs

No response

System Info

System:
    OS: macOS 11.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 233.43 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.3.0 - ~/.nvm/versions/node/v16.3.0/bin/node
    npm: 7.15.1 - ~/.nvm/versions/node/v16.3.0/bin/npm
  Browsers:
    Brave Browser: 92.1.27.109
    Chrome: 92.0.4515.107
    Safari: 14.1.1
    Safari Technology Preview: 15.0
  npmPackages:
    @sveltejs/vite-plugin-svelte: ^1.0.0-next.11 => 1.0.0-next.15 
    svelte: ^3.37.0 => 3.41.0 
    vite: ^2.4.4 => 2.4.4

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions