+ All Categories
Home > Technology > ES6 in Production [JSConfUY2015]

ES6 in Production [JSConfUY2015]

Date post: 17-Jul-2015
Category:
Upload: guillermo-paz
View: 132 times
Download: 0 times
Share this document with a friend
119
ES6 in Production JSConf Uruguay 2015
Transcript
Page 1: ES6 in Production [JSConfUY2015]

ES6 in ProductionJSConf Uruguay 2015

Page 2: ES6 in Production [JSConfUY2015]

- Made in Buenos Aires, Argentina

- Front-end Developer

- Working at Mango

@pazguille (twitter / github)

Guille Paz

Page 3: ES6 in Production [JSConfUY2015]

#ES6inProdYour feedback is welcome!

Page 4: ES6 in Production [JSConfUY2015]

ES6

Page 6: ES6 in Production [JSConfUY2015]

Why?

Page 7: ES6 in Production [JSConfUY2015]

Why?

Page 8: ES6 in Production [JSConfUY2015]

Future

Why?

Page 9: ES6 in Production [JSConfUY2015]

Why?

Code

Page 10: ES6 in Production [JSConfUY2015]

Why?

● Write expressive code

Page 11: ES6 in Production [JSConfUY2015]

Why?

● Write expressive code

● Easier to understand

Page 12: ES6 in Production [JSConfUY2015]

Why?

● Write expressive code

● Easier to understand

● Standardizes commons practices

Page 13: ES6 in Production [JSConfUY2015]

Why?

ES6 Modules

Page 14: ES6 in Production [JSConfUY2015]

define('Slideout',

// Deps

['inherit', 'Emitter'],

// Slideout

function(inherit, Emitter) {

function Slideout(options) { … }

// Export

return Slideout;

});

Why?

AMD

Page 15: ES6 in Production [JSConfUY2015]

// Deps

var inherit = require('inherit');

var Emitter = require('emitter');

// Slideout

function Slideout(options) { … }

// Export

module.exports = Slideout;

Why?

CommonJS

Page 16: ES6 in Production [JSConfUY2015]

Why?

ES6 Modules

// Deps

import inherit from 'inherit';

import Emitter from 'emitter';

// Slideout

function Slideout(options) { … }

// Export

export default Slideout;

Page 17: ES6 in Production [JSConfUY2015]

Why?

Classes

Page 18: ES6 in Production [JSConfUY2015]
Page 19: ES6 in Production [JSConfUY2015]

// Slideout

function Slideout(options) { … }

// Inherit from Emitter

inherit(Slideout, Emitter);

// Extend prototype

Slideout.prototype.open = function() { … };

Why?

Classes

Page 20: ES6 in Production [JSConfUY2015]

// Slideout

class Slideout extends Emitter {

constructor(options={}) { … }

open() { … }

}

Why?

Classes

Page 21: ES6 in Production [JSConfUY2015]

// 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?

Page 22: ES6 in Production [JSConfUY2015]

// 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;

Page 23: ES6 in Production [JSConfUY2015]

Why?

Classesarrow = > functions

Module Syntax

let/const

Rest Parameters

Templates Strings Default Parameters

Page 24: ES6 in Production [JSConfUY2015]

getmango.com/blog

https://getmango.com/blog/writing-es6-modules-with-6to5/

Page 25: ES6 in Production [JSConfUY2015]

How?

Page 26: ES6 in Production [JSConfUY2015]

Transpilers

How?

Page 27: ES6 in Production [JSConfUY2015]

How?

ES6 ES5

Page 28: ES6 in Production [JSConfUY2015]

How?

Page 29: ES6 in Production [JSConfUY2015]

How?

Page 30: ES6 in Production [JSConfUY2015]

Build Process

How?

Page 31: ES6 in Production [JSConfUY2015]

How?

Page 32: ES6 in Production [JSConfUY2015]

browserify({'entries': opts.entries, 'debug': true})

.plugin('factor-bundle', {'outputs': opts.bundles})

.on('error', function(err) { … })

.bundle()

.pipe(fs.createWriteStream(opts.output));

How?

Page 33: ES6 in Production [JSConfUY2015]

How?

Babelify

Page 34: ES6 in Production [JSConfUY2015]

browserify({'entries': opts.entries, 'debug': true})

.plugin('factor-bundle', {'outputs': opts.bundles})

.transform('babelify')

.on('error', function(err) { … })

.bundle()

.pipe(fs.createWriteStream(opts.output));

How?

Page 35: ES6 in Production [JSConfUY2015]

Browser

How?

Page 36: ES6 in Production [JSConfUY2015]

How?

http://kangax.github.io/compat-table/es5/

ES5

Page 37: ES6 in Production [JSConfUY2015]

Polyfills

How?

Page 38: ES6 in Production [JSConfUY2015]

How?

http://kangax.github.io/compat-table/es6/

ES6

Page 39: ES6 in Production [JSConfUY2015]

core-js

How?

https://github.com/zloirock/core-js

Page 40: ES6 in Production [JSConfUY2015]

How?

es5.js(IE < 9)

Custom build

https://github.com/zloirock/core-js

Page 41: ES6 in Production [JSConfUY2015]

How?

es5.js(IE < 9)

es6.js(all)

Custom build

https://github.com/zloirock/core-js

Page 42: ES6 in Production [JSConfUY2015]

index.htmlHow?

<!--[if lt IE 9]>

<script src="/js/es5.js"></script>

<![endif]-->

<script src="/js/es6.js"></script>

<script src="/js/build.js"></script>

</body>

Page 43: ES6 in Production [JSConfUY2015]
Page 44: ES6 in Production [JSConfUY2015]
Page 45: ES6 in Production [JSConfUY2015]

Issues

Page 46: ES6 in Production [JSConfUY2015]

Issues

Context

Page 47: ES6 in Production [JSConfUY2015]

~110 modules

Issues

Page 48: ES6 in Production [JSConfUY2015]

Issues

Page 49: ES6 in Production [JSConfUY2015]

ES5 / ES6

Issues

Page 50: ES6 in Production [JSConfUY2015]

Dependencies

Issues

Page 51: ES6 in Production [JSConfUY2015]

Issues

├─ src

├─ boot.js

└─ bus.js

├─ package.json

├─ test

└─ node_modules

├─ slideout

└─ emitter

Dashboard

ES6

ES6

Page 52: ES6 in Production [JSConfUY2015]

Issues

bus.js

// Deps

import Emitter from 'emitter';

// bus

const bus = new Emitter();

// Export

export default bus;

Page 53: ES6 in Production [JSConfUY2015]

Issues

Page 54: ES6 in Production [JSConfUY2015]

exports['default'] = bus;

module.exports = exports['default'];

},{'emitter':2}],2:[function(require,module,exports){

class Emitter {

on(event, listener) {

Issues

output.js

Page 55: ES6 in Production [JSConfUY2015]
Page 56: ES6 in Production [JSConfUY2015]

Issues

Dashboard

├─ src

├─ boot.js

└─ bus.js

├─ package.json

├─ test

└─ node_modules

├─ slideout

└─ emitter

Babelify

Page 57: ES6 in Production [JSConfUY2015]

Issues

Dashboard

├─ src

├─ boot.js

└─ bus.js

├─ package.json

├─ test

└─ node_modules

├─ slideout

└─ emitter

Babelify

Page 58: ES6 in Production [JSConfUY2015]

Issues

Dashboard

├─ src

├─ boot.js

└─ bus.js

├─ package.json

├─ test

└─ node_modules

├─ slideout

└─ emitter

Babelify

Page 59: ES6 in Production [JSConfUY2015]

global : true

Issues

Page 60: ES6 in Production [JSConfUY2015]

browserify({'entries': opts.entries, 'debug': true})

.plugin('factor-bundle', {'outputs': opts.bundles})

.transform('babelify')

.on('error', function(err) { … })

.bundle()

.pipe(fs.createWriteStream(opts.output));

Issues

Page 61: ES6 in Production [JSConfUY2015]

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

Page 62: ES6 in Production [JSConfUY2015]

exports['default'] = bus;

module.exports = exports['default'];

},{'emitter':2}],2:[function(require,module,exports){

var Emitter = (function () {

function Emitter() {

Issues

output.js

Page 63: ES6 in Production [JSConfUY2015]

package.json

Issues

Page 64: ES6 in Production [JSConfUY2015]

"browserify": {

"transform": ["babelify"]

},

Issues

Emitter.js - package.json

Page 65: ES6 in Production [JSConfUY2015]

"browserify": {

"transform": ["babelify"]

},

"dependencies": {

"babelify": "6.0.2"

},

Issues

Emitter.js - package.json

Page 66: ES6 in Production [JSConfUY2015]

Issues

Page 67: ES6 in Production [JSConfUY2015]

Writing ES6

Issues

Page 68: ES6 in Production [JSConfUY2015]

Publishing ES5

Issues

Page 69: ES6 in Production [JSConfUY2015]

Issues

Module

├─ src

└─ index.js

├─ package.json

└─ test

Page 70: ES6 in Production [JSConfUY2015]

Issues

Module

├─ src

└─ index.js

├─ package.json

└─ test

ES6

Page 71: ES6 in Production [JSConfUY2015]

Issues

Module

ES5

├─ src

└─ index.js

├─ package.json

└─ test

├─ dist

└─ index.js

Page 72: ES6 in Production [JSConfUY2015]

"main": "dist/index.js",

Issues

package.json

Page 73: ES6 in Production [JSConfUY2015]

Issues

Compile Task(npm, grunt, gulp, broccoli)

Page 74: ES6 in Production [JSConfUY2015]

"main": "dist/index.js",

"script": {

"compile": "babel src --out-dir dist"

},

Issues

Compile Task

Page 75: ES6 in Production [JSConfUY2015]

Issues

npm run compile

Page 76: ES6 in Production [JSConfUY2015]

"main": "dist/index.js",

"script": {

"compile": "babel src --out-dir dist",

"prepublish": "npm run compile"

},

Issues

Prepublish Task

Page 77: ES6 in Production [JSConfUY2015]

mango/emitter

Issues

https://github.com/mango/emitter

Page 78: ES6 in Production [JSConfUY2015]

Inheritance

Issues

Page 79: ES6 in Production [JSConfUY2015]

'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

Page 80: ES6 in Production [JSConfUY2015]

Issues

Emitter.js - ES6

class Emitter {

constructor(options={}) { … }

on() { … }

emit() { … }

}

export default Emitter;

Page 81: ES6 in Production [JSConfUY2015]

// 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

Page 82: ES6 in Production [JSConfUY2015]

// 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

Page 83: ES6 in Production [JSConfUY2015]

console.log(Slideout.prototype);

// { open: function }

Issues

Slideout.js - ES5

Page 84: ES6 in Production [JSConfUY2015]

console.log(Emitter.prototype);

// { }

Issues

Emitter.js - ES6

Page 85: ES6 in Production [JSConfUY2015]

Issues

http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts

Page 86: ES6 in Production [JSConfUY2015]

Issues

Page 87: ES6 in Production [JSConfUY2015]

class Emitter {

on() { … }

}

Issues

Emitter.js - ES6

Page 88: ES6 in Production [JSConfUY2015]

class Emitter {

on() { … }

}

Issues

Emitter.js - ES5

function Emitter() {}

Object.defineProperties(Emitter.prototype, {

'on': {

'writable': true,

'configurable': true,

'enumerable': false,

'value': function on() {}

}

});

Page 89: ES6 in Production [JSConfUY2015]

class Emitter {

on() { … }

}

Issues

Emitter.js - ES5

function Emitter() {}

Object.defineProperties(Emitter.prototype, {

'on': {

'writable': true,

'configurable': true,

'enumerable': false,

'value': function on() {}

}

});

Page 90: ES6 in Production [JSConfUY2015]

Loose Mode(babel)

Issues

Page 91: ES6 in Production [JSConfUY2015]

Issues

es6.classes

Page 92: ES6 in Production [JSConfUY2015]

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

Page 93: ES6 in Production [JSConfUY2015]

class Emitter {

on() { … }

}

Issues

Emitter.js - ES5

var Emitter = (function () {

function Emitter() { … }

Emitter.prototype.on = function on() {};

return Emitter;

})();

Page 94: ES6 in Production [JSConfUY2015]

console.log(Slideout.prototype);

// { open: function, on: function }

Issues

Slideout.js - ES5

Page 95: ES6 in Production [JSConfUY2015]

Object.create

Issues

Page 96: ES6 in Production [JSConfUY2015]

'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

Page 97: ES6 in Production [JSConfUY2015]

'use strict';

// Inherits prototype properties.

module.exports = function inherit(child, parent) {

child.prototype = Object.create(parent.prototype);

return parent.prototype;

};

Issues

inherit.js - ES5

Page 98: ES6 in Production [JSConfUY2015]

super() - this

Issues

Page 99: ES6 in Production [JSConfUY2015]

class Slideout extends Emitter {

constructor(options={}) {

this._padding = options.padding;

}

}

Issues

Slideout.js - ES6

Page 100: ES6 in Production [JSConfUY2015]

Line 12: 'this' is not allowed before super()

Issues

Page 101: ES6 in Production [JSConfUY2015]

class Slideout extends Emitter {

constructor(options={}) {

this._padding = options.padding;

}

}

Issues

Slideout.js - ES6

Page 102: ES6 in Production [JSConfUY2015]

class Slideout extends Emitter {

constructor(options={}) {

super(options);

this._padding = options.padding;

}

}

Issues

Slideout.js - ES6

Page 103: ES6 in Production [JSConfUY2015]

Issues

https://twitter.com/jashkenas/status/585458831993528320

Page 105: ES6 in Production [JSConfUY2015]

import & hoisting

Issues

Page 106: ES6 in Production [JSConfUY2015]

import _ from 'i18n';

import translations from 'translations.json';

_.add(translations);

import login from './login';

Issues

Login View - ES6

Page 107: ES6 in Production [JSConfUY2015]

import _ from 'i18n';

import translations from 'translations.json';

_.add(translations);

import login from './login';

Issues

Login View - ES6

Page 108: ES6 in Production [JSConfUY2015]

Issues

Page 109: ES6 in Production [JSConfUY2015]

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']);

Page 110: ES6 in Production [JSConfUY2015]

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

Page 111: ES6 in Production [JSConfUY2015]

Issues

Page 112: ES6 in Production [JSConfUY2015]

Babel 4.1.7

Issues

Page 113: ES6 in Production [JSConfUY2015]

Takeaway

Page 114: ES6 in Production [JSConfUY2015]

● Transpile to ES5 (Babel)

Takeaway

Page 115: ES6 in Production [JSConfUY2015]

● Transpile to ES5 (Babel)

● Use ES5/ES6 polyfills (core-js)

Takeaway

Page 116: ES6 in Production [JSConfUY2015]

● Transpile to ES5 (Babel)

● Use ES5/ES6 polyfills (core-js)

● Babelify: opts.global or package.json

Takeaway

Page 117: ES6 in Production [JSConfUY2015]

Takeaway

● Transpile to ES5 (Babel)

● Use ES5/ES6 polyfills (core-js)

● Babelify: opts.global or package.json

● Write ES6, publish ES5 (compile task)

Page 118: ES6 in Production [JSConfUY2015]

● 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

Page 119: ES6 in Production [JSConfUY2015]

Thank you!

<3


Recommended