Compare commits

...

1 Commits

Author SHA1 Message Date
webzwo0i 7d34c2c821 refactor clean-css to use builtin promise 2021-01-21 23:57:24 +01:00
3 changed files with 56 additions and 46 deletions

View File

@ -53,6 +53,14 @@ const LIBRARY_WHITELIST = [
// Rewrite tar to include modules with no extensions and proper rooted paths. // Rewrite tar to include modules with no extensions and proper rooted paths.
const LIBRARY_PREFIX = 'ep_etherpad-lite/static/js'; const LIBRARY_PREFIX = 'ep_etherpad-lite/static/js';
exports.tar = {}; exports.tar = {};
/**
* Prefix a path with `LIBRARY_PREFIX`
* If path starts with '$' it is not prefixed
*
* @param {string} path
* @returns {string}
*/
const prefixLocalLibraryPath = (path) => { const prefixLocalLibraryPath = (path) => {
if (path.charAt(0) === '$') { if (path.charAt(0) === '$') {
return path.slice(1); return path.slice(1);
@ -134,8 +142,7 @@ const requestURIs = (locations, method, headers, callback) => {
/** /**
* creates the minifed javascript for the given minified name * creates the minifed javascript for the given minified name
* @param req the Express request *
* @param res the Express response
*/ */
const minify = (req, res) => { const minify = (req, res) => {
let filename = req.params.filename; let filename = req.params.filename;
@ -349,6 +356,11 @@ const _requireLastModified = new Date();
const requireLastModified = () => _requireLastModified.toUTCString(); const requireLastModified = () => _requireLastModified.toUTCString();
const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`; const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`;
/**
* @param {string} filename
* @param {string} contentType
* @param {Function} callback
*/
const getFileCompressed = (filename, contentType, callback) => { const getFileCompressed = (filename, contentType, callback) => {
getFile(filename, (error, content) => { getFile(filename, (error, content) => {
if (error || !content || !settings.minify) { if (error || !content || !settings.minify) {
@ -379,11 +391,11 @@ const getFileCompressed = (filename, contentType, callback) => {
logger.info('Compress CSS file %s.', filename); logger.info('Compress CSS file %s.', filename);
content = await compressCSS(filename, ROOT_DIR); content = await compressCSS(filename, ROOT_DIR);
callback(null, content.styles);
} catch (error) { } catch (error) {
console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`); console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`);
callback(null, content)
} }
callback(null, content);
}); });
} else { } else {
callback(null, content); callback(null, content);

View File

@ -7,13 +7,17 @@ const Terser = require('terser');
const path = require('path'); const path = require('path');
const Threads = require('threads'); const Threads = require('threads');
/**
* Returns content compressed with Terser
*
* @param {string} content javascript to be compressed
* @returns {MinifyOutput} compressed javascript
*/
function compressJS(content) { function compressJS(content) {
return Terser.minify(content); return Terser.minify(content);
} }
function compressCSS(filename, ROOT_DIR) { function compressCSS(filename, ROOT_DIR) {
return new Promise((res, rej) => {
try {
const absPath = path.join(ROOT_DIR, filename); const absPath = path.join(ROOT_DIR, filename);
/* /*
@ -43,20 +47,11 @@ function compressCSS(filename, ROOT_DIR) {
const basePath = path.dirname(absPath); const basePath = path.dirname(absPath);
new CleanCSS({ return new CleanCSS({
rebase: true, rebase: true,
rebaseTo: basePath, rebaseTo: basePath,
}).minify([absPath], (errors, minified) => { returnPromise: true
if (errors) return rej(errors); }).minify([absPath])
return res(minified.styles);
});
} 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);
}
});
} }
Threads.expose({ Threads.expose({

View File

@ -77,6 +77,9 @@ if (_crypto) {
should replace this. should replace this.
*/ */
/**
* @class
*/
function CachingMiddleware() { function CachingMiddleware() {
} }
CachingMiddleware.prototype = new function () { CachingMiddleware.prototype = new function () {