问题
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?
回答1:
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().
来源:https://stackoverflow.com/questions/39932621/keep-folder-structure-with-concat-in-gulp