Skip to content

Ensure unused values created by pure functions (like createGlobalState) are tree-shakeable #4906

@serkodev

Description

@serkodev

Clear and concise description of the problem

Some VueUse functions are pure, meaning they produce no side effects and their outputs depend only on their inputs. Values created by such functions should be tree-shaken if they are unused.

For example, when using createGlobalState to define a global state:

export const useGlobalState = createGlobalState(() => {
  return { count: 0 }
})

If useGlobalState is never imported or used, it should be removed during tree-shaking.

Suggested solution

Add the /* #__NO_SIDE_EFFECTS__ */ annotation to all pure VueUse functions to hint bundlers that the call has no side effects and can safely be removed if unused.

Reference: vuejs/pinia#2740

Temporary Workaround

In the meantime, users can manually annotate the call with /* @__PURE__ */ to enable tree-shaking:

export const useGlobalState = /* @__PURE__ */ createGlobalState(() => {
  return { count: 0 }
})

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy