Gulp is a build framework, much like Grunt, Assetic, or Capistrano, but whose particularity is to work by streams, or pipes.
This way, it privilegies code over configuration,
making building tasks fairly simple and easily maintainable.
gulp.task('task', function(){
return gulp.src('**/*.file', {cwd: 'app/'})
.pipe(coffee())
.pipe(concat())
.pipe(uglify())
.pipe(gulp.dest('dist/'))
});
Think of a stream as a flow of data flowing into pipes.
Data comes raw into the pipe, then passes a serial of processors one after another, to finally return the desired result.
Not this...
This is what comes into mind:
grunt.initConfig({
htmlmin: {
options: {
collapseWhitespace: true,
removeComments: false
},
docs: {
files: [{
expand: true,
cwd: '<%= yo.pages %>',
src: ['*.html'],//, 'views/{,*/}*.html'],
dest: '<%= yo.pages %>'
}]
}
},
copy: {
dist: {
files: [{
expand: true,
cwd: '.tmp/styles/',
dest: '<%= yo.dist %>',
src: '{,*/}*.css'
}]
},
docs: {
files: [{
expand: true,
cwd: '<%= yo.docs %>/',
dest: '<%= yo.pages %>',
src: [
'images/*',
'1.0/**/*'
]
}]
}
}
});
There are people who prefer configuration over code (especially non-Node developers).
gulp.task('compass', ['clean'], function(){
// Process data inside...
});
gulp.task('compass', ['clean'], function(){
return gulp.src('**/*.scss', {cwd: 'app/styles'})
// what to do with this stream
});
gulp.task('compass', ['clean'], function(){
return gulp.src('**/*.scss', {cwd: 'app/styles'})
.pipe(compass())
.pipe(autoprefixer('last 1 update'))
.pipe(concat('style.css'))
// Now we need to "spit out" the result
});
gulp.task('compass', ['clean'], function(){
return gulp.src('**/*.scss', {cwd: 'app/styles'})
.pipe(compass())
.pipe(autoprefixer('last 1 update'))
.pipe(concat('style.css'))
.pipe(gulp.dest('dist'));
});
gulp.task('compass', ['clean'], function(){
return gulp.src('**/*.scss', {cwd: 'app/styles'})
.pipe(compass())
.pipe(autoprefixer('last 1 update'))
.pipe(concat('style.css'))
.pipe(gulp.dest('dist'))
// Immediately after writing style.css, create a minified version
.pipe(rename(function (path) { path.extname = '.min.css'; }))
// uglify
.pipe(csso())
.pipe(gulp.dest('dist'));
});
gulp.task('watch', function(){
gulp.watch('**/*.scss', ['clean', 'compass']).pipe(connect.reload());
gulp.watch('**/*.js', ['clean', 'compile']).pipe(connect.reload());
});
Now everyone can build up gulp tasks without reading pages of documentation!
var gulp = require('gulp');
var pkg = require('./package.json');
var concat = require('gulp-concat');
var minify = require('gulp-minify');
var jshint = require('gulp-jshint');
var spawn = require('child_process').spawn;
var scriptFiles = './src/**/*.js';
gulp.task('compile', function(){
// concat all scripts, minify, and output
gulp.src(scriptFiles)
.pipe(concat({fileName: pkg.name+".js"})
.pipe(minify())
.pipe(gulp.dest('./dist/'));
});
gulp.task('test', function(){
// lint our scripts
gulp.src(scriptFiles).pipe(jshint());
// run our tests
spawn('npm', ['test'], {stdio: 'inherit'});
});
gulp.task('default', function(){
gulp.run('test', 'compile');
gulp.watch(scriptFiles, function(){
gulp.run('test', 'compile');
});
});