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

Tree shaking of vue library #9614

Closed
dandel10n opened this issue Aug 21, 2019 · 1 comment
Closed

Tree shaking of vue library #9614

dandel10n opened this issue Aug 21, 2019 · 1 comment

Comments

@dandel10n
Copy link

dandel10n commented Aug 21, 2019

Bug report

What is the current behavior?
I have a Vue icons library (npm package), which is using rollup to build the code in various formats (including ES modules) which should be tree-shakable. When I add it to my project, which is built with webpack, and use only one icon from the library, I still see whole library in the production bundle.

If the current behavior is a bug, please provide the steps to reproduce.

I've made super simple vue cli app https://github.com/dandel10n/vue-app-sample - which uses https://github.com/dandel10n/vue-library-sample for easier illustration.

There are 3 components in the library. In the app I use only one (LibComponentOne). But in the vendors.bundle.js I still see all three components being imported.

If I build the app in dev mode with passing optimization: { usedExports: true } then in the bundled js file I see the unused components marked as /* unused harmony export LibComponentThree */\n/* unused harmony export LibComponentTwo */ as expected. But in production build I still see the unused components being included.

What is the expected behavior?

As I understand in app's vendors bundle I should see only one component, not all 3.

Other relevant information:
webpack version: v4.28.4
Node.js version: v10.15.0
Operating System: macOS
Additional tools: Vue CLI, Rollup

@dandel10n dandel10n changed the title Three shaking of vue library Tree shaking of vue library Aug 22, 2019
@sokra
Copy link
Member

sokra commented Aug 22, 2019

https://github.com/dandel10n/vue-library-sample/blob/master/src/entry.js

Here you "use" all components no matter which one are referenced by the user.

Even if this would be fixed, the way rollup-plugin-vue generates component code doesn't allow tree-shaking as it would be considered as side-effect.

I think this would help:

- var LibComponentOne = normalizeComponent_1(
+ var LibComponentOne = /*#__PURE__*/normalizeComponent_1(
    { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
    __vue_inject_styles__,
    __vue_script__,
    __vue_scope_id__,
    __vue_is_functional_template__,
    __vue_module_identifier__,
    undefined,
    undefined
  );

Send a PR to rollup-plugin-vue to fix that.

And fix your entry.js. Remove the global Vue registration in the esm.js build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants