Trouble excluding @babylonjs/core from bundle in vite

The Babylon HtmlMesh extension is a library that is an extension to Babylon. Since it is an extension, it should not bundle Babylon. To accomplish this I make it as external in the vite config exclude it from vite optimizeDeps and include it in packge.json only under peerDependencies and devDependencies. Despite all of this, vite still bundles @babylonjs/core, which is causing issues with breaking changes between versions of Babylon. I have tried everything I can think of and nothing can prevent vite from bundling it. I wonder if there is something in @babylonjs/core itself that is causing vite to include it even though it is marked as external, such as dynamic imports or side effects. Has anyone successfully create a package using vite (or webpack for that matter) that depends on @babylonjs/core does not bundle @babylonjs/core? To see the issue, you can pull down the GitHub - BabylonJS/Extensions: Extensions for Babylon.js project and build HtmlMesh. Go into the dist directory and look at the output js files and you will see they contain Babylon. You can also tell by the size of the built code which should be much smaller, if Babylon is not being bundled.

Let me know if you figure that out…the only way I could think to do this was either a) dynamic import or b) do a copy paste similar to what aframe did for threejs (effectively bundle all the necessary classes via copy/paste from a specific version).

1 Like

cc @RaananW as well who proposed in Github to have a look.

This PR fixes it.

Fix HTMLMesh build process by RaananW · Pull Request #300 · BabylonJS/Extensions (github.com)

2 Likes

@RaananW wish I had your knowledge about bundling stuff! :muscle:

1 Like

image

1 Like

1 Like

Wizard!

1 Like

Vite externalizing the babylon dependency works great, as long as you aren’t a bonehead like me and import the entire package!