Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ES6 in Production

ES6 in Production

Everybody is talking about ES6, but is anyone out there using it in production? Mango recently started adopting ES6 features that make our front-end code easier to write and maintain.

In this talk, I will explain why we decided to use ES6, how we started to use it in production (using npm, browserify and babel) and I'll explain what problems we found along the way and how we solved them.

Links:
https://getmango.com
https://getmango.com/blog/writing-es6-modules-with-6to5/
http://kangax.github.io/compat-table/
https://github.com/zloirock/core-js
https://github.com/mango/emitter
http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts
http://benmccormick.org/2015/04/07/es6-classes-and-backbone-js/

Guille Paz

April 25, 2015
Tweet

More Decks by Guille Paz

Other Decks in Technology

Transcript

  1. - Made in Buenos Aires, Argentina - Front-end Developer -

    Working at Mango @pazguille (twitter / github) Guille Paz
  2. ES6

  3. define('Slideout', // Deps ['inherit', 'Emitter'], // Slideout function(inherit, Emitter) {

    function Slideout(options) { … } // Export return Slideout; }); Why? AMD
  4. // Deps var inherit = require('inherit'); var Emitter = require('emitter');

    // Slideout function Slideout(options) { … } // Export module.exports = Slideout; Why? CommonJS
  5. Why? ES6 Modules // Deps import inherit from 'inherit'; import

    Emitter from 'emitter'; // Slideout function Slideout(options) { … } // Export export default Slideout;
  6. // Slideout function Slideout(options) { … } // Inherit from

    Emitter inherit(Slideout, Emitter); // Extend prototype Slideout.prototype.open = function() { … }; Why? Classes
  7. // Deps var inherit = require('inherit'); var Emitter = require('emitter');

    // Slideout function Slideout(options) { … } // Inherit from Emitter inherit(Slideout, Emitter); // Extend prototype Slideout.prototype.open = function() { … }; // Export module.exports = Slideout; Why?
  8. // Deps var inherit = require('inherit'); var Emitter = require('emitter');

    // Slideout function Slideout(options) { … } // Inherit from Emitter inherit(Slideout, Emitter); // Extend prototype Slideout.prototype.open = function() { … }; // Export module.exports = Slideout; Why? // Deps import Emitter from 'emitter'; // Slideout class Slideout extends Emitter { constructor(options={}) { … } open() { … } } // Export export default Slideout;
  9. Why? Classes arrow = > functions Module Syntax let/const Rest

    Parameters Templates Strings Default Parameters
  10. index.html How? … <!--[if lt IE 9]> <script src="/js/es5.js"></script> <![endif]-->

    <script src="/js/es6.js"></script> <script src="/js/build.js"></script> </body>
  11. Issues ├─ src ├─ boot.js └─ bus.js ├─ package.json ├─

    test └─ node_modules ├─ slideout └─ emitter Dashboard ES6 ES6
  12. Issues bus.js // Deps import Emitter from 'emitter'; // bus

    const bus = new Emitter(); // Export export default bus;
  13. Issues Dashboard ├─ src ├─ boot.js └─ bus.js ├─ package.json

    ├─ test └─ node_modules ├─ slideout └─ emitter Babelify
  14. Issues Dashboard ├─ src ├─ boot.js └─ bus.js ├─ package.json

    ├─ test └─ node_modules ├─ slideout └─ emitter Babelify
  15. Issues Dashboard ├─ src ├─ boot.js └─ bus.js ├─ package.json

    ├─ test └─ node_modules ├─ slideout └─ emitter Babelify
  16. browserify({'entries': opts.entries, 'debug': true}) .plugin('factor-bundle', {'outputs': opts.bundles}) .transform('babelify', {'global': true})

    .on('error', function(err) { … }) .bundle() .pipe(fs.createWriteStream(opts.output)); Issues
  17. … "main": "dist/index.js", "script": { "compile": "babel src --out-dir dist",

    "prepublish": "npm run compile" }, … Issues Prepublish Task
  18. 'use strict'; var extend = require('extend'); // Inherits prototype properties

    module.exports = function inherit(child, parent) { extend(child.prototype, parent.prototype); return parent.prototype; }; Issues inherit.js - ES5
  19. Issues Emitter.js - ES6 class Emitter { constructor(options={}) { …

    } on() { … } emit() { … } … } export default Emitter;
  20. // Deps var inherit = require('inherit'); var Emitter = require('emitter');

    // Slideout function Slideout(options) { … } // Inherit from Emitter inherit(Slideout, Emitter); // Extend prototype Slideout.prototype.open = function() { … }; // Export module.export = Slideout; Issues Slideout.js - ES5
  21. // Deps var inherit = require('inherit'); var Emitter = require('emitter');

    // Slideout function Slideout(options) { … } // Inherit from Emitter inherit(Slideout, Emitter); // Extend prototype Slideout.prototype.open = function() { … }; // Export module.export = Slideout; Issues Slideout.js - ES5
  22. class Emitter { … on() { … } … }

    Issues Emitter.js - ES6
  23. class Emitter { … on() { … } … }

    Issues Emitter.js - ES5 function Emitter() {} Object.defineProperties(Emitter.prototype, { 'on': { 'writable': true, 'configurable': true, 'enumerable': false, 'value': function on() {} } });
  24. class Emitter { … on() { … } … }

    Issues Emitter.js - ES5 function Emitter() {} Object.defineProperties(Emitter.prototype, { 'on': { 'writable': true, 'configurable': true, 'enumerable': false, 'value': function on() {} } });
  25. browserify({'entries': opts.entries, 'debug': true}) .plugin('factor-bundle', {'outputs': opts.bundles}) .transform('babelify', {'global': true,

    'loose': ['es6.classes']}) .on('error', function(err) { … }) .bundle() .pipe(fs.createWriteStream(opts.output)); Issues Build Process
  26. class Emitter { … on() { … } … }

    Issues Emitter.js - ES5 var Emitter = (function () { function Emitter() { … } Emitter.prototype.on = function on() {}; … return Emitter; })();
  27. 'use strict'; var extend = require('extend'); // Inherits prototype properties.

    module.exports = function inherit(child, parent) { extend(child.prototype, parent.prototype); return parent.prototype; }; Issues inherit.js - ES5
  28. 'use strict'; // Inherits prototype properties. module.exports = function inherit(child,

    parent) { child.prototype = Object.create(parent.prototype); return parent.prototype; }; Issues inherit.js - ES5
  29. Issues Login View - ES5 var _import = require('i18n'); var

    _import2 = _interopRequireWildcard(_import); var _translations = require('translations.json'); var _translations2 = _interopRequireWildcard(_translations); var _login = require('./login'); var __login2 = _interopRequireWildcard(__login); _import2['default'].add(_translations2['default']);
  30. var _import = require('i18n'); var _import2 = _interopRequireWildcard(_import); var _translations

    = require('translations.json'); var _translations2 = _interopRequireWildcard(_translations); var _login = require('./login'); var __login2 = _interopRequireWildcard(__login); _import2['default'].add(_translations2['default']); Issues Login View - ES5
  31. • Transpile to ES5 (Babel) • Use ES5/ES6 polyfills (core-js)

    • Babelify: opts.global or package.json Takeaway
  32. Takeaway • Transpile to ES5 (Babel) • Use ES5/ES6 polyfills

    (core-js) • Babelify: opts.global or package.json • Write ES6, publish ES5 (compile task)
  33. • Transpile to ES5 (Babel) • Use ES5/ES6 polyfills (core-js)

    • Babelify: opts.global or package.json • Write ES6, publish ES5 (compile task) • Babel - loose mode (es6.classes, es6.modules, … ) Takeaway