#import modules into custom namespace

18 messages · Page 1 of 1 (latest)

safe rock
#

I'm trying to import specific objects from @babylonjs/core (https://doc.babylonjs.com/setup/frameworkPackages/es6Support#available-packages), e.g. Engine, Scene, but would like them to only be accessible via a BABYLON namespace,

So something like

namespace BABYLON {
    export import { Engine } from '@babylonjs/core';
    export import { Scene } from '@babylonjs/core';
    export import { ArcRotateCamera } from '@babylonjs/core';
    export import { HemisphericLight } from '@babylonjs/core';
};

// Engine, Scene, etc, should only be accessible via the BABYLON namespace 
// These should work 
const engine = new BABYLON.Engine(...); 
const scene = new BABYLON.Scene(...);
// This should not 
const engine2 = new Engine(...); // Error: Engine is not defined, or something. 

Is this possible? Can't seem to get it.

Learn about tree shaking and package management in Babylon.js.

#

this seems to work automatically when importing from babylonjs/core so maybe this question is useless

#

or maybe not IDK

#

I am confused

coral viper
#
namespace BABYLON {
    export import { Engine } from '@babylonjs/core';
    export import { Scene } from '@babylonjs/core';
    export import { ArcRotateCamera } from '@babylonjs/core';
    export import { HemisphericLight } from '@babylonjs/core';
};

doesn't sound like it's valid, event less with code right after

safe rock
#

it's not valid, typescript throws an error

#

that's kinda like pseudocode demonstrating what I'm after

coral viper
#
// @filename: index.ts

import { Engine } from '@babylonjs/core';
import { Scene } from '@babylonjs/core';
import { ArcRotateCamera } from '@babylonjs/core';
import { HemisphericLight } from '@babylonjs/core';

export * from "./index" as BABYLON;

something like that maybe 🤔

#

tho it's usually the consumer that can rename the stuff they are importing

safe rock
#

that's a reasonable idea

#

I was hoping for a solution that kept things in the same file but Im not sure that's possible

coral viper
#
import { Engine } from '@babylonjs/core';
import { Scene } from '@babylonjs/core';
import { ArcRotateCamera } from '@babylonjs/core';
import { HemisphericLight } from '@babylonjs/core';

const BABYLON = {
    Engine,
    Scene,
    ArcRotateCamera,
    HemisphericLight
};

export default BABYLON;
safe rock
#

oh that makes sense

#

I dunno why I didn't think of that

coral viper
#

more ways to re-export stuff

safe rock
#

yeah