Gulp is a powerful utility for automating development workflows. Tasks are written using code, not configuration, enabling the easy creation of highly-custom and flexible automations. This talk introduces developers to the core concepts of gulp.js, and how to leverage it for new & existing projects. We’ll cover several examples of common tasks for managing CSS, JS and PHP, including: compiling Sass, minifying files, running PHP tests, checking code styles, ensuring legacy browser support & more.
2. Colin O’Dell
• Lead Web Developer at Unleashed Technologies
• PHP League Member
league/commonmark
league/html-to-markdown
• PHP 7 Upgrade Guide e-book
@colinodell - joind.in/talk/17992
3. Agenda
• What Are Task Runners?
• Getting Started with Gulp.js
• Gulp API
• Task Examples
• Q&A
@colinodell - joind.in/talk/17992
4. What Are Task Runners?
Software that automates, coordinates, and controls the
process of running build tasks.
• Define tasks & dependencies
• Execute those tasks
@colinodell - joind.in/talk/17992
5. What Are Task Runners?
Three types of tasks:
1. Tranformations
2. Tests
3. Commands
@colinodell - joind.in/talk/17992
6. Transformations
• Compiling SASS/LESS, CoffeeScript, etc.
• Combining/minifying files
• Compressing images
• Copying files to a different location
@colinodell - joind.in/talk/17992
0110011
1001111
0010011
1010010
0110011
1001111
0010011
1010010
12. Apache Ant
• Like Make, but built in Java
• Typically installed via package manager (apt-get)
• XML configuration files
@colinodell - joind.in/talk/17992
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
14. Phing
• Based on Apache Ant, but built in PHP
• Typically installed via PEAR or Composer
• XML configuration files
@colinodell - joind.in/talk/17992
<?xml version="1.0"?>
<project name="Hello World Project" default="info">
<target name="info">
<echo>Hello World - Welcome to Apache Ant!</echo>
</target>
</project>
17. Grunt
• Built in JavaScript
• Many third-party plugins; easy to install
• JS-based configuration with heavy JSON usage
@colinodell - joind.in/talk/17992
var grunt = require('grunt');
grunt.registerTask('default', 'The default build task', function(){
console.log('Hello World!');
});
19. Gulp.js
• Built in JavaScript
• Many third-party plugins; easy to install
• Uses JS code instead of configuration
• Built around file streams
@colinodell - joind.in/talk/17992
var gulp = require('gulp');
gulp.task('default', function() {
console.log('Hello world.');
});
30. gulp.task(name [, deps] [, fn])
• Defines tasks and their dependencies
• Three parameters:
Name (required)
Array of task dependencies
Function to run
@colinodell - joind.in/talk/17992
gulp.task('sometask', function() {
// Do stuff
});
gulp.task('sometask', ['dep1', 'dep2'], function() {
// Do stuff
});
gulp.task('js', ['jshint', 'jsmin']);
31. gulp.src(globs [, options])
• Two parameters:
Globs of files (required)
String or array
Options
• Returns a stream of Vinyl files that can be piped to plugins
@colinodell - joind.in/talk/17992
gulp.src('styles/foo.css')
gulp.src('styles/*.css')
gulp.src('styles/**/*.css')
gulp.src(['styles/**/*.css', '!*.min.css'])
33. .pipe(destination)
• Called on gulp.src() result stream
• destination is a stream (typically an input stream created by a plugin)
@colinodell - joind.in/talk/17992
return gulp.src(['foo.css', 'bar.css'])
.pipe(minifycss())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('build/css'));
34. gulp.dest(path [,options])
• Writes the streamed files to the given path
@colinodell - joind.in/talk/17992
return gulp.src(['foo.css', 'bar.css'])
.pipe(minifycss())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('build/css'));
65. Other Plugins & Integrations
• Browsersync – Live browser reloads
• gulp-notify – Sends notifications to OS notification center or Growl
• gulp-autopolyfiller – Like autoprefixer, but for JS polyfills
• gulp-git – Run Git commands within Gulp
• gulp-ssh – Connect to servers via SSH and SFTP
@colinodell - joind.in/talk/17992
66. Summary
@colinodell - joind.in/talk/17992
• Task runners help automate build processes
• Automate during development, CI, and deployment
• Gulp is fast, easy-to-use, and has tons of great plugins
Vinyl is a very simple metadata object that describes a file. When you think of a file, two attributes come to mind: path and contents. These are the main attributes on a Vinyl object. A file does not necessarily represent something on your computer’s file system. You have files on S3, FTP, Dropbox, Box, CloudThingly.io and other services. Vinyl can be used to describe files from all of these sources.
No intermediate writes
Non-blocking
Similar to gulp-changed, but supports many-to-one mappings