More Related Content Similar to ES6 in Production [JSConfUY2015] (20) More from Guillermo Paz (7) ES6 in Production [JSConfUY2015]2. - Made in Buenos Aires, Argentina
- Front-end Developer
- Working at Mango
@pazguille (twitter / github)
Guille Paz
15. // Deps
var inherit = require('inherit');
var Emitter = require('emitter');
// Slideout
function Slideout(options) { … }
// Export
module.exports = Slideout;
Why?
CommonJS
16. Why?
ES6 Modules
// Deps
import inherit from 'inherit';
import Emitter from 'emitter';
// Slideout
function Slideout(options) { … }
// Export
export default Slideout;
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.exports = Slideout;
Why?
22. // 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;
23. Why?
Classes
arrow = > functions
Module Syntax
let/const
Rest Parameters
Templates Strings Default Parameters
42. 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>
61. 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
79. '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
81. // 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
82. // 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
88. class Emitter {
…
on() { … }
…
}
Issues
Emitter.js - ES5
function Emitter() {}
Object.defineProperties(Emitter.prototype, {
'on': {
'writable': true,
'configurable': true,
'enumerable': false,
'value': function on() {}
}
});
89. class Emitter {
…
on() { … }
…
}
Issues
Emitter.js - ES5
function Emitter() {}
Object.defineProperties(Emitter.prototype, {
'on': {
'writable': true,
'configurable': true,
'enumerable': false,
'value': function on() {}
}
});
92. 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
93. class Emitter {
…
on() { … }
…
}
Issues
Emitter.js - ES5
var Emitter = (function () {
function Emitter() { … }
Emitter.prototype.on = function on() {};
…
return Emitter;
})();
96. '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
97. 'use strict';
// Inherits prototype properties.
module.exports = function inherit(child, parent) {
child.prototype = Object.create(parent.prototype);
return parent.prototype;
};
Issues
inherit.js - ES5
99. class Slideout extends Emitter {
constructor(options={}) {
this._padding = options.padding;
…
}
}
Issues
Slideout.js - ES6
101. class Slideout extends Emitter {
constructor(options={}) {
this._padding = options.padding;
…
}
}
Issues
Slideout.js - ES6
102. class Slideout extends Emitter {
constructor(options={}) {
super(options);
this._padding = options.padding;
…
}
}
Issues
Slideout.js - ES6
106. import _ from 'i18n';
import translations from 'translations.json';
_.add(translations);
import login from './login';
…
Issues
Login View - ES6
107. import _ from 'i18n';
import translations from 'translations.json';
_.add(translations);
import login from './login';
…
Issues
Login View - ES6
109. 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']);
110. 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
115. ● Transpile to ES5 (Babel)
● Use ES5/ES6 polyfills (core-js)
Takeaway
116. ● Transpile to ES5 (Babel)
● Use ES5/ES6 polyfills (core-js)
● Babelify: opts.global or package.json
Takeaway
117. Takeaway
● Transpile to ES5 (Babel)
● Use ES5/ES6 polyfills (core-js)
● Babelify: opts.global or package.json
● Write ES6, publish ES5 (compile task)
118. ● 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