如何处理来自不同文件夹的资源 [英] how to handle assets from different folders in gulp

查看:110
本文介绍了如何处理来自不同文件夹的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个glob模式列表:

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

我想创建一个任务,将这些模式表示的所有文件复制到我的dest文件夹,并保持结构。例如,在我的dest文件夹中,我需要以下内容:

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

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

问题是我在'dest'下只有一级目录。例如:\

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

?如何解决这个问题?

使用gulp从不同文件夹处理资产的常见方法是什么?



更新

观看寻找方式来复制gulp文件和基于父目录重命名,我试图设置 {base:'./'} as一个参数为 gulp.src(assets,{base:'./'})但我得到的是我当前目录下的文件嵌套在另一个文件夹下。我没有得到所需的行为。任何想法?

解决方案

您可以分别处理当前目录下不存在的文件。您可以使用单独的任务或使用 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');
$ b $ gulp.task('scripts',['clean'],function(){
return es.merge(gulp.src(assets,{base:'./'}) ,gulp.src(projectAssets,{base:'../'}))
.pipe(gulp.dest('dest'));
});


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?

解决方案

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

这篇关于如何处理来自不同文件夹的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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