Skip to content

Tree shaking fails for imported objects accessed inside $: reactive statements #16120

@adamvleggett

Description

@adamvleggett

Describe the bug

When importing a const object from another module and accessing a property of it inside a Svelte $: reactive statement, the entire object is retained in the final bundle, even if only one property is used.

This prevents effective tree shaking and can result in a significant increase in build size if a large object is partly used.

Reproduction

constants.js:

const Constants = {
  AppId: "abc123",
  Unused: "should be tree-shaken"
};
export default Constants;

component.svelte:

<script>
  import Constants from "$lib/utils/constants";

  $: value = Constants.AppId;
</script>

Logs

System Info

System:
    OS: macOS 15.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 9.42 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
    bun: 0.7.3 - /opt/homebrew/bin/bun
  Browsers:
    Brave Browser: 135.1.77.100
    Chrome: 137.0.7151.69
    Edge: 135.0.3179.85
    Safari: 18.5
    Safari Technology Preview: 18.4
  npmPackages:
    svelte: 5.33.10 => 5.33.10

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