how to handle assets from different folders in gulp

为君一笑 提交于 2020-01-04 13:25:43

问题


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


来源:https://stackoverflow.com/questions/27364924/how-to-handle-assets-from-different-folders-in-gulp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!