const tinify = require("tinify"); tinify.key = "本人的appkey"; const fs=require('fs'); const glob=require('glob'); const mkdir=require('./utils/mkdir'); const srcDir='./src/'; //被搜索的资源 let sourceFileArr=glob.sync(srcDir+'**/*.+(png|jpg|jpeg)'); sourceFileArr.forEach(function (path1,i) { const path2=path1.replace(srcDir,'./dist/') mkdir(path2) if(fs.existsSync(path2)&&fs.existsSync(path1)){ const buffer1=fs.readFileSync(path1); const buffer2=fs.readFileSync(path2); if(!Buffer.compare(buffer1,buffer2)){ console.log(path1) } }else{ console.log(path1) build(path1,path2) } }); function build(path1,path2){ tinify.fromFile(path1).toFile(path2); }