Dangling file after vite lib build

Hi, I’m testing ES6 tree shaking with Vite. Building a test scene (identical to doc) that invokes thinInstanceSetBuffer func throws error when run client-side. Suspecting a missing import, I added

import "@babylonjs/core/Meshes/thinInstanceMesh";

as a side effect. Now the vite build result shows the thinInstanceMesh.js as a dangling file that has to be preloaded with index.html. This is in contrast to other side effects imported. Is there any way to avoid this ?

fwiw, dependencies are:

"devDependencies": {
    "@babylonjs/core": "^5.0.0-beta.12",
    "@babylonjs/materials": "^5.0.0-beta.12",
    "vite": "^2.8.0"
  }

Hi! Tagging @RaananW for build related stuff :smiley:

1 Like

That’s the oddest occurrence of side effects! :slight_smile:
It should behave as any other imported script. I wonder what’s the issue… Would you be able to share a reproduction? some simpler github project I can clone and test with?

Thanks!

fwiw, I was curious as well, but unable to reproduce. This is from a very simple setup using vite and same dependencies. (Also using tsc, which shouldn’t matter).

npm run build

> babylon-webxr-test@0.0.0 build
> tsc && vite build

vite v2.8.3 building for production...
✓ 254 modules transformed.
dist/assets/favicon.17e50649.svg   1.49 KiB
dist/index.html                    0.66 KiB
dist/assets/index.8ec6b9b5.js      1.26 KiB / gzip: 0.71 KiB
dist/assets/index.c9c4d42b.css     0.27 KiB / gzip: 0.17 KiB
dist/assets/vendor.14a40c5d.js     1119.00 KiB / gzip: 250.03 KiB

Import looks like this, and methods added by thinInstanceMesh seem to work as expected:

import { ArcRotateCamera } from '@babylonjs/core/Cameras/arcRotateCamera.js'
import { Color3 } from "@babylonjs/core/Maths/math.color.js"
import { Engine } from '@babylonjs/core/Engines/engine.js'
import { HemisphericLight } from '@babylonjs/core/Lights/hemisphericLight.js'
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder.js"
import { Scene } from '@babylonjs/core/scene.js'
import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial.js"
import { Matrix, Vector3 } from '@babylonjs/core/Maths/math.vector.js'

import "@babylonjs/core/Meshes/thinInstanceMesh";
2 Likes

just wondering - is adding “.js” not helping?
Ignore that, I just read it again :slight_smile:
So it seems like it is working as expected…

haha. Yes, maybe I should have been clearer: It seems to work for me. No issues. So @phaselock will need to provide more info on their specific setup.

But just to be sure I also tried the import with:

import '@babylonjs/core/Meshes/thinInstanceMesh.js'

No difference, as expected. For anyone that comes across, the import file extensions usually aren’t required in vite due to it’s built-in handling. More info:

2 Likes

Hey, thanks all for trying. My bad, I forgot to post my vite.config.js and I’ve since managed to repro and narrow the problem to dynamic imports statements. My files are a mess, cos its a sandbox. To recreate on your end, just add into code alr in the doc.

  1. add a dynamic import statement using switch. In my main.js, its
let v = 0;
switch(v){
	case 0:
		import("./dynImp1").then(out=>{}).catch(err=>{
			console.log("import file error:"+err);
		})
		break;
	case 1:
		import("./dynImp2").then(out=>{}).catch(err=>{
			console.log("import file error:"+err);
		})
		break;
}
  1. make 2 identical dynImp files and let your last import statement be import "@babylonjs/core/Meshes/thinInstanceMesh";
  2. vite build. I built as lib into ./dist folder, so my vite.config.js is
import { defineConfig } from 'vite';
import path from 'path';

export default defineConfig({
	mode:'development',
	build: {
		lib: {
			entry: "main.js",
			formats: ['es'],
			fileName: "entry",	// becomes entry.es.js
		},
		// outDir can potentially erase existing assets if wrong dir is set, beware!
		outDir:"dist",
		manifest: true,
	}
})
  1. You should get a manifest result similar to below.
{
  "main.js": {
    "file": "entry.es.js",
    "src": "main.js",
    "isEntry": true,
    "imports": [
      "_main.js"
    ]
  },
  "_main.js": {
    "file": "main.js",
    "dynamicImports": [
      "dynImp1.js",
      "dynImp2.js"
    ]
  },
  "dynImp1.js": {
    "file": "dynImp1.js",
    "src": "dynImp1.js",
    "isDynamicEntry": true,
    "imports": [
      "_main.js",
      "_thinInstanceMesh.js"
    ]
  },
  "_thinInstanceMesh.js": {
    "file": "thinInstanceMesh.js",
    "imports": [
      "_main.js"
    ]
  },
  "dynImp2.js": {
    "file": "dynImp2.js",
    "src": "dynImp2.js",
    "isDynamicEntry": true,
    "imports": [
      "_main.js",
      "_thinInstanceMesh.js"
    ]
  }
}

If you have other import statements in dynImp, just comment out thinInstanceMesh's and rebuild. The dangling file now shows whatever is the last import. At this point I dunno if the problem is with bjs or vite atm but its consistent and reproducible as far as using switch is concerned. Hmm, maybe I should just revert to if-else for now…shrugs

oufff. I keep posting and then deleting my post a few minutes later after I look at things more, so that I don’t cause confusion for future generations.

@phaselock Are you sure there is an issue here? I have replicated your setup fairly closely and get almost the same manifest. Things look as I would expect. You said:

  • “dangling file that has to be preloaded with index.html”

But is that correct? The built dynImpl1.js and dynImpl2.js files have this at the top:

  • import "./thinInstanceMesh.js";

…as expected, correct? And would indicate that thinInstanceMesh.js does not have to be preloaded in index.html.

I’m not sure if there is an issue here, which is why I’m bringing it up for discussion.

The dangling file was preloaded by vite as I observed in the browser, not by me. As I was in the midst of testing vite’s functionalities with bjs, I played with several options and noticed that vite modifies the index.html and will inject preloads automatically (I can’t remember the exact options, might have been ssr, might have been smthg else).

The issue that I found most annoying was the dangling file after build. If there were multiple imports in the dynamic imports, like

// side effect
import "@babylonjs/core/Loading";
import "@babylonjs/core/Rendering/depthRendererSceneComponent";
import "@babylonjs/core/Meshes/thinInstanceMesh";

and you comment out thinInstanceMesh. After vite build, you get a dangling depthRendererSceneComponent file and so on and forth. It’s ugly, I have to take care of it during downstream custom templating but its not that big a deal, shrugs.

Honestly, its looking more and more like a vite issue than bjs’s but I’m not the expert here…

1 Like

Hi @phaselock just checking in, do you still have any issues? :slight_smile:

Issue as per topic still persists. Could move this thread to off-topic since its likely a vite issue but their backlog is huge so it may take a while. Thanks.

2 Likes