54
PRODUCTION READY JS WITH GRUNT Shai Reznik / HiRez.io

Production Ready Javascript With Grunt

Embed Size (px)

DESCRIPTION

An overview of the steps required to build Javascript apps before and get them ready for deployment. It explains how Grunt helps us to validate, minimize and package our code.

Citation preview

Page 1: Production Ready Javascript With Grunt

PRODUCTION READY JSWITH GRUNT

Shai Reznik / HiRez.io

Page 2: Production Ready Javascript With Grunt

WHO AM I?

Page 3: Production Ready Javascript With Grunt

Angular.js Consultant, HiRez.io

Entrepreneur, Talking Lean

Improvisation Performer

Javascript IL, GDG, NG-CONF IL, Lecturer

Shai Reznik

Page 4: Production Ready Javascript With Grunt

BEFORE WE STARTThanks

Meetup Game

Stop Me If Something Isn’t Clear

Page 5: Production Ready Javascript With Grunt

TODAY’S MENUWhy Building JS?

Build Theory Overview

Grunt API

Questions… SEXY TIME!

Page 6: Production Ready Javascript With Grunt

WHY BUILDING JS?

Page 7: Production Ready Javascript With Grunt

WE DEVELOP STUFF

Page 8: Production Ready Javascript With Grunt

WE WANNA SHARE ITWITH OUR CLIENTS

Page 9: Production Ready Javascript With Grunt

SO WE COPY IT TO THE SERVER AS IS...

Page 10: Production Ready Javascript With Grunt

‘CAUSE IT’S JS... WE DON’T NEED TO TOUCH ANYTHING.. RIGHT?

Page 11: Production Ready Javascript With Grunt

WRONG!

Page 12: Production Ready Javascript With Grunt

ISSUES WITH

CLIENT SIDE JAVASCRIPT APPS

Page 13: Production Ready Javascript With Grunt

NOWADAYS WE DO COMPILE

ECMASCRIPT 6

COFFEESCRIPTTYPESCRIPT

SASS

#1

Page 14: Production Ready Javascript With Grunt

★ Minimum of 20-100ms Per Request

★ No Matter The File Size

★ Try Loading Lots Of Scripts...

#2 HTTP REQUEST OVERHEAD

Page 15: Production Ready Javascript With Grunt

#3THE BIGGER THE FILE...

THE LONGER THE DOWNLOAD!

Page 16: Production Ready Javascript With Grunt

CAPTAIN OBVIOUS!

Page 17: Production Ready Javascript With Grunt

#4

DON’T DOWNLOAD THE SAME FILE TWICE...

USE THE BROWSER’S CACHE!

Page 18: Production Ready Javascript With Grunt

WHY SHOULD WE CARE?

PERFORMANCE MATTERS

Page 19: Production Ready Javascript With Grunt

WE DON’T WANT TO LOSE OUR CLIENTS

Page 20: Production Ready Javascript With Grunt

SO WE NEED TO MASSAGE OUR FILES

Page 21: Production Ready Javascript With Grunt

1. COMPILE - NEW to OLD Syntax

2. CONCAT - Create Fewer Files

3. MINIFY - Create Smaller Files

4. REVISION - Optimize For Caching

MASSAGING OUR FILES

Page 22: Production Ready Javascript With Grunt

JS APP - 5 PARTS

INDEX.HTML

JS

ASSETS

CSS

TEMPLATES

Page 23: Production Ready Javascript With Grunt

COMPILE CONCAT MINIFY REVISION

ASSETS ✓ ✓

TEMPLATES ? ? ✓ ?

CSS ? ✓ ✓ ✓

JS ? ✓ ✓ ✓

INDEX.HTML ? ✓

Page 24: Production Ready Javascript With Grunt

LETS ZOOM OUT

Page 25: Production Ready Javascript With Grunt

BUILD PROCESS

1. TEST - Stop If Something Breaks

2. PREPARE - Clean, Generate, Config

3. MASSAGE - You Know...

4. REWIRE - Replace Index.html Pointers

Page 26: Production Ready Javascript With Grunt

dist/

assets/images/bg.8d0c.jpgassets/**

scripts/scripts.3fe3.jsscripts/vendor.d90d.js

styles/styles.d7j4.cssstyles/vendor.c8a9.css

templates/main.tpl.html

index.html

FINAL PACKAGE:

Page 27: Production Ready Javascript With Grunt

FURTHER ZOOM OUT

Page 28: Production Ready Javascript With Grunt

DEPLOYMENT PROCESS

VERSION CONTROL (GIT PUSH)

→ CI BUILD SERVER (JENKINS) ←

DEPLOY SCRIPT

Page 29: Production Ready Javascript With Grunt

ZOOM BACK IN

Page 30: Production Ready Javascript With Grunt

dist/

assets/images/bg.8d0c.jpgassets/**

scripts/scripts.3fe3.jsscripts/vendor.d90d.js

styles/styles.d7j4.cssstyles/vendor.c8a9.css

templates/main.tpl.html

index.html

FINAL PACKAGE

Page 31: Production Ready Javascript With Grunt

WE USED TO DO IT MANUALLY

Page 32: Production Ready Javascript With Grunt

NOW WE DON’T NEED TO

Page 33: Production Ready Javascript With Grunt

WE HAVE AUTOMATIC TOOLS

GRUNT

Page 34: Production Ready Javascript With Grunt

★ Built On Top Of Node.js

★ Goal: Mostly To Transform Files

★ Configuration Based

GRUNT

Page 35: Production Ready Javascript With Grunt

GOOD NEWS!

Page 36: Production Ready Javascript With Grunt

IF YOU KNOW

JAVASCRIPTYOU ALREADY KNOW HOW TO WRITE

GRUNT

Page 37: Production Ready Javascript With Grunt

GRUNT SETUP

Gruntfile.js - Configuration File

Package.json - Core + Plugins

npm install -g grunt-cli

Page 38: Production Ready Javascript With Grunt

Load Tasks

Configure Tasks

Run Tasks

GRUNT API - SIMPLIFIED

Page 39: Production Ready Javascript With Grunt

LOAD TASKS

grunt.loadNpmTasks(‘copy’)

require(‘load-grunt-tasks’)(grunt)

OR

Page 40: Production Ready Javascript With Grunt

CONFIGURE TASKSgrunt.initConfig({

copy: { options: {},target1: {}

}})

← Task

← Target

Page 41: Production Ready Javascript With Grunt

RUN TASKS

grunt copy:target1

Page 42: Production Ready Javascript With Grunt

YOU CAN ALSO...

Page 43: Production Ready Javascript With Grunt

ALIAS TASKSgrunt.registerTask(‘copyTarget1’,

[‘copy:target1’, ...]

);

grunt copyTarget1

Page 44: Production Ready Javascript With Grunt

GRUNT API EXAMPLE

Page 45: Production Ready Javascript With Grunt

STREAMS

A WORD ABOUT GULP

CODE OVER CONFIGURATION

GROWINGCOMMUNITY

SRP IN USE

Page 46: Production Ready Javascript With Grunt

SO… WHERE DO I START?

YEOMAN

Page 47: Production Ready Javascript With Grunt

YEOMAN’S GRUNT FILE

Page 48: Production Ready Javascript With Grunt

npm install -g yo

npm install -g generator-angular

yo angular

Page 49: Production Ready Javascript With Grunt

THAT’S A GOOD START BUT...

➔ Yeoman’s Gruntfile.js Needs Modifications

➔ Can Be Scary To Look At

➔ Best Practices Takes Time To Figure Out

➔ Make Changes On Your Own

➔ Bigger Than This Lecture’s Scope

Page 50: Production Ready Javascript With Grunt

HiRez.io

FOR A MORE DETAILED ONLINE WORKSHOP...

Page 51: Production Ready Javascript With Grunt

✓ Why Building JS

✓ Build Theory Overview

✓ Grunt API

✓ A Little Bit Of Yeoman

✓ Oh… And One More Thing...

WE’VE LEARNED...

Page 52: Production Ready Javascript With Grunt

IF YOU’RE GONNA BE LAZY...

Page 53: Production Ready Javascript With Grunt

DO IT WITH STYLE

Page 54: Production Ready Javascript With Grunt

HiRez.io@[email protected]: http://goo.gl/j1pYOt

THANK YOU!