Intro to Gulp

35
@TheMattBrunt Intro to Gulp Intro to Gulp The Streaming Build System “Automate and enhance your workflow”

Transcript of Intro to Gulp

Page 1: Intro to Gulp

@TheMattBruntIntro to Gulp

Intro to GulpThe Streaming Build System

“Automate and enhance your workflow”

Page 2: Intro to Gulp

@TheMattBruntIntro to Gulp

Before I talk about Gulp

Page 3: Intro to Gulp

@TheMattBruntIntro to Gulp

Task Runners

Page 4: Intro to Gulp

@TheMattBruntIntro to Gulp

Gulp vs Gruntvs Cake vs Broccoli vs Brunch

vs NPM scripts

Anyone else hungry?

Page 5: Intro to Gulp

@TheMattBruntIntro to Gulp

Don’t use Grunt! Use Gulp!

Don’t use Gulp! Use Grunt!

Don’t use either! Use NPM!

(why bother with anything, it’s all pointless when the robots take over…)

Page 6: Intro to Gulp

@TheMattBruntIntro to Gulp

What should I use?Whatever you want.

You’re the ones working with it. Choose what you like.

Page 7: Intro to Gulp

@TheMattBruntIntro to Gulp

Disadvantages?• More dependencies

• At the mercy of plugins supporting things • gulp-ruby-sass to use ruby-sass for example

• ‘Bloat’ • Gulp is a relative newcomer compared to Grunt

• Smaller install base than Grunt - though it’s catching

up • Pretty much everything here can be done in npm-scripts

Page 8: Intro to Gulp

@TheMattBruntIntro to Gulp

Grunt• Single Gruntfile

• + plugins • No code logic • Plugins based on a

configuration object

Gulp• Single Gulpfile

• + plugins • Relies on code logic via pipes • Configuration isn’t as ‘clean’ as

Grunt • It’s up to you how you want to

configure and write your file

They both essentially do the same thing, just in different ways.

Page 9: Intro to Gulp

@TheMattBruntIntro to Gulp

In your head, picture a build system

It should take files, modify them in a few ways, then output the results.

Page 10: Intro to Gulp

@TheMattBruntIntro to Gulp

You may have pictured something like this:

This is what Gulp does.

Page 11: Intro to Gulp

@TheMattBruntIntro to Gulp

Gulp uses streams and pipes

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

Plugins do the rest

Page 12: Intro to Gulp

@TheMattBruntIntro to Gulp

Getting started with Gulp• Install Node.js (there’s multiple ways to do this) • CD to project directory and create package.json

• npm init # asks lots of questions

• echo {} > package.json # quick & dirty

• Install gulp globally • npm install -g gulp

• Install gulp to the project • npm install --save-dev gulp

• Create gulpfile.js and go from there!

Page 13: Intro to Gulp

@TheMattBruntIntro to Gulp

package.jsonNode packages!

Page 14: Intro to Gulp

@TheMattBruntIntro to Gulp

Installing plugins• npm install --save-dev package-name

• Configuration options for when using a plugin are plugin-

specific, docs / README on the package pages!

Page 15: Intro to Gulp

@TheMattBruntIntro to Gulp

Learn just 4 Gulp functions

Page 16: Intro to Gulp

@TheMattBruntIntro to Gulp

1

Page 17: Intro to Gulp

@TheMattBruntIntro to Gulp

gulp.src(globs[, options])

Emits a readable stream of files that matches the glob.

This is piped to other streams.

Page 18: Intro to Gulp

@TheMattBruntIntro to Gulp

2

Page 19: Intro to Gulp

@TheMattBruntIntro to Gulp

gulp.dest(path[, options])

Returns a writeable stream.

Files piped to this are written to the filesystem

Page 20: Intro to Gulp

@TheMattBruntIntro to Gulp

3

Page 21: Intro to Gulp

@TheMattBruntIntro to Gulp

gulp.task(name[, deps], fn)

Registers the function as a gulp task with a name

Can specify dependencies on other tasks

Page 22: Intro to Gulp

@TheMattBruntIntro to Gulp

4

Page 23: Intro to Gulp

@TheMattBruntIntro to Gulp

gulp.watch(glob, fn)

Runs the function whenever a file matching the glob changes.

Page 24: Intro to Gulp

@TheMattBruntIntro to Gulp

Congratulations!You are now an expert in using Gulp.

Page 25: Intro to Gulp

@TheMattBruntIntro to Gulp

The GulpfileWhere the magic happens

Page 26: Intro to Gulp

@TheMattBruntIntro to Gulp

gulpfile.js

Page 27: Intro to Gulp

@TheMattBruntIntro to Gulp

Best Practices?

Page 28: Intro to Gulp

@TheMattBruntIntro to Gulp

How should it be structured?• Split tasks into separate files and include them with a plugin. • Or just define all tasks in the file. • Use a separate config file. • Or include config within gulpfile.js

• Object • JSON

However you want.

Page 29: Intro to Gulp

@TheMattBruntIntro to Gulp

What can we do with it?• Pretty much anything you want. • Compile SASS / LESS / Stylus etc into CSS. • Compile CoffeeScript etc into JS. • Concatenate files. • Minify files. • Clean up / rename / move files. • Syncing of your browser across multiple devices. • Re-run tests. • and so on…

Page 30: Intro to Gulp

@TheMattBruntIntro to Gulp

Outcomes & Benefits• Performance gains:

• Concatenating files reduces the number of requests to fetch

assets for our site. • Minifying reduces file size of CSS / Images / JS.

Smaller files lead to faster page load times. • Portable.

• Setup once, commit gulpfile.js and dependencies, every dev

has the same setup. • Automatic re-running of tasks. • Easy to use.

Page 31: Intro to Gulp

@TheMattBruntIntro to Gulp

Favourite feature?

Page 32: Intro to Gulp

@TheMattBruntIntro to Gulp

File watchers.No more save file, switch to terminal, re-run task.

Repetitive & boring.

Page 33: Intro to Gulp

@TheMattBruntIntro to Gulp

DemoIt’s easier to see it in action.

Page 34: Intro to Gulp

@TheMattBruntIntro to Gulp

Talk through gulpfile.js

Configuration

Gulp utils

Run a task

See changes

File watchers

Browser sync

PHP Stuff!

Page 35: Intro to Gulp

@TheMattBruntIntro to Gulp

Links & info

http://blog.keithcirkel.co.uk/why-we-should-stop-using-grunt/

http://xkcd.com/378/

https://github.com/Brunty/intro-to-gulp-talk/