我正在使用 gulp 来处理网站的页面,其中一些页面是 php 文件。问题是,在所有页面都通过模板引擎运行之后,它们具有.html
文件扩展名。我正在向文件添加一个属性,指定它是否应该是除 html 之外的文件,然后重命名文件以匹配。但是,出于某种原因,gulp-rename 一直说我用来存储扩展名的变量是undefined
.
gulpfile.js 中对应的任务:
var gulp = require('gulp'),
gutil = require('gulp-util'),
lodash = require('lodash'),
data = require('gulp-data'),
filesize = require('gulp-filesize'),
frontMatter = require('gulp-front-matter'),
rename = require('gulp-rename'),
util = require('util');
gulp.task('metalsmith', function(){
var ext; //Variable to store file extension
return gulp.src(CONTENT_DIR)
.pipe(frontMatter()).on("data", function(file){
lodash.assign(file, file.frontMatter);
delete file.frontMatter;
})
.pipe(data(function(file){ //ext is defined here
if(typeof file.filetype === 'undefined'){
ext = {extname: '.html'};
}else{
ext = {extname: file.filetype};
}
}))
.pipe(tap(function(file){
console.log(ext); //when I print to the console, ext is defined and correct
}))
.pipe(rename(ext)) //ext is undefined
.pipe(gulp.dest(BUILD_DIR))
});
当我运行上面的错误时Unsupported renaming parameter type supplied
。
我也试过它在同一行有选项 obj rename()
, ext
只存储文件扩展名,例如:.pipe(rename({extname: ext}))
这导致文件未定义添加为扩展名(而不是phpfile.php
下面的md
文件将被命名phpfileundefined
)
phpfile.md 中的 yaml
---
layout: php.hbt
filetype: ".php"
title: "php"
---
包.json
"devDependencies": {
"gulp": "^3.9.1",
"gulp-data": "^1.2.1",
"gulp-filter": "^4.0.0",
"gulp-front-matter": "^1.3.0",
"gulp-rename": "^1.2.2",
"gulp-util": "^3.0.7",
"lodash": "^4.11.1"
}