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

34 lines
910 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 fsp = require('fs').promises;
2020-11-23 13:24:19 -05:00
const path = require('path');
const Threads = require('threads');
2021-01-21 21:06:52 +00:00
const compressJS = (content) => Terser.minify(content);
2021-11-15 03:11:54 -05:00
const compressCSS = async (filename, ROOT_DIR) => {
const absPath = path.resolve(ROOT_DIR, filename);
2021-01-21 21:06:52 +00:00
try {
const basePath = path.dirname(absPath);
2021-11-15 03:11:54 -05:00
const output = await new CleanCSS({
2021-01-21 21:06:52 +00:00
rebase: true,
rebaseTo: basePath,
2021-11-15 03:11:54 -05:00
}).minify([absPath]);
return output.styles;
2021-01-21 21:06:52 +00:00
} catch (error) {
// on error, just yield the un-minified original, but write a log message
console.error(`Unexpected error minifying ${filename} (${absPath}): ${error}`);
return await fsp.readFile(absPath, 'utf8');
2021-01-21 21:06:52 +00:00
}
2021-11-15 03:11:54 -05:00
};
Threads.expose({
compressJS,
2020-11-23 13:24:19 -05:00
compressCSS,
});