PRODUCTION READY JS 
WITH GRUNT 
Shai Reznik / HiRez.io
WHO AM I?
Shai Reznik 
Angular.js Consultant, HiRez.io 
Javascript IL, GDG, NG-CONF IL, Lecturer 
Entrepreneur, Talking Lean 
Improvisation Performer
BEFORE WE START 
Thanks 
Meetup Game 
Stop Me If Something Isn’t Clear
TODAY’S MENU 
Why Building JS? 
Build Theory Overview 
Grunt API 
Questions… SEXY TIME!
WHY BUILDING JS?
WE DEVELOP STUFF
WE WANNA SHARE IT 
WITH OUR CLIENTS
SO WE COPY IT TO 
THE SERVER AS IS...
‘CAUSE IT’S JS... WE DON’T NEED 
TO TOUCH ANYTHING.. RIGHT?
WRONG!
ISSUES WITH 
CLIENT SIDE 
JAVASCRIPT APPS
NOWADAYS WE DO COMPILE 
ECMASCRIPT 6 
COFFEESCRIPT 
TYPESCRIPT 
SASS 
#1
#2 
HTTP REQUEST OVERHEAD 
★ Minimum of 20-100ms Per Request 
★ No Matter The File Size 
★ Try Loading Lots Of Scripts...
#3 
THE BIGGER THE FILE... 
THE LONGER THE 
DOWNLOAD!
CAPTAIN OBVIOUS!
#4 
DON’T DOWNLOAD 
THE SAME FILE TWICE... 
USE THE 
BROWSER’S CACHE!
WHY SHOULD WE CARE? 
PERFORMANCE MATTERS
WE DON’T WANT TO 
LOSE OUR CLIENTS
SO WE NEED TO 
MASSAGE OUR FILES
MASSAGING OUR FILES 
1. COMPILE - NEW to OLD Syntax 
2. CONCAT - Create Fewer Files 
3. MINIFY - Create Smaller Files 
4. REVISION - Optimize For Caching
JS APP - 5 PARTS 
INDEX.HTML 
JS 
ASSETS 
CSS 
TEMPLATES
COMPILE CONCAT MINIFY REVISION 
ASSETS ✓ ✓ 
TEMPLATES ? ? ✓ ? 
CSS ? ✓ ✓ ✓ 
JS ? ✓ ✓ ✓ 
INDEX.HTML ? ✓
LETS ZOOM OUT
BUILD PROCESS 
1. TEST - Stop If Something Breaks 
2. PREPARE - Clean, Generate, Config 
3. MASSAGE - You Know... 
4. REWIRE - Replace Index.html Pointers
dist/ 
assets/images/bg.8d0c.jpg 
assets/** 
scripts/scripts.3fe3.js 
scripts/vendor.d90d.js 
styles/styles.d7j4.css 
styles/vendor.c8a9.css 
templates/main.tpl.html 
index.html 
FINAL 
PACKAGE:
FURTHER ZOOM OUT
DEPLOYMENT 
PROCESS 
VERSION CONTROL (GIT PUSH) 
→ CI BUILD SERVER (JENKINS) ← 
DEPLOY SCRIPT
ZOOM BACK IN
FINAL PACKAGE 
dist/ 
assets/images/bg.8d0c.jpg 
assets/** 
scripts/scripts.3fe3.js 
scripts/vendor.d90d.js 
styles/styles.d7j4.css 
styles/vendor.c8a9.css 
templates/main.tpl.html 
index.html
WE USED TO DO IT MANUALLY
NOW WE DON’T NEED TO
WE HAVE AUTOMATIC TOOLS 
GRUNT
GRUNT 
★ Built On Top Of Node.js 
★ Goal: Mostly To Transform Files 
★ Configuration Based
GOOD NEWS!
IF YOU KNOW 
JAVASCRIPT 
YOU ALREADY KNOW HOW TO WRITE 
GRUNT
GRUNT SETUP 
npm install -g grunt-cli 
Gruntfile.js - Configuration File 
Package.json - Core + Plugins
GRUNT API - SIMPLIFIED 
Load Tasks 
Configure Tasks 
Run Tasks
LOAD TASKS 
grunt.loadNpmTasks(‘copy’) 
OR 
require(‘load-grunt-tasks’)(grunt)
CONFIGURE TASKS 
grunt.initConfig({ 
← Task 
copy: { 
options: {}, 
target1: {} 
} 
}) 
← Target
RUN TASKS 
grunt copy:target1
YOU CAN ALSO...
ALIAS TASKS 
grunt.registerTask(‘copyTarget1’, 
[‘copy:target1’, ...] 
); 
grunt copyTarget1
GRUNT API EXAMPLE
A WORD ABOUT GULP 
STREAMS 
CODE OVER 
CONFIGURATION 
SRP IN USE 
GROWING 
COMMUNITY
SO… WHERE DO I START? 
YEOMAN
YEOMAN’S GRUNT FILE
npm install -g yo 
npm install -g generator-angular 
yo angular
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
FOR A MORE DETAILED 
ONLINE WORKSHOP... 
HiRez.io
WE’VE LEARNED... 
✓ Why Building JS 
✓ Build Theory Overview 
✓ Grunt API 
✓ A Little Bit Of Yeoman 
✓ Oh… And One More Thing...
IF YOU’RE GONNA BE LAZY...
DO IT WITH STYLE
THANK YOU! 
HiRez.io 
@HiRez_io 
shai@hirez.io 
slides: http://goo.gl/j1pYOt

Production Ready Javascript With Grunt