[英]Switch output folder based on filename in gulp task
I have different *.scss
files in my src folder and I want one file to be compiled in its own separate folder. 我的src文件夹中有不同的*.scss
文件,我希望将一个文件编译到其自己的单独文件夹中。
Lets assume I have the files normalFile_1.scss
, specialFile.scss
, normalFile_2.scss
. 假设我有文件normalFile_1.scss
, specialFile.scss
和normalFile_2.scss
。 I want the two normal files to be compiled to the folder Public/Css
, the special file however should end up in the folder Public/Css/Special
. 我希望将两个普通文件编译到文件夹Public/Css
,但是特殊文件应该最终位于文件夹Public/Css/Special
。
I have tried to get the current filename in the task with gulp-tap
, which works fine. 我试图用gulp-tap
获取任务中的当前文件名,效果很好。
.pipe($.tap(function (file, t) {
filename = path.basename(file.path);
console.log(filename); //outputs normalFile_1.css, specialFile.css, normalFile_2.css
}))
And with gulp-if
I then wanted to switch the output folder based on the filename
variable (PATHS.dist is the output "root" folder Public
): 然后用gulp-if
,然后我想根据filename
变量切换输出文件夹(PATHS.dist是输出“ root”文件夹Public
):
.pipe($.if(filename == 'specialFile.css', gulp.dest(PATHS.dist + '/Css/Special'), gulp.dest(PATHS.dist + '/Css')));
But everything still ends up in the Public/Css
folder. 但是,所有内容仍然保留在Public/Css
文件夹中。 Why does this not work? 为什么这不起作用? Is this even a good way of trying to accomplish that or are there better methods? 这是否是实现目标的好方法,还是有更好的方法?
There are two ways to do this shown below: 如下显示了两种方法:
var gulp = require("gulp");
var sass = require("gulp-sass");
var rename = require("gulp-rename");
var path = require('path');
gulp.task('sass', function () {
return gulp.src('src/*.scss')
.pipe(sass().on('error', sass.logError))
.pipe(rename(function (path) {
if (path.basename == "specialFile") {
path.dirname = "Special";
}
}))
.pipe(gulp.dest('Public/Css'))
// .pipe(gulp.dest(function(file) {
// var temp = file.path.split(path.sep);
// var baseName = temp[temp.length - 1].split('.')[0];
// console.log(baseName);
// if (baseName == "specialFile") {
// return 'Public/Css/Special';
// }
// else return 'Public/Css';
// }))
});
gulp.task('default', ['sass']);
Obviously I suggest the rename version. 显然我建议重命名版本。
[Why a simple file.stem or file.basename doesn't work for me in the gulp.dest(function (file) {} version I don't know - that would certainly be easier but I just get undefined.] [为什么不知道一个简单的file.stem或file.basename在gulp.dest(function(file){}版本中对我不起作用-这当然会更容易,但我只是未定义。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.