node.js 为什么 gulp.src 不喜欢传递文件的完整路径数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21386940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does gulp.src not like being passed an array of complete paths to files?
提问by morganesque
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.
我正在尝试向 gulp.src 传递一个我希望它处理的文件数组。这是目前的阵列。
['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.
我发现虽然 gulp.src 似乎不喜欢那样,并且第三个元素没有进入最终目的地。
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?
也许这不是通配符的预期目的,但它应该以这种方式工作对我来说没有意义。任何人都可以透露一些信息吗?
回答by OverZealous
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.
当您传入完整路径数组时,每个文件都会独立处理。globbing 不知道路径的根在哪里(实际上,它是根据第一个 glob 猜测的)。因此,每个文件都以它包含的文件夹为根,相对路径为空。
However, there is an easy solution. Pass an object with the key baseas the second argument to gulp.src, and everything will have the correct relative path:
但是,有一个简单的解决方案。将带有键的对象base作为第二个参数传递给gulp.src,一切都会有正确的相对路径:
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(...);

