mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-19 14:13:34 +01:00
refactor clean-css to use builtin promise
This commit is contained in:
parent
517fc88c54
commit
7d34c2c821
3 changed files with 56 additions and 46 deletions
|
@ -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);
|
||||
|
|
|
@ -7,13 +7,17 @@ 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);
|
||||
|
||||
/*
|
||||
|
@ -43,20 +47,11 @@ function compressCSS(filename, ROOT_DIR) {
|
|||
|
||||
const basePath = path.dirname(absPath);
|
||||
|
||||
new CleanCSS({
|
||||
return 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);
|
||||
}
|
||||
});
|
||||
returnPromise: true
|
||||
}).minify([absPath])
|
||||
}
|
||||
|
||||
Threads.expose({
|
||||
|
|
|
@ -77,6 +77,9 @@ if (_crypto) {
|
|||
should replace this.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class
|
||||
*/
|
||||
function CachingMiddleware() {
|
||||
}
|
||||
CachingMiddleware.prototype = new function () {
|
||||
|
|
Loading…
Reference in a new issue