how to handle assets from different folders in gul

2019-07-24 16:50发布

问题:

I have list of glob patterns:

var assets = [
    '../projects.a/dir1/dir2/file1.js',
    '../projects.b/dir1/dir2/file2.js',
    './app/scripts/dir1/dir2/module.js'
];

I want to create a task that will copy all the files that represented by those patterns to my dest folder, and keep the structure. For example, in my dest folder I want the following:

'dest/projects.a/dir1/dir2/file1.js',
'dest/projects.b/dir1/dir2/file2.js',
'dest/app/scripts/dir1/dir2/module.js'

I built the following task:

gulp.task('scripts', ['clean'], function() {
    return gulp.src(assets)
        .pipe(gulp.dest('dest'));
});

The problem is that I get under 'dest' only one level of directories. For example:\

'dest/dir2/file1.js',
'dest/dir2/file2.js',
'dest/dir2/module.js'

Any idea what is wrong? How can I solve it?
What is the common approach of handling assets from different folders with gulp?

UPDATE
Looking on Looking for way to copy files in gulp and rename based on parent directory, I tried to set {base: './'} as a parameter to gulp.src(assets, {base: './'}) but I get the files that under my current directory nested under another folder. I didn't get the needed behavior. Any idea?

回答1:

You could handle the files which are not under the current directory separately. You could use separate tasks, or join streams using event-stream:

var projectAssets = [
    '../projects.a/dir1/dir2/file1.js',
    '../projects.b/dir1/dir2/file2.js'
];
var assets = [
    './app/scripts/dir1/dir2/module.js'
];

var es = require('event-stream');

gulp.task('scripts', ['clean'], function() {
    return es.merge(gulp.src(assets, {base: './'}), gulp.src(projectAssets, {base: '../'}))
        .pipe(gulp.dest('dest'));
});