summaryrefslogtreecommitdiff
path: root/src/node/utils
diff options
context:
space:
mode:
authorDan Bornstein <danfuzz@milk.com>2016-09-09 12:32:24 -0700
committerDan Bornstein <danfuzz@milk.com>2016-09-09 12:32:24 -0700
commita5a7ebea3dab9e14240a5258ae5477e2f2c00e2a (patch)
tree8eec815d2c7784fc5dd8ddd408ca723bc11af89f /src/node/utils
parent7dd252f76305fa76f5ab9b28d1e6dc6bbcce0379 (diff)
downloadetherpad-lite-a5a7ebea3dab9e14240a5258ae5477e2f2c00e2a.zip
Handle `@import` during CSS minification.
This meant plumbing a callback through to `compressCSS()`, which meant that I had to alter the innards of `getFileCompressed()`. I tried to leave that function looking more understandable than when I found it; for example, I flattened out the nested `if`. I went ahead and upgraded the version of `clean-css` while I was in the territory.
Diffstat (limited to 'src/node/utils')
-rw-r--r--src/node/utils/Minify.js53
1 files changed, 30 insertions, 23 deletions
diff --git a/src/node/utils/Minify.js b/src/node/utils/Minify.js
index ee8f5f45..101dc97a 100644
--- a/src/node/utils/Minify.js
+++ b/src/node/utils/Minify.js
@@ -1,7 +1,7 @@
/**
- * This Module manages all /minified/* requests. It controls the
- * minification && compression of Javascript and CSS.
- */
+ * This Module manages all /minified/* requests. It controls the
+ * minification && compression of Javascript and CSS.
+ */
/*
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
@@ -151,7 +151,7 @@ function minify(req, res, next)
} else {
res.writeHead(404, {});
res.end();
- return;
+ return;
}
/* Handle static files for plugins/libraries:
@@ -371,21 +371,19 @@ function requireDefinition() {
function getFileCompressed(filename, contentType, callback) {
getFile(filename, function (error, content) {
- if (error || !content) {
+ if (error || !content || !settings.minify) {
callback(error, content);
- } else {
- if (settings.minify) {
- if (contentType == 'text/javascript') {
- try {
- content = compressJS([content]);
- } catch (error) {
- // silence
- }
- } else if (contentType == 'text/css') {
- content = compressCSS([content]);
- }
+ } else if (contentType == 'text/javascript') {
+ try {
+ content = compressJS(content);
+ } catch (error) {
+ // silence
}
callback(null, content);
+ } else if (contentType == 'text/css') {
+ compressCSS(filename, content, callback);
+ } else {
+ callback(null, content);
}
});
}
@@ -400,20 +398,29 @@ function getFile(filename, callback) {
}
}
-function compressJS(values)
+function compressJS(content)
{
- var complete = values.join("\n");
- var ast = jsp.parse(complete); // parse code and get the initial AST
+ var ast = jsp.parse(content); // parse code and get the initial AST
ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
return pro.gen_code(ast); // compressed code here
}
-function compressCSS(values)
+function compressCSS(filename, content, callback)
{
- var complete = values.join("\n");
- var minimized = new CleanCSS().minify(complete).styles;
- return minimized;
+ try {
+ new CleanCSS({relativeTo: ROOT_DIR}).minify(content, function (errors, minified) {
+ if (errors) {
+ // On error, just yield the un-minified original.
+ callback(null, content);
+ } else {
+ callback(null, minified.styles);
+ }
+ });
+ } catch (error) {
+ // On error, just yield the un-minified original.
+ callback(null, content);
+ }
}
exports.minify = minify;