Skip to content

Commit

Permalink
less support
Browse files Browse the repository at this point in the history
  • Loading branch information
jairajs89 committed Mar 30, 2013
1 parent b62d834 commit c46dd15
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)", "author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
"name": "zerver", "name": "zerver",
"description": "client-integrated webapp server", "description": "client-integrated webapp server",
"version": "0.8.7", "version": "0.9.0",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/jairajs89/zerver.git" "url": "git://github.com/jairajs89/zerver.git"
Expand All @@ -15,6 +15,7 @@
"mime" : "1.2.7" , "mime" : "1.2.7" ,
"uglify-js" : "1.3.4" , "uglify-js" : "1.3.4" ,
"clean-css" : "0.8.3" , "clean-css" : "0.8.3" ,
"less" : "1.3.3" ,
"stalker" : "0.0.20" , "stalker" : "0.0.20" ,
"findit" : "0.1.2" , "findit" : "0.1.2" ,
"socket.io" : "0.9.11" "socket.io" : "0.9.11"
Expand Down
90 changes: 59 additions & 31 deletions zerver.js
@@ -1,6 +1,7 @@
/* Imports and static vars */ /* Imports and static vars */


var cleanCSS = require('clean-css'), var cleanCSS = require('clean-css'),
less = require('less'),
fs = require('fs' ), fs = require('fs' ),
http = require('http'), http = require('http'),
mime = require('mime'), mime = require('mime'),
Expand Down Expand Up @@ -28,6 +29,7 @@ var ROOT_DIR = process.cwd(),
CONCAT_MATCH = /\<\!\-\-\s*zerver\:(\S+)\s*\-\-\>((\s|\S)*?)\<\!\-\-\s*\/zerver\s*\-\-\>/g, CONCAT_MATCH = /\<\!\-\-\s*zerver\:(\S+)\s*\-\-\>((\s|\S)*?)\<\!\-\-\s*\/zerver\s*\-\-\>/g,
SCRIPT_MATCH = /\<script(?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s+src\=[\'\"]\s*([^\>]+)\s*[\'\"](?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s*\>\<\/script\>/g, SCRIPT_MATCH = /\<script(?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s+src\=[\'\"]\s*([^\>]+)\s*[\'\"](?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s*\>\<\/script\>/g,
STYLES_MATCH = /\<link(?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s+href\=[\'\"]\s*([^\>]+)\s*[\'\"](?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s*\/?\>/g, STYLES_MATCH = /\<link(?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s+href\=[\'\"]\s*([^\>]+)\s*[\'\"](?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s*\/?\>/g,
IS_LESS = /^.*\.less$/,
REQUEST_TIMEOUT = 25 * 1000, REQUEST_TIMEOUT = 25 * 1000,
CONCAT_FILES = false, CONCAT_FILES = false,
GZIP_ENABLED = false, GZIP_ENABLED = false,
Expand Down Expand Up @@ -541,7 +543,7 @@ function validateManifest (data, pathname) {
} }


function inlineImages (type, data, pathname, callback) { function inlineImages (type, data, pathname, callback) {
if (!INLINING_ENABLED || DEBUG || (type !== 'text/css') || (typeof data !== 'string')) { if (!INLINING_ENABLED || DEBUG || ((type !== 'text/css') && (type !== 'text/less')) || (typeof data !== 'string')) {
callback(data); callback(data);
return; return;
} }
Expand Down Expand Up @@ -582,7 +584,7 @@ function inlineImages (type, data, pathname, callback) {
} }




var mimeType = mime.lookup(fileName), var mimeType = lookupMime(fileName),
dataURL = 'data:'+mimeType+';base64,'+fileData; dataURL = 'data:'+mimeType+';base64,'+fileData;


return 'url(' + dataURL + ')'; return 'url(' + dataURL + ')';
Expand All @@ -592,35 +594,53 @@ function inlineImages (type, data, pathname, callback) {
} }


function compileOutput (type, data, callback) { function compileOutput (type, data, callback) {
if (!COMPILATION_ENABLED || DEBUG) { compileLess(type, data, function (type, data) {
callback(data); if (!COMPILATION_ENABLED || DEBUG) {
return; callback(type, data);
} return;
}


var code; var code;


switch (type) { switch (type) {
case 'application/javascript': case 'application/javascript':
case 'text/javascript': case 'text/javascript':
data = data.replace(DEBUG_LINES, ''); data = data.replace(DEBUG_LINES, '');
try { try {
var ast = uglify.parser.parse(data); var ast = uglify.parser.parse(data);
ast = uglify.uglify.ast_mangle(ast); ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast); ast = uglify.uglify.ast_squeeze(ast);
code = uglify.uglify.gen_code(ast); code = uglify.uglify.gen_code(ast);
} }
catch (err) {} catch (err) {}
break; break;


case 'text/css': case 'text/css':
try { try {
code = cleanCSS.process(data); code = cleanCSS.process(data);
} }
catch (err) {} catch (err) {}
break; break;
}

callback(type, code || data);
});
}

function compileLess (type, data, callback) {
if (type !== 'text/less') {
callback(type, data);
return;
} }


callback(code || data); less.render(data, function(err, css) {
if (err) {
callback(type, data);
}
else {
callback('text/css', css);
}
});
} }


function setupGZipOutput (type, data, headers, callback) { function setupGZipOutput (type, data, headers, callback) {
Expand Down Expand Up @@ -692,12 +712,11 @@ function respond (handler, status, type, data, headers) {
} }


function respondBinary (handler, status, type, data, headers) { function respondBinary (handler, status, type, data, headers) {
headers['Content-Type'] = type;

prepareConcatFiles(type, data, handler.pathname, function (data) { prepareConcatFiles(type, data, handler.pathname, function (data) {
inlineImages(type, data, handler.pathname, function (data) { inlineImages(type, data, handler.pathname, function (data) {
compileOutput(type, data, function (data) { compileOutput(type, data, function (type, data) {
setupGZipOutput(type, data, headers, function (data, headers) { setupGZipOutput(type, data, headers, function (data, headers) {
headers['Content-Type'] = type;
finishResponse(handler, status, headers, data, true); finishResponse(handler, status, headers, data, true);
}); });
}); });
Expand Down Expand Up @@ -758,7 +777,7 @@ function fileRequest (handler, fileName) {
return; return;
} }


respondBinary(handler, 200, mime.lookup(fileName), file, { respondBinary(handler, 200, lookupMime(fileName), file, {
'Cache-Control' : CACHE_CONTROL 'Cache-Control' : CACHE_CONTROL
}); });
}); });
Expand Down Expand Up @@ -832,7 +851,7 @@ function concatRequest (handler, pathname) {
} }
} }
else { else {
respondBinary(handler, 200, mime.lookup(pathname), file, { respondBinary(handler, 200, lookupMime(pathname), file, {
'Cache-Control' : CACHE_CONTROL 'Cache-Control' : CACHE_CONTROL
}); });
} }
Expand Down Expand Up @@ -1235,6 +1254,15 @@ function addCORSHeaders (headers, methods, host) {
return headers; return headers;
} }


function lookupMime (fileName) {
if ( IS_LESS.test(fileName) ) {
return 'text/less';
}
else {
return mime.lookup(fileName);
}
}





function setupAutoRefresh () { function setupAutoRefresh () {
Expand Down

0 comments on commit c46dd15

Please sign in to comment.