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.
pull/3053/head
Dan Bornstein 2016-09-09 12:32:24 -07:00
parent 7dd252f763
commit a5a7ebea3d
2 changed files with 31 additions and 24 deletions

View File

@ -371,21 +371,19 @@ function requireDefinition() {
function getFileCompressed(filename, contentType, callback) { function getFileCompressed(filename, contentType, callback) {
getFile(filename, function (error, content) { getFile(filename, function (error, content) {
if (error || !content) { if (error || !content || !settings.minify) {
callback(error, content); callback(error, content);
} else { } else if (contentType == 'text/javascript') {
if (settings.minify) { try {
if (contentType == 'text/javascript') { content = compressJS(content);
try { } catch (error) {
content = compressJS([content]); // silence
} catch (error) {
// silence
}
} else if (contentType == 'text/css') {
content = compressCSS([content]);
}
} }
callback(null, content); 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(content); // parse code and get the initial AST
var ast = jsp.parse(complete); // parse code and get the initial AST
ast = pro.ast_mangle(ast); // get a new AST with mangled names ast = pro.ast_mangle(ast); // get a new AST with mangled names
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
return pro.gen_code(ast); // compressed code here return pro.gen_code(ast); // compressed code here
} }
function compressCSS(values) function compressCSS(filename, content, callback)
{ {
var complete = values.join("\n"); try {
var minimized = new CleanCSS().minify(complete).styles; new CleanCSS({relativeTo: ROOT_DIR}).minify(content, function (errors, minified) {
return minimized; 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; exports.minify = minify;

View File

@ -22,7 +22,7 @@
"express-session" : "1.13.0", "express-session" : "1.13.0",
"cookie-parser" : "1.3.4", "cookie-parser" : "1.3.4",
"async" : "0.9.0", "async" : "0.9.0",
"clean-css" : "3.4.12", "clean-css" : "3.4.19",
"uglify-js" : "2.6.2", "uglify-js" : "2.6.2",
"formidable" : "1.0.17", "formidable" : "1.0.17",
"log4js" : "0.6.35", "log4js" : "0.6.35",