0

I am building a number of BabylonJS web applications, but all of them related and all of them use the same set of dependencies. In short, this means using Vite to scaffold a project, use yarn to add BabylonJS packages, and NPM to build a distribution.

Because many of these different apps all require similar functions, I have abstracted these functions to a file outside of any one project. Therefore I have my current project in one directory, but also the library of functions in a different directory. For example (the details of what's in the library aren't very important for my question, I don't think, but if you want to see more then let me know and I can put more code in):

lib\library.ts

import { ArcRotateCamera, Color3, Engine, HemisphericLight, MeshBuilder, Scene, Vector3 } from "@babylonjs/core";
import { GridMaterial } from "@babylonjs/materials";
import { AdvancedDynamicTexture, Rectangle } from '@babylonjs/gui';

const black = new Color3(0,0,0);

export let init = function(canvasId: string, width="500px", height="500px") {
    const canvas = document.querySelector<HTMLCanvasElement>(canvasId);
    if (canvas == null) {throw new Error("canvas id not found");}

    canvas.style.width = width;
    canvas.style.height = height;

    const engine = new Engine(canvas,true);
    const scene = new Scene(engine);

    scene.useRightHandedSystem = true;

    const camera = new ArcRotateCamera("Camera", 0,0,1, Vector3.Zero(), scene);
    camera.attachControl(canvas, true);
    camera.setPosition( new Vector3(0,-2,20) );
    camera.cameraDirection.set( 0,5,-20 );

    const light = new HemisphericLight("light", new Vector3(.5,1,0), scene);
    light.intensity = .7;
    scene.ambientColor = new Color3(.1,.2,.3);

    return {canvas, engine, scene, camera, light};
}

... (so on with more functions)

\vite-project\src\proj.ts

import './style.css';
import {Engine, Scene, ArcRotateCamera, Vector3, Color3, HemisphericLight, MeshBuilder, AxesViewer, UniversalCamera, DynamicTexture, StandardMaterial} from "@babylonjs/core";
import {GridMaterial} from '@babylonjs/materials';
import {AdvancedDynamicTexture, GUI3DManager, HolographicSlate, Rectangle, TextBlock} from '@babylonjs/gui';

import {init, spinsphere, textbox, twodgrid} from "/Users/ax/BabylonScripts/main";

const p1bag = init("#p1");
const p1scene = p1bag.scene;

twodgrid(p1scene);
spinsphere(p1scene, 0,0);
spinsphere(p1scene, 1,1,0, 0,1,0, .2, true);
spinsphere(p1scene, 2,1,0, 0,0,1);

... (so on with making the rest of the app)

Now I know that all the code works, since I can run the app and looks how I intend, with no console errors. (By running yarn dev in the terminal.)

But when I try to build it, I think understandably, NPM complains.

/Users/adamt/BabylonScripts/main.ts:3:51 - error TS2307: Cannot find module '@babylonjs/gui' or its corresponding type declarations.

3 import { AdvancedDynamicTexture, Rectangle } from '@babylonjs/gui';

... (many other errors about unused imports, but the imports are being used inside the library functions).

The problem is that the library script \lib\library.ts lives outside of any project -- which is what I want, because it shouldn't be attached to any project. But it also calls on BabylonJS packages, which it can't find, because it's developed outside of ... whatever it is. NPM, Yarn, Vite, whichever of those are responsible for "knowing" about the BabylonJS packages.

Somehow I want the build to know that the library file is outside of the build directory, go get it, and use it in the build process as if it were inside. Does anyone know if this is possible?

Addem
  • 3,635
  • 3
  • 35
  • 58
  • Would publishing your `\lib` directory to npm be an option? If so, try reading [Publishing a package](https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages) or [Publishing scoped packages](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages). If you do this, then you can add your package just like any other dependency and then use it in your projects. – TalinTheDev Sep 02 '23 at 00:03
  • @TalinTheDev I'm not personally against it ... but my lib is embarrassingly modest and I'm not sure if libs need to be a little more serious before publishing them to NPM. Or maybe publishing to NPM isn't that big of a deal? I dunno, I've never considered it. Should I? – Addem Sep 02 '23 at 00:05
  • 2
    You don't actually need to publish a public npm package to use packages. See e.g. https://stackoverflow.com/questions/14381898/local-dependency-in-package-json – Bergi Sep 02 '23 at 05:11
  • I suggest you check out the question @Bergi linked, i didn’t know about that but it seems perfect for your needs! – TalinTheDev Sep 02 '23 at 14:47
  • @TalinTheDev I've been reading it for a little while and ... I'm a little unclear of both what the question is and what the answer is. This all might be a little more NPM stuff than I am able to process right now. I've been watching a bunch of videos on NPM and I've learned a bit, but not sure how much this is progressing me toward knowing how to address this particular problem. But for right now I'm watching a video about "NPM link" and wondering if maybe this is relevant. – Addem Sep 02 '23 at 15:58
  • Yes, I took a quick, very not detailed look into `npm link` and from what I picked up, it seems to be something that could work. However, if you would like to try it, make sure you read the [documentation](https://docs.npmjs.com/cli/v9/commands/npm-link?v=true) and make sure you know what you are doing since this seems like it could allow annoying to debug problems to popup in different machines. – TalinTheDev Sep 03 '23 at 00:51

0 Answers0