Add full support for ES6 modules (for Gui and Inspector)
Let's open the door to tree Shacking ;)
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 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.
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?
I assume you are using es6, and I believe I know what the problem is. Fix is coming soon, thanks for reporting!
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.
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.
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.
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
Thanks for the info, I will keep you updated here. Quick question - what lib are you using to bundle your package?
I am using webpack as configured by Create React App with TypeScript support
create-react-app my-app --scripts-version=react-scripts-ts
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)
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 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'
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!
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
Pinging @raananw
Problem found, I will take care of that until the next npm release.
@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
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.
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.