refactor clean-css to use builtin promise
parent
517fc88c54
commit
7d34c2c821
|
@ -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);
|
||||||
|
|
|
@ -7,56 +7,51 @@ 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) => {
|
const absPath = path.join(ROOT_DIR, filename);
|
||||||
try {
|
|
||||||
const absPath = path.join(ROOT_DIR, filename);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Changes done to migrate CleanCSS 3.x -> 4.x:
|
* Changes done to migrate CleanCSS 3.x -> 4.x:
|
||||||
*
|
*
|
||||||
* 1. Rework the rebase logic, because the API was simplified (but we have
|
* 1. Rework the rebase logic, because the API was simplified (but we have
|
||||||
* less control now). See:
|
* less control now). See:
|
||||||
* https://github.com/jakubpawlowicz/clean-css/blob/08f3a74925524d30bbe7ac450979de0a8a9e54b2/README.md#important-40-breaking-changes
|
* https://github.com/jakubpawlowicz/clean-css/blob/08f3a74925524d30bbe7ac450979de0a8a9e54b2/README.md#important-40-breaking-changes
|
||||||
*
|
*
|
||||||
* EXAMPLE:
|
* EXAMPLE:
|
||||||
* The URLs contained in a CSS file (including all the stylesheets
|
* The URLs contained in a CSS file (including all the stylesheets
|
||||||
* imported by it) residing on disk at:
|
* imported by it) residing on disk at:
|
||||||
* /home/muxator/etherpad/src/static/css/pad.css
|
* /home/muxator/etherpad/src/static/css/pad.css
|
||||||
*
|
*
|
||||||
* Will be rewritten rebasing them to:
|
* Will be rewritten rebasing them to:
|
||||||
* /home/muxator/etherpad/src/static/css
|
* /home/muxator/etherpad/src/static/css
|
||||||
*
|
*
|
||||||
* 2. CleanCSS.minify() can either receive a string containing the CSS, or
|
* 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 array of strings. In that case each array element is interpreted as
|
||||||
* an absolute local path from which the CSS file is read.
|
* an absolute local path from which the CSS file is read.
|
||||||
*
|
*
|
||||||
* In version 4.x, CleanCSS API was simplified, eliminating the
|
* In version 4.x, CleanCSS API was simplified, eliminating the
|
||||||
* relativeTo parameter, and thus we cannot use our already loaded
|
* relativeTo parameter, and thus we cannot use our already loaded
|
||||||
* "content" argument, but we have to wrap the absolute path to the CSS
|
* "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.
|
* in an array and ask the library to read it by itself.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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({
|
||||||
|
|
|
@ -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 () {
|
||||||
|
|
Loading…
Reference in New Issue