[Lite] Something wrong with treeshaking/bundling with Vite

I’m trying to figure out how to build babylon-lite projects with Vite.

Using:

  • @babylonjs/lite@1.40.1
  • vite@8.3.8

Here’s my sample project in full:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="icon" href="data:," />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body>
        <canvas id="renderCanvas"></canvas>
        <script type="module">
            import {
                createEngine,
                createSceneContext,
                registerScene,
                startEngine,
            } from "@babylonjs/lite";

            export async function main(canvas) {
                const engine = await createEngine(canvas);
                const scene = createSceneContext(engine);

                // nothing here, it's totally empty scene

                await registerScene(scene);
                await startEngine(engine);
            }

            main(document.getElementById("renderCanvas")).catch((err) =>
                console.error(err)
            );
        </script>
    </body>
</html>

Building it with very default (empty) Vite settings produces 150 chunks, 139 of which are from @babylonjs/lite/lib/flow-graph/

Building it with rolldownOptions: { output: { codeSplitting: false } } produces just 2 chunks: index (came from the html) and havok (came from nowhere)

There’s definitely something wrong going on there with dependencies.

Any hints?


Playground is not relevant here.
However, this code of the same totally empty scene:

When it’s run, initiates quite a bunch of requests for ≈160 chunks, including the havok and lots of other non-relevant stuff. (And an amazing typed-arrays artefact that does nothing but remangling mangled names lol)

Using an inline <script type="module"> block directly in index.html is not the standard Vite convention, and it’s likely the direct cause of the bundling anomalies you’re seeing.

It goes the same with <script type="module" src="./main.js"> and with bare rolldown on the main.js

P.S.
But it works differently with rollup, although it separates scene-rebuild and scene-runtime-mesh-build

Apparently, that’s because of circular dependencies via static/dynamic imports.

Sonda detected 10 such circular dependencies:

"issues": [
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/_chunks/gaussian-splatting-pipeline-DZrt63ve.js",
          "node_modules/@babylonjs/lite/lib/picking/gs-picking-pipeline.js",
          "node_modules/@babylonjs/lite/lib/_chunks/gaussian-splatting-pipeline-DZrt63ve.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/_chunks/mesh-lod-scene-BpbUgjBU.js",
          "node_modules/@babylonjs/lite/lib/material/pbr/pbr-mesh-lod-renderable.js",
          "node_modules/@babylonjs/lite/lib/_chunks/mesh-lod-scene-BpbUgjBU.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/engine/gpu-task-timer.js",
          "node_modules/@babylonjs/lite/lib/engine/gpu-task-timing.js",
          "node_modules/@babylonjs/lite/lib/engine/gpu-task-timer.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/loader-gltf/gltf-json-asset.js",
          "node_modules/@babylonjs/lite/lib/loader-gltf/gltf-multi-buffer.js",
          "node_modules/@babylonjs/lite/lib/loader-gltf/gltf-json-asset.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/material/node/node-material.js",
          "node_modules/@babylonjs/lite/lib/material/node/node-renderable.js",
          "node_modules/@babylonjs/lite/lib/material/node/node-material.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/physics/havok-floating-origin.js",
          "node_modules/@babylonjs/lite/lib/physics/havok.js",
          "node_modules/@babylonjs/lite/lib/physics/havok-floating-origin.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/scene/scene-rebuild.js",
          "node_modules/@babylonjs/lite/lib/scene/scene-runtime-mesh-build.js",
          "node_modules/@babylonjs/lite/lib/scene/scene-rebuild.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 2 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/skeleton/bone-control-hooks.js",
          "node_modules/@babylonjs/lite/lib/skeleton/bone-control.js",
          "node_modules/@babylonjs/lite/lib/skeleton/bone-control-hooks.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 3 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/flow-graph/block-registry.js",
          "node_modules/@babylonjs/lite/lib/flow-graph/blocks/animation/play-animation.js",
          "node_modules/@babylonjs/lite/lib/flow-graph/runtime.js",
          "node_modules/@babylonjs/lite/lib/flow-graph/block-registry.js"
        ]
      }
    },
    {
      "type": "circular-import",
      "severity": "warning",
      "message": "Circular import detected between 3 modules.",
      "data": {
        "cycle": [
          "node_modules/@babylonjs/lite/lib/material/shader/shader-group-builder.js",
          "node_modules/@babylonjs/lite/lib/material/shader/shader-renderable.js",
          "node_modules/@babylonjs/lite/lib/material/shader/shader-material.js",
          "node_modules/@babylonjs/lite/lib/material/shader/shader-group-builder.js"
        ]
      }
    }
  ],

Somehow rolldown fails to handle them. And rollup fails differently. And neither complains.

cc @RaananW

Thanks for digging into this. Your diagnosis is correct, and the Sonda output pointed right at it.

Several Lite modules that are loaded lazily (import()) also statically imported, directly or indirectly, the module that loads them. The worst case was flow-graph: every block imported the runtime, the runtime imported the block registry, and the registry lazily imports every block. Rolldown (Vite 8) treats such a chunk as keeping its own importer alive, so it emits the whole set even when your app never touches flow graphs. With your empty scene I got 156 chunks, and only 12 were reachable from the entry. Rollup is affected differently: it emitted stray havok* chunks that were never fetched. That’s also where your “havok came from nowhere” comes from.

Most of those extra chunks are emitted but never downloaded by a production build. Still, it’s wrong output and confusing, so we fixed it at the source. All nine cycles are broken, and the same empty scene now builds to 10 chunks, all reachable. There are no API changes.

To make sure it doesn’t come back, CI now checks every PR for this pattern and also runs a real Vite 8 build of an empty scene that fails if any emitted chunk is unreachable.

Fix: BabylonJS/Babylon-Lite#941. It will ship in the next release after it merges.

About the Playground fetching ~160 chunks: the Playground doesn’t use your bundler, so that’s separate from what you saw with Vite. I’ll look at it on its own.

There is still one cycle remains:

Bundling to 10 chunks is of course much better that 1320, but still doesn’t look reasonable for a totally empty scene.

The chunks include absolutely redundant files like:

var F32 = Float32Array;
var F64 = Float64Array;
var U32 = Uint32Array;
export { F64 as n, U32 as r, F32 as t };
var MAX_LIGHTS = 16;
export { MAX_LIGHTS as t };

In esbuild output one of the chunks is:

var _rightHanded = null;

export {
  _rightHanded
};

I have checked 4 different bundlers just for completness.
Only esbuild --no-splitting produce a single bundle. Otherwise it produces 738 chunks.

Here is my setup:

checking, thanks a lot!

The scene-rebuild ↔ scene-runtime-mesh-build cycle was real: two lazy modules loading each other. It’s fixed in BabylonJS/Babylon-Lite#951 (the shared build-tracking code moved into its own module), and CI now fails on any import cycle that goes through an import(), not just static ones.

The tiny chunks (typed-arrays, MAX_LIGHTS, …) come from the top-level await in your main.js. When the entry module is suspended on a top-level await, Rolldown and Rollup deliberately keep modules shared with lazy chunks out of the entry, so a lazy chunk never imports from a module that hasn’t finished running. That’s what produces those small shared chunks. Wrap your startup in async function main() { … } main(); and an empty scene builds to one chunk loaded up front with Vite 8, Rolldown, and Rollup. The remaining chunks are opt-in features (high-precision matrices, floating origin, runtime material builds) that an empty scene never fetches.

esbuild with --splitting is a different story: it turns every import() in the package into its own chunk entry, including features your app never reaches, and splits shared code per file without merging it back into the entry. A library that lazy-loads as much as Lite does can’t avoid that. Without --splitting, esbuild produces a single tree-shaken file (33 KB minified for your empty scene), which is what we recommend.

I’ve added these notes to the getting-started guide and the package README.