1
我在我的source
目錄的結構如下:在咕嘟咕嘟任務有條件創建從文件名的目錄
|-source
|-home.pug
|-page1.pug
|-page2.pug
我希望在我的dest
目錄得到這個:
|-dest
|-index.html (former home.pug)
|-page1/index.html (former page1.pug)
|-page2/index.html (former page2.pug)
我Gulpfile.js外觀像這樣:
var
gulp = require('gulp'),
gulpif = require('gulp-if'),
gzip = require('gulp-gzip'),
htmlmin = require('gulp-htmlmin'),
path = require('path'),
pug = require('gulp-pug'),
rename = require('gulp-rename');
gulp.task('views', function() {
gulp.src('source/!(home)*.pug')
.pipe(pug())
.pipe(rename(function(file) {
file.dirname = path.join(file.dirname, file.basename);
file.basename = 'index';
file.extname = '.html';
}))
.pipe(htmlmin())
.pipe(gulp.dest('dest/'))
gulp.src('source/home.pug')
.pipe(pug())
.pipe(rename(function(file) {
file.basename = 'index';
file.extname = '.html';
}))
.pipe(htmlmin())
.pipe(gulp.dest('dest/'))
});
正如你所看到的,兩個塊在頂部和底部使用相同的代碼。我想找到一個更優化的解決方案。
我加入gulp-if
並試圖執行的if-else邏輯:
gulp.task('views', function() {
gulp.src('source/*.pug')
.pipe(pug())
.pipe(gulp-if(
'home.pug',
rename(function(file) {
file.basename = 'index';
file.extname = '.html';
}),
rename(function(file) {
file.dirname = path.join(file.dirname, file.basename);
file.basename = 'index';
file.extname = '.html';
})))
.pipe(htmlmin())
.pipe(gulp.dest('dest/'))
});
但這並沒有工作。 Gulp創建了多餘的dest/home/index.html
而不是僅僅是dest/index.html
。
謝謝,斯文。它很棒! –