使用src glob时,排除嵌套目录被复制

时间:2022-11-07 19:51:26

I can't exclude a directory structure from being copied over when using a glob in gulp src method. My task:

在gulp src方法中使用glob时,我不能排除目录结构被复制。我的任务:

gulp.task( 'compile', function () {
    return gulp.src( 'src/modules/*/wrappers/**' )
        .pipe( gulp.dest( 'build/' ) );
} );

I am expecting to copy only files, not whole dir structure.

我期望只复制文件,而不是整个dir结构。

My dir structure:

我的目标结构:

+ src/
  + modules/
      + module_1/
          + wrappers/
              file_1
              file_2
      + module_2/
          + wrappers/
              file_3
              file_4
+ build/

Dir structure after running gulp task:

运行gulp任务后的目录结构:

+ src/
  + modules/
      + module_1/
          + wrappers/
              file_1
              file_2
      + module_2/
          + wrappers/
              file_3
              file_4
+ build/
  + modules/
      + module_1/
          + wrappers/
              file_1
              file_2
      + module_2/
          + wrappers/
              file_3
              file_4

Expected dir structure after running gulp task:

运行gulp任务后预期的目录结构:

+ src/
  + modules/
      + module_1/
          + wrappers/
              file_1
              file_2
      + module_2/
          + wrappers/
              file_3
              file_4
+ build/
      file_1
      file_2
      file_3
      file_4

It's worth noting that using e.g. glob 'src/modules/module_1/wrappers/**' gives desired result.

值得注意的是使用例如glob'src / modules / module_1 / wrappers / **'给出了期望的结果。

Used modules:

  • gulp 3.8.10

Am I doing something wrong or is that bug in js glob implementation?

我做错了什么或是js glob实现中的错误?

1 个解决方案

#1


You're not doing anything wrong; gulp stores files with it's relative paths, which is why you're getting the whole directory structure.

你没有做错什么; gulp用它的相对路径存储文件,这就是你得到整个目录结构的原因。

You could use gulp-flatten:

你可以使用gulp-flatten:

var flatten = require('gulp-flatten');
gulp.task( 'compile', function () {
    return gulp.src( 'src/modules/*/wrappers/**' )
        .pipe(flatten())
        .pipe( gulp.dest( 'build/' ) );
} );

#1


You're not doing anything wrong; gulp stores files with it's relative paths, which is why you're getting the whole directory structure.

你没有做错什么; gulp用它的相对路径存储文件,这就是你得到整个目录结构的原因。

You could use gulp-flatten:

你可以使用gulp-flatten:

var flatten = require('gulp-flatten');
gulp.task( 'compile', function () {
    return gulp.src( 'src/modules/*/wrappers/**' )
        .pipe(flatten())
        .pipe( gulp.dest( 'build/' ) );
} );