用gulp保持concat()的文件夹结构 [英] Keep folder structure with concat() in gulp

查看:163
本文介绍了用gulp保持concat()的文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件夹结构:

 项目
|
+ - 咖啡
| |
| + -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

我如何正确认识这一点?

解决方案

以下解决方案使用
$ 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()


Folder structure:

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

The idea is to keep the folder structure from the 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...

I currently have this gulp task:

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

Without the 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

How I can realize this correctly?

解决方案

Here's a solution using 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)))
     })) 
});

This first puts the 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().

这篇关于用gulp保持concat()的文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆