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.
Little bit of a disclaimer: we're still evaluating it at the moment (which may go on hold due to my upcoming paternity leave) so these are the current answers, but may not be the final answers if/when we roll it out at scale.
It's not without additional tradeoffs (longer build times for prod; if you're logging uncaught errors, make sure to capture the UA as well; etc.) but not having to ship polyfills/transpile helpers/unnecessarily large transpiled code could be a huge win for the users.
There are certainly some open questions around the performance optimization of ES2015+ in browsers at this time, but even if we have to wait a while for the runtime perf comes up to scratch, or only target really recent browsers, this path forward looks like a good solution for shipping only the minimum amount of complexity over the wire as the language evolves.