MinifyWorker: Promisify `compressCSS`

pull/5284/head
Richard Hansen 2021-11-15 03:11:54 -05:00
parent c94906f1f8
commit 98f0e931fe
1 changed files with 5 additions and 9 deletions

View File

@ -10,25 +10,21 @@ const Threads = require('threads');
const compressJS = (content) => Terser.minify(content); const compressJS = (content) => Terser.minify(content);
const compressCSS = (filename, ROOT_DIR) => new Promise((res, rej) => { const compressCSS = async (filename, ROOT_DIR) => {
try { try {
const absPath = path.resolve(ROOT_DIR, filename); const absPath = path.resolve(ROOT_DIR, filename);
const basePath = path.dirname(absPath); const basePath = path.dirname(absPath);
const output = await new CleanCSS({
new CleanCSS({
rebase: true, rebase: true,
rebaseTo: basePath, rebaseTo: basePath,
}).minify([absPath], (errors, minified) => { }).minify([absPath]);
if (errors) return rej(errors); return output.styles;
return res(minified.styles);
});
} catch (error) { } catch (error) {
// on error, just yield the un-minified original, but write a log message // on error, just yield the un-minified original, but write a log message
console.error(`Unexpected error minifying ${filename} (${absPath}): ${error}`); console.error(`Unexpected error minifying ${filename} (${absPath}): ${error}`);
callback(null, content); callback(null, content);
} }
}); };
Threads.expose({ Threads.expose({
compressJS, compressJS,