Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system...

52
treehouse Get Started With Grunt

Transcript of Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system...

Page 1: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

Get Started With Grunt

Page 2: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

What is Grunt?

‟The JavaScript Task Runner”

Compiles Sass into CSS

Compress JavaScript, CSS, HTML files

Combine JS files into a single file

Page 3: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

task

An automated command. You can create your own or choose from around 4,000 Grunt plugins

concatenate, minify, compile, etc.

Page 4: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

Demo

Page 5: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

Software dependencies

Node: a JavaScript runtime

npm: Node Package Manager

Grunt-CLI: command-line interface to run Grunt

Grunt: the actual task runner

Grunt plugins: thousands to choose from

Page 6: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

High Level Overview

Install node.js

Install grunt-cli

Create package.json file

Install grunt

Install plugins

Create gruntfile.js file

Page 7: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

http://nodejs.org

Page 8: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

http://blog.teamtreehouse.com/install-node-js-npm-mac

Page 9: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™http://teamtreehouse.com/library/nodejs-basics

Page 10: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

About Grunt Node Packages

There are two Grunt node packages:

1. grunt-cli is installed one time for the entire system (-g)

npm install -g grunt-cli

2. grunt is installed for each project in the project directory

npm install grunt

Page 11: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

npm install -g grunt-cli

Install Grunt-cli

doesn’t matter what directory you run this command from, it’s a global installation

Page 12: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

The Project

Page 13: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

Two Files You Need Understand

1. package.json describes your project: name, git repository, but most importantly your npm dependencies

2. gruntfile.js is the file that loads plugins, configures those plugins and defines tasks

these files live in the project’s main directory

Page 14: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

cd ~/Documents/project_directory

npm init

Creating the package.json file

then follow the instructions...

Page 15: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

npm install grunt --save-dev

Install Grunt

--save-dev adds this to your list of packages in the package.json file

Page 16: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

Create gruntfile.js

Create in project’s main directory

What does it do?

Configures tasks

Loads plugins

Registers tasks

Page 17: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

The gruntfile.js skeleton

module.exports = function(grunt) {

// Configure task(s) grunt.initConfig({

});

// Load the plugins grunt.loadNpmTasks( );

// Register task(s). grunt.registerTask('default', [ ]);

};

Page 18: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

The gruntfile.js skeleton

module.exports = function(grunt) {

// Configure task(s) grunt.initConfig({

});

// Load the plugins grunt.loadNpmTasks( );

// Register task(s). grunt.registerTask('default', [ ]);

};

this is a node thing...

Page 19: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

The gruntfile.js skeleton

module.exports = function(grunt) {

// Configure task(s) grunt.initConfig({

});

// Load the plugins grunt.loadNpmTasks( );

// Register task(s). grunt.registerTask('default', [ ]);

}; this is the stuff you change

Page 20: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

Load the package.json file

module.exports = function(grunt) {

// Configure task(s) grunt.initConfig({

pkg: grunt.file.readJSON('package.json'), });

// Load the plugins grunt.loadNpmTasks( );

// Register task(s). grunt.registerTask('default', [ ]);

};

Page 21: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

Steps to Adding a Task

Load the plugin

npm install <plugin-name> --save-dev

Add configuration to gruntfile.js

Load plugin in gruntfile.js

Register the task in gruntfile.js

Page 22: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

uglify your JavaScript

Shrink your file footprint and combine your JavaScript files with Uglify

Page 23: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

npm install grunt-contrib-uglify --save-dev

Install uglify

must run this command within the project directory

Page 24: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Configure task(s) grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { } });

Configure: gruntfile.js

Page 25: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Configure task(s) grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { build: { src: 'src/js/*.js', dest: 'js/script.min.js' }

} });

Configure: gruntfile.js

Page 26: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

About File Organization

src/js: directory holding my working JavaScript files

js: directory holding my final compressed and concatenated file

Page 27: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Configure task(s) grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { build: { src: 'src/js/*.js', dest: 'js/script.min.js' }

} });

Configure: gruntfile.js

output file

source javascript files

Page 28: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Load the plugins grunt.loadNpmTasks('grunt-contrib-uglify');

Load the plugin: gruntfile.js

Page 29: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Register task(s). grunt.registerTask('default', ['uglify:build']);

Register the task: gruntfile.js

Page 30: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

all together

// Configure task(s) grunt.initConfig({

pkg: grunt.file.readJSON('package.json'),

uglify: { src: 'src/js/*.js', dest: 'js/script.min.js' } });

// Load the plugins grunt.loadNpmTasks('grunt-contrib-uglify');

// Register task(s). grunt.registerTask('default', ['uglify']);

Page 31: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

grunt

Run it!

must run this command within the project directory

Page 32: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

dev tasks vs. build tasks

Page 33: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Configure task(s) grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), uglify: { build : { src: 'src/js/*.js', dest: 'js/script.min.js' }

} });

Create a build task: gruntfile.js

Page 34: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

Update the task name: gruntfile.js

// Register task(s). grunt.registerTask('build', ['uglify:build']);

Page 35: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

grunt build

Run it!

Page 36: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

dev tasks

Don’t minify code, but do concatenate files

Page 37: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

dev: { options: { beautify: true, mangle: false, compress: false, preserveComments: 'all' }, src: 'src/js/*.js', dest: 'js/script.min.js' }

Create a dev task: gruntfile.js

Page 38: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

Register the dev task: gruntfile.js

// Register task(s) grunt.registerTask('default', ['uglify:dev']); grunt.registerTask('build', ['uglify:build']);

Page 39: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

watching tasks

Page 40: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

npm install grunt-contrib-watch --save-dev

Install watch

must run this command within the project directory

Page 41: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

watch : { js: { files: ['src/js/*.js'], tasks: ['uglify:dev'] } }

Configure: gruntfile.js

when these files change

run this task

Page 42: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Load the plugin grunt.loadNpmTasks('grunt-contrib-watch');

Load the plugin: gruntfile.js

Page 43: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

grunt watch

Run watch

You don’t need to register the watch task, just call it  

Page 44: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

compile Sass

Page 45: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

npm install grunt-sass --save-dev

Install Grunt Sass

Don't use grunt-contrib-sass — it uses slower Ruby-based compiler

Page 46: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

treehouse ™

About File Organization

src/scss/application.scss:main Sass file

css/styles.css: my compiled CSS file

Page 47: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

sass: { dev: { options: { outputStyle: 'expanded' }, files: { 'css/styles.css' : 'src/scss/application.scss' } } }

Configure: gruntfile.js

...into this file compile this file...

Page 48: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Load the plugins grunt.loadNpmTasks('grunt-sass');

Load the plugin: gruntfile.js

Page 49: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

// Register task(s). grunt.registerTask('default', ['uglify:dev', 'sass:dev']);

Register the task: gruntfile.js

'default'

Page 50: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

watch : { js: { files: ['src/js/*.js'], tasks: ['uglify:dev'] }, css: { files: ['src/scss/**/*.scss'], tasks: ['sass:dev'] } }

Add to watch: gruntfile.js

when these files change

run this task

Page 51: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

build: { options : { outputStyle: 'compressed' }, files: { 'css/styles.css' : 'src/scss/application.scss' } }

Add Sass Build Task: gruntfile.js

minify the files

Page 52: Get Started With Grunt - treehouse-code-samples.s3 ... is installed one time for the entire system (-g) npm install -g grunt-cli 2. grunt is installed for each project in the project

grunt build

Run it!