imagemin([__dirname + 'images/raw/*.{jpg,png}'], {
destination: __dirname + '/images/converted/',
plugins: [
imageminWebp({
quality: 75,
resize: {
width: 1000,
height: 0
}
})
]
})
.then(() => {
console.log('Images optimized');
})
代码工作正常,我收到消息"图像优化",但目标文件夹为空。代码没有任何反应。有人可以帮助我解决这个问题吗?
问题出在您的输入路径上。__dirname
去掉尾部斜杠,所以假设你的工作目录是/Users/user/my-project
,你最终会得到一个这样的 glob:
/Users/user/my-projectimages/raw/*.{jpg,png}
.
通常建议通过path.join()
连接路径以保持一致性:
const path = require('path');
const inputPath = path.join(__dirname, 'images/raw/*.{jpg,png}');
// /Users/user/my-project/images/raw/*.{jpg,png}