let grunt do the work, focus on the fun! [open web camp 2013]

84
Let Grunt do the work, focus on the fun! Dirk Ginader, Google, 2013 @ginader http://dir.kg/slides

Upload: dirk-ginader

Post on 27-Jan-2015

108 views

Category:

Technology


0 download

DESCRIPTION

Google’s Dirk Ginader thinks great developers are lazy, and there’s nothing wrong with that. After all, would you rather spend your time working on the mundane stuff — like minification, linting, compilation, unit testing, etc — or actually developing your code? In this presentation, Dirk will show you how to set up the Grunt JavaScript Task Runner so that you and your team can focus on the fun!

TRANSCRIPT

Page 1: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Let Grunt do the work, focus on the fun!

Dirk Ginader, Google, 2013@ginader http://dir.kg/slides

Page 2: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Let Grunt do the endlessly repetitive tedious tasks, focus

on the important stuff like Accessibility!

Dirk Ginader, Google, 2013

Page 3: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Let Grunt do the work, focus on the fun!

Dirk Ginader, Google, 2013

Page 4: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Why Build scripts?

Page 5: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Because great Developers

are lazy.

Page 6: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Because great Developers

are lazy. FACT.

Page 7: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

timespent

task sizenon-geekgeek does it manually

makes fun of geek’scomplicated method

loses

does it manually

gets annoyed

writes scriptto automate

runs script

wins

Page 8: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Build systems have been around for ages• Make

• Maven

• and so many more ...

• Ant

• Rake

Page 9: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

They’re all great and powerful and all...

Page 10: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Minify with Ant<target name="js-compile-all" description="Compile JavaScript files with Closure" unless="skip-js-compile"> <echo>Compiling JS files in ${input.scripts.dir} in closure...</echo> <apply executable="java" dest="${output.scripts.dir}"> <arg value="-jar"/> <arg path="${jar.lib.dir}/closure-compiler.jar"/> <arg line="--js"/> <srcfile/> <arg line="--js_output_file"/> <targetfile/> <fileset dir="${output.scripts.dir}" includes="**/*-main.debug.js" /> <mapper type="glob" from="*-main.debug.js" to="*-main.min.js"/> </apply> <echo>Finished compiling JS files</echo></target>

http://mechanics.flite.com/blog/2012/06/19/why-we-use-node-dot-js-and-grunt-to-build-javascript/

Page 11: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 12: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

How much I liked to configure with XML?

Page 13: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 14: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

I’m a Front End Developer!

Page 15: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

I like Javascript

Page 16: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

I like LOVE Javascript

Page 17: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 18: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Just one year ago Ben Alman did me a

great favor:

Page 19: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

GRUNTThe JavaScript Task Runner

Page 20: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

written in Javascript

Page 21: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

using the node package manager

Page 22: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 23: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

FAST adoption rate• jQuery

• Modernizr

• Adobe

• twitter

• ...

Page 24: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

because it’s easy!

Page 25: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

System Setup:

Page 26: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

download and install node.js from:

http://nodejs.org/

Page 27: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 28: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 29: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

$ npm install -g grunt-cli

Page 30: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 31: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Project Setup:

Page 32: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

2 important Files:package.jsonGruntfile.js

Page 33: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

package.json

Page 34: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

{ "name": "jQuery-Accessible-Tabs", "version": "1.9.7", "homepage": "http://github.com/ginader/Accessible-Tabs", "author": { "name": "Dirk Ginader", "url": "http://ginader.com" }, "devDependencies": {

}}

https://npmjs.org/doc/json.html

Page 35: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Gives you:

• Variables you can use in your scripti.e. version and name

• Dev Dependencies that allows you to quickly install all required npm modules

Page 36: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

{ "name": "jQuery-Accessible-Tabs", "version": "1.9.7", "homepage": "http://github.com/ginader/Accessible-Tabs", "author": { "name": "Dirk Ginader", "url": "http://ginader.com" }, "devDependencies": {

}}

https://npmjs.org/doc/json.html

Page 37: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

$ npm install grunt --save-dev

Page 38: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 39: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

{ "name": "jQuery-Accessible-Tabs", "version": "1.9.7", "homepage": "http://github.com/ginader/Accessible-Tabs", "author": { "name": "Dirk Ginader", "url": "http://ginader.com" }, "devDependencies": { "grunt": "~0.4.0" }}

https://npmjs.org/doc/json.html

Page 40: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

$ npm install

install all the defined Dependencies in one go

Page 41: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Gruntfile.js

Page 42: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Minify with Grunt

http://mechanics.flite.com/blog/2012/06/19/why-we-use-node-dot-js-and-grunt-to-build-javascript/

module.exports = function(grunt) { grunt.initConfig({ uglify: { dist: { src: 'dist/myfile.js', dest: 'dist/myfile.min.js' }, } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']);};

Page 43: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Minify with Ant<target name="js-compile-all" description="Compile JavaScript files with Closure" unless="skip-js-compile"> <echo>Compiling JS files in ${input.scripts.dir} in closure...</echo> <apply executable="java" dest="${output.scripts.dir}"> <arg value="-jar"/> <arg path="${jar.lib.dir}/closure-compiler.jar"/> <arg line="--js"/> <srcfile/> <arg line="--js_output_file"/> <targetfile/> <fileset dir="${output.scripts.dir}" includes="**/*-main.debug.js" /> <mapper type="glob" from="*-main.debug.js" to="*-main.min.js"/> </apply> <echo>Finished compiling JS files</echo></target>

http://mechanics.flite.com/blog/2012/06/19/why-we-use-node-dot-js-and-grunt-to-build-javascript/

Page 44: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 45: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Minify with Grunt

http://mechanics.flite.com/blog/2012/06/19/why-we-use-node-dot-js-and-grunt-to-build-javascript/

module.exports = function(grunt) { grunt.initConfig({ uglify: { dist: { src: 'dist/myfile.js', dest: 'dist/myfile.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']);};

Page 46: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Minify with Grunt

http://mechanics.flite.com/blog/2012/06/19/why-we-use-node-dot-js-and-grunt-to-build-javascript/

module.exports = function(grunt) { grunt.initConfig({ uglify: { dist: { src: 'dist/myfile.js', dest: 'dist/myfile.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']);};

Page 47: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Minify with Grunt

http://mechanics.flite.com/blog/2012/06/19/why-we-use-node-dot-js-and-grunt-to-build-javascript/

module.exports = function(grunt) { grunt.initConfig({ uglify: { dist: { src: 'dist/myfile.js', dest: 'dist/myfile.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.registerTask('default', ['uglify']);};

Page 48: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

let’s see what it does!$ grunt

Page 49: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 50: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 51: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

easy to add more$ npm i grunt-contrib-jshint --save-dev

Page 52: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 53: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

add JS Lintingmodule.exports = function(grunt) { grunt.initConfig({ jshint: { all: ['*.js'] }, uglify: { dist: { src: 'myfile.js', dest: 'myfile.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', ['jshint','uglify']);};

https://npmjs.org/package/grunt-contrib-jshint

Page 54: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 55: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 56: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

tired of typing already?$ grunt watch

Page 57: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

watch: { gruntfile: { files: [ 'Gruntfile.js', 'js/myfile.js'], tasks: ['jshint'] }}

https://npmjs.org/package/grunt-contrib-watch

Page 58: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 59: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 60: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 61: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

add data from package.jsonmodule.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), jshint: { all: ['*.js'] }, uglify: { options: { banner: '/*! <%= pkg.name %>' + ' <%= grunt.template.today("yyyy-mm-dd") %> */\n' }, dist: { src: 'myfile.js', dest: 'myfile.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', ['jshint','uglify']);};

Page 62: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

{ "name": "jQuery-Accessible-Tabs", "version": "1.9.7", "homepage": "http://github.com/ginader/Accessible-Tabs", "author": { "name": "Dirk Ginader", "url": "http://ginader.com" }}

https://npmjs.org/doc/json.html

Page 63: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

add data from package.jsonmodule.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), jshint: { all: ['*.js'] }, uglify: { options: { banner: '/*! <%= pkg.name %>' + ' <%= grunt.template.today("yyyy-mm-dd") %> */\n' }, dist: { src: '<%= pkg.name %>.js', dest: '<%= pkg.name %>.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', ['jshint','uglify']);};

Page 64: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

add data from package.jsonmodule.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), jshint: { all: ['*.js'] }, uglify: { options: { banner: '/*! <%= pkg.name %>' + ' <%= grunt.template.today("yyyy-mm-dd") %> */\n' }, dist: { src: '<%= pkg.name %>.js', dest: '<%= pkg.name %>.<%= pkg.version %>.min.js' } } }); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.registerTask('default', ['jshint','uglify']);};

Page 65: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

minify and combine CSS cssmin: { compress: { options: { banner: '<%= banner %>' }, files: { 'project.min.css': ['1.css','2.css', '...'] } } } grunt.loadNpmTasks('grunt-contrib-cssmin'); grunt.registerTask('default', ['jshint','uglify', 'cssmin']);

https://npmjs.org/package/grunt-contrib-cssmin

Page 66: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

optimize Imagesimagemin: { dist: { options: { optimizationLevel: 3 }, files: { // 'destination': 'source' 'dist/img.png': 'src/img.png', 'dist/img.jpg': 'src/img.jpg' } }}

grunt.registerTask('default', ['imagemin']);

https://npmjs.org/package/grunt-contrib-imagemin

Page 67: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

but it’s more than just optimizations

Page 68: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Build HTML Pages

markdown: { all: { files: ['readme.markdown','version-history.markdown'], template: 'web/template.html', dest: 'web', options: { gfm: true, codeLines: { before: '<span>', after: '</span>' } } } }https://npmjs.org/package/

grunt-markdown

Page 69: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

remove debug code

removelogging: { dist: { src: 'js/jquery.tabs.min.js', dest: 'js/jquery.tabs.min.js' } }

https://npmjs.org/package/grunt-remove-logging

Page 70: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

compile Sass/Compass // setup Compass/Sass to load from existing config.rb compass: { dist: { options: { config: 'config.rb' } } }

https://npmjs.org/package/grunt-contrib-compass

Page 71: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

...but Kitt will tell you all about that in the next Session :-)

Page 73: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

all done? deploy!

Page 74: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

FTP upload dance?

Page 76: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 77: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Scaffolding

Page 78: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

$ npm install -g grunt-init

Page 79: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

many templates for grunt-init

• Gruntfile

• Grunt plugin

• jQuery plugin

• node.js

• ...

Page 80: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

$ git clone git://github.com/gruntjs/grunt-init-jquery.git ~/.grunt-init/jquery

Page 81: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

$ grunt-init jquery

Page 82: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]
Page 83: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

The opinions I expressed here represent my own and not necessarily those of my employer.

btw: We’re hiring! Talk to me :-)

Thank you! Questions?

Page 84: Let Grunt do the work, focus on the fun! [Open Web Camp 2013]

Resources• Grunt: http://gruntjs.com/

• Great article: http://dir.kg/grunt.workflow

• Extending Grunt big time: http://yeoman.io

• Me: http://dir.kg/me

• @ginader on twitter

• the example projects: http://github.com/ginader/

• http://ginader.com

• http://dir.kg/slides