如何在 Gulp 中从字符串创建文件? [英] How do you create a file from a string in Gulp?
本文介绍了如何在 Gulp 中从字符串创建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的 gulpfile 中,我在字符串中有一个版本号.我想将版本号写入文件.在 Gulp 中有没有很好的方法来做到这一点,还是我应该看看更通用的 NodeJS API?
In my gulpfile I have a version number in a string. I'd like to write the version number to a file. Is there a nice way to do this in Gulp, or should I be looking at more general NodeJS APIs?
推荐答案
如果您想以类似 gulp 的方式执行此操作,您可以创建一个假"流乙烯基文件并像往常一样调用 pipe
.这是一个用于创建流的函数.流"是一个核心模块,所以你不需要安装任何东西:
If you'd like to do this in a gulp-like way, you can create a stream of "fake" vinyl files and call pipe
per usual. Here's a function for creating the stream. "stream" is a core module, so you don't need to install anything:
const Vinyl = require('vinyl')
function string_src(filename, string) {
var src = require('stream').Readable({ objectMode: true })
src._read = function () {
this.push(new Vinyl({
cwd: "",
base: "",
path: filename,
contents: Buffer.from(string, 'utf-8')
}))
this.push(null)
}
return src
}
你可以这样使用它:
gulp.task('version', function () {
var pkg = require('package.json')
return string_src("version", pkg.version)
.pipe(gulp.dest('build/'))
})
这篇关于如何在 Gulp 中从字符串创建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文