如何在 gulp.dest() 中使用来自 gulp.src() 的路径? [英] How can I use the path from gulp.src() in gulp.dest()?

查看:40
本文介绍了如何在 gulp.dest() 中使用来自 gulp.src() 的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个 gulp 任务,该任务将通过 LESS 将来自不同文件夹的一堆文件通过管道传输,然后将它们输出到基于原始源的文件夹.考虑这个文件夹结构:

Trying to create a gulp task that will pipe a bunch of files from different folders through LESS and then output them to a folder based on the original source. Consider this folder structure:

Project
+-- /Module_A
|   +- /less
|   |  +- a.less
|   +- a.css
|
+-- /Module_B
    +- /less
    |  +- b.less
    +- b.css

这是我的 gulpfile:

Here's my gulpfile:

var gulp = require('gulp');
var gutil = require('gulp-util');
var less = require('gulp-less');

gulp.task('compileLess', function () {
  gulp.src('./*/less/*.less')
    .pipe(less())
    .pipe(gulp.dest( ??? ));
});

gulp.task('default', ['compileLess']);

我知道 gulp.dest() 需要传入一个路径,但在我的示例中,路径会因源文件而异.那么如何从源中获取路径,对其进行修改,然后将其传递给 gulp.dest()?

I know gulp.dest() expects a path to be passed in but in my example the path will be different based on the source file. So how can I grab the path from source, modify it and then pass it into gulp.dest()?

或者我是不是走错了路?

Or am I going about this the wrong way?

推荐答案

你应该看看 gulp-rename

差不多:

gulp.src('./*/less/*.less')
  .pipe(less())
  .pipe(rename(function(path){
    // Do something / modify the path here         
  }))
  .pipe(gulp.dest('./finalRootDestination'))

你让 gulp.dest 指向你的最终输出目录,但是根据你想要在 gulp-rename 回调中的任何逻辑动态修改各个文件路径.

You leave gulp.dest pointing at your final output dir, but modify on the fly the individual file paths based on whatever logic you want inside the callback to gulp-rename.

这篇关于如何在 gulp.dest() 中使用来自 gulp.src() 的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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