Skip to content

Commit c4146ae

Browse files
committed
Try encoding skin as webp
1 parent d4ccbf5 commit c4146ae

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

packages/webamp/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"@babel/preset-typescript": "^7.7.2",
6666
"@babel/runtime": "^7.7.2",
6767
"@sentry/browser": "5.9.1",
68+
"@squoosh/lib": "^0.4.0",
6869
"@types/classnames": "^2.2.6",
6970
"@types/css-font-loading-module": "^0.0.2",
7071
"@types/fscreen": "^1.0.1",

packages/webamp/scripts/postcss-optimize-data-uri-pngs.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,38 @@ const postcss = require("postcss");
22
const dataUriToBuffer = require("data-uri-to-buffer");
33
const imagemin = require("imagemin");
44
const imageminOptipng = require("imagemin-optipng");
5+
const { ImagePool } = require("@squoosh/lib");
56

67
const DATA_URL_REGEX = new RegExp(/url\((data:image\/png;base64,.+)\)/gi);
78
const DATA_URL_PROPS_REGEX = /^(background(?:-image)?)|(content)|(cursor)/;
89

9-
async function optimizeDataUri(dataUri) {
10+
async function optimizeDataUri(imagePool, dataUri) {
1011
const buffer = dataUriToBuffer(dataUri);
1112
const optimized = await imagemin.buffer(buffer, {
1213
use: [imageminOptipng()],
1314
});
15+
try {
16+
const image = imagePool.ingestImage(optimized);
17+
await image.decoded;
18+
const encodeOptions = {
19+
webp: {
20+
lossless: true,
21+
},
22+
};
23+
await image.encode(encodeOptions);
24+
const rawEncodedImage = (await image.encodedWith.webp).binary;
25+
const b = new Buffer(rawEncodedImage);
26+
return `data:image/webp;base64,${b.toString("base64")}`;
27+
} catch (e) {
28+
console.error("e", e);
29+
}
1430
return `data:image/png;base64,${optimized.toString("base64")}`;
1531
}
1632

1733
module.exports = postcss.plugin("postcss-optimize-data-uri-pngs", () => {
1834
return async function (css) {
35+
const imagePool = new ImagePool();
36+
1937
// walkDecls does not let us work async, so we collect the async work we
2038
// need to do here, and await it at the end
2139
const promisesFactories = [];
@@ -27,11 +45,12 @@ module.exports = postcss.plugin("postcss-optimize-data-uri-pngs", () => {
2745
promisesFactories.push(async () => {
2846
decl.value = decl.value.replace(
2947
dataUri,
30-
await optimizeDataUri(dataUri)
48+
await optimizeDataUri(imagePool, dataUri)
3149
);
3250
});
3351
}
3452
});
3553
await Promise.all(promisesFactories.map((p) => p()));
54+
await imagePool.close();
3655
};
3756
});

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,14 @@
29212921
dependencies:
29222922
"@sinonjs/commons" "^1.7.0"
29232923

2924+
"@squoosh/lib@^0.4.0":
2925+
version "0.4.0"
2926+
resolved "https://registry.yarnpkg.com/@squoosh/lib/-/lib-0.4.0.tgz#31d18cb082c69e404589e2e281414d10f91e1668"
2927+
integrity sha512-O1LyugWLZjMI4JZeZMA5vzfhfPjfMZXH5/HmVkRagP8B70wH3uoR7tjxfGNdSavey357MwL8YJDxbGwBBdHp7Q==
2928+
dependencies:
2929+
wasm-feature-detect "^1.2.11"
2930+
web-streams-polyfill "^3.0.3"
2931+
29242932
"@testing-library/dom@^8.0.0":
29252933
version "8.11.1"
29262934
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.11.1.tgz#03fa2684aa09ade589b460db46b4c7be9fc69753"
@@ -16018,6 +16026,11 @@ warning@^4.0.3:
1601816026
dependencies:
1601916027
loose-envify "^1.0.0"
1602016028

16029+
wasm-feature-detect@^1.2.11:
16030+
version "1.2.11"
16031+
resolved "https://registry.yarnpkg.com/wasm-feature-detect/-/wasm-feature-detect-1.2.11.tgz#e21992fd1f1d41a47490e392a5893cb39d81e29e"
16032+
integrity sha512-HUqwaodrQGaZgz1lZaNioIkog9tkeEJjrM3eq4aUL04whXOVDRc/o2EGb/8kV0QX411iAYWEqq7fMBmJ6dKS6w==
16033+
1602116034
watchpack-chokidar2@^2.0.0:
1602216035
version "2.0.0"
1602316036
resolved "https://registry.yarnpkg.com/watchpack-chokidar2/-/watchpack-chokidar2-2.0.0.tgz#9948a1866cbbd6cb824dea13a7ed691f6c8ddff0"
@@ -16040,6 +16053,11 @@ wbuf@^1.1.0, wbuf@^1.7.3:
1604016053
dependencies:
1604116054
minimalistic-assert "^1.0.0"
1604216055

16056+
web-streams-polyfill@^3.0.3:
16057+
version "3.2.0"
16058+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz#a6b74026b38e4885869fb5c589e90b95ccfc7965"
16059+
integrity sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==
16060+
1604316061
webidl-conversions@^4.0.2:
1604416062
version "4.0.2"
1604516063
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"

0 commit comments

Comments
 (0)