Adrián Bolonio
Gulp is a javascript task runner that lets you automate tasks such as bundling, preprocessing and minifying files, libraries and stylesheets.
What is Gulp?
Gulp vs Grunt
"Gulp is a streaming build system".
Think Grunt, but faster and less config.
.less
.less
.less
.css
.min.css
.less
.less
.min.css
concat()
less()
minify()
rename()
PIPE
1. Installation
Install Node
$ sudo npm install -g gulp
Install Gulp
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
2. Setup package.json
$ npm init
package.json
Create new node project
{
"name": "project-name",
"version": "1.0.0",
...
}
$ npm install --save-dev gulp
{
"name": "project-name",
"version": "1.0.0",
...
"devDependencies": {
"gulp": "^3.9.0"
}
}
package.json
Add gulp to the project
3. Create gulpfile.js
$ touch gulpfile.js
var gulp = require('gulp');
gulpfile.js
Create gulpfile.js
4. Gulp tasks
var gulp = require('gulp');
gulp.task('taskName1', function(){
...
});
gulp.task('default', ['taskName1', 'taskName2']);
gulpfile.js
$ gulp taskName1
Run task
Gulp API
gulp.task('taskName', [dependencies], function(done){
...
});
gulp.task('default', ['taskName1', 'taskName2']);
gulp.src(['source/styles/*.css', '...'])
gulp.src(['...', 'build/styles/*.css'])
gulp.src('source/styles/*.css')
.pipe(gulp.dest('build/styles/'));
gulp.watch('source/styles/*.css', ['taskName']);
Common Plugins and Tasks
...at least by me
var gulp = require('gulp');
var concat = require('gulp-concat');
gulp.task('css', function(){
gulp.src('source/styles/*.css')
.pipe(concat('style.css'))
.pipe(gulp.dest('build/styles/'));
});
$ npm install --save-dev gulp-concat
gulpfile.js
var gulp = require('gulp');
var less = require('gulp-less');
gulp.task('css', function(){
gulp.src('source/styles/*.less')
.pipe(less())
.pipe(gulp.dest('build/styles/'));
});
$ npm install --save-dev gulp-less
gulpfile.js
var gulp = require('gulp');
var uglify = require('gulp-uglify');
gulp.task('js', function(){
gulp.src('source/js/*.js')
.pipe(uglify())
.pipe(gulp.dest('build/js/'));
});
npm install --save-dev gulp-uglify
gulpfile.js
var gulp = require('gulp');
var minify = require('gulp-minify-css');
gulp.task('css', function(){
gulp.src('source/styles/*.less')
.pipe(minify({ keepBreaks: true }))
.pipe(gulp.dest('build/styles/'));
});
npm install --save-dev gulp-minify-css
gulpfile.js
var gulp = require('gulp');
var rename = require('gulp-rename');
gulp.task('css', function(){
gulp.src('source/styles/*.less')
.pipe(minify({ keepBreaks: true }))
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('build/styles/'));
});
npm install --save-dev gulp-rename
gulpfile.js
gulp.task('watch', function(){
gulp.watch(['source/styles/components/*.css'], ['css']);
gulp.watch(['source/styles/*.less'], ['less']);
gulp.watch(['source/js/*.js'], ['js']);
gulp.watch(['source/*.html'], ['html']);
gulp.watch(['source/img/**/*'], ['images']);
gulp.watch(['source/fonts/**/*'], ['fonts']);
});
gulp.task('build', ['css', 'less', 'js', 'html', 'images','fonts', 'watch']);
gulpfile.js
Grunt requires plug-ins for basic functionality such as file watching; Gulp has them built-in.
http://gulpjs.com/plugins/
around 1800 plugins
How to run Gulp?
var gulp = require('gulp');
...
gulp.task('default', ['css', 'less', 'js', 'html', 'images','fonts', 'watch']);
gulpfile.js
$ gulp
command line
var gulp = require('gulp');
...
gulp.task('myGulp', ['css', 'less', 'js', 'html', 'images','fonts', 'watch']);
gulpfile.js
$ gulp myGulp
command line