Bringing ES6 to programmers as soon as possible
I guess the reason point 3 "is difficult" is that versioning is not part of the script tag or at least is not standard.
In Mozilla browsers we have script type="text/javascript;version=1.8" as example but nothing about script type="text/javascript" ecma="3" with possibility to "5", "5.1", and minor versions too or similar techniques (whatever that could work).
Said that, looks like developers just don't want to use simple approaches and I have no idea why ...
<script> // the "all you really need" + "works everywhere on web" loader function load(u){ var d = document, e = d.documentElement, s = e.insertBefore( d.createElement("script"), e.lastChild ); s.type = "text/javascript"; // probably not needed today s.src = u; } </script> <script>// the strict code
load(function(){ "use strict"; return this ? "js5/main.js" : "js3/main.js" ; }()); </script> <script>// the let code
load(function(){ try{ return eval("let x=0;"), "jsNext/library.js"; } catch(e) { return "shared/library.js"; } }()); // and so on ... </script>
So that you can have mini language features detection and serve the best file accordingly.
This might require a transpiler that transform a single file into ES3 capable files and as you pointed out is not that difficult to do so ... again, I have really no idea why that 3rd point is considered difficult.
Nice post in any case, an extra consideration about this matter coul dbe AnotherJS approach, just pointing out: WebReflection/another-js
br
I would really like to see a shared resource collecting ES6 shims and techniques.
Since this involves checking those shims against the evolving spec, it would be good to have this as a wiki page moderated by tc39 (otherwise we'll keep seeing new blog posts showing how to use old specs).
Then transpilation ideas could be shared and reused, but also be whittled down and improved.
For instance, traceur maps let to try/throw/catch, which is clever for a prototype, but not practical for production use (defeating optimizers). TypeScript maps arrow functions to a 'that=this' pattern, which is clever, but would be rather complicated to get right, and TypeScript doesn't (better to use .bind, and shim that for ES3). Mapping let to IIFE is simple, but needs refinement for 'this'/'arguments'/.. . And so on, and so on.
Claus
Smaller notes on the blog post
-
harmonizr != modernizr (url typo)
-
for transpilers, feel free to link to clausreinke.github.com/js-tools/resources.html#group:language-extensions
-
it is good to see the TypeScript project holding their ES6+types position against all those .net folks who want to change it into something else; if that wall was ever breached, TS would hold the same disadvantages as CS
FWIW: This topic has been on my mind for a long time. I’ve finally written down my thoughts. Feedback welcome.
www.2ality.com/2012/12/es6-workflow.html