automating front-end workflow

33
Automate your front-end workflow with grunt.js

Upload: dimitris-tsironis

Post on 27-Jan-2015

111 views

Category:

Technology


0 download

DESCRIPTION

Automating and optimize your everyday workflow with Grunt.js, bower, be more productive, happier with your work.

TRANSCRIPT

Page 1: Automating Front-End Workflow

Automate your front-end workflow

with grunt.js

Page 2: Automating Front-End Workflow

About this

This talk is about

workflow patterns

using Grunt

reinvent your work, love JS more

make your life easier

Page 3: Automating Front-End Workflow

Who

Dimitris Tsironis

Front-end Engineer at Splunk (ex-Bugsense) !passionate about bringing BigData to the masses, eating bacon & geeing around

Page 4: Automating Front-End Workflow

First, a story

from good ol’ days

Page 5: Automating Front-End Workflow

<a href="javascript:void(0)" onclick=“myJsFunc();”> Run JavaScript Code!!!!11! </a>

First

Page 6: Automating Front-End Workflow

<a href="javascript:void(0)" onclick=“myJsFunc();”> Run JavaScript Code!!!!11! </a>

First

Page 7: Automating Front-End Workflow

Now

Page 8: Automating Front-End Workflow

Now

Page 9: Automating Front-End Workflow
Page 10: Automating Front-End Workflow
Page 11: Automating Front-End Workflow
Page 12: Automating Front-End Workflow

*cough*

*cough*

Page 13: Automating Front-End Workflow

Problem

typical js file, named stuff.js

Page 14: Automating Front-End Workflow

grunt

is a task-based command line build tool for JavaScript projects

Page 15: Automating Front-End Workflow

Grunt.js is used for

Concatenation

Testing with headless browsers

JS linting

basically, whatever

Page 16: Automating Front-End Workflow

Installation

$ npm install -g grunt-cli

Install the grunt cli tool

Page 17: Automating Front-End Workflow

Installation

Edit your package.json file

{ "name": "my-project-name", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.2", "grunt-contrib-jshint": "~0.6.3" } }

grunt.loadNpmTasks(‘grunt-contrib-uglify');

Then in your Gruntfile.js add

Page 18: Automating Front-End Workflow

Gruntfile.js

module.exports = function(grunt) { ! // Project configuration. grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { options: { banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n' }, build: { src: 'src/<%= pkg.name %>.js', dest: 'build/<%= pkg.name %>.min.js' } } }); ! // Load the plugin that provides the "uglify" task. grunt.loadNpmTasks('grunt-contrib-uglify'); ! // Default task(s). grunt.registerTask('default', ['uglify']); !};

Page 19: Automating Front-End Workflow

Concatenating

module: grunt-contrib-concat

concat: { ‘assets/js/main.js’: [ ‘js/libs/jquery.js’, ‘js/libs/underscore.js’ ‘js/libs/backbone.js, ‘js/src/app.js’], ‘assets/css/style.css’: [ ‘css/bootstrap.css’, ‘css/main.css’] }

Page 20: Automating Front-End Workflow

Compiling LESS

module: grunt-contrib-less

less: { dashboard: { files: { "css/main.css": "less/style.less", "css/landing.css": "less/landing.less", } } }

Page 21: Automating Front-End Workflow

Running specs #1

module: grunt-contrib-jasmine

jasmine: { src: [ 'specs/project.js'], options: { host: 'http://localhost:7000/', vendor: [ ‘libs/jquery.js’], helpers: [ 'specs/jasmine-jquery/lib/jasmine-jquery.js', ‘specs/bower_components/sinonjs/sinon.js' ], template: 'specs/index.tmpl', specs: 'specs/build/specs.js',

keepRunner: true } },

Page 22: Automating Front-End Workflow

Running specs #2

module: grunt-contrib-connect

connect: { server: { options: { port: 7000 } } }

grunt.registerTask('specs', ['connect', 'jasmine']);

Registering our custom “specs” tasks

Page 23: Automating Front-End Workflow

Watching files

module: grunt-contrib-watch

watch: { gruntfile: { files: '<%= jshint.gruntfile.src %>', tasks: ['jshint:gruntfile', 'concat', 'less'] }, less: { files: “less/**/*.less”, /* or use an array */ tasks: [‘less’, ‘concat’] }, specs: { files: “specs/**/*Specs.js” tasks: [‘specs’] } }

Page 24: Automating Front-End Workflow

Live reload

module: grunt-reload

reload: { port: 6001, proxy: { host: 'localhost' } },

watch: { less: { files: “less/**/*.less”, /* or use an array */ tasks: [‘less’, ‘concat’, ‘reload’] } }

Page 25: Automating Front-End Workflow

bower

a package manager for the web

Page 26: Automating Front-End Workflow

Installation

$ npm install -g bower

Install the bower tool

Page 27: Automating Front-End Workflow

Usage

$ bower install <package>

Install the bower tool

$ bower install <package>#<version>

or just install dependencies from bower.json

$ bower install

Page 28: Automating Front-End Workflow

Search

$ bower search <search-term>

Search the packages

Page 29: Automating Front-End Workflow

/etc/

$ bower help

Help is always provided

Page 30: Automating Front-End Workflow

Defining a new package

$ bower init

{ "name": "my-project", "version": "1.0.0", "main": "path/to/main.css", "ignore": [ ".jshintrc", "**/*.txt" ], "dependencies": { "<name>": "<version>" }, "devDependencies": { "<test-framework-name>": "<version>" } }

Page 31: Automating Front-End Workflow

Registering a package

$ bower register <package-name> <git-endpoint>

a valid manifest JSON

Git tags (using semver)

be available at a Git endpoint (e.g., GitHub); remember to push your Git tags!

Page 32: Automating Front-End Workflow

Bugsense.js plugin (see Gruntfile.js & bower.json)

Addy Osmani presentation

Grunt plugins

Bower homepage

(incl. Yeomann)

Grunt homepage

Page 33: Automating Front-End Workflow

Thanks!

twitter @tsironakos github @tsironis