Here is an example repository that shows an example of the issue reported in this thread: https://github.com/Eux86/tree-shaking-barrel-test/blob/master/README.md
I'm trying to understand what's the effect of using a Barrel file to export functions and classes from a library project when importing it into another project that use webpack and should be able to treeshake the bundle.
Imagine I have a project:
library
index.ts
libA.ts
libB.ts
index.ts has this code:
export { LibAMain } from 'LibA';
export { LibBMain } from 'LibB';
So I'm using index as a barrel file to export all the functions I'm going to develop in my library.
The second project will be:
library-user
- index.ts
Index.ts has this code:
import { LibAMain } from 'library'
LibAMain();
Now: library-user is builded using webpack, which I expect to be able to treeshake the unused libraries in MyLib, but when I look into the generated bundle I see that it contains both LibA.js and LibB.js, which shouldn't be there:
If I change index.ts to:
import { LibAMain } from 'library/lib/LibA'
LibAMain();
then webpack does its job well and I only see LibA in the final bundle:
TL;DR: How can I keep using the barrel index file and just import everything from 'library' but still have the treeshaking working?
Thank you for any help :)