Echo JS 0.11.0

<~>
MaxArt 2870 days ago. link parent 1 point
Then the module should rightfully stay out of the bundle.

If you load a module based on a condition, you're doing one of these things:

1. checking something on the client (e.g. if it supports a certain feature) and lazy-load a module (e.g. the polyfill of said feature);
2. checking on the server (e.g. the Accept-Language request header) and the load the module.

In the first case, it's pretty clear you can't (or better, shouldn't) put the module in the bundle, because it would be loaded by everyone regardless if they actually need it.
In the latter, you can't do that either, because it's a condition evaluated at runtime. Unless we're talking about generating bundle files on the fly?

What I mean is that the concept of tree-shaking entirely relies on static imports, and it's perfectly fine to keep dynamic imports out of the bundle.
Every module that could be loaded with import() must be served in separate files.

Your problems will eventually begin when those modules import things that have already been included in the bundle. That's the real catch.

If you really want to bundle everything up anyway, all you have to do is to statically import all those files, and then doing nothing with them. But then you won't even *need* to use import() anyway, because you'll already have everything you need defined in the scope. Much more, probably.
But all this has nothing to do with tree-shaking anyway.

Replies