Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a function imported from another file as a default value in withDefaults may prevent it from being tree-shaken #12625

Closed
wattanx opened this issue Dec 29, 2024 · 2 comments

Comments

@wattanx
Copy link

wattanx commented Dec 29, 2024

Vue version

3.5.13

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-sr5itbdf

Steps to reproduce

  1. npm install
  2. npm run build
  3. NotTreeShaken component exists in dist/assets/index-xxx.js

What is expected?

NotTreeShaken component is not imported, so it should be tree-shaken

What is actually happening?

NotTreeShaken component exists in dist/assets/index-xxx.js

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vue: ^3.5.13 => 3.5.13

Any additional comments?

In the case of Props Destructure, it is tree-shaken.

In the case of withDefaults, props are compiled as follows:

props: {
  msg: { type: String, required: false, default: format('Hello World!!!   ') }
},

In the case of Props Destructure, it is compiled as follows:

props: {
  msg: { type: String, required: false, default: () => (format('Hello World!!!   ')) }
},
@wattanx
Copy link
Author

wattanx commented Dec 29, 2024

Does this case correspond to the content of the following note?

Note that default values for mutable reference types (like arrays or objects) should be wrapped in functions when using withDefaults to avoid accidental modification and external side effects. This ensures each component instance gets its own copy of the default value. This is not necessary when using default values with destructure.

https://vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration

@edison1105
Copy link
Member

edison1105 commented Dec 30, 2024

Does this case correspond to the content of the following note?

Note that default values for mutable reference types (like arrays or objects) should be wrapped in functions when using withDefaults to avoid accidental modification and external side effects. This ensures each component instance gets its own copy of the default value. This is not necessary when using default values with destructure.

vuejs.org/api/sfc-script-setup.html#default-props-values-when-using-type-declaration

Yes, The meaning is roughly this: if a function is not used, the default value will be considered to have side effects and will not be tree-shaken. see rollupjs repl. you can try changing the default to a non-function and observe the outputted JS.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants