Gulp v. Grunt

35
BUILD SYSTEMS Input file Exec. task Output file

description

Gulp and Grunt are very powerful and useful tools for front end web developers and designers. This talk aims to introduce each tool, explain their differences, and provide use cases for each.

Transcript of Gulp v. Grunt

Page 1: Gulp v. Grunt

BUILD SYSTEMS

Inputfile

Exec.task

Outputfile

Page 2: Gulp v. Grunt

gruntjs.com +2 years old

npm install -g grunt-cli

Page 3: Gulp v. Grunt

“Grunt is a task-based command line build tool for JavaScript projects.”

Original purpose of Grunt - 2012

Page 4: Gulp v. Grunt

gruntfile.js

Page 5: Gulp v. Grunt

Plugins

grunt.loadNpmTasks('grunt-contrib-less'); grunt.loadNpmTasks('grunt-contrib-uglify'); grunt.loadNpmTasks('grunt-contrib-watch');

1. Compile LESS to CSS 2. Concatenate and minify JavaScript 3. Watch source files for changes

Page 6: Gulp v. Grunt

Simple Task

autoprefixer: { options: { browsers: ['last 2 version', 'ie 8', 'ie 9'] }, styles: { expand: true, flatten: true, src: '<%=src%>/assets/css/*.css', dest: '<%=src%>/assets/css/' }, },

Page 7: Gulp v. Grunt

Complex Task

assemble: { options: { flatten: true, data: ['<%=site.source%>/data/*.{json,yml}', 'package.json'], plugins: [ 'assemble-middleware-permalinks', 'assemble-middleware-anchors', 'assemble-middleware-wordcount' ], helpers: [ 'handlebars-helper-compose', 'handlebars-helper-moment', './src/js/helpers/*.js' // Custom helpers ], assets: '<%=site.development%>/assets', partials: [opt.tpl + '/partials/**/*.{hbs,md}', opt.tpl + '/snippets/**/*.{hbs,md}'], layoutdir: opt.tpl + '/layouts', layout: 'default.hbs', collections: [ { name: 'post', sortby: 'date', sortorder: 'descending', pages: [opt.posts] } ], marked: { highlight: function (code, lang) { if (lang === undefined) lang = 'bash'; if (lang === 'js') lang = 'javascript'; if (lang === 'less') lang = 'scss'; return hljs.highlight(lang, code).value; } }, sitemap: { homepage: '<%=site.url%>', changefreq: 'daily', priority: '0.8', robot: true }, permalinks: { structure: ':basename/index.html' }, compose: { cwd: opt.posts } }, pages: { files: [ { src: opt.pages + '/*.{hbs,md}', dest: opt.dev + '/' } ] }, posts: { options: { layout: 'layout-blog.hbs', permalinks: { structure: ':year/:basename/index.html' }, feed: { debug: true, prettify: true, dest: 'rss.xml' }, wordcount: { selector: '.article-content' } }, files: [ { src: opt.posts + '/**/*.{hbs,md}', dest: opt.dev + '/' }, { src: opt.pages + '/index.hbs', dest: opt.dev + '/index.html' } ] }, projects: { permalinks: { structure: ':year/:basename/index.html' }, files: [ { src: opt.projects + '/*.{hbs,md}', dest: opt.dev + '/' } ] },

Page 8: Gulp v. Grunt

gulpjs.com >1 year old

npm install -g gulp

Page 9: Gulp v. Grunt

“Gulp does nothing but provide some streams and a basic task system.”

- Eric Schoffstall

Page 10: Gulp v. Grunt

STREAMS

Page 11: Gulp v. Grunt

Why are steams so great?Imagine the I/O and tasks of a build system

Page 12: Gulp v. Grunt

Read file(s) Task 1 Task 2 Task 3

Writefile(s)

Filesystem

File system What you probably imaginedInput

Output

Page 13: Gulp v. Grunt

Read file(s) Task 1 Read

file(s) Task 2Writefile(s)

“temp”folder

File system

Hopefully not what you imagined

Input Output

Writefile(s)

FilesystemFinal Output

Page 14: Gulp v. Grunt

Plugins do more than one task

Plugins do tasks that shouldn’t be in a plugin, like tests

gruntfile.js is configuration, not code

Grunt

Page 15: Gulp v. Grunt

Build systems should empower, not impede

Page 16: Gulp v. Grunt

GulpWith Gulp your build file is code and not config

Plugins are simple and do one thing —most are a ~20 line function

Tasks are executed with maximum concurrency

I/O works the way you picture it

Page 17: Gulp v. Grunt

There are only five things you need to know to

understand gulp.

Page 18: Gulp v. Grunt

gulp.task(name,fn)Registers the function with a name that can be executed from the CLI

Page 19: Gulp v. Grunt

gulp.src(glob)Returns a readable stream

Page 20: Gulp v. Grunt

gulp.dest(glob)Returns a writable stream

Page 21: Gulp v. Grunt

.pipe()Node.js function that takes the readable source stream, gulp.src

and hooks the output to the destination writable stream, gulp.dest

github.com/substack/stream-handbook#pipe

Page 22: Gulp v. Grunt

.pipe(foo) returns foo, e.g.,

a.pipe(b).pipe(c).pipe(d)

is the same as

a.pipe(b); b.pipe(c); c.pipe(d);

Page 23: Gulp v. Grunt

.pipe() in gulp

var a = gulp.src(‘styles.less'); // input

a.pipe(less()) // => compiled css .pipe(auto()) // => prefixed css .pipe(gulp.dest(‘styles.css')); // => output

Page 24: Gulp v. Grunt

gulp.task('styles', function () { gulp.src('/less/styles.less')

});

.pipe(less()) .pipe(gulp.dest('/public/css'))

Page 25: Gulp v. Grunt

gulp.watch(glob,fn)Executes a function when a file that matches the glob changes

Page 26: Gulp v. Grunt

gulpfile.js vs gruntfile.js demo

Page 27: Gulp v. Grunt

gulp-less gulp-uglify

gulp-concat gulp-header

gulp-minify-css

gulp-help gulp-notify gulp-autoprefixer gulp-recess moment

npm install

Page 28: Gulp v. Grunt

One can use Node packages instead

of gulp plugins

npmjs.org/package/del = npmjs.org/package/gulp-clean

Page 29: Gulp v. Grunt

npm install gulp-gruntnpmjs.org/package/gulp-grunt

Page 30: Gulp v. Grunt

Gulp ain’t all awesome

Page 31: Gulp v. Grunt

Error management ≠ good

Page 32: Gulp v. Grunt

Use casesBecause build systems should

empower, not impede

Page 33: Gulp v. Grunt

Continue using Grunt on pre-existing projects Continue using Grunt with dev teams that are used to it

Start using Gulp for personal projects Start using Gulp for work projects whenever possible

Page 34: Gulp v. Grunt

questions

Page 35: Gulp v. Grunt

Patrick Burtchaell

@pburtchaell pburchaell.com

gibbon.co/pb/gulp