Transition to modern JavaScript #2641
Conversation
I think this is mistake. As ES2015 (should be written as Tested few modern features, and babel seems to be weird with |
|
I've tested more of Babel and Closure, what it outputs, and for ES5 both seem to do a good job, but for ES6, they start to differ. Example 1:Source: class Ray {
constructor(origin = new Vec3(0, 0, 0), direction = new Vec3(0, 0, -1)) {
this.origin = origin;
this.direction = Object.values(direction);
}
}Closure Compiler ES6: class Qd {
constructor(a=new z(0,0,0), b=new z(0,0,-1)) {
this.origin = a;
this.direction = Object.values(b)
}
}Babel + Terser ES6: class Ray {
constructor() {
var e = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : new Vec3(0,0,0)
, t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : new Vec3(0,0,-1);
this.origin = e,
this.direction = Object.values(t)
}
}Issues:
Example 2:Source: containsPoint(point) {
for (const plane of this.planes) {
if (plane[0] * point.x + plane[1] * point.y + plane[2] * point.z + plane[3] <= 0) {
return false;
}
}
return true;
}Closure Compiler ES6: containsPoint(a) {
for (const b of this.planes)
if (0 >= b[0] * a.x + b[1] * a.y + b[2] * a.z + b[3])
return !1;
return !0
}Babel + Terser ES6: containsPoint(e) {
for (var t of this.planes)
if (t[0] * e.x + t[1] * e.y + t[2] * e.z + t[3] <= 0)
return !1;
return !0
}Issues:
File sizes:Right now, I've tested only on engine with small part moved to ES6+ code, but it already produces different file sizes:
Size difference between them is insignificant. ES5 Babel + Terser is smaller than Closure Compiler ES5, for around ~2.5%. While ES6 version of Babel + Terser is bigger than Closure Compiler ES6 by around ~2.3%. Size wise, both paths produce good results, but I assume that Closure Compiler will lead for ES6, as Babael + Terser sometimes transpiles valid ES6 code to ES5. My only concern, is why Babel + Terser is transpiling some of valid ES6 features into ES5, when instructed to use ES6 target. |
|
@Maksims Ha, yes, sorry, I get confused with all the JavaScript language naming. You're right - I have corrected my original post.
Fixed with this commit.
I'm not seeing this. With the commit above, I'm getting the following (after beautifying): class Ray {
constructor(e = new Vec3, t = new Vec3(0, 0, -1)) {
this.origin = e, this.direction = t
}
set(e, t) {
return this.origin.copy(e), this.direction.copy(t), this
}
}
Yeah, I noticed that. I'm not sure why but I don't think it's making a significant difference. |
|
Oh, and that |
| @@ -4,6 +4,11 @@ import { Vec3 } from './vec3.js'; | |||
| import { Vec4 } from './vec4.js'; | |||
|
|
|||
| var _halfSize = new Vec2(); | |||
| var x = new Vec3(); | |||
mvaligursky
Dec 29, 2020
Contributor
just curious if these could be inside class now as static or similar?
just curious if these could be inside class now as static or similar?
mvaligursky
Dec 29, 2020
Contributor
also, will these variables with names "x" "y" "z" create possible conflicts in the rest of the class?
also, will these variables with names "x" "y" "z" create possible conflicts in the rest of the class?
willeastcott
Dec 29, 2020
•
Author
Contributor
We could do. I think I'd probably put them on the Vec3 class as Vec3._temp1, Vec3._temp2, Vec3._temp3 maybe. But I personally don't have a big problem with these being global to the module. It's kind of nice because the app code cannot access them (and therefore misuse them), which isn't true of they're on the class.
We could do. I think I'd probably put them on the Vec3 class as Vec3._temp1, Vec3._temp2, Vec3._temp3 maybe. But I personally don't have a big problem with these being global to the module. It's kind of nice because the app code cannot access them (and therefore misuse them), which isn't true of they're on the class.
willeastcott
Dec 29, 2020
•
Author
Contributor
And no, conflicts aren't possible. Rollup renames variables that conflict when building the bundled library. It's pretty cool!
And no, conflicts aren't possible. Rollup renames variables that conflict when building the bundled library. It's pretty cool!
mvaligursky
Dec 29, 2020
Contributor
I mean more like conflict with a local variable inside function of Vec3. Few functions have "var x". If I don't have this var line would x reference the global x?
I mean more like conflict with a local variable inside function of Vec3. Few functions have "var x". If I don't have this var line would x reference the global x?
willeastcott
Dec 29, 2020
Author
Contributor
Yes.
Yes.
| this.center.copy(src.center); | ||
| this.halfExtents.copy(src.halfExtents); | ||
| this.type = src.type; |
mvaligursky
Dec 29, 2020
Contributor
should be ok to remove, I don't see references to it .. but curious what it was?
should be ok to remove, I don't see references to it .. but curious what it was?
willeastcott
Dec 29, 2020
Author
Contributor
So this used to be a string type property on each shape instance that specified what it was. But it was removed years ago. This bug was actually pointed out to me by the Typescript compiler but since that PR is unlikely to be merged, I'm adding the fix here.
So this used to be a string type property on each shape instance that specified what it was. But it was removed years ago. This bug was actually pointed out to me by the Typescript compiler but since that PR is unlikely to be merged, I'm adding the fix here.
|
looking great! |
This comment has been minimized.
This comment has been minimized.
|
should use "const" instead of "let"? |
This comment has been minimized.
This comment has been minimized.
|
Fixed |


Inspired by this talk from Chrome Dev Summit.
This PR migrates a portion of the PlayCanvas codebase from strict ES5 to support the very latest JS language features. Initially, the code has been updated to leverage:
const(sparingly for now)Code updated so far is:
bundlecoremathresourcesshapetemplateextras(MiniStats)This is all made possible by adding Babel to the engine's rollup build process.
The rollup configuration has also been updated to generate
playcanvas.mjs, an ES Module build of the engine. So now we have:playcanvas.mjs: Transpiled to ES6 (ECMAScript 2015)playcanvas.js: Transpiled to ES5 (ECMAScript 2009)This PR follows up on the previous unmerged PR that migrated the engine codebase to Typescript. There was much debate about the overall benefit of adopting Typescript. Typescript provides two main benefits:
But we can achieve 2 with Babel. Therefore, it is probably easier to migrate to TS-like JS (ES6 classes in particular), and then revisit the benefits of Typescript. If Typescript is adopted at some point, this will be a stepping stone almost directly in that direction.
Let's look at output in terms of
pc.Vec2for example. Previously, it was:Now it's:
So essentially, a closure is introduced and
Object.assignis removed. Still just as easy to debug.Feedback most definitely welcome!
Closes #2642
I confirm I have signed the Contributor License Agreement.