Gulp:保留单个文件的目标文件夹结构 [英] Gulp: preserving folder structure on destination for individual files
问题描述
rootDirectory
│
│
├──a
│├──a.txt
│
├──b
│├──a
│├──a.txt
│
├──c.txt
另外,假设我们需要在 a
文件夹和 c中写一个Gulp任务,该任务将带
。 TXT ;对它们执行一些操作,然后将它们传送到 build
目录中。我想要的是复制到build文件夹中的文件将它们的目录结构保存在 build
文件夹中(并且我不希望那些不应该由任务获取 build
文件夹),如下所示:
root Directory
│
│
├──a
│├──a.txt
│
├──b
│├── a
├──a.txt
│
├──c.txt
│
├──建立
│
├ ──
│├──a.txt
│
├──c.txt
现在,这是我困惑的事情。如果我指定特定文件的路径:
gulp.task('foo',function(){
var files = [
path.join(__ dirname,'c.txt'),
path.join(__ dirname,'a / a.txt')
];
gulp.src (files)
.pipe(gulp.dest(path.join(__ dirname,build));
});
然后我会将 build
目录中的文件夹结构扁平化。
然而,如果我使用globbing模式:
pre $ gulp.task('foo',function(){
var files = [
path.join(__ dirname,'** / c.txt'),
path.join(__ dirname,'** / a / a.txt')
] ;
gulp.src(files)
.pipe(gulp.dest(path.join(__ dirname,build));
});
然后文件夹结构被保留,但是这种匹配模式也会以 b / a.txt
,我不想要。
有没有办法实现我所说的在基本上,告诉Gulp:在这里,拿着这个文件,用这个做任何你需要的东西,并把它放在另一个路径中,保持文件夹结构从这个根路径开始? 除了具体否定了我不想匹配的通配符路径吗? 为解决方案
您应该通过 {base:。} 参数到gulp.src。像这样:
gulp.task('foo',function(){
var files = [
path.join(__ dirname,'c.txt'),
path.join(__ dirname,'a / a.txt')
];
gulp.src(文件,' ')
.pipe(gulp.dest(path.join(__ dirname,build));
});
Let’s say we have the following folder structure:
rootDirectory
│
│
├──a
│ ├──a.txt
│
├──b
│ ├──a
│ ├──a.txt
│
├──c.txt
also, let’s say we need to write a Gulp task that will take a.txt
in the a
folder, and c.txt
; perform some manipulations on them, and then pipe them into the build
directory. What I want is for the files copied in the build folder to keep their directory structure in the build
folder (and I do not want files that were not supposed to be processed by the task to get in the build
folder), like so:
rootDirectory
│
│
├──a
│ ├──a.txt
│
├──b
│ ├──a
│ ├──a.txt
│
├──c.txt
│
├──build
│
├──a
│ ├──a.txt
│
├──c.txt
Now, here is what puzzles me. If I specify paths to specific files:
gulp.task('foo', function(){
var files = [
path.join(__dirname, 'c.txt'),
path.join(__dirname, 'a/a.txt')
];
gulp.src(files)
.pipe(gulp.dest(path.join(__dirname, build));
});
then I will flatten the folder structure in the build
directory.
If, however, I use the globbing pattern:
gulp.task('foo', function(){
var files = [
path.join(__dirname, '**/c.txt'),
path.join(__dirname, '**/a/a.txt')
];
gulp.src(files)
.pipe(gulp.dest(path.join(__dirname, build));
});
then the folder structure is preserved, but this globbing pattern will also target b/a.txt
, which I do not want.
Is there a way to achieve what I describe in Gulp? Basically, telling Gulp: "Here, take this file, do whatever you need with this, and put it in another path keeping the folder structure starting from this root path"? Apart from specifically negating globbing paths that I do not want to match?
In order to preserve hierarchy you should pass {base: "."} parameter to the gulp.src. Something like this:
gulp.task('foo', function(){
var files = [
path.join(__dirname, 'c.txt'),
path.join(__dirname, 'a/a.txt')
];
gulp.src(files, '.')
.pipe(gulp.dest(path.join(__dirname, build));
});
这篇关于Gulp:保留单个文件的目标文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!