gpt4 book ai didi

Gulp:为单个文件保留目标文件夹结构

转载 作者:行者123 更新时间:2023-12-01 08:05:41 25 4
gpt4 key购买 nike

假设我们有以下文件夹结构:

rootDirectory


├──a
│ ├──a.txt

├──b
│ ├──a
│ ├──a.txt

├──c.txt

此外,假设我们需要编写一个 Gulp 任务,该任务将获取 a 文件夹中的 a.txtc.txt ;对它们执行一些操作,然后将它们通过管道传输到 build 目录中。我想要的是复制到 build 文件夹中的文件将它们的目录结构保留在 build 文件夹中(我不希望任务不应该处理的文件进入 build 文件夹),像这样:

rootDirectory


├──a
│ ├──a.txt

├──b
│ ├──a
│ ├──a.txt

├──c.txt

├──build

├──a
│ ├──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 目录中展开文件夹结构。

但是,如果我使用通配模式:

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));
});

然后文件夹结构被保留,但是这个 globbing 模式也会以 b/a.txt 为目标,这是我不想要的。

有没有办法实现我在 Gulp 中描述的内容?基本上,告诉 Gulp:“在这里,拿这个文件,做任何你需要的,然后把它放在另一个路径中,保持文件夹结构从这个根路径开始”? 除了特别否定我不想匹配的 globbing 路径?

最佳答案

为了保留层次结构,您应该传递 {base: "."} gulp.src 的参数。像这样:

gulp.task('foo', function(){
var files = [
path.join(__dirname, 'c.txt'),
path.join(__dirname, 'a/a.txt')
];
gulp.src(files, {base: '.'})
.pipe(gulp.dest(path.join(__dirname, build));
});

关于Gulp:为单个文件保留目标文件夹结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35220618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com