如何在一项gulp任务中创建多个文件 [英] How to create multiple files in one gulp task

查看:99
本文介绍了如何在一项gulp任务中创建多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,代表生成的文件及其内容,

I have an array which represents generated files and their contents,

[{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文件,但是我不知道如何将它们粘在一起并使它们工作.任何帮助将不胜感激.

The files do not yet exist on the file system, how do I plug them into a gulp pipe line so they get created before any other gulp tasks are set loose on them? I have looked at the gulp-foreach and gulp-file but I have no clue how to stick them together and get that to work. Any help would be much appreciated.

推荐答案

这是我所使用的解决方案……虽然有点黑,但它确实有效!请提出所有改进建议.如果您喜欢,请投票支持我!

Here's the solution I went with... it's kind of hackish but hey it works! Please suggest any and all improvements. If you like it vote me up thanks!

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"));
}

这篇关于如何在一项gulp任务中创建多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆