我有一个表示生成的文件及其内容的数组
[{name: "src/js/file1.js",content: "some js content"},
{name: "src/file2.html",content: "some html content"},
{name: "src/css/file3.css",content: "some css content"}]
文件系统上尚不存在文件,如何将它们插入gulp管道中,以便在任何其他gulp任务设置松散之前创建它们?我已经查看了gulp-foreach和gulp-file但我不知道如何将它们粘在一起并使其工作。任何帮助将不胜感激。
答案 0 :(得分:3)
这是我使用的解决方案......它有点像hackish但是嘿它有效!请建议任何和所有改进。如果你愿意,请投票给我谢谢!
var gulp = require("gulp");
var foreach = require("gulp-foreach");
var file = require("gulp-file");
var addsrc = require('gulp-add-src');
var files = [{name: "src/js/file1.js",content: "some js content"},
{name: "src/file2.html",content: "some html content"},
{name: "src/css/file3.css",content: "some css content"}];
gulp.task("default", function() {
return gulp.src("./dummy.txt", {base: "./.build"})
.pipe(foreach(function(stream, f){
files.forEach(function(gfile){
stream
.pipe(file(gfile.name, gfile.content))
.pipe(gulp.dest("./"));
});
return stream;
}))
.pipe(addsrc(["./.build/**/*", "!./dummy.txt"]))
.pipe(gulp.dest("./app"));
}