在 gulp 中使用 concat() 保持文件夹结构
Keep folder structure with concat() in gulp
文件夹结构:
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
我怎样才能正确地意识到这一点?
这是一个使用 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
文件。最后,使用 path.relative()
.
确定每个生成的 modules.js
文件的适当目标文件夹
文件夹结构:
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
我怎样才能正确地意识到这一点?
这是一个使用 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
文件。最后,使用 path.relative()
.
modules.js
文件的适当目标文件夹