Search code examples
gulpglobgulp-4

Gulp 4 glob to exclude specific files and folders not working


I recently installed gulp 4 [from 3.9.1] and I'm having an issue getting my globs to work as they used to.

I have a whole directory [less some other sub-directories and files] that I want to transfer.

Here's the relevant code:

var path_in_str = 'my_input_dir/';
var path_out_str = 'my_output_dir/';

return gulp.src([path_in_str+'**', path_in_str+'.*', '!'+path_in_str+'node_modules', '!'+path_in_str+'node_modules/**', '!'+path_in_str+'*.json'], {dot: true})
    .pipe(gulp.dest(path_out_str));

Basically, I'm trying to prevent the node_modules folder from being transferred, and I also want to prevent all .json files in the home folder from being transferred as well.

What is happening is that the node_modules directory only [no content] is being transferred. Also, all the .json files are being transferred.

How can I tweak this to fix for Gulp 4's way of handling globs [as it appears to have changed slightly]?


Solution

  • I couldn't get this to work with native gulp, so I thought I'd try the node glob package, and this option seems to work best for the use-case.

    First you would need to install it like so:

    npm install glob --save-dev
    

    Then reference it in your gulp gile:

    var gp_glob = require('glob');
    

    And finally, use it as the glob filter:

    var input_list_arr = gp_glob.sync(path_in_str+'**', {ignore: [path_in_str+'node_modules/**', path_in_str+'*.json'], dot: true});
    
    return gulp.src(input_list_arr, {base: path_in_str})
        .pipe(gulp.dest(path_out_str));  
    

    Take note that we're using the ignore option instead of ! notation to filter the file/directory paths. Also note that the base option is specified when passing the filtered list into gulp.src.

    This worked for me as intended. Hope it helps.