We are preparing a big tree-shaking improvement for Babylon.js, planned for the beginning of next week.
The short version: Babylon.js will add a new pure barrel import path for @babylonjs/core. This lets apps import from a convenient single entry point while still allowing bundlers to remove unused Babylon.js code more effectively.
The main idea is to import everything from a single pure barrel:
import { Engine, Scene, Vector3 } from "@babylonjs/core/pure";
Some features that used to be enabled automatically by imports will need explicit registration when using the pure path. The docs explain the new pattern, when registration is needed, and how to troubleshoot missing registrations.
I’m really looking forward to this release!
I believe this will not only benefit standard use cases but also improve worker thread startup times and reduce memory consumption for OffscreenCanvas.
Just to confirm, will this be properly tree-shaken even with an import style like the following?
import * as BABYLON from "@babylonjs/core/pure";
Also, are there any plans to move toward a more tree-shaking-friendly implementation in the future (even if it potentially breaks backward compatibility)?
For example, cameras are currently created with several InputsManager instances registered by default. Consequently, even if we call inputs.clear() at runtime and don’t use the managers at all, they likely won’t be tree-shaken. It would be even better if the implementation became fully tree-shakable in that regard.
Regardless, I have high expectations for this improvement.
Thank you for this great framework!
wouldn’t work as expected, as long as you register the correct side effects. I personally would not do that because i don’t like namespacing like that, but this is just my personal opinion.
notice that the pure barrel is NOT a magical solution. We are still maintaining backwards-compatibility, so we can’t make major changes to the code structure (which is, sadly, a bit too “coupled” in some areas).
The benefit of the pure barrel is to be able to tree-shake while importing from a single import path.
I have one more question/request: when only using WebGPU engine (and not using materials or other features that require WGSL-unsupported shaders), will the GLSL (or GLSL-related) code in Babylon.js be tree-shaken?
No. We haven’t changed the architecture for shader loading. But we are working on a different solution that you will probably be happy with. More in the coming weeks.
Update - the changes were just merged. 9.8.0 was also released with the new “pure” barrel. Documentation is being built right now with the latest changes.
Asking everyone with issues - please write me if anything is not working! Thank you so much!
I wanted to say a special thank you for this change. Now, the transition from babylonjs-* to @babylonjs/*finally makes sense and it actually saves on final bundle size (around 0.9MB in my case). Before the 9.8.0, the mentioned transition was blowing my output bundle with about 1.5MB extra, that’s why I wasn’t rushing to switch.