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

Library whose components use defineComponent not tree shakeable #13087

Closed
HeavyMedl opened this issue May 4, 2023 · 1 comment
Closed

Library whose components use defineComponent not tree shakeable #13087

HeavyMedl opened this issue May 4, 2023 · 1 comment

Comments

@HeavyMedl
Copy link

Describe the bug

Vite is not tree-shaking unused Vue 3 components from a consuming Vue 3 application. The Vue 3 component library is built with Vite, configured in "library mode", and imported into a vanilla Vue 3 application. The Vue 3 components from the library use the defineComponent type helper.

It was unclear whether this should be filed in vuejs/core or here since theoretically Vite should be handling this.

Reproduction

https://github.com/HeavyMedl/library-not-tree-shakeable

Steps to reproduce

git clone https://github.com/HeavyMedl/library-not-tree-shakeable.git
cd ~/library-not-tree-shakeable/vue-app
npm ci
npm run build

Inspect the bundle produced by Vite in dist/assets.

vite-build

What is expected?

The bundle should only contain vue-library/src/components/ComponentA.vue code, with vue-library/src/components/ComponentB.vue code tree shaken out

What is happening?

vue-library/src/components/ComponentB.vue code is built into the bundle.

// vue-app/dist/assets/index-0ce1e96c.js
...
const ComponentA = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["render", _sfc_render$1], ["__scopeId", "data-v-ea310018"]]);
defineComponent({
  name: "ComponentB",
  props: {
    msg: {
      type: String,
      default: "Component B"
    }
  },
  setup() {
    const count = ref(0);
    return {
      count
    };
  }
});
...

Any additional comments?

There doesn't seem to be any problem with tree shaking when using the <script setup/> syntactic sugar or Options API to define a component. The problem seems isolated to using defineComponent. You can work around this by prefixing /*@__PURE__*/ to defineComponent, i.e.

export default /*@__PURE__*/ defineComponent(/* ... */)

However, according to the docs, this shouldn't be required when using Vite.

Note this is not necessary if you are using Vite, because Rollup (the underlying production bundler used by Vite) is smart enough to determine that defineComponent() is in fact side-effect-free without the need for manual annotations.

System Info

System:
    OS: macOS 13.3.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 60.81 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
    npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
  Browsers:
    Chrome: 113.0.5672.63
    Firefox: 111.0.1
    Safari: 16.4
  npmPackages:
    @vitejs/plugin-vue: ^4.1.0 => 4.2.1 
    vite: ^4.3.2 => 4.3.4 


### Used Package Manager

npm

### Logs

_No response_

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitejs/vite/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitejs.dev/guide).
- [X] Check that there isn't [already an issue](https://github.com/vitejs/vite/issues) that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to [vuejs/core](https://github.com/vuejs/core) instead.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitejs/vite/discussions) or join our [Discord Chat Server](https://chat.vitejs.dev/).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
@sapphi-red
Copy link
Member

It seems to be an issue in Vue.
See vuejs/core#8236

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

No branches or pull requests

2 participants