Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

node.js - Why does gulp.src not like being passed an array of complete paths to files?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(...);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...