65

If I use :

 gulp.src(['app/client/**/*.html'])
  .pipe(gulp.dest('dist'));

The folder structure in which my .html files were in, is maintained in the dist folder, but I would like to remove the folder structure completely and just a flat hierarchy in my dist folder.

Guy
  • 10,931
  • 5
  • 36
  • 47
i_am_elliot
  • 653
  • 1
  • 5
  • 6

2 Answers2

127

You could use gulp-rename to accomplish this:

var rename = require('gulp-rename');

gulp.src('app/client/**/*.html')
  .pipe(rename({dirname: ''}))
  .pipe(gulp.dest('dist'));
Ben
  • 10,056
  • 5
  • 41
  • 42
  • 8
    I'm surprised this isn't a standard gulp feature - works perfectly though. Thanks!! – i_am_elliot Sep 26 '14 at 16:08
  • 2
    How would you rate *gulp-flatten* versus *gulp-rename*? Also, is there a built-in feature for that in Gulp 4? – Konrad Viltersten Nov 17 '16 at 16:40
  • 1
    Throwing copper on an almost year old question: gulp-flatten appears to do the exact thing whereas as rename seems made for a different purpose...it just so happens to achieve the desired result. Therefore, in the interest of "right tool for the job"...if you want to *change* the name, go with rename...if you want to *ditch* the folders, go with flatten. – Josh Bruce Sep 19 '17 at 00:07
  • 2
    I go for gulp-rename instead of gulp-flatten - each lib less is a pain less... and gulp-rename is more generic and common. – ESP32 Oct 11 '17 at 13:36
32

You can use gulp-flatten https://www.npmjs.com/package/gulp-flatten

    app
    ├── logo
    │   └── logo.styl
    └── sidebar
        └── sidebar.styl
    
    var flatten = require('gulp-flatten');
    gulp.src('app/**/*.styl')
      .pipe(flatten())
      .pipe(gulp.dest('dist/'));
    
    dist
    ├── logo.styl
    └── sidebar.styl
    
c01nd01r
  • 598
  • 5
  • 9
  • 1
    How would you rate *gulp-flatten* versus *gulp-rename*? Also, is there a built-in feature for that in Gulp 4? – Konrad Viltersten Nov 17 '16 at 16:40
  • From todays perspective I'd reccomend _gulp-rename_ over _gulp-flatten_. The reasons are that rename is more generic, by far more widely used, more lightweight and also but not least, the last release it not as long ago as in _gulp-flatten_. – Manuel Manhart Oct 17 '21 at 11:20