softarc-consulting / sheriff

Lightweight Modularity for TypeScript Projects
https://softarc-consulting.github.io/sheriff/
MIT License
205 stars 16 forks source link

Tree-Shaking optimizations #47

Open rainerhahnekamp opened 1 year ago

rainerhahnekamp commented 1 year ago

Using barrel files like an index.ts might cause an in-efficient tree shaking. This means build processes, tests, and bundle sizes, take much longer and are too large.

First, we need to come up with a proof of concept which helps us to understand the issue in detail and in which circumstances/tools it appears.

Second, we need to find out what potential alternatives to barrel files exist and how Sheriff could support that in the best way.

Backwards compatibility is mandatory.

This issue has been raised by @vmasek.

jogelin commented 1 year ago

Do you still want to #36?

jogelin commented 1 year ago

And maybe something like #19 could help? Instead of using a barrel file, creating a "module" based on a glob is more flexible?

rainerhahnekamp commented 1 year ago

We still have to understand the problem fully. I've bookmarked some articles which popped up under "tree shaking barrel file". Still have to read them in detail, but

So if you can provide something on this topic, that would be wonderful.

That's why, for the time being, #36 is still a thing.


If we have to provide an alternative, I would first maybe prioritise the conventional approach. Like, instead an index.ts defines a module, it could be a directory like _private or something similar.


Btw. Spring Modulith is very similar but works without barrel file: https://spring.io/projects/spring-modulith

rainerhahnekamp commented 11 months ago

As it currently stands, we have identified issues when it comes to code splitting.

Let's say, a module exposes a huge grid component and a simple button via a module. Consumer A (eagerly) depends on the button and Consumer B depends on the grid lazily.

Code splitting doesn't happen. Consumer A also inherits the grid component. Once the index.ts is removed and the import goes directly to the grid component, it ends up in the chunk file.