ESM output of vue components not tree-shakeable in webpack #344
Description
Version
5.0.0
Reproduction link
https://github.com/mgdodge/rollup-plugin-vue-treeshake-bug
Steps to reproduce
- Clone the repo, which uses rollup-plugin-vue to build a component library via "npm run build".
- Copy the file
dist/testlib.esm.js
(the es module build) to a minimal vue-cli app. - In the vue-cli app, add
import { larry, moe } from '@/testlib.esm.js';
to import only two of the three vue components provided by the library, and display them on the page. - Run
npm run build
in the vue-cli app to produce bundled output
What is expected?
Bundled vue-cli output will include only the vue components used in the app (larry and moe)
What is actually happening?
Bundled vue-cli output includes components which were not used in the app (curly)
Minimal reproduction includes a "helpers" module with one unused helper, to illustrate that the output for raw modules is being properly shaken. ESM output includes the unused helper as an export of the library, but webpack leaves the unused helper out, as expected.
Reported to me via this issue, after which I discovered this solution indicating that the plugin output itself is still an issue.
It seems that each "normalizeComponent" call needs a /*#__PURE__*/
annotation comment for webpack to properly recognize them as tree-shakeable. For example: const __vue_component__$1 = /*#__PURE__*/normalizeComponent({...