首页 > 解决方案 > gulp-imagemin 如何将压缩值获取到变量

问题描述

我试图获得压缩值:原始大小,缩小大小和空间节省到一个变量。到目前为止,只有我发现的插件仅在控制台中显示此数据。

在此处输入图像描述

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

我的 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);
      })
    ])
  );

标签: javascriptnode.jsgulpcompressionimagemin

解决方案


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


推荐阅读