Feat/lightningcss (#6427)

* Added lightning css for minification of css

* Added esbuild for minification

* Upgraded to Node 22

* Fixed.

* Temporary change branch

* Rebased changes.
This commit is contained in:
SamTV12345 2024-06-03 22:32:06 +02:00 committed by GitHub
parent b376688f75
commit 426174d491
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 5329 additions and 3946 deletions

View file

@ -3,31 +3,45 @@
* Worker thread to minify JS & CSS files out of the main NodeJS thread
*/
const CleanCSS = require('clean-css');
const Terser = require('terser');
const fsp = require('fs').promises;
const path = require('path');
const Threads = require('threads');
import path from 'node:path'
import {expose} from 'threads'
import lightminify from 'lightningcss'
import {transform} from 'esbuild';
const compressJS = (content) => Terser.minify(content);
/*
* Minify JS content
* @param {string} content - JS content to minify
*/
const compressJS = async (content) => {
return await transform(content, {minify: true});
}
/*
* Minify CSS content
* @param {string} filename - name of the file
* @param {string} ROOT_DIR - the root dir of Etherpad
*/
const compressCSS = async (filename, ROOT_DIR) => {
const absPath = path.resolve(ROOT_DIR, filename);
try {
const basePath = path.dirname(absPath);
const output = await new CleanCSS({
rebase: true,
rebaseTo: basePath,
}).minify([absPath]);
return output.styles;
const file = await fsp.readFile(absPath, 'utf8');
let { code } = lightminify.transform({
errorRecovery: true,
filename: basePath,
minify: true,
code: Buffer.from(file, 'utf8')
});
return code.toString();
} catch (error) {
// 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}): ${JSON.stringify(error)}`);
return await fsp.readFile(absPath, 'utf8');
}
};
Threads.expose({
compressJS,
expose({
compressJS: compressJS,
compressCSS,
});