javascript - 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);
})
])
);
解决方案
我不确定在 gulp 任务完成后变量的范围是否会保留(因为我不知道你的代码是如何结构的),所以我可以给你的一个建议是将值写入文件并从中检索它它用于在前端显示。
推荐阅读
- matplotlib - 在 Jupyter Notebooks 中增加 matplotlib 图形 DPI,但仅用于导出为 PDF
- php - Laravel 缓存视图中的 PHP League Commonmark $environment 错误
- python - 如何将可可注释正确转换为蒙版图像?
- java - 根据输入查找每周日期范围
- java - Kotlin 中的 XML 阅读器
- python - Python/Python 中的 Webscraping 问题首先在 CSV 中打印类,然后打印信息
- python - 仅在 Plotly 图中显示特定点的标签?
- flutter - 为什么我的 Firebase 更新功能并不总是有效?
- reactjs - 是的惰性选项调用上的验证类型错误
- python - 在 lxml 中查找具有未知名称空间的元素