Experimenting ECMAScript 6 in an easy way on Babylon.js with TypeScript 1.5

I’m definitely more than happy that we’ve decided more than one year ago to switch over to TypeScript. David has already explained the reasons behind: Why we decided to move from plain JavaScript to TypeScript for Babylon.js

Thanks to TypeScript, we’ve been able to improve the quality of our code, improve our productivity and create our fabulous Playground we’re so proud of: https://www.babylonjs-playground.com/ which provides auto-completion in the browser! We’ve also been able to welcome some new team members coming from a C# background and few JS skills with no pain. But thanks to the TypeScript compiler, we can also test the future without rewriting a single line of code!

David and I are still coding babylon.js using Visual Studio and TFS while pushing in a regular manner our code to the github repo. By upgrading our project to Visual Studio 2015 RTM, we’ve been able to upgrade it to TypeScript 1.5.

Once done, let me show you how quickly I’ve upgraded Babylon.js from ES5 to ES6. Right-click on your project, navigate to “TypeScript Build” and switch the “ECMAScript version” from ES5 to ES6:

image

And that’s it! Sourire

Recompile the solution and you can test the future of ES6 today.

I’ve published an ES6 version of babylonjs.com here: https://www.babylonjs.com/indexES6.html to let you play with it.

Microsoft Edge and ES6

If you’re interested in ES6, I would recommend you watching this BUILD session: What’s New in JavaScript for Fast and Scalable Apps

You’ll then note you need a very recent modern browser such as Microsoft Edge to be able to execute this demo & code. MS Edge with Firefox 41 are currently the most advanced browsers in ES6 support. You can check your current browser ES6 support by navigating to this URL: https://kangax.github.io/compat-table/es6/

Launching it in MS Edge on Windows 10 (build 10240), you’ll have those results:

image

67% of ES6 features supported in MS Edge and 68% in Firefox 41 out of the box. Those results are impressive!

Still, if you’re launching the ES6 version of Babylon.js in MS Edge, you’ll see some errors in F12:

image

Let’s navigate to babylon.math.js to check the error. We’re falling here:

image

The “class” keyword is not supported yet for production. This is because the spec has changed recently and all browsers are thus putting it behind a flag.

To enable it, navigate to “about:flags” in MS Edge and “Enable experimental JavaScript features

image

If you now run again the ES6 compatibility tool: https://kangax.github.io/compat-table/es6/, you’ll see now that MS Edge is jumping to 81% of ES6 features supported. It now supports class, super & generators:

image

To make this demo works in Firefox or Chrome, you’ll probably need a nightly build.

Time to play with it in F12

Now that MS Edge is properly configured, navigate to: https://www.babylonjs.com/indexES6.html and open F12 in a separate window. In the “Debugger” tab, open “babylon.gamepadCamera.js” and set a breakpoint on the “super” line of code:

 image

Launch the “Mansion” demo and switch the camera to “Gamepad Camera”:

image

You’ll properly break into the code as expected:

image

Press F11 to jump into the extended class (BABYLON.FreeCamera):

image

You’re currently debugging ES6 code! Isn’t that cool? Sourire

In the “Debugger” tab, open “babylon.virtualJoystick.js” and set a breakpoint on line 78 inside the arrow function:

image

Switch the camera to “Virtual joysticks camera”, touch the screen or left click it and you’ll be able to debug the arrow function:

image

Now, imagine you’d like to edit your ECMAScript 6 code to improve your debbuging experience. Go to the “Experiments” tab and enable the “Edit JavaScript” option:

image

And restart the browser. Now, add this line of code on the previous handle “let foo = ‘test’ ” and do again the previous action:

image

Ok, let’s recap. This demo is using: ECMAScript 6 with classes, super & arrow functions, WebGL, Web Audio, Gamepad API and Pointer Events. Thank you Babylon.js, thank you TypeScript & thank you MS Edge! Clignement d'œil

If you’re interested about other improvements we’ve done in F12, have a look to this article: Announcing the latest improvements for the F12 developer tools in Windows 10 

David