【问题标题】:Keep folder structure with concat() in gulp在 gulp 中使用 concat() 保持文件夹结构
【发布时间】:2016-10-08 12:56:01
【问题描述】:

文件夹结构:

project
|
+-coffee
| |
| +-main.coffee
| |
| +-testDir
| | |
| | +-models.coffee
| | |
| | +-views.coffee
| |
| +-anotherDir
| | |
| | +-routes.coffee
| | |
| | +-views.coffee
| | |
| | +-modules.coffee
| |
| +- etc...
| 
+-www

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

www
|
+-modules.js
|
+-testDir
| |
| +-modules.js
|
+-anotherDir
| |
| +-modules.js
|
+- etc...

我目前有这个 gulp 任务:

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

如果没有concat(),文件将被放置到正确的子文件夹中(但它们没有连接)。使用concat(),所有文件都连接到一个modules.js 文件中:

www
|
+-modules.js

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

【问题讨论】:

  • 不清楚。你需要举一个更好的例子。您是否希望将每个目录的所有文件连接到单独的 modules.js 文件中?还是只需要两个modules.js 文件(一个用于测试,一个用于其余)?
  • all files of every directory to be concatenated in a separate modules.js file 如果在咖啡文件夹中有许多目录 test1 test2 ... test100 in www 必须与每个文件夹中的 modules.js 相同的文件夹结构。
  • 您应该编辑问题中的示例以更好地解释您想要的内容(例如,多个子目录级别如何?)。
  • 本项目没有子关卡。

标签: gulp gulp-concat


【解决方案1】:

这是使用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)))
     })) 
});

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

【讨论】:

  • 非常感谢。我不知道平面图。
猜你喜欢
  • 2016-07-18
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-06
相关资源
最近更新 更多