How to Tell Gulp to Skip or Ignore Some Files in Gulp.Src([...])

How to tell Gulp to skip or ignore some files in gulp.src([...])?

Add a !:

gulp.task('styles', function() {
gulp.src([
'!css/ignnore.css', // <== !
'css/*.css'
])
.pipe(concat('styles.css'))
.pipe(autoprefix('last 2 versions'))
.pipe(minifyCss())
.pipe(gulp.dest('local/view/style/base/css/dist/'));
});

Conditionally Ignore files in gulp.src

Your gulpfile is just JavaScript so you can use the ternary operator to conditionally exclude files in gulp.src():

var isQA = $.yargs.argv.stage === 'qa';

gulp.task('default', function() {
return gulp.src([config.appMarkupFiles].concat((isQA) ? ['!app/settings{,/**}'] : []))
.pipe(gulp.dest('dist'));
});

Using gulp-filter works, but you had two mistakes in your code:

  1. There's an unnecessary space in !app/settings{, /**} and
  2. You're applying $.filter twice.

Here's a working solution using gulp-filter:

var isQA = $.yargs.argv.stage === 'qa';

gulp.task('default', function() {
return gulp.src(config.appMarkupFiles)
.pipe($.if(isQA, $.filter(['!app/settings{,/**}'])))
.pipe(gulp.dest('dist'));
});

Excluding files/directories from Gulp task

Quick answer

On src, you can always specify files to ignore using "!".

Example (you want to exclude all *.min.js files on your js folder and subfolder:

gulp.src(['js/**/*.js', '!js/**/*.min.js'])

You can do it as well for individual files.

Expanded answer:

Extracted from gulp documentation:

gulp.src(globs[, options])

Emits files matching provided glob or an array of globs. Returns a stream of Vinyl files that can be piped to plugins.

glob refers to node-glob syntax or it can be a direct file path.

So, looking to node-glob documentation we can see that it uses the minimatch library to do its matching.

On minimatch documentation, they point out the following:

if the pattern starts with a ! character, then it is negated.

And that is why using ! symbol will exclude files / directories from a gulp task

In Gulp how do you negate multiple items e.g. ignore a file and a directory?

Just add them on to your list:

gulp.src(['./**/*.{css,js,gif,png,php,eot,svg,ttf,woff}',
'!./src/**/*',
'!./gulpfile.js'])

You can have as many items in your glob list as you want.

How can I tell gulp to skip files it's already processed?

Gulp 3

You can use gulp-changed for this. Any matched file that's in 'originals/logos/*.{png,jpg}' and already in 'dist/logos' will be filtered out before the responsive step:

var changed = require('gulp-changed'); //         <----  add

gulp.task('logos', function () {
return gulp.src('originals/logos/*.{png,jpg}')
.pipe(changed('dist/logos')) // <---- add
.pipe(responsive({
'*.*': sizes.map(function(size) {
return {
width: '' + (size * 100) + '%',
rename: {
suffix: size === 1 ? '' : ('@' + size + 'x')
}
}
})
}))
.pipe(imageOptim.optimize())
.pipe(gulp.dest('dist/logos'));
});

 

Update: Gulp 4

Use Gulp's lastRun() in combination with Gulp src's option since(), no external dependencies needed.

const gulp = require('gulp');

function logos() { // <-- Gulp 4-ified this line
return gulp.src(/* source glob here */, { since: gulp.lastRun(logos) }) // <-- added since:lastRun(name-of-this-function)
/* -- your pipes here -- */
.pipe(gulp.dest(/* destination path here */));
};

Or selectively import the Gulp pieces you need. In that example, only src, dest, and lastRun are used so

const { src, dest, lastRun } = require('gulp');

function logos() {
return src(/* source glob here */, { since: lastRun(logos) }) // <-- no `gulp.`s
/* -- your pipes here -- */
.pipe(dest(/* destination path here */)); // <-- no `gulp.`
};

How do I exclude only .min.js files in gulp task, while staying in same directory?

You can use negated patterns to exclude .min.js files.

gulp.src(['dev/js/*.js', '!dev/js/*.min.js'])

Exclude files from Gulp stream if contents contain a string

Use gulp-filter

const filter = require('gulp-filter');

gulp.task("TaskExample", function () {

// return true if want the file in the stream
// return file to exclude the file

const excludeMessageFilter = filter(function (file) {

let contents = file.contents.toString();
return !contents.match('mail::message');
});

return gulp.src('storage/framework/views/*.php')

.pipe(excludeMessageFilter)

// .pipe(htmlmin(...
// .pipe(gulp.dest('''''''));
});

How to gulp ignore files with a specific pattern?

Use an exclamation point.

You can do gulp.src(['src/*.styl', '!src/_*.styl']);

or gulp.src(['src/*', '!src/_*']);

Answered here: Excluding files/directories from Gulp task



Related Topics



Leave a reply



Submit