62
DEVELOPER, TRAINER, AUTHOR @cmckeachie www.funnyant.com Babel: Get Started Craig D. McKeachie BASICS OF USING BABEL

Craig McKeachie - Babel Get Startedby

Embed Size (px)

Citation preview

Page 1: Craig McKeachie - Babel Get Startedby

DEVELOPER, TRAINER, AUTHOR

@cmckeachie www.funnyant.com

Babel: Get Started

Craig D. McKeachie

BASICS OF USING BABEL

Page 2: Craig McKeachie - Babel Get Startedby

Overview Understanding Babel

Installation and configuration

Module formatters

Page 3: Craig McKeachie - Babel Get Startedby

Understanding Babel

Page 4: Craig McKeachie - Babel Get Startedby

Comparisons

Page 5: Craig McKeachie - Babel Get Startedby

Comparison

Traceur Babel

Experimental

Runtime dependency

Removes comments and whitespace

Missing some features

Production

Minimal inline helpers/optional polyfills

Readable maintains source formatting

Feature rich

Page 6: Craig McKeachie - Babel Get Startedby

Comparison

TypeScript Babel

Language: Typed superset of JavaScript

Happens to have transpiler

More lenient, less spec compliant

Not a language

Transpiler

Spec compliant

Page 7: Craig McKeachie - Babel Get Startedby

Prerequisites & Environment

Page 8: Craig McKeachie - Babel Get Startedby

DEVELOPER, TRAINER, AUTHOR

@cmckeachie www.funnyant.com

Basics of Using Babel

Craig D. McKeachie

Page 9: Craig McKeachie - Babel Get Startedby

Installation

Page 10: Craig McKeachie - Babel Get Startedby

Babel Packages

babel-core babel-cli

babel-plugin-* babel-preset-*

Page 11: Craig McKeachie - Babel Get Startedby

Installing Babel Globally

$ npm install -global babel #version 5 $ npm install -global babel-cli

$ babel --version

Page 12: Craig McKeachie - Babel Get Startedby

Global Packages Location

$ npm ls -g --depth 0

Mac: /Users/[username]/.npm-packages/lib/node_modules

PC: %AppData%\npm\node_modules (Windows 7) %USERPROFILE%\Application Data\npm\node_modules (Windows XP)

Page 13: Craig McKeachie - Babel Get Startedby

Installing Babel Locally

$ npm init #creates package.json

$ npm install babel-cli --save-dev #saves in package.json

$ babel --version #fails because can't find package

$ node_modules/.bin/babel --version

Page 14: Craig McKeachie - Babel Get Startedby

Command Line Interface (CLI)

Page 15: Craig McKeachie - Babel Get Startedby

Configuration

Page 16: Craig McKeachie - Babel Get Startedby

Understanding Module Formatters

Page 17: Craig McKeachie - Babel Get Startedby

ModuleA module is a piece of reusable JavaScript: it exports certain objects which are thus made available to dependent code.

Page 18: Craig McKeachie - Babel Get Startedby

ECMAScript Module

// lib/math.js export function sum(x, y) { return x + y; } export var pi = 3.141593;

// app.js import {sum, pi} from "lib/math"; alert("2π = " + sum(pi, pi));

Page 19: Craig McKeachie - Babel Get Startedby

Module Specifications

AMD CommonJS

UMD SystemJS

Page 20: Craig McKeachie - Babel Get Startedby

Module FormatterA module formatter is a transformer that turns ES2015 module syntax (exports and imports) into their equivalent target module specification format.

Page 21: Craig McKeachie - Babel Get Startedby

ECMAScript Modules

AMD

CommonJS

UMD

SystemJS

Page 22: Craig McKeachie - Babel Get Startedby

Specification Implementations

AMD CommonJS

RequireJS

Dojo Toolkit

ScriptManJS

Node.js (on the server)

Browserify (in the browser)

Page 23: Craig McKeachie - Babel Get Startedby

Module Formatters Demo

Page 24: Craig McKeachie - Babel Get Startedby

Summary

Understanding Babel

- Compile to ES5

- Traceur and TypeScript

Installation and configuration

- npm

- .babelrc

Module formatters

- AMD, CommonJS

Page 25: Craig McKeachie - Babel Get Startedby

DEVELOPER, TRAINER, AUTHOR

@cmckeachie www.funnyant.com

Browser Support

Craig D. McKeachie

Page 26: Craig McKeachie - Babel Get Startedby

OverviewShims and polyfills

Project overview

Chrome support

IE9 support

IE8 support

Page 27: Craig McKeachie - Babel Get Startedby

ShimA shim refers to any piece of code that performs interception of an API call and provides a layer of abstraction.

Page 28: Craig McKeachie - Babel Get Startedby

PolyfillA polyfill is a type of shim that retrofits legacy browsers with modern HTML/CSS/JavaScript features (browser APIs) usually using Javascript.

Page 29: Craig McKeachie - Babel Get Startedby

if (!Function.prototype.bind) { Function.prototype.bind = ... ; }

Polyfill

Page 30: Craig McKeachie - Babel Get Startedby

$ npm install babel-polyfill

Babel Polyfill

Page 31: Craig McKeachie - Babel Get Startedby

Babel Polyfill

Page 32: Craig McKeachie - Babel Get Startedby

core-js

Scope

- ES5, ES6, ES7

- not just safe parts of spec

Modular

No global namespace pollution

Page 33: Craig McKeachie - Babel Get Startedby

ES Shims

Scope

- just safe parts of spec

- just ES5 or just ES6

Not as modular

Pollute global namespace

Page 34: Craig McKeachie - Babel Get Startedby

Regeneratortakes proposed syntax

for generators/yield

and transforms to ES5

Page 35: Craig McKeachie - Babel Get Startedby

ECMAScript Versions

ES3 ES5

IE8+

CH5+

FF3+

SF4+

IE9+

CH6-19+

FF4+

SF5-6+

Page 36: Craig McKeachie - Babel Get Startedby

ECMAScript Versions

ES3 ES5

Babel polyfill

Plugin: es3-member-expression-literals

Plugin: s3-member-property-literals

Other Plugins:

object-set-prototype-of-to-assign

Babel polyfill

Page 37: Craig McKeachie - Babel Get Startedby

Demo All ES2015 features in the

latest version of Chrome to IE8

- before polyfill and transpiling

- with polyfill

- transpiled

Page 38: Craig McKeachie - Babel Get Startedby

Summary Babel polyfill

Project overview

Chrome, IE9, IE8 support

Page 39: Craig McKeachie - Babel Get Startedby

DEVELOPER, TRAINER, AUTHOR

@cmckeachie www.funnyant.com

Babel in Your Build

Craig D. McKeachie

Page 40: Craig McKeachie - Babel Get Startedby

Overview

Build Automation

• Npm scripts

• Gulp

• Grunt

Module Loaders

• Webpack

• Browserify

Frameworks

• React.js

Page 41: Craig McKeachie - Babel Get Startedby

Babel with npm Scripts

Page 42: Craig McKeachie - Babel Get Startedby

npm Scripts

Included in package.json

Like batch file or bash script

Run local node packages w/out path

Easy to learn

Page 43: Craig McKeachie - Babel Get Startedby

//package.json

"scripts": { "test": "echo \"Error: no test specified\" && exit 1" }

$ npm run-script test

npm Scripts

Page 44: Craig McKeachie - Babel Get Startedby

$ npm run-script [script-name] $ npm run [script-name]

npm ScriptsShortcuts

Page 45: Craig McKeachie - Babel Get Startedby

$ npm run-script test $ npm run test $ npm test

npm ScriptsShortcuts

Works for test, start, stop, restart

Page 46: Craig McKeachie - Babel Get Startedby

Babel with Gulp

Page 47: Craig McKeachie - Babel Get Startedby

Babel with Grunt

Page 48: Craig McKeachie - Babel Get Startedby

Babel with Webpack

Page 49: Craig McKeachie - Babel Get Startedby

Babel with Browserify

Page 50: Craig McKeachie - Babel Get Startedby

Babel with React

Page 51: Craig McKeachie - Babel Get Startedby

Summary

Build Automation Module Loaders Frameworks

Page 52: Craig McKeachie - Babel Get Startedby

DEVELOPER, TRAINER, AUTHOR

@cmckeachie www.funnyant.com

Babel with Node.js and Unit Tests

Craig D. McKeachie

Page 53: Craig McKeachie - Babel Get Startedby

Overview

Node.js

• Babel-node

• Require hook

• Command-line interface (CLI)

• Babel runtime

Unit Testing

• Jasmine

• Mocha

Page 54: Craig McKeachie - Babel Get Startedby

Summary

Node.js Unit Tests

Babel-node

Require hook

Command-line interface

Babel runtime

Jasmine

Mocha

Page 55: Craig McKeachie - Babel Get Startedby

Babel-Node

Page 56: Craig McKeachie - Babel Get Startedby

Require Hook

Page 57: Craig McKeachie - Babel Get Startedby

Command-line Interface (CLI)

Page 58: Craig McKeachie - Babel Get Startedby

Babel Runtime

Page 59: Craig McKeachie - Babel Get Startedby

Babel with Jasmine

Page 60: Craig McKeachie - Babel Get Startedby

Babel with Mocha

Page 61: Craig McKeachie - Babel Get Startedby

Babel RuntimeES2015 environment

No global namespace pollution

Page 62: Craig McKeachie - Babel Get Startedby

DEVELOPER, TRAINER, AUTHOR

@cmckeachie www.funnyant.com

Babel: Get Started

Craig D. McKeachie

CONCLUSION