The attached file file spits out a string for direct injection into the head of a page, which will be a lightweight test of the ability to parse and run ES2015 syntax.
This is the original source for the test-case below:
class ಠ_ಠ extends Array{
constructor(j = "a", ...c) {
const q = (({u: e}) => {
return {
[`s${c}`]: Symbol(j)
};
})({});
super(j, q, ...c);
}
}
new Promise((f) => {
const a = function* (){
return "\u{20BB7}".match(/./u)[0].length === 2 || true;
};
for(let vre of a()){
const [uw, as, he, re] = [new Set(), new WeakSet(), new Map(), new WeakMap()];
break;
}
f(new Proxy({}, {get: (han, h) => h in han ? han[h] : "42".repeat(0o10)}));
}).then(bi => new ಠ_ಠ(bi.rd));
This code is a set of ES2015 syntax and features that represent a broad but not deep test case for a browser supporting es2015.
- arrows functions
- classes
- enhanced object literals
- template strings
- destructuring
- default + rest + spread
- let + const
- iterators + for..of
- generators
- unicode
- proxies
- symbols
- subclassable built-ins
- promises
- math + number + string + array + object APIs
- binary and octal literals
Explanation of how this code is used can be found in the talk Jem and I gave: "Progressive Transpilation at Netflix and the road to running native ES2015 in production".
Video here. The Progressive Transpilation part starts at 25:25:
Slides here:
Remember, this isn't a full test suite; the intended use case is to:
- Parse the UA string and determine ES2015 eligibility
- If ES2015 is indicated, ship a page with ES2015-level code.
- Inject the string produced in this file in a script tag at the top of the page which will check to make sure the browser can parse ES2015 syntax and run some of the newer constructs.
- If it turns out the browser's UA was a lie, and it can't actually parse or run ES2015, we set a cookie to limit future requests to ES5, and bounce the page as we know the incoming JS package won't work on this browser. Because the cookie is sticky, the interaction happens only once, and is only for corner cases where browsers are lying about their capabilities -- a minority case already.
Soooooo... this is all sorts of awesome!!
The team here at Eventbrite is really loving the idea!
Got a couple of questions for ya.
1/ Did you benchmark how long it took to do the ES2015 test?
2/ It seems like you could set the cookie if it passes as well, and then not have to run the ES2015 test ever again!
3/ Could you also use the info from the cookie to inform the UA lookup that the UA actually doesn't support ES2015? Or is it that the same UA could pass for one person's computer and not for another?
4/ Did you use a library to do the UA lookup or is it something hand-rolled from the ES6 compat table?
5/ Assuming that you're also using ES2016, ES2017 or Stage-X features? Do those also get transpiled or are you doing progressive transpilation for each version of ES?
Thanks again!
(@gagoar & @albybarber)