用gulp保持concat()的文件夹结构 [英] Keep folder structure with concat() in gulp
问题描述
项目
|
+ - 咖啡
| |
| + -main.coffee
| |
| + -testDir
| | |
| | + -models.coffee
| | |
| | + -views.coffee
| |
| + -anotherDir
| | |
| | + -routes.coffee
| | |
| | + -views.coffee
| | |
| | + -modules.coffee
| |
| + - 等等...
|
+ -www
这个想法是保持<$ c $将文件写入 www /
目录时,将文件保存为c> coffee / 目录。在 coffee /
中可以有任意数量的子文件夹。每个文件夹中的所有 .coffee
文件应该连接成一个 modules.js
文件:
www
|
+ -modules.js
|
+ -testDir
| |
| + -modules.js
|
+ -anotherDir
| |
| + -modules.js
|
+ - etc ...
我现在有这个大嘴巴任务:
gulp.task('coffee',function(){
gulp.src('./ coffee / ** / *。 ('error',gutil.log)){
.pipe(uglify(' ))
// .pipe(concat('modules.js'))
.pipe(gulp.dest('./ www'))
});
如果没有 concat()
放置到正确的子文件夹中(但它们没有连接)。使用 concat()
,所有文件都被连接成一个 modules.js
文件:
www
|
+ -modules.js
我如何正确认识这一点?
以下解决方案使用 这首先将 Folder structure: The idea is to keep the folder structure from the I currently have this gulp task: Without the How I can realize this correctly? Here's a solution using This first puts the 这篇关于用gulp保持concat()的文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ 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 ('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()
。project
|
+-coffee
| |
| +-main.coffee
| |
| +-testDir
| | |
| | +-models.coffee
| | |
| | +-views.coffee
| |
| +-anotherDir
| | |
| | +-routes.coffee
| | |
| | +-views.coffee
| | |
| | +-modules.coffee
| |
| +- etc...
|
+-www
coffee/
directory when writing files to the www/
directory. There can be an arbitrary number of subfolders in coffee/
. All .coffee
files from each folder should be concatened into a modules.js
file:www
|
+-modules.js
|
+-testDir
| |
| +-modules.js
|
+-anotherDir
| |
| +-modules.js
|
+- etc...
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()
the files are placed into the correct subfolders (but they're not concatened). With the concat()
all files are concatened into a single modules.js
file: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/
directory and all of its direct subdirectories in a stream. Each of those directories then gets mapped to a new stream that concatenates all .coffee
files in the respective directory. Finally the appropriate destination folder for each resulting modules.js
file is determined by using path.relative()
.