Gulp:如何将文件内容读入变量?

2022-01-12 00:00:00 javascript gulp

我有一个 gulp 任务,需要将文件读入变量,然后将其内容用作在管道中的文件上运行的不同函数的输入.我该怎么做?

I have a gulp task that needs to read a file into a variable, and then use its content as input for a different function that runs on the files in the pipe. How do I do that?

伪伪代码示例

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

推荐答案

Thargor 指出了正确的方向:

Thargor pointed me out in the right direction:

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

相关文章