mirror of
https://github.com/captbaritone/webamp.git
synced 2026-01-23 02:15:01 +00:00
Upgrade png optimizer
This commit is contained in:
parent
14c0d24a47
commit
1d39600284
1 changed files with 27 additions and 21 deletions
|
|
@ -1,4 +1,3 @@
|
|||
const postcss = require("postcss");
|
||||
const dataUriToBuffer = require("data-uri-to-buffer");
|
||||
const imagemin = require("imagemin");
|
||||
const imageminOptipng = require("imagemin-optipng");
|
||||
|
|
@ -14,24 +13,31 @@ async function optimizeDataUri(dataUri) {
|
|||
return `data:image/png;base64,${optimized.toString("base64")}`;
|
||||
}
|
||||
|
||||
module.exports = postcss.plugin("postcss-optimize-data-uri-pngs", () => {
|
||||
return async function (css) {
|
||||
// walkDecls does not let us work async, so we collect the async work we
|
||||
// need to do here, and await it at the end
|
||||
const promisesFactories = [];
|
||||
css.walkDecls(DATA_URL_PROPS_REGEX, (decl) => {
|
||||
let matches;
|
||||
// WTF JavaScript. This is the worst API for iterating RegEx matches.
|
||||
while ((matches = DATA_URL_REGEX.exec(decl.value))) {
|
||||
const [, dataUri] = matches;
|
||||
promisesFactories.push(async () => {
|
||||
decl.value = decl.value.replace(
|
||||
dataUri,
|
||||
await optimizeDataUri(dataUri)
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
await Promise.all(promisesFactories.map((p) => p()));
|
||||
const plugin = () => {
|
||||
return {
|
||||
postcssPlugin: "postcss-optimize-data-uri-pngs",
|
||||
async Once(css) {
|
||||
// walkDecls does not let us work async, so we collect the async work we
|
||||
// need to do here, and await it at the end
|
||||
const promisesFactories = [];
|
||||
css.walkDecls(DATA_URL_PROPS_REGEX, (decl) => {
|
||||
let matches;
|
||||
// WTF JavaScript. This is the worst API for iterating RegEx matches.
|
||||
while ((matches = DATA_URL_REGEX.exec(decl.value))) {
|
||||
const [, dataUri] = matches;
|
||||
promisesFactories.push(async () => {
|
||||
decl.value = decl.value.replace(
|
||||
dataUri,
|
||||
await optimizeDataUri(dataUri)
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
await Promise.all(promisesFactories.map((p) => p()));
|
||||
},
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
plugin.postcss = true;
|
||||
|
||||
module.exports = plugin;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue