1. webp
1.1. 批量转换 webp
const webp=require('webp-converter');
const { resolve, basename, extname } = require('path');
const glob = require("glob")
webp.grant_permission();
glob("./imgs/*.{gif,png,jp{,e}g}", { root: resolve(__dirname, './')}, function (er, files) {
files.forEach(file => {
extname(file) === '.gif' ?
webp.gwebp(file, resolve(__dirname,`./webp/${basename(file)}.webp`), "-q 80", logging="-v") :
webp.cwebp(file, resolve(__dirname,`./webp/${basename(file)}.webp`), "-q 80", logging="-v");
})
})
const webp=require('webp-converter');
const { resolve, basename, extname } = require('path');
const glob = require("glob")
webp.grant_permission();
const limitGenerate = (files, handler, limit) => {
const sequence = [].concat(files)
let promises = []
promises = sequence.splice(0, limit).map((file, index) => handler(file).then(() => index))
let p = Promise.race(promises)
for (let i = 0; i < sequence.length; i++) {
p = p.then(res => {
promises[res] = handler(sequence[i]).then(() => res)
return Promise.race(promises)
}).then()
}
}
const generateImg = file => {
return new Promise((res) => {
console.log(`${file} start`)
const result = extname(file) === '.gif' ?
webp.gwebp(file, resolve(__dirname,`./webp/${basename(file)}.webp`), "-q 80", logging="-v") :
webp.cwebp(file, resolve(__dirname,`./webp/${basename(file)}.webp`), "-q 80", logging="-v");
result.then(() => {
console.log(`${file} end`)
res()
});
})
}
glob("./imgs/*.{gif,png,jp{,e}g}", { root: resolve(__dirname, './')}, function (er, files) {
limitGenerate(files, generateImg, 3)
})
1.2. webp 在页面中的应用: image + background-image
const isSupportWebp = document.createElement('canvas').toDataURL('image/webp', 0.5).indexOf('data:image/webp') === 0;
Vue.directive('webp', {
inserted (el: any) {
try {
if (isSupportWebp) {
if (process.env.RUN_ENV === 'development') {
el.src = el.src.replace(/https*:\/\/[^/]+\//, '/');
return;
}
if (el.src.indexOf('base64')) {
return;
}
el.src = `${el.src}_.webp`;
}
} catch (err) {
return false;
}
},
});
Vue.directive('webp-bg', {
inserted (el: any) {
try {
if (isSupportWebp) {
const str = (el).getAttribute('class').split(' ');
str.push('webpa');
el.setAttribute('class', str.join(' '));
}
} catch (err) {
return false;
}
},
});
<div class="index-nav" v-webp-bg></div>
<img src="../../assets/styles/images/file_bg.png" v-webp alt="">