Babylon.js icon indicating copy to clipboard operation
Babylon.js copied to clipboard

Add full support for ES6 modules (for Gui and Inspector)

Open deltakosh opened this issue 8 years ago • 61 comments

Let's open the door to tree Shacking ;)

deltakosh avatar Dec 06 '17 00:12 deltakosh

Starting 3.2.0-alpha2 on NPM we have es6 modules available in beta. I am keeping this open until I am satisfied there are no bugs.

RaananW avatar Jan 10 '18 20:01 RaananW

@RaananW I get TypeError: Cannot set property 'FreeCameraGamepadInput' of undefined.

I think this is because CameraInputTypes is not defined. When searching in the resulting JS code I can not find where it is created.

edzis avatar Jan 10 '18 21:01 edzis

Thanks! CameraInputTypes is a part of the targetCamera module, which the FreeCamera depends on. let me check why it is not automatically included. Could you try importing CameraInputTypes from "targetCamera" and see if it exists? Also - es6, or commonjs?

RaananW avatar Jan 10 '18 22:01 RaananW

I assume you are using es6, and I believe I know what the problem is. Fix is coming soon, thanks for reporting!

RaananW avatar Jan 10 '18 22:01 RaananW

That is what I just tried and got by bay adding

import { CameraInputTypes } from 'babylonjs/targetCamera'
console.log('CameraInputTypes', CameraInputTypes)

in front of my import of Gamepad

But now getting other errors which I will look into working around and report back here.

edzis avatar Jan 10 '18 22:01 edzis

Next one I get is

Uncaught TypeError: BABYLON.VRExperienceHelper is not a constructor
at Scene../node_modules/babylonjs/core/index.js.Scene.createDefaultVRExperience

VRExperienceHelper is not imported in core.

edzis avatar Jan 10 '18 22:01 edzis

Core will have a few classes missing, that will need to be imported and added. This will be a part of the documentation when I write it. We do try to prevent importing everything to core while still keeping backwards compatibility. The Scene will slowly be cleaned from unneeded dependencies, but for now we will need to import those missing classes.

And I just noticed I have a small bug with es6 libs that depend on modules that are not core. Which will probably be the main source of errors in es6.

RaananW avatar Jan 10 '18 22:01 RaananW

I will look out for any es6 updates and test again.

Not sure if this is helpful now, but I just noticed that after switching to es6 babylon on my 10 files the production bundle grew quite a bit - 850.62 KB (+283.2 KB) build/static/js/main.c19e52b2.js

edzis avatar Jan 10 '18 22:01 edzis

Thanks for the info, I will keep you updated here. Quick question - what lib are you using to bundle your package?

RaananW avatar Jan 10 '18 22:01 RaananW

I am using webpack as configured by Create React App with TypeScript support

create-react-app my-app --scripts-version=react-scripts-ts

edzis avatar Jan 11 '18 10:01 edzis

import { ArcRotateCamera } from '../node_modules/babylonjs/arcRotateCamera/es6.js' gives error Uncaught TypeError: Failed to resolve module specifier 'babylonjs/targetCamera/es6' as on line 19, there is: import * as targetCamera from 'babylonjs/targetCamera/es6'; I'm not using any bundler, so I am curios is this a oversight or will I not be able to use native js? ( I understand it's a alpha :) I am just curios keep up the good work)

browsermage avatar Jan 14 '18 21:01 browsermage

Wow, super question!!

I believe that eventually we will support native javascript with es6 and leave commonjs to the bundlers. That's the entire point of supporting es6 :smile: The problem is the lack of standard (at least one that I know of) in terms of location of the js files. Should I add "./node_modules/", should I create a form of a packer? All interesting questions that will be slowly answered!

We have internally decided to first support es6 with a single large file (the entire framework with es6 exports), so people will be able to start working with native es6.

Most bugs were already eliminated (but not yet merged), and I am working on a solution for circular dependencies. It will sadly take longer than I expected...

RaananW avatar Jan 15 '18 11:01 RaananW

@RaananW I just upgraded to alpha4 and it fails to locate modules:

import { Engine, Scene, Texture, Tools } from 'babylonjs/core'

results into

[ts] Cannot find module 'babylonjs/core'

edzis avatar Jan 17 '18 11:01 edzis

Until I fix the entire build process, I have decided to remove the modules. Instead of using babylonjs/core , please use babylonjs . It will load the entire framework. It is not optimal, but it works and tested. Native es6 can be loading using: import { Whatever } from 'babylonjs/es6'

I will keep this thread updated when the modules are back and working. Sorry about the hassle!

RaananW avatar Jan 17 '18 14:01 RaananW

using babylonjs v3.2.0-alphaB and webpack v4.1.0:

import { Space } from 'babylonjs/es6';
console.log(Space); // undefined

webpack gives this:

WARNING in ./main.js
2:12-17 "export 'Space' was not found in 'babylonjs/es6'
 @ ./main.js

cruzdanilo avatar Mar 05 '18 22:03 cruzdanilo

Pinging @raananw

deltakosh avatar Mar 06 '18 01:03 deltakosh

Problem found, I will take care of that until the next npm release.

RaananW avatar Mar 06 '18 17:03 RaananW

@RaananW, i also had to use this hack in my webpack.config.js for shaking off oimo and cannon:

  plugins: [
    new DefinePlugin({ OIMO: JSON.stringify(true), CANNON: JSON.stringify(true) }),
  ],

webpack v4.1.0

cruzdanilo avatar Mar 06 '18 19:03 cruzdanilo

I am planning on fixing the cannon and oimo dependencies in a different way. But a question - why are you using the es6 module if you use webpack? the UMD would fit much better. ES6 was mainly created for native es6 developers.

RaananW avatar Mar 06 '18 19:03 RaananW

i was just trying the new stuff :) but the oimo/cannon hack is necessary using umd too. also, it is less weird to write import { Engine } from 'babylonjs/es6' than import { Engine } from 'babylonjs/babylon.max'. the minified version gives less accurate build size stats.

cruzdanilo avatar Mar 06 '18 19:03 cruzdanilo