Hallo,
Not sure if this is a bug or I messed things up. Probably the last one.
I have a clean project with Webpack, typescript (ts-loader) and Babylonjs 4.1 with only the following code:
import * as BABYLON from 'babylonjs';
import 'babylonjs-loaders';
const eCanvas = document.getElementById('canvas');
const engine = new BABYLON.Engine(eCanvas, true);
BABYLON.SceneLoader.Load("./objects/generator/", "Emergency_Backup_Generator.obj", engine, function (scene) {
alert('Loaded!')
});
But I get an âUnable to loadâ error:
However when I comment the import babylonjs as shown below it works and I receive the alert âLoaded!â.
//import * as BABYLON from 'babylonjs'
It also looks like that âimport âbabylonjs-loadersââ itself makes the window.BABYLON available, so âimport * as BABYLON from âbabylonjsââ can be left out? According to the docs this isnât the case.
1 Like
Why not just use the ânew wayâ to import things based on the modular packages?
import {
SceneLoader
} from "@babylonjs/core/Loading/sceneLoader";
You might have to do a npm install --save-dev @babylonjs
(not the â@â!) first, but then everything should work. This is the âproperâ way to do it but the catch is that you have to import everything individually.
You can also try
import * as BABYLON from '@babylonjs/core/Legacy/legacy';
instead of what you have now, although I have never tried that myself.
See also: ES6 - Babylon.js Documentation
Welcome to the forum, by the way!
1 Like
Thank you! You gave me the right direction. It works and my imports are as follows:
import '@babylonjs/loaders';
import {
SceneLoader
} from "@babylonjs/core";
Not sure if it makes any difference but I imported the SceneLoader from babylonjs/core.
AFAIK it makes tree-shaking less effective because it includes everything in core for the final file, but thatâs it. Some of the modules are not very intuitively named and I donât want to look at my node_modules/@babylonjs folder every time, so I do âbroadâ imports like that too sometimes.
You could try the Google Closure Compiler plugin to still get some benefits: closure-webpack-plugin - npm. It does not do traditional tree-shaking but it has the ability to optimize out functions/methods youâre not using and optimizes your code in general, a bit like a C/C++ compiler. Havenât used this plugin personally though, only the raw compiler, which however works great, also with Babylon.js.
And yeah, you need the import '@babylonjs/loaders';
if you want to import anything but babylon files.
Glad I could help.