{"id":536,"date":"2015-03-25T20:51:13","date_gmt":"2015-03-25T18:51:13","guid":{"rendered":"https:\/\/kari.world.ikari.fi\/?p=536"},"modified":"2015-03-25T20:51:13","modified_gmt":"2015-03-25T18:51:13","slug":"boxing-match-traceur-vs-babel","status":"publish","type":"post","link":"https:\/\/kari.world.ikari.fi\/?p=536","title":{"rendered":"Boxing match: traceur vs babel"},"content":{"rendered":"<p>Evaluating traceur and babel in rails environment to see how well they behave in converting simple Ecmascript 6 file.<\/p>\n<p>Our competitors today are<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/aackerman\/traceur-rails\">traceur-rails<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/gunpowderlabs\/sprockets-traceur\">sprockets-traceur<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/josh\/sprockets-es6\">sprockets-es6<\/a><\/li>\n<\/ul>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li>traceur-rails works for some cases but fails for &#8221;let&#8221; statement<\/li>\n<li>sprockets-traceur seems to fare better than traceur-rails<\/li>\n<li>sprockets-es6 is not production ready yet (depends from beta version of sprockets 4)<\/li>\n<\/ul>\n<p><strong>Original<\/strong><br \/>\n[code lang=&#8221;javascript&#8221;]<br \/>\nimport { Example } from &#8217;test\/example&#8217;;<\/p>\n<p>class Test {<br \/>\n  constructor() {<br \/>\n    console.log(&#8221;test class instantiated&#8221;);<br \/>\n  }<\/p>\n<p>  foo() {<br \/>\n    return &#8221;x&#8221;;<br \/>\n  }<br \/>\n}<br \/>\nconsole.log(&#8221;loaded test module&#8221;);<\/p>\n<p>new Test();<br \/>\nnew Example();<br \/>\n[\/code]<\/p>\n<p><strong>traceur-rails<\/strong><\/p>\n<p>Configuration:<br \/>\n[code lang=&#8221;ruby&#8221;]<br \/>\nTraceur::Config.compile_to = :register<br \/>\n[\/code]<\/p>\n<p>Result:<br \/>\n[code lang=&#8221;javascript&#8221;]<br \/>\nSystem.register(&#8221;test\/test&#8221;, [], function() {<br \/>\n  &#8221;use strict&#8221;;<br \/>\n  var __moduleName = &#8221;test\/test&#8221;;<br \/>\n  var Example = $traceurRuntime.assertObject(System.get(&#8221;test\/example&#8221;)).Example;<br \/>\n  var Test = function Test() {<br \/>\n    console.log(&#8221;test class instantiated&#8221;);<br \/>\n  };<br \/>\n  ($traceurRuntime.createClass)(Test, {foo: function() {<br \/>\n      return &#8221;x&#8221;;<br \/>\n    }}, {});<br \/>\n  console.log(&#8221;loaded test module&#8221;);<br \/>\n  new Test();<br \/>\n  new Example();<br \/>\n  return {};<br \/>\n});<br \/>\n[\/code]<\/p>\n<p><strong>sprockets-traceur<\/strong><\/p>\n<p>Configuration:<br \/>\n[code lang=&#8221;ruby&#8221;]<br \/>\nTraceur.configure do |c|<br \/>\n  precompiled = !(Rails.env.development? || Rails.env.test?)<br \/>\n  c.default_compilation_options.modules = :register<br \/>\n  c.default_compilation_options.source_map = !precompiled<br \/>\nend<br \/>\n[\/code]<\/p>\n<p>Result:<br \/>\n[code lang=&#8221;javascript&#8221;]<br \/>\nSystem.registerModule(&#8221;test\/test&#8221;, [], function() {<br \/>\n  &#8221;use strict&#8221;;<br \/>\n  var __moduleName = &#8221;test\/test&#8221;;<br \/>\n  var Example = System.get(&#8221;test\/example&#8221;).Example;<br \/>\n  var Test = function Test() {<br \/>\n    console.log(&#8221;test class instantiated&#8221;);<br \/>\n  };<br \/>\n  ($traceurRuntime.createClass)(Test, {foo: function() {<br \/>\n      return &#8221;x&#8221;;<br \/>\n    }}, {});<br \/>\n  console.log(&#8221;loaded test module&#8221;);<br \/>\n  new Test();<br \/>\n  new Example();<br \/>\n  return {};<br \/>\n});<\/p>\n<p>\/\/# sourceMappingURL=data:application\/json;base64,eyJ2ZX&#8230;<br \/>\n;<br \/>\n[\/code]<br \/>\n<strong>sprockets-es6<\/strong><br \/>\n[code lang=&#8221;javascript&#8221;]<br \/>\n\/\/ NA &#8211; failed to get sprockets-es6 to work<br \/>\n[\/code]<\/p>\n<p><strong>sprockets-es6 using online test in babel website<\/strong><br \/>\n<a href=\"https:\/\/babeljs.io\/repl\/\">Babel online transform tester<\/a><br \/>\n[code lang=&#8221;javascript&#8221;]<br \/>\n&#8221;use strict&#8221;;<\/p>\n<p>var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();<\/p>\n<p>var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(&#8221;Cannot call a class as a function&#8221;); } };<\/p>\n<p>var Example = require(&#8221;test\/example&#8221;).Example;<\/p>\n<p>var Test = (function () {<br \/>\n  function Test() {<br \/>\n    _classCallCheck(this, Test);<\/p>\n<p>    console.log(&#8221;test class instantiated&#8221;);<br \/>\n  }<\/p>\n<p>  _createClass(Test, {<br \/>\n    foo: {<br \/>\n      value: function foo() {<br \/>\n        return &#8221;x&#8221;;<br \/>\n      }<br \/>\n    }<br \/>\n  });<\/p>\n<p>  return Test;<br \/>\n})();<\/p>\n<p>console.log(&#8221;loaded test module&#8221;);<\/p>\n<p>new Test();<br \/>\nnew Example();<br \/>\n[\/code]<\/p>\n<p><strong>Notes<\/strong><\/p>\n<ul>\n<li>traceur-rails wants files to use <strong>.tc<\/strong> extension, which is a bit ackward<\/li>\n<li>sprockets-traceur uses <strong>.js.es6<\/strong> or <strong>.es6<\/strong> file extension<\/li>\n<li>sprockets-es6 uses <strong>.es6<\/strong> file extension for files<\/li>\n<li>sprockets-traceur generates embedded source maps by default into generated files. These source maps work well in development mode, in production they naturally won&#8217;t work, thus generation of them for production should be turned off<\/li>\n<li>Regarding editors, Emacs with latest version of <a href=\"https:\/\/github.com\/mooz\/js2-mode\">js2-mode<\/a> combined with latest version of <a href=\"https:\/\/github.com\/flycheck\/flycheck\">flycheck<\/a> (with <a href=\"http:\/\/jshint.com\/docs\/\">jshint<\/a>) appears to be now in usable state. Just check and install latest versions of packages via &#8221;M+x package-list-packages.\n<li>Caveat Emptor: Traceur requires that nodejs is installed, which causes slightly undesired extra dependency for assets precompile. Possible fix for this is to precompile assets locally and scp results to deployed server.<\/li>\n<\/ul>\n<p><strong>Conclusion<\/strong><br \/>\nFor a while, I would recommend <strong><a href=\"https:\/\/github.com\/gunpowderlabs\/sprockets-traceur\">sprockets-traceur<\/a><\/strong><\/strong>, since it appears to be most robust from this set of competitors. Considering output format, not seeing real benefits into either direction, but I slightly prefer more compact output of traceur. However, traceur output format with &#8221;:register&#8221; mode works neatly with rails javascript assets precompile. In the future if sprockets-es6 becomes standard part of sprockets gem, then I would start considering switching into it. However, it&#8217;s very likely thjat sprockets 4 will work only with very latest version of rails, which would hinder it very quickly as no-go option.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Evaluating traceur and babel in rails environment to see how well they behave in converting simple Ecmascript 6 file. Our competitors today are traceur-rails sprockets-traceur sprockets-es6 Notes: traceur-rails works for some cases but fails for &#8221;let&#8221; statement sprockets-traceur seems to fare better than traceur-rails sprockets-es6 is not production ready yet (depends from beta version of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,8,13],"tags":[],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-development","category-javascript","category-rails"],"_links":{"self":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=536"}],"version-history":[{"count":0,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kari.world.ikari.fi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}