Pure barrel for better tree shaking in @babylonjs/core

Hi everyone,

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.

Please take a look here:

If you have time, feedback on the docs or even the code would be very helpful. In particular:

  • Is the explanation clear?
  • Are the examples easy to follow?
  • Is anything missing for your use case?
  • Are there migration

concerns we should call out better?

This is an important change, and we want people to have time to read, test, and prepare before it lands.

For people importing from @babylonjs/core directly nothing should change - this should work as it always worked.

Thanks!

10 Likes

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!

There is no reason why

import * as BABYLON from "@babylonjs/core/pure";

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.

2 Likes

Thank you for your reply!

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.

2 Likes

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!

5 Likes

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.