MinifyWorker: Promisify `compressCSS`
parent
c94906f1f8
commit
98f0e931fe
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue