我有一个将图像转换为节点的脚本,但遇到了一个问题,我收到了成功的消息,但没有输出任何内容。
import imagemin from "imagemin";
import webp from "imagemin-webp";
var outputFolder = "./FSS-assets/webp"; // Output folder
var PNGImages = "./FSS-assets/*.png"; // PNG images
var JPEGImages = "./FSS-assets/*.jpg"; // JPEG images
imagemin([PNGImages], outputFolder, {
plugins: [webp({
lossless: true // Losslessly encode images
})]
}).then(function() {
console.log("Images converted!");
});
imagemin([JPEGImages], outputFolder, {
plugins: [webp({
quality: 65 // Quality setting from 0 to 100
})]
}).then(function() {
console.log("Images converted!");
});
我正在运行
- 节点v14.16.0
- imagemin v8
- imagemin webp v6
我也尝试过使用相对文件夹路径,但没有成功。这是在Windows 10上。
这是因为imagemin
有两个参数input
和options
你给出的是3个参数。
来自文档:
imagemin(input, options?)
Returns Promise<object[]> in the format {data: Buffer, sourcePath: string, destinationPath: string}.
因此,对于您的代码,它将是:
imagemin([PNGImages], {
destination: outputFolder,
plugins: [webp({
lossless: true // Losslessly encode images
})]
}).then(function() {
console.log("Images converted!");
});
看起来您只需要稍微调整一下参数结构。To be([images],{destination:'/output directory',plugins:{}(;
const files = await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminJpegtran(),
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
希望这能帮助&作品