grunt & front-end workflow

58
GRUNT Front-end Workflow

Upload: pagepro

Post on 07-Aug-2015

118 views

Category:

Internet


1 download

TRANSCRIPT

Page 1: Grunt & Front-end Workflow

GRUNTFront-end Workflow

Page 2: Grunt & Front-end Workflow

About Pagepro:

• Internet Software House from Poland • Operating since 2010 • Clients from Western Europe and U.S. • 176 projects done in 2014 • 1758 PSDs converted into

HTML5 in 2014

Page 3: Grunt & Front-end Workflow

Common Front-end tasks• Compile SASS / LESS

• Minify

• Uglyfi

• Test

• Optimize

• Analyze

• Modules & Dependencies

Page 4: Grunt & Front-end Workflow
Page 5: Grunt & Front-end Workflow

node.js

• Server-side JavaScript

• V8 (Google Chrome Browser)

• Command Line Tool

• 40% JS 60 % C++

Page 6: Grunt & Front-end Workflow

node.js

• EVENT DRIVEN, non-blocking I/O model

• event-loops

• no DOM implementation

• single-thread

Page 7: Grunt & Front-end Workflow

mkdir grunt; cd grunt;

git https://github.com/Pagepro/training-grunt.git .

Clone repository

Page 8: Grunt & Front-end Workflow

node.js

node -v;// check version if installed

https://nodejs.org/download/

brew install node

Page 9: Grunt & Front-end Workflow

node.js

• process (node)

• document (JavaScript)

Page 10: Grunt & Front-end Workflow

node.js

Traditional I/O

var result = db.query('select x from table_Y'); doSomethingWith(result); //wait for result! doSomethingWithOutResult(); //execution is blocked

Page 11: Grunt & Front-end Workflow

node.js

Non-blocking I/O

db.query('select x from table_Y', function (result) { doSomethingWith(result); //wait for result! }); doSomethingWithOutResult(); //executes without any delay!

Page 12: Grunt & Front-end Workflow

node.js

• Access File

• Listen to network traffic

• HTTP requests

• DB Access

Page 13: Grunt & Front-end Workflow

node.js• Utilites

• Gulp

• Grunt

• Yeoman

• Live Reload

• Web servers

Page 14: Grunt & Front-end Workflow

node.js

console.log('helo world');

node hello

node hello.js (.js is optional)

Page 15: Grunt & Front-end Workflow

node.js

var age = 23, person = { name: 'Chris', location: 'Poland' } console.log(age); console.log(person);

node hello-extended

Page 16: Grunt & Front-end Workflow

node.js

var requiredModule = require('./example-module'); console.log(requiredModule);

node modules

module.exports.a = 55; module.exports.b = [1, 3, 4, 5];

Page 17: Grunt & Front-end Workflow

node.js

node server var http = require('http'); http.createServer(function (req, res) { console.log(req); res.write('hello!'); res.end(); }).listen(5000, "127.0.0.1");

Page 18: Grunt & Front-end Workflow

node.js

node watch

var fs = require('fs');fs.watch('./server.js', { persistent: true}, function(event, filename) { console.log(event + " event occurred on " + filename);});

Page 19: Grunt & Front-end Workflow

npm

• Node Package Manager

• npm install mongojs

• https://npmjs.org

• npm init

Page 20: Grunt & Front-end Workflow

npm

npm init;

Page 21: Grunt & Front-end Workflow

package.json{ "name": "libsasserplate", "version": "1.0.1", "description": "Libsass starter", "author": "Pagepro <[email protected]>", "devDependencies": { "grunt": "^0.4.2", "time-grunt": "latest", "grunt-sass": "latest", "grunt-autoprefixer": "latest", "grunt-spritesmith": "latest", "grunt-contrib-uglify": "~0.2.4", "grunt-contrib-watch": "^0.5.3", "grunt-contrib-copy": "^0.5.0", "grunt-contrib-kraken": "^0.1.3", "grunt-contrib-connect": "^0.8.0", "matchdep": "^0.3.0", "jquery": "1.11.2" }}

Page 22: Grunt & Front-end Workflow

package.json

http://browsenpm.org/package.json

http://semver.org

Page 23: Grunt & Front-end Workflow

npm

{ "name": "npm", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "" }, "author": "", "license": "ISC", "devDependencies": { "grunt": "^0.4.5" }}

{ "name": "npm", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "" }, "author": "", "license": "ISC"}

npm install grunt —save-dev

Page 24: Grunt & Front-end Workflow

npm

rm -rf node_modules/*

ls

npm install

Page 25: Grunt & Front-end Workflow

When not to use node.js?• When you are doing heavy and

CPU intensive calculations on server side, because event-loops are CPU hungry

• Node.js API is still in beta, it keeps on changing a lot from one revision to another and there is a very little backward compatibility. Most of the packages are also unstable. Therefore is not yet production ready.

Page 26: Grunt & Front-end Workflow
Page 27: Grunt & Front-end Workflow

GRUNT• Lint

• Test

• Compile

• Open browser

• Run browser

• Watch assets

• Recompile

• Reload browser

Page 28: Grunt & Front-end Workflow

GRUNTpython -m SimpleHttpServer

open index.html

sass —watch sass:css

js hint main.js

./conquer_the_world.sh

etc.

Page 29: Grunt & Front-end Workflow

GRUNT

We can setup long flows and run it with just one task.

We can stop the flow if taks fails.

Everyone in the team can follow same workflow.

Page 30: Grunt & Front-end Workflow

GRUNT

• JavaScript task runner

• npm install -g grunt-cli

Page 31: Grunt & Front-end Workflow

Gruntfile.js

• Grunt configuration file

• Makefile, Rakefile, etc.

Page 32: Grunt & Front-end Workflow

GRUNT• cd grunt/linter;

• npm init;

• npm install grunt —save-dev

• npm install grunt-contrib-jshint —save-dev

• grunt jshint

Page 33: Grunt & Front-end Workflow

GRUNT

'use strict';module.exports = function (grunt) { // load jshint plugin grunt.loadNpmTasks('grunt-contrib-jshint');};

Page 34: Grunt & Front-end Workflow

GRUNT

>> No "jshint" targets found.

Warning: Task "jshint" failed. Use --force to continue.

Aborted due to warnings.

Page 35: Grunt & Front-end Workflow

What’s a target?

Page 36: Grunt & Front-end Workflow

GRUNTIt’s a core concept of Grunt. When we create a task, we add targets to it.

Every target represents a set of actions and files the task will be run over.

We can run a task’s target by simply appending it to the task name.

grunt mytask:mytarget

Page 37: Grunt & Front-end Workflow

GRUNT

'use strict';module.exports = function (grunt) { // load jshint plugin grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.initConfig({ jshint: { all: [ 'Gruntfile.js' ] } });};

Page 38: Grunt & Front-end Workflow

GRUNT

'use strict';module.exports = function (grunt) { // load jshint plugin grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.initConfig({ jshint: { all: [ 'Gruntfile.js', 'app/js/**/*.js' ] } });};

Page 39: Grunt & Front-end Workflow

GRUNT

'use strict';module.exports = function (grunt) { // load jshint plugin grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.initConfig({ jshint: { all: [ 'Gruntfile.js', 'app/js/**/*.js', '!app/js/vendor/**/*.js' ] } });};

Page 40: Grunt & Front-end Workflow

GRUNT'use strict';module.exports = function (grunt) { // load jshint plugin grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.initConfig({ jshint: { options: { jshintrc: '.jshintrc' }, all: [ 'Gruntfile.js', 'app/js/**/*.js', '!app/js/vendor/**/*.js' ] } });};

Page 41: Grunt & Front-end Workflow

GRUNT & SASS

cd sass;

npm install;

grunt sass:dev

Page 42: Grunt & Front-end Workflow

GRUNT & SASS

Prepare grunt sass:prod for getting minified css.

Page 43: Grunt & Front-end Workflow

GRUNT & SERVER

cd server;

npm install;

grunt connect;

Page 44: Grunt & Front-end Workflow

GRUNT & SERVER'use strict';module.exports = function (grunt) { // load plugins grunt.loadNpmTasks('grunt-contrib-connect'); grunt.initConfig({ connect: { server: { options: { port: grunt.option('port') || 8080, hostname: 'localhost', base: '', keepalive: true } } } });};

Page 45: Grunt & Front-end Workflow

GRUNT & WATCH

Listen for something.

Do something.

Page 46: Grunt & Front-end Workflow

GRUNT & SERVER

cd watch;

npm install;

Page 47: Grunt & Front-end Workflow

GRUNT & SERVER

watch: {sass: {

files: ['app/sass/*.scss'], tasks: ['sass:dev'] }}

Page 48: Grunt & Front-end Workflow

GRUNT & SERVER

grunt connect:server watch

Page 49: Grunt & Front-end Workflow

GRUNT & SERVER

grunt.registerTask('mytask', 'Desc', [ 'connect:server', 'watch',]);

Page 50: Grunt & Front-end Workflow

GRUNT & SERVER

grunt.registerTask('default', ['copy:dev', 'compass:dev', 'connect:server', 'watch']);

Page 51: Grunt & Front-end Workflow

What is matchdep?

Page 52: Grunt & Front-end Workflow

Matchdep

// Load pluginsrequire('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);// Default task(s).grunt.registerTask('default', ['sass', 'connect:server', 'copy:dev', 'watch']);// SASSS compilation onlygrunt.registerTask('compile', ['sass']);// sprite generationgrunt.registerTask('sprites', ['sprite']);grunt.registerTask('krak', ['kraken']);

Page 53: Grunt & Front-end Workflow

Other examples

Page 54: Grunt & Front-end Workflow

Uglifyuglify: {

lp: { files: { 'dist_lp/js/app.min.js': [ 'node_modules/jquery/dist/jquery.js', 'src/js/plugins.js', 'src/js/main.js' ] } }, thx: { files: { 'dist_thx/js/app.min.js': [ 'node_modules/jquery/dist/jquery.js', 'src/js/plugins.js', 'src/js/main.js' ] } } },

Page 55: Grunt & Front-end Workflow

Shell

shell: { lpPackage: { command: 'cd dist_lp && zip -r ../package_lp.zip *' }, thxPackage: { command: 'cd dist_thx && zip -r ../package_thx.zip *' }}

Page 56: Grunt & Front-end Workflow

Process

process: function (content, srcpath) { content = content.replace('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>', '<script src="./js/app.min.js"></script>'); content = content.replace('<script src="static/js/main.js"></script>', ''); content = content.replace('<script src="static/js/plugins.js"></script>', ''); return content.replace(/static\//g, "./");}

Page 57: Grunt & Front-end Workflow

Useful links1. https://www.youtube.com/watch?v=pU9Q6oiQNd0

2. http://howtonode.org/introduction-to-npm

3. https://speakerdeck.com/dmosher/frontend-workflows-and-tooling

4. http://gruntjs.com/

5. http://browsenpm.org/package.json

Page 58: Grunt & Front-end Workflow

Thank you!Developing the web in the heart of Europe.