This commit is contained in:
SamTV12345 2024-03-21 11:25:55 +01:00
parent dc5259ca5b
commit 5d8c8f9092
2 changed files with 15 additions and 9 deletions

View file

@ -32,7 +32,6 @@ const RequireKernel = require('etherpad-require-kernel');
import mime from 'mime-types';
import log4js from 'log4js';
const sanitizePathname = require('./sanitizePathname');
const logger = log4js.getLogger('Minify');
const ROOT_DIR = path.join(settings.root, 'src/static/');
@ -274,6 +273,17 @@ const _requireLastModified = new Date();
const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`;
const getFileCompressed = async (filename: string, contentType: string) => {
const ensureBuffer = (input: string|Buffer|object)=> {
if (Buffer.isBuffer(input)) {
return input;
} else if (typeof input === 'string') {
return Buffer.from(input);
} else {
throw new Error('Input should be a Buffer or a string');
}
}
let content = await getFile(filename);
if (!content || !settings.minify) {
return content;
@ -281,10 +291,9 @@ const getFileCompressed = async (filename: string, contentType: string) => {
try {
logger.info('Compress JS file %s.', filename);
content = content.toString();
const compressResult = compressJS(content);
const compressResult = compressJS(ensureBuffer(content).toString());
content = compressResult.code.toString(); // Convert content obj code to string
return compressResult.code.toString(); // Convert content obj code to string
} catch (error) {
console.error('getFile() returned an error in ' +
`getFileCompressed(${filename}, ${contentType}): ${error}`);
@ -293,8 +302,7 @@ const getFileCompressed = async (filename: string, contentType: string) => {
} else if (contentType === 'text/css') {
try {
logger.info('Compress CSS file %s.', filename);
content = compressCSS(filename, ROOT_DIR, Buffer.from(content)).toString();
return content
return compressCSS(filename, ROOT_DIR, ensureBuffer(content));
} catch (error) {
console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`);
}

View file

@ -4,7 +4,6 @@
*/
import {promises as fsp} from "fs";
import path from 'path';
import {minifySync} from "@swc/core";
import lightminify from 'lightningcss';
@ -14,13 +13,12 @@ export const compressJS = (content: string) => minifySync(content);
export const compressCSS = (filename: string, ROOT_DIR: string, content: Buffer) => {
const absPath = path.resolve(ROOT_DIR, filename);
try {
const basePath = path.dirname(absPath);
let { code } = lightminify.transform({
filename: absPath,
minify: true,
code: content
});
return code;
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}`);