在 gulp 中使用 concat() 保持文件夹结构

use*_*588 3 gulp gulp-concat

文件夹结构:

project
|
+-coffee
| |
| +-main.coffee
| |
| +-testDir
| | |
| | +-models.coffee
| | |
| | +-views.coffee
| |
| +-anotherDir
| | |
| | +-routes.coffee
| | |
| | +-views.coffee
| | |
| | +-modules.coffee
| |
| +- etc...
| 
+-www
Run Code Online (Sandbox Code Playgroud)

这个想法是在将文件coffee/写入目录时将文件夹结构与目录保持一致www/。中可以有任意数量的子文件夹coffee/.coffee每个文件夹中的所有文件都应该连接到一个modules.js文件中:

www
|
+-modules.js
|
+-testDir
| |
| +-modules.js
|
+-anotherDir
| |
| +-modules.js
|
+- etc...
Run Code Online (Sandbox Code Playgroud)

我目前有这个吞咽任务:

gulp.task('coffee', function() {
    gulp.src('./coffee/**/*.coffee', {base: './coffee/'})
        .pipe(coffee({bare: true}).on('error', gutil.log))
        .pipe(uglify())
        // .pipe(concat('modules.js'))
        .pipe(gulp.dest('./www'))
});
Run Code Online (Sandbox Code Playgroud)

没有concat()文件被放置在正确的子文件夹中(但它们没有连接)。将concat()所有文件连接成一个modules.js文件:

www
|
+-modules.js
Run Code Online (Sandbox Code Playgroud)

我怎么能正确地意识到这一点?

Sve*_*ung 5

这是使用的解决方案gulp-flatmap

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

gulp.task('coffee', function() {
  return gulp.src('./coffee/{*,}/', {base:'./coffee'})
    .pipe(flatmap(function(stream, dir) {
       return gulp.src(dir.path + '/*.coffee')
         .pipe(coffee({bare: true}).on('error', gutil.log))
         .pipe(uglify())
         .pipe(concat('modules.js'))
         .pipe(gulp.dest('./www/' + path.relative(dir.base, dir.path)))
     })) 
});
Run Code Online (Sandbox Code Playgroud)

这首先将coffee/目录及其所有直接子目录放入流中。然后,这些目录中的每一个都被映射到一个新的流,该流.coffee将相应目录中的所有文件连接起来。最后,每个结果modules.js文件的适当目标文件夹通过使用path.relative().