How to enable es6?

I am receiving a warning when I use ES6 features (I want to use some arrow functions rather than binding this to that/self, as well as some other features) and it mentions “use ‘esversion: 6’”, but I can’t find any documentation, previous threads, or google results for where to enable this. Is it like strict mode? Or is there a preset somewhere I should enable? It it using a linter I need to configure or something?

Thanks for your time in reading this! If I find the answer myself I’ll update this post.

EDIT: Found it. Credit to user csmith. Apparently it is a linter thing (which I didn’t know because I don’t use that particular linter). Just add:

/*jshint esversion: 6 */

to line 1 of your file and you appear to be good to go. At least all the ES6 features I was using no longer give warnings and do work.

3 Likes

Just remember, by using some of new features, they might be not everywhere available, this means some platforms which are WebGL capable, but not ES6 capable won’t run your project.

To check how much of an impact it will make on your audience, it is good to use some generic statistics, for example website https://caniuse.com/

Lets say for Arrow Functions: https://caniuse.com/#search=arrow%20functions ~73% of global browser share is supporting them. Meaning you potentially loos up to 27% of audience.
WebGL (https://caniuse.com/#search=webgl) has around ~92% of global support.

Just make sure this is what you want, as changing back to ES5 is not as easy when project codebase is large.

1 Like

@max So, if ES6 is what I really want, can I set basic script template? Now, there is several comments and two empty functions. But I want create script without comments and with .extend() function, for example. This is no problem to erase all and retype each time I creating new script, but… ><

I’ve successfully used Webpack, a custom loader and plugin to use Babel to compile fully modularised ES6 to Playcanvas scripts. Works pretty well, though it takes monkeypatching the script loader in my loading screen to make it work lol. But debugging and everything work fine with the source maps.

[ I mean integrating ES6 with the Playcanvas Editor generated scenes etc, clearly it’s easy with just the Engine ]

1 Like

What is the current status of ES6 support in PC? All modern browsers support it already (https://caniuse.com/?search=es6). I have used PS in some projects now and have noticed that among the developers that get to know PC the most repulsive feature is that you must write vanilla JavaScript with it.

I know I can use ES6 with the /*jshint esversion: 6 */, but then the built-in minifier/obfuscator doesnt do its job. Is this likely to change at some point?

There’s an issue in Github on this at moment. Given what’s in the pipeline at the moment, we probably won’t look at this until the new year.

See: