【问题标题】:Gulp: preserving folder structure on destination for individual filesGulp:在目标上为单个文件保留文件夹结构
【发布时间】:2016-02-05 09:33:17
【问题描述】:

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

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目录下的文件夹结构展平。

但是,如果我使用 globbing 模式:

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 中描述的内容?基本上,告诉 Gulp:“在这里,拿这个文件,用它做任何你需要的事情,然后把它放在另一个路径中,保持文件夹结构从这个根路径开始”? 除了专门否定我不想匹配的通配路径?

【问题讨论】:

  • 这可能会有所帮助:github.com/gulpjs/gulp/issues/151
  • @sobolevn:您的意思是使用base 参数吗? (gulp.src(["blah-blah"], {base: "."}))?我需要尝试一下;看起来很有希望。
  • 是的,base 选项作为第二个参数传递给 gulp.src 有效!非常感谢!

标签: gulp


【解决方案1】:

为了保留层次结构,您应该将{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));
});

【讨论】:

  • 是的,这就是我最终所做的。谢谢!
  • 看起来你只是在传递 '.'作为第二个论点。那不应该是 {base:'.'} 吗?
  • @light24bulbs 是对的,它必须是 {base: '.'} 而不仅仅是一个点
猜你喜欢
  • 2013-06-18
  • 1970-01-01
  • 2022-06-29
  • 2017-02-08
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 2019-05-02
  • 2012-10-13
相关资源
最近更新 更多