2021-01-21 21:06:52 +00:00
|
|
|
'use strict';
|
2020-06-04 15:00:50 +02:00
|
|
|
/**
|
|
|
|
* 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');
|
2020-06-04 15:00:50 +02:00
|
|
|
|
2021-01-21 21:06:52 +00:00
|
|
|
const compressJS = (content) => Terser.minify(content);
|
2020-06-04 15:00:50 +02:00
|
|
|
|
2021-11-15 03:11:54 -05:00
|
|
|
const compressCSS = async (filename, ROOT_DIR) => {
|
2021-01-21 21:06:52 +00:00
|
|
|
try {
|
2021-03-01 17:00:05 -05:00
|
|
|
const absPath = path.resolve(ROOT_DIR, filename);
|
2021-01-21 21:06:52 +00:00
|
|
|
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}`);
|
|
|
|
callback(null, content);
|
|
|
|
}
|
2021-11-15 03:11:54 -05:00
|
|
|
};
|
2020-06-04 15:00:50 +02:00
|
|
|
|
|
|
|
Threads.expose({
|
|
|
|
compressJS,
|
2020-11-23 13:24:19 -05:00
|
|
|
compressCSS,
|
|
|
|
});
|