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}