Permalink
Browse files

less support

  • Loading branch information...
1 parent b62d834 commit c46dd153c1b5c5794ce4a274c1fcc8c47fe0435e @jairajs89 committed Mar 30, 2013
Showing with 61 additions and 32 deletions.
  1. +2 −1 package.json
  2. +59 −31 zerver.js
View
@@ -2,7 +2,7 @@
"author": "Jairaj Sethi <j@jairaj.org> (http://jairaj.org/)",
"name": "zerver",
"description": "client-integrated webapp server",
- "version": "0.8.7",
+ "version": "0.9.0",
"repository": {
"type": "git",
"url": "git://github.com/jairajs89/zerver.git"
@@ -15,6 +15,7 @@
"mime" : "1.2.7" ,
"uglify-js" : "1.3.4" ,
"clean-css" : "0.8.3" ,
+ "less" : "1.3.3" ,
"stalker" : "0.0.20" ,
"findit" : "0.1.2" ,
"socket.io" : "0.9.11"
View
@@ -1,6 +1,7 @@
/* Imports and static vars */
var cleanCSS = require('clean-css'),
+ less = require('less'),
fs = require('fs' ),
http = require('http'),
mime = require('mime'),
@@ -28,6 +29,7 @@ var ROOT_DIR = process.cwd(),
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,
STYLES_MATCH = /\<link(?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s+href\=[\'\"]\s*([^\>]+)\s*[\'\"](?:\s+\w+\=[\'\"][^\>]+[\'\"])*\s*\/?\>/g,
+ IS_LESS = /^.*\.less$/,
REQUEST_TIMEOUT = 25 * 1000,
CONCAT_FILES = false,
GZIP_ENABLED = false,
@@ -541,7 +543,7 @@ function validateManifest (data, pathname) {
}
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);
return;
}
@@ -582,7 +584,7 @@ function inlineImages (type, data, pathname, callback) {
}
- var mimeType = mime.lookup(fileName),
+ var mimeType = lookupMime(fileName),
dataURL = 'data:'+mimeType+';base64,'+fileData;
return 'url(' + dataURL + ')';
@@ -592,35 +594,53 @@ function inlineImages (type, data, pathname, callback) {
}
function compileOutput (type, data, callback) {
- if (!COMPILATION_ENABLED || DEBUG) {
- callback(data);
- return;
- }
+ compileLess(type, data, function (type, data) {
+ if (!COMPILATION_ENABLED || DEBUG) {
+ callback(type, data);
+ return;
+ }
- var code;
+ var code;
- switch (type) {
- case 'application/javascript':
- case 'text/javascript':
- data = data.replace(DEBUG_LINES, '');
- try {
- var ast = uglify.parser.parse(data);
- ast = uglify.uglify.ast_mangle(ast);
- ast = uglify.uglify.ast_squeeze(ast);
- code = uglify.uglify.gen_code(ast);
- }
- catch (err) {}
- break;
+ switch (type) {
+ case 'application/javascript':
+ case 'text/javascript':
+ data = data.replace(DEBUG_LINES, '');
+ try {
+ var ast = uglify.parser.parse(data);
+ ast = uglify.uglify.ast_mangle(ast);
+ ast = uglify.uglify.ast_squeeze(ast);
+ code = uglify.uglify.gen_code(ast);
+ }
+ catch (err) {}
+ break;
- case 'text/css':
- try {
- code = cleanCSS.process(data);
- }
- catch (err) {}
- break;
+ case 'text/css':
+ try {
+ code = cleanCSS.process(data);
+ }
+ catch (err) {}
+ 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) {
@@ -692,12 +712,11 @@ function respond (handler, status, type, data, headers) {
}
function respondBinary (handler, status, type, data, headers) {
- headers['Content-Type'] = type;
-
prepareConcatFiles(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) {
+ headers['Content-Type'] = type;
finishResponse(handler, status, headers, data, true);
});
});
@@ -758,7 +777,7 @@ function fileRequest (handler, fileName) {
return;
}
- respondBinary(handler, 200, mime.lookup(fileName), file, {
+ respondBinary(handler, 200, lookupMime(fileName), file, {
'Cache-Control' : CACHE_CONTROL
});
});
@@ -832,7 +851,7 @@ function concatRequest (handler, pathname) {
}
}
else {
- respondBinary(handler, 200, mime.lookup(pathname), file, {
+ respondBinary(handler, 200, lookupMime(pathname), file, {
'Cache-Control' : CACHE_CONTROL
});
}
@@ -1235,6 +1254,15 @@ function addCORSHeaders (headers, methods, host) {
return headers;
}
+function lookupMime (fileName) {
+ if ( IS_LESS.test(fileName) ) {
+ return 'text/less';
+ }
+ else {
+ return mime.lookup(fileName);
+ }
+}
+
function setupAutoRefresh () {

0 comments on commit c46dd15

Please sign in to comment.