95

I'm attempting to pass gulp.src an array of files that I want it to deal with. This is the array as it stands.

['bower_components/jquery/jquery.js',
 'bower_components/superscrollorama/js/greensock/TweenMax.min.js',
 'bower_components/superscrollorama/jquery.superscrollorama.js' ]

I'm finding though that gulp.src doesn't seem to like that and the third element doesn't make it through into the end destination.

I've found that everything works fine when I introduce some wildcard characters like this:

['bower_components/**/jquery.js',
 'bower_components/**/js/greensock/TweenMax.min.js',
 'bower_components/**/jquery.superscrollorama.js' ]

But why? Something to do with the way globbing works? I've googled but can't find out.

Maybe this isn't the intended purpose of globbing but it doesn't make sense to me that it should work this way. Can anyone shed some light?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
morganesque
  • 953
  • 1
  • 6
  • 4

1 Answers1

165

When you pass in an array of full paths, each file is processed independently. The globbing doesn't know where the root of the path is (in fact, it guesses based on the first glob). Therefore, each file is rooted in the folder it contains, and the relative path is empty.

However, there is an easy solution. Pass an object with the key base as the second argument to gulp.src, and everything will have the correct relative path:

return gulp.src(['bower_components/jquery/jquery.js',
                'bower_components/superscrollorama/js/greensock/TweenMax.min.js',
                'bower_components/superscrollorama/jquery.superscrollorama.js' ],
            {base: 'bower_components/'})
        .pipe(...);
OverZealous
  • 39,252
  • 15
  • 98
  • 100
  • 24
    Also: This is not mentioned in the gulp docs at all, you have to click through to [the docs for `glob-stream`](https://github.com/wearefractal/glob-stream) to figure this out. – OverZealous Jan 27 '14 at 17:19
  • 10
    what if the paths don't all have the same base? I have a similar question open for that particular scenario: [gulp src not reading required json file's array values](http://stackoverflow.com/questions/31579421/gulp-src-not-reading-required-json-files-array-values) – a_dreb Jul 23 '15 at 16:53
  • 1
    same question from my side. I have two different bases for the task I need to run – Cynthia Sanchez Sep 07 '15 at 10:44
  • 5
    There's probably always a common base *somewhere* in the filesystem right? Even if it's `"/"`. If gulp is running from the root of your project dir you'd just specify the current dir as the base, and pipe to the current dir. `gulp.src(mixed, {base: "."}).pipe(doStuff).pipe(dest("."))` – numbers1311407 Nov 27 '15 at 18:25