progressive transpilation and the road to es2015 in production

63
1 The Road to ES2015 in Production Jacques Favreau

Upload: jacques-favreau

Post on 11-Feb-2017

49 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Progressive transpilation and the road to ES2015 in production

1

The Road to ES2015 in ProductionJacques Favreau

Page 2: Progressive transpilation and the road to ES2015 in production

2What time is it?

Doom time!

Page 3: Progressive transpilation and the road to ES2015 in production

3

• Const & Let

• Arrow Functions

• Destructuring

• Backtick/Template Strings

• Default Parameters

💧

Where we are today:

Limited ES2015

Page 4: Progressive transpilation and the road to ES2015 in production

4What are we trying to do?

Goals

👼 Best experience for our users.

🤓 Best experience for our developers.

Page 5: Progressive transpilation and the road to ES2015 in production

5

🤓 Senior UI Engineer @ Netflix

😅 Squad lead for the Web Core team

😍 Travel

😭 Clever code

Who?

Jacques Favreau

formComplete = function(){ var u = user.getUser(); var ct = 0 + (typeof(u.nickname)==='string' && u.nickname.length>0) + (typeof(u.websiteUrl)==='string' && u.websiteUrl.length>0) + (typeof(u.linkedin)==='string' && u.linkedin.length>0) + (typeof(u.locationString)==='string' && u.locationString.length>0) + (typeof(u.title)==='string' && u.title.length>0) + (typeof(u.bio)==='string' && u.bio.length>0) + (typeof(u.imageUrl)==='string' && u.imageUrl.length>0) + (typeof(u.resume)==='string' && u.resume.length>0) + (u.employerSharing && true); return Math.ceil(ct/0.09);};

Page 6: Progressive transpilation and the road to ES2015 in production

6 What’s the future look like?

1.Are we stuck?

2.Rethinking what we ship, and why

3.ES2015 in the browser

4.Balancing UX and DX now and in the future

Page 7: Progressive transpilation and the road to ES2015 in production

7With great transpilation comes great…

Hidden Cost

http://enomis94.deviantart.com

Page 8: Progressive transpilation and the road to ES2015 in production

8Where we are today

Heavy on DX

Transpiled to ES5 Polyfill ES2015

Modern

Transpiled to ES5Polyfill ES2015Polyfill ES5

Legacy

No Support

Unsupported

Page 9: Progressive transpilation and the road to ES2015 in production

9Where we are today

Heavy on DX

🤓🤓 🤓🤓

👼👼 👼👼💧

💧 💧

💧

Page 10: Progressive transpilation and the road to ES2015 in production

10Where we are today

Heavy on DX

Transpiled to ES5 Polyfill ES2015

Modern

Transpiled to ES5Polyfill ES2015Polyfill ES5

Legacy

No Support

Unsupported

Page 11: Progressive transpilation and the road to ES2015 in production

11

Ok, so let’s go the other way

Page 12: Progressive transpilation and the road to ES2015 in production

12We could go the other way

Heavy on UX

ES3

Supported

No Support

Unsupported

ES5ES5 Polyfill

Page 13: Progressive transpilation and the road to ES2015 in production

13We could go the other way

Heavy on UX

👼👼 👼👼

💧💧

💧 💧

Page 14: Progressive transpilation and the road to ES2015 in production

14

How can we do better?

Page 15: Progressive transpilation and the road to ES2015 in production

15 Things change

1.Modern browsers speak ES2015

2.Not all browsers are modern browsers

http://sanlaris.deviantart.com

Page 16: Progressive transpilation and the road to ES2015 in production

16 Things change

Is getting ES2015 into the browser a good thing for our users?

Page 17: Progressive transpilation and the road to ES2015 in production

17 Things change

20 to 40% over-the-wire payload reduction!

Page 18: Progressive transpilation and the road to ES2015 in production

18 Things change

Browsers have varying support.

Page 19: Progressive transpilation and the road to ES2015 in production

19Haven’t we done this?

Shoulders of Giants!

We have a long history of polyfilling in the browser

Page 20: Progressive transpilation and the road to ES2015 in production

20

ES5 Code

Modern

ES5 CodePolyfill ES5Shim/Shams

Legacy

No Support

Unsupported

Flatten the earth:

Polyfilling

Page 21: Progressive transpilation and the road to ES2015 in production

21

• Write expecting features

• Trade some performance for legacy browser support

Flatten the earth:

Polyfilling

Page 22: Progressive transpilation and the road to ES2015 in production

22

<script src="//polyfill.io/v2/polyfill.js?features=es6,es5&flags=gated"></script>

Polyfills:

Ship only what you need

Flatten the earth:

Polyfilling

Page 23: Progressive transpilation and the road to ES2015 in production

23What if we just shipped everything?

😭

I do not like the cone of shame

I do not like the cone of shame

Page 24: Progressive transpilation and the road to ES2015 in production

24

Why is transpilation different?

(It’s not.)

Page 25: Progressive transpilation and the road to ES2015 in production

25

👼 Best experience for our users.

🤓 Best experience for our developers.

Goals

Page 26: Progressive transpilation and the road to ES2015 in production

26

So here it comes…

Page 27: Progressive transpilation and the road to ES2015 in production

27

Netflix will start shipping native ES2015 to eligible browsers.

Page 28: Progressive transpilation and the road to ES2015 in production

28

How?

Page 29: Progressive transpilation and the road to ES2015 in production

29Our future:

Progressive Transpilation

ES2015

Modern

Legacy

No Support

Unsupported

Transpiled to ES5 Polyfill ES2015

Trailing

Transpiled to ES5 Polyfill ES2015Polyfill ES5

Shims

Page 30: Progressive transpilation and the road to ES2015 in production

30Our future:

Progressive Transpilation

👼 Best experience for our users.

🤓 Best experience for our developers.

Page 31: Progressive transpilation and the road to ES2015 in production

31Sidebar:

Look for other win opportunities

ES2015

Modern

Legacy

No Support

Unsupported

Transpiled to ES5 Polyfill ES2015Polyfill ES5

Trailing

Transpiled to ES5 Polyfill ES2015Polyfill ES5

Shims

Page 32: Progressive transpilation and the road to ES2015 in production

32

ES2015

Modern

Legacy

No Support

Unsupported

Transpiled to ES5 Polyfill ES2015Polyfill ES5

Trailing

Fallback

Sidebar:

We ship fallback to legacy browsers

Page 33: Progressive transpilation and the road to ES2015 in production

33Our future:

Progressive Transpilation

👼 Best experience for our users.

🤓 Best experience for our developers.

Page 34: Progressive transpilation and the road to ES2015 in production

34

Let’s do it!

Page 35: Progressive transpilation and the road to ES2015 in production

35On 🔥

Let’s do this!

Well… There are a few hurdles.

Page 36: Progressive transpilation and the road to ES2015 in production

36On 🔥

Let’s do this!

1. Build systems lack support

2. Minifiers don’t work

3. Targeting browsers is hard

4. Browsers are broken and lie

Page 37: Progressive transpilation and the road to ES2015 in production

37Issue #1:

Build systems

Issue #1:

Build systems lack ES2015 support

Page 38: Progressive transpilation and the road to ES2015 in production

38Issue #1:

Build systems

Issue #1:

Build systems lack ES2015 support

Page 39: Progressive transpilation and the road to ES2015 in production

39Issue #1:

Build systems

😞

Issue #1:

Build systems lack ES2015 support

Page 40: Progressive transpilation and the road to ES2015 in production

40Issue #1:

Build systems

Solution #1:

There are some build systems that do

support ES2015 as a target!

Page 41: Progressive transpilation and the road to ES2015 in production

41

const babelConfig = { modern: { presets: [] }, trailing: { presets: ['es2015'] }};

return gulp.src(glob) // Run babel with the modern config .pipe(babel(babelConfig.modern)) // Save the modern JS version. .pipe(gulp.dest('generated/modern/')) // Run Babel with trailing config. .pipe(babel(babelConfig.trailing)) // Save the trailing JS version. .pipe(gulp.dest('generated/trailing/'));

Just don’t use the

ES2015 preset.

Solutions to issue #1:

Workflows that work

Page 42: Progressive transpilation and the road to ES2015 in production

42

{ "compilerOptions": { "module": "amd", "target": "es6" }}ES2015 supported as

a target.

Solutions to issue #1:

Workflows that work

Page 43: Progressive transpilation and the road to ES2015 in production

43Issue #2:

Minfiers

Issue #2: Minifiers don’t work

Page 44: Progressive transpilation and the road to ES2015 in production

44

Issue #2: Minifiers don’t work

Issue #2:

Minfiers

Page 45: Progressive transpilation and the road to ES2015 in production

45Issue #2:

Minfiers

…but they’re working on it!

Page 46: Progressive transpilation and the road to ES2015 in production

46Solutions to Issue #2:

Bable as a minifier

While we wait…

babel-preset-minify-es2015

Page 47: Progressive transpilation and the road to ES2015 in production

47

It’s still a win!

Solutions to Issue #2:

Bable as a minifier

Page 48: Progressive transpilation and the road to ES2015 in production

48Issue #3:

Who gets what?

Issue #3: Targeting browsers is hard

Page 49: Progressive transpilation and the road to ES2015 in production

49

We unabashedly use the User Agent string

“[A]n ever-growing pack of lies[…]“

- Patrick H. Lauke

Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136

Solution to Issue #3:

Our old friend, UA

Page 50: Progressive transpilation and the road to ES2015 in production

50

On first load, it’s all we have

¯\_( )_/¯

Solution to Issue #3:

Our old friend, UA

Page 51: Progressive transpilation and the road to ES2015 in production

51

Select browsers with ES2015 support.

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

At this time, our ES2015 browsers are:

- Chrome 51

- Firefox 49

- Edge 14

- Safari 10

Solution to Issue #3:

Our old friend, UA

Page 52: Progressive transpilation and the road to ES2015 in production

52Issue #4:

Browsers lie

Issue #4: Browsers can and do lie.

Page 53: Progressive transpilation and the road to ES2015 in production

53Solution to issue #4:

Test for lying browsers

Execute a bit of Javascript

and test for lies.

Page 54: Progressive transpilation and the road to ES2015 in production

54

✓ unicode✓ proxies✓ symbols✓ subclassable built-ins✓ promises✓ math + number + string APIs✓ array + object APIs✓ binary and octal literals

Solution to issue #4:

Test for lying browsers

✓ arrows✓ classes✓ enhanced object literals✓ template strings✓ destructuring✓ default + rest + spread✓ let + const✓ iterators + for..of✓ generators

Page 55: Progressive transpilation and the road to ES2015 in production

55

class ಠ_ಠ extends Array{ constructor(j = 'a', ...c) { const q = (({u: e}) => { return { [`s${c}`]: Symbol(j) }; })({}); super(j, q, ...c); }}new Promise((f) => { const a = function* (){ return "\u{20BB7}".match(/./u)[0].length === 2 || true; }; for(let vre of a()){ const [uw, as, he, re] = [new Set(), new WeakSet(), new Map(), new WeakMap()]; break; } f(new Proxy({}, {get: (han, h) => h in han ? han[h] : "42".repeat(0o10)}));}).then(bi => new ಠ_ಠ(bi.rd));

Solution to issue #4:

Test for lying browsers

Execute a bit of Javascript

and test for lies.

Page 56: Progressive transpilation and the road to ES2015 in production

56

eval( 'class ಠ_ಠ extends Array{constructor(j="a",...c){const q=(({u:e})=>{return{[' + '`s${c}`]:Symbol(j)}})({});super(j,q,...c)}}new Promise(f=>{const a=function' + '*(){return"\u{20BB7}".match(/./u)[0].length===2||true};for(let vre of a()){' + 'const[uw,as,he,re]=[new Set,new WeakSet,new Map,new WeakMap];break} f(new ' + 'Proxy({},{get:(han,h)=>h in han?han[h]:"42".repeat(8)}))}).then(bi=>new ಠ_ಠ(bi.rd));');

Execute a bit of Javascript

and test for lies.

Solution to issue #4:

Test for lying browsers

Page 57: Progressive transpilation and the road to ES2015 in production

57

try { eval( 'class ಠ_ಠ extends Array{constructor(j="a",...c){const q=(({u:e})=>{return{[' + '`s${c}`]:Symbol(j)}})({});super(j,q,...c)}}new Promise(f=>{const a=function' + '*(){return"\u{20BB7}".match(/./u)[0].length===2||true};for(let vre of a()){' + 'const[uw,as,he,re]=[new Set,new WeakSet,new Map,new WeakMap];break} f(new ' + 'Proxy({},{get:(han,h)=>h in han?han[h]:"42".repeat(8)}))}).then(bi=>new ಠ_ಠ(bi.rd));' );} catch (e) {

}

Execute a bit of Javascript

and test for lies.

Solution to issue #4:

Test for lying browsers

Page 58: Progressive transpilation and the road to ES2015 in production

58

try { eval( 'class ಠ_ಠ extends Array{constructor(j="a",...c){const q=(({u:e})=>{return{[' + '`s${c}`]:Symbol(j)}})({});super(j,q,...c)}}new Promise(f=>{const a=function' + '*(){return"\u{20BB7}".match(/./u)[0].length===2||true};for(let vre of a()){' + 'const[uw,as,he,re]=[new Set,new WeakSet,new Map,new WeakMap];break} f(new ' + 'Proxy({},{get:(han,h)=>h in han?han[h]:"42".repeat(8)}))}).then(bi=>new ಠ_ಠ(bi.rd));' );} catch (e) { document.cookie = 'esSupportLevel=5; expires=' + (new Date((new Date()).getTime() + 2678400000)).toGMTString() + '; path=/ ;domain=netflix.com'; if(location.reload){location.reload(true);}else{location.href = location.href;}}

Execute a bit of Javascript

and test for lies.

Solution to issue #4:

Test for lying browsers

Page 59: Progressive transpilation and the road to ES2015 in production

59

<script> try { eval( 'class ಠ_ಠ extends Array{constructor(j="a",...c){const q=(({u:e})=>{return{[' + '`s${c}`]:Symbol(j)}})({});super(j,q,...c)}}new Promise(f=>{const a=function' + '*(){return"\u{20BB7}".match(/./u)[0].length===2||true};for(let vre of a()){' + 'const[uw,as,he,re]=[new Set,new WeakSet,new Map,new WeakMap];break} f(new ' + 'Proxy({},{get:(han,h)=>h in han?han[h]:"42".repeat(8)}))}).then(bi=>new ಠ_ಠ(bi.rd));' ); } catch (e) { document.cookie = 'esSupportLevel=5; expires=' + (new Date((new Date()).getTime() + 2678400000)).toGMTString() + '; path=/ ;domain=netflix.com'; if(location.reload){location.reload(true);}else{location.href = location.href;} }</script>

Execute a bit of Javascript

and test for lies.

Solution to issue #4:

Test for lying browsers

Page 60: Progressive transpilation and the road to ES2015 in production

60 What we’re doing (for now)

1. Build Babel as a transpiler.

2. Minifiers Babel plugins > babel-minify

3. Targeting User Agent to target supporting browsers

4. Lies Evaluated ES2015 to test; cookie fallback

Page 61: Progressive transpilation and the road to ES2015 in production

61Our future:

Progressive Transpilation

ES2015

Modern

Legacy

No Support

Unsupported

Transpiled to ES5 Polyfill ES2015

Trailing

Fallback

Page 62: Progressive transpilation and the road to ES2015 in production

62Mission accomplished

Goals balanced ⚖

*For now

*

👼 Best experience for our users.

🤓 Best experience for our developers.

Page 63: Progressive transpilation and the road to ES2015 in production

63

Jem Young @jemyoung

Jacques Favreau @betaorbust

Questions?

Come ask us!