Gulp: 如何将文件内容读入变量?
Gulp: How do I read file content into a variable?
我有一个 gulp 任务需要将文件读入变量,然后将其内容用作在管道中的文件上运行的不同函数的输入。我该怎么做?
示例伪代码
gulp.task('doSometing', function() {
var fileContent=getFileContent("path/to/file.something"); //How?
return gulp.src(dirs.src + '/templates/*.html')
.pipe(myFunction(fileContent))
.pipe(gulp.dest('destination/path));
});
这是您要找的吗?
fs = require("fs"),
gulp.task('doSometing', function() {
return gulp.src(dirs.src + '/templates/*.html')
.pipe(fs.readFile("path/to/file.something", "utf-8", function(err, _data) {
//do something with your data
}))
.pipe(gulp.dest('destination/path'));
});
Thargor 为我指明了正确的方向:
gulp.task('doSomething', function() {
var fileContent = fs.readFileSync("path/to/file.something", "utf8");
return gulp.src(dirs.src + '/templates/*.html')
.pipe(myFunction(fileContent))
.pipe(gulp.dest('destination/path'));
});
我有一个 gulp 任务需要将文件读入变量,然后将其内容用作在管道中的文件上运行的不同函数的输入。我该怎么做?
示例伪代码
gulp.task('doSometing', function() {
var fileContent=getFileContent("path/to/file.something"); //How?
return gulp.src(dirs.src + '/templates/*.html')
.pipe(myFunction(fileContent))
.pipe(gulp.dest('destination/path));
});
这是您要找的吗?
fs = require("fs"),
gulp.task('doSometing', function() {
return gulp.src(dirs.src + '/templates/*.html')
.pipe(fs.readFile("path/to/file.something", "utf-8", function(err, _data) {
//do something with your data
}))
.pipe(gulp.dest('destination/path'));
});
Thargor 为我指明了正确的方向:
gulp.task('doSomething', function() {
var fileContent = fs.readFileSync("path/to/file.something", "utf8");
return gulp.src(dirs.src + '/templates/*.html')
.pipe(myFunction(fileContent))
.pipe(gulp.dest('destination/path'));
});