Javascript gulp imagemin如何获取变量的压缩值

Javascript gulp imagemin如何获取变量的压缩值,javascript,node.js,gulp,compression,imagemin,Javascript,Node.js,Gulp,Compression,Imagemin,我尝试将压缩值:原始大小、缩小大小和节省空间设置为一个变量。到目前为止,只有我找到的插件只在控制台中显示这些数据 有没有简单的方法可以将控制台中显示的相同数据转换为变量 我的gulp imagemin代码: gulp.task("compress-images", () => Promise.all([ new Promise((resolve, reject) => { gulp .src("./uploads/*"

我尝试将压缩值:原始大小、缩小大小和节省空间设置为一个变量。到目前为止,只有我找到的插件只在控制台中显示这些数据

有没有简单的方法可以将控制台中显示的相同数据转换为变量

我的gulp imagemin代码:

    gulp.task("compress-images", () =>
    Promise.all([
      new Promise((resolve, reject) => {
        gulp
          .src("./uploads/*")
          .pipe(printSpaceSavings.init())
          .pipe(
            imagemin([
              imageminMozjpeg({ quality: 75 }),
              imagemin.svgo({
                plugins: [{ removeViewBox: true }, { cleanupIDs: false }]
              })
            ])
          )
          .pipe(
            tap(function(file) {
              file.contents = Buffer.from(
                yourFunction(file.contents.toString())
              );
              function yourFunction(input) {
                return input.toString(); // Possible to get this data to a variable?
              }
            })
          )
          .on("error", reject)
          .pipe(printSpaceSavings.print()) //consoleLog
          .pipe(gulp.dest("./compressed"))
          .on("end", resolve);
      })
    ])
  );

我不确定gulp任务完成后变量的作用域是否会保留(因为我不知道您的代码是如何构造的),因此我可以给您一个建议,将值写入一个文件并从中检索,以便在前端显示。

为什么要在变量中显示它?它的目的是什么?在前端显示图像的值我只是觉得它很奇怪,你可以访问数据并在控制台中显示,但不能访问它,所以你可以将数据显示为变量。但是,是的,我想我必须再次检索数据,以便在前端显示它。