如何将globbed gulp.src文件移动到嵌套的gulp.dest文件夹中
问题第1部分:输出到嵌套的动态文件夹 我使用Gulp.js进行图形电子邮件开发。我的雇主正在切换到不同的营销平台,这要求我们的电子邮件模板位于不同的文件夹结构中当gulp.src使用globbing时,我在输出到嵌套文件夹时遇到问题。非常感谢您的帮助 下面是gulp.src文件夹的简化示例:如何将globbed gulp.src文件移动到嵌套的gulp.dest文件夹中,gulp,glob,gulp-watch,gulp-dest,Gulp,Glob,Gulp Watch,Gulp Dest,问题第1部分:输出到嵌套的动态文件夹 我使用Gulp.js进行图形电子邮件开发。我的雇主正在切换到不同的营销平台,这要求我们的电子邮件模板位于不同的文件夹结构中当gulp.src使用globbing时,我在输出到嵌套文件夹时遇到问题。非常感谢您的帮助 下面是gulp.src文件夹的简化示例: build/template1/template1.html build/template2/template2.html build/template3/template4.html build/temp
build/template1/template1.html
build/template2/template2.html
build/template3/template4.html
build/template4/template4.html
build/template1/theme/html/template1.html
build/template2/theme/html/template2.html
build/template3/theme/html/template4.html
build/template4/theme/html/template4.html
下面是gulp.src文件夹的简化示例:
build/template1/template1.html
build/template2/template2.html
build/template3/template4.html
build/template4/template4.html
build/template1/theme/html/template1.html
build/template2/theme/html/template2.html
build/template3/theme/html/template4.html
build/template4/theme/html/template4.html
我想为动态模板文件夹做一些类似于通配符的事情
gulp.task('moveFile', function(){
return gulp.src('./build/*/*.html')
.pipe(gulp.dest('./build/*/theme/html'));
});
。。。但是globbing只在gulp.src中起作用当使用globbed gulp.src时,如何输出到动态文件夹?我能得到的最接近的方法是将/theme文件夹与模板文件夹放在同一级别,而不是按需要放在每个文件夹中
谢谢你的帮助
问题第2部分:将*重命名文件*输出到嵌套的动态文件夹
Mark回答了我的问题(谢谢@Mark!),但我过度简化了我的用例,所以我添加了第2部分
除了嵌套文件,我还需要重命名它。(我最初让这个部分工作,但无法让这两个部分一起工作。)参考gulp重命名文档,我做了3次不同的尝试。离这里很近,但我希望能多帮点忙。:-)
这项工作:
const rename = require("gulp-rename");
const path = require("path");
gulp.task('moveFile', function(){
return gulp.src(['build/**/*.html'])
.pipe(rename(function (file) {
console.log(file.dirname);
file.dirname = path.join(file.dirname, 'theme/html');
}))
.pipe(gulp.dest('build')) // build/template1/theme/html
});
我尝试了几种方法,包括尝试base
选项和gulp flant
以及在gulp.dest
中使用函数,但这是最简单的
问题第二部分:
path.basename/extname
只是“getter”,您无法设置这些值。谢谢您的回复@Mark。当我使用gulp重命名插件尝试您提供的代码时,我得到一个终端错误:ReferenceError:path未定义。我尝试将path
参数添加到函数中,但没有帮助。我在您的代码中没有看到任何其他可以解释path对象的内容。有什么建议吗?谢谢很抱歉,我忘了添加path
require语句-它是节点内置的,您不需要单独安装它。看见我把它加在上面了,真管用!谢谢@Mark!你能再帮我一件事吗?我原来的问题过于简化了。我在问题上增加了第二部分。我不仅需要嵌套文件,还需要重命名它。我本来可以很好地进行重命名,但现在这两个部分不能像我尝试的那样一起工作。又来了!它们是否都被重命名为email.html.twig
,只是进入单独的目录?每个电子邮件模板都必须有一个名为“email.html.twig”的文件。谢谢你回答第二部分,@Mark!这就成功了。我不明白(现在仍然不明白)为什么在gulp重命名文档中使用path.property,但file.property似乎可以互换。