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 mime from 'mime-types';
import log4js from 'log4js'; import log4js from 'log4js';
const sanitizePathname = require('./sanitizePathname'); const sanitizePathname = require('./sanitizePathname');
const logger = log4js.getLogger('Minify'); const logger = log4js.getLogger('Minify');
const ROOT_DIR = path.join(settings.root, 'src/static/'); 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 requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`;
const getFileCompressed = async (filename: string, contentType: string) => { 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); let content = await getFile(filename);
if (!content || !settings.minify) { if (!content || !settings.minify) {
return content; return content;
@ -281,10 +291,9 @@ const getFileCompressed = async (filename: string, contentType: string) => {
try { try {
logger.info('Compress JS file %s.', filename); logger.info('Compress JS file %s.', filename);
content = content.toString(); const compressResult = compressJS(ensureBuffer(content).toString());
const compressResult = compressJS(content);
content = compressResult.code.toString(); // Convert content obj code to string return compressResult.code.toString(); // Convert content obj code to string
} catch (error) { } catch (error) {
console.error('getFile() returned an error in ' + console.error('getFile() returned an error in ' +
`getFileCompressed(${filename}, ${contentType}): ${error}`); `getFileCompressed(${filename}, ${contentType}): ${error}`);
@ -293,8 +302,7 @@ const getFileCompressed = async (filename: string, contentType: string) => {
} else if (contentType === 'text/css') { } else if (contentType === 'text/css') {
try { try {
logger.info('Compress CSS file %s.', filename); logger.info('Compress CSS file %s.', filename);
content = compressCSS(filename, ROOT_DIR, Buffer.from(content)).toString(); return compressCSS(filename, ROOT_DIR, ensureBuffer(content));
return content
} catch (error) { } catch (error) {
console.error(`CleanCSS.minify() returned an error on ${filename}: ${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 path from 'path';
import {minifySync} from "@swc/core"; import {minifySync} from "@swc/core";
import lightminify from 'lightningcss'; 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) => { export const compressCSS = (filename: string, ROOT_DIR: string, content: Buffer) => {
const absPath = path.resolve(ROOT_DIR, filename); const absPath = path.resolve(ROOT_DIR, filename);
try { try {
const basePath = path.dirname(absPath);
let { code } = lightminify.transform({ let { code } = lightminify.transform({
filename: absPath, filename: absPath,
minify: true, minify: true,
code: content code: content
}); });
return code; return code.toString();
} catch (error) { } catch (error) {
// on error, just yield the un-minified original, but write a log message // 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}): ${error}`);