Node.js glob include/exclude files

2.2k Views Asked by At

this is my current code to copy relevant files from my source to my build dir. It does not work as expected

return gulp.src([
    'src/**/*',
    '!src/**/*(*.sass|*.scss|*.js)',
    '!src/(components|config|elements|less|sass|scss|platforms)',
    'src/console.js',
    'src/cordova.js',
    'src/config.xml',
]).pipe(plugins.copy("www", {prefix: 1}));

My idea is this:

  1. Select all files and folders in src
  2. Remove all js and sass files because they get compiled/concat/minified and copied to build in another task
  3. However console.js and cordova.js have to be copied, so i want to reinclude them into the glob

Well, i thought the elements order in the globArray is a priority or something so that e.g. console.js gets reincluded after it got filtered out. It doesnt seem so :(

What can i do?

1

There are 1 best solutions below

0
On

So the real issue that you are facing is the lack of ability to 'debug' or 'inspect' what is currently being processed in the gulp stream. In my workflow, two irreplaceable gulp plugins to help with figuring out what is going on are:

  • gulp-util
  • gulp-rename

    var gutil = require('gulp-util'); var rename = require('gulp-rename');

Now, try appending this code snippet to your example like so:

var gutil = require('gulp-util');
var rename = require('gulp-rename');
.....

return gulp.src([
    'src/**/*',
    '!src/**/*(*.sass|*.scss|*.js)',
    '!src/(components|config|elements|less|sass|scss|platforms)',
    'src/console.js',
    'src/cordova.js',
    'src/config.xml',
]).pipe(rename(function(filepath) {
          gutil.log(filepath.basename);
})).pipe(plugins.copy("www", {prefix: 1}));

Then all you have to do is look at the output in your logs to see which files are being processed, and then update your globbing accordingly.

Also, your better off testing your paths one by one or in pairs to see if your on the right track and so you are not overwhelmed when all globs aren't working as expected. Isolate the glob issue and update accordingly.

Also just a hint to get you on the right track, this glob:

'!src/**/*(*.sass|*.scss|*.js)'

needs to be:

'!src/**/*.{sass,scss,js}'

I hope this gets you on the right track!