refactor clean-css to use builtin promise

promise-clean-css
webzwo0i 2020-08-25 14:33:11 +02:00
parent 517fc88c54
commit 7d34c2c821
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.
const LIBRARY_PREFIX = 'ep_etherpad-lite/static/js';
exports.tar = {};
/**
* Prefix a path with `LIBRARY_PREFIX`
* If path starts with '$' it is not prefixed
*
* @param {string} path
* @returns {string}
*/
const prefixLocalLibraryPath = (path) => {
if (path.charAt(0) === '$') {
return path.slice(1);
@ -134,8 +142,7 @@ const requestURIs = (locations, method, headers, callback) => {
/**
* creates the minifed javascript for the given minified name
* @param req the Express request
* @param res the Express response
*
*/
const minify = (req, res) => {
let filename = req.params.filename;
@ -349,6 +356,11 @@ const _requireLastModified = new Date();
const requireLastModified = () => _requireLastModified.toUTCString();
const requireDefinition = () => `var require = ${RequireKernel.kernelSource};\n`;
/**
* @param {string} filename
* @param {string} contentType
* @param {Function} callback
*/
const getFileCompressed = (filename, contentType, callback) => {
getFile(filename, (error, content) => {
if (error || !content || !settings.minify) {
@ -379,11 +391,11 @@ const getFileCompressed = (filename, contentType, callback) => {
logger.info('Compress CSS file %s.', filename);
content = await compressCSS(filename, ROOT_DIR);
callback(null, content.styles);
} catch (error) {
console.error(`CleanCSS.minify() returned an error on ${filename}: ${error}`);
callback(null, content)
}
callback(null, content);
});
} else {
callback(null, content);

View File

@ -7,56 +7,51 @@ const Terser = require('terser');
const path = require('path');
const Threads = require('threads');
/**
* Returns content compressed with Terser
*
* @param {string} content javascript to be compressed
* @returns {MinifyOutput} compressed javascript
*/
function compressJS(content) {
return Terser.minify(content);
}
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);
/*
* Changes done to migrate CleanCSS 3.x -> 4.x:
*
* 1. Rework the rebase logic, because the API was simplified (but we have
* less control now). See:
* https://github.com/jakubpawlowicz/clean-css/blob/08f3a74925524d30bbe7ac450979de0a8a9e54b2/README.md#important-40-breaking-changes
*
* EXAMPLE:
* The URLs contained in a CSS file (including all the stylesheets
* imported by it) residing on disk at:
* /home/muxator/etherpad/src/static/css/pad.css
*
* Will be rewritten rebasing them to:
* /home/muxator/etherpad/src/static/css
*
* 2. CleanCSS.minify() can either receive a string containing the CSS, or
* an array of strings. In that case each array element is interpreted as
* an absolute local path from which the CSS file is read.
*
* In version 4.x, CleanCSS API was simplified, eliminating the
* relativeTo parameter, and thus we cannot use our already loaded
* "content" argument, but we have to wrap the absolute path to the CSS
* in an array and ask the library to read it by itself.
*/
/*
* Changes done to migrate CleanCSS 3.x -> 4.x:
*
* 1. Rework the rebase logic, because the API was simplified (but we have
* less control now). See:
* https://github.com/jakubpawlowicz/clean-css/blob/08f3a74925524d30bbe7ac450979de0a8a9e54b2/README.md#important-40-breaking-changes
*
* EXAMPLE:
* The URLs contained in a CSS file (including all the stylesheets
* imported by it) residing on disk at:
* /home/muxator/etherpad/src/static/css/pad.css
*
* Will be rewritten rebasing them to:
* /home/muxator/etherpad/src/static/css
*
* 2. CleanCSS.minify() can either receive a string containing the CSS, or
* an array of strings. In that case each array element is interpreted as
* an absolute local path from which the CSS file is read.
*
* In version 4.x, CleanCSS API was simplified, eliminating the
* relativeTo parameter, and thus we cannot use our already loaded
* "content" argument, but we have to wrap the absolute path to the CSS
* in an array and ask the library to read it by itself.
*/
const basePath = path.dirname(absPath);
const basePath = path.dirname(absPath);
new CleanCSS({
rebase: true,
rebaseTo: basePath,
}).minify([absPath], (errors, minified) => {
if (errors) return rej(errors);
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);
}
});
return new CleanCSS({
rebase: true,
rebaseTo: basePath,
returnPromise: true
}).minify([absPath])
}
Threads.expose({

View File

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