etherpad-lite/src/node/utils/MinifyWorker.js

37 lines
937 B
JavaScript
Raw Normal View History

2021-01-21 21:06:52 +00:00
'use strict';
/**
* Worker thread to minify JS & CSS files out of the main NodeJS thread
*/
2020-11-23 13:24:19 -05:00
const CleanCSS = require('clean-css');
const Terser = require('terser');
const path = require('path');
const Threads = require('threads');
2021-01-21 21:06:52 +00:00
const compressJS = (content) => Terser.minify(content);
2021-01-21 21:06:52 +00:00
const compressCSS = (filename, ROOT_DIR) => new Promise((res, rej) => {
try {
const absPath = path.resolve(ROOT_DIR, filename);
2021-01-21 21:06:52 +00:00
const basePath = path.dirname(absPath);
new CleanCSS({
rebase: true,
rebaseTo: basePath,
}).minify([absPath], (errors, minified) => {
if (errors) return rej(errors);
return res(minified.styles);
});
} catch (error) {
// on error, just yield the un-minified original, but write a log message
console.error(`Unexpected error minifying ${filename} (${absPath}): ${error}`);
callback(null, content);
}
});
Threads.expose({
compressJS,
2020-11-23 13:24:19 -05:00
compressCSS,
});