Skip to content

Commit 0316dba

Browse files
committed
Try encoding skin as webp
1 parent f038832 commit 0316dba

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
@@ -2711,6 +2711,14 @@
27112711
dependencies:
27122712
"@sinonjs/commons" "^1.7.0"
27132713

2714+
"@squoosh/lib@^0.4.0":
2715+
version "0.4.0"
2716+
resolved "https://registry.yarnpkg.com/@squoosh/lib/-/lib-0.4.0.tgz#31d18cb082c69e404589e2e281414d10f91e1668"
2717+
integrity sha512-O1LyugWLZjMI4JZeZMA5vzfhfPjfMZXH5/HmVkRagP8B70wH3uoR7tjxfGNdSavey357MwL8YJDxbGwBBdHp7Q==
2718+
dependencies:
2719+
wasm-feature-detect "^1.2.11"
2720+
web-streams-polyfill "^3.0.3"
2721+
27142722
"@tootallnate/once@1":
27152723
version "1.1.2"
27162724
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
@@ -15597,6 +15605,11 @@ warning@^4.0.3:
1559715605
dependencies:
1559815606
loose-envify "^1.0.0"
1559915607

15608+
wasm-feature-detect@^1.2.11:
15609+
version "1.2.11"
15610+
resolved "https://registry.yarnpkg.com/wasm-feature-detect/-/wasm-feature-detect-1.2.11.tgz#e21992fd1f1d41a47490e392a5893cb39d81e29e"
15611+
integrity sha512-HUqwaodrQGaZgz1lZaNioIkog9tkeEJjrM3eq4aUL04whXOVDRc/o2EGb/8kV0QX411iAYWEqq7fMBmJ6dKS6w==
15612+
1560015613
watchpack-chokidar2@^2.0.0:
1560115614
version "2.0.0"
1560215615
resolved "https://registry.yarnpkg.com/watchpack-chokidar2/-/watchpack-chokidar2-2.0.0.tgz#9948a1866cbbd6cb824dea13a7ed691f6c8ddff0"
@@ -15619,6 +15632,11 @@ wbuf@^1.1.0, wbuf@^1.7.3:
1561915632
dependencies:
1562015633
minimalistic-assert "^1.0.0"
1562115634

15635+
web-streams-polyfill@^3.0.3:
15636+
version "3.2.0"
15637+
resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.2.0.tgz#a6b74026b38e4885869fb5c589e90b95ccfc7965"
15638+
integrity sha512-EqPmREeOzttaLRm5HS7io98goBgZ7IVz79aDvqjD0kYXLtFZTc0T/U6wHTPKyIjb+MdN7DFIIX6hgdBEpWmfPA==
15639+
1562215640
webidl-conversions@^4.0.2:
1562315641
version "4.0.2"
1562415642
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad"

0 commit comments

Comments
 (0)