Created
February 18, 2012 17:20
-
-
Save ktmud/1860262 to your computer and use it in GitHub Desktop.
inline compressed static files for express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var conf = central.conf; | |
var reg_val = /#{([^}]+)}/g; | |
module.exports = function(app) { | |
app.helpers({ | |
DEBUG: conf.debug | |
}); | |
app.dynamicHelpers({ | |
// inline static | |
istatic: function(req, res) { | |
return function() { | |
var locals = this; | |
var str = central.utils.istatic.apply(central.utils, arguments) || ''; | |
// replace inlined arguments | |
if (str.indexOf('#{') > -1) { | |
str = str.replace(reg_val, function(p0, p1) { | |
// look like dangerous... | |
var fn = new Function('locals', 'with (locals) { return ' + p1 + '; }'); | |
return fn(locals); | |
}); | |
} | |
return str; | |
}; | |
} | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var less = require('less'); | |
var uglifycss = require('uglifycss'); | |
var jsp = require('uglify-js').parser; | |
var pro = require('uglify-js').uglify; | |
var central = global.central = { lazylid: {}, cwd: process.cwd(), conf: { debug: false; } }; | |
module.exports = { | |
uglify: function uglify(str, opt) { | |
if (global.central && central.conf.debug) return str; | |
var ast = jsp.parse(str); // 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, opt); // compressed code here | |
}, | |
uglifycss: function uglifycss(str) { | |
return uglifycss.processString(str); | |
}, | |
// inline static files | |
istatic: function istatic(path, forceReload) { | |
var ft = path.split('.').slice(-1)[0]; | |
var root = central.cwd + (path[0] == '/' ? '' : '/public/'); | |
path = root + path; | |
var str = central.lazylib[path]; | |
var notDebug = !central.conf.debug; | |
if (!str || forceReload) { | |
str = fs.readFileSync(path, 'utf-8'); | |
switch (ft) { | |
case 'js': | |
// if not debug, uglify the js | |
if (notDebug) str = module.exports.uglify(str); | |
break; | |
case 'less': | |
less.render(str, function(err, str) { | |
if (notDebug) str = uglifystr(str); | |
if (!err) central.lazylib[path] = str; | |
}); | |
// better simple than none | |
// once the render is done, we can safely return good css | |
break; | |
case 'css': | |
if (notDebug) str = module.exports.uglifycss(str); | |
default: | |
// return the original str | |
} | |
central.lazylib[path] = str; | |
} | |
return str; | |
} | |
} | |
central.utils = module.exports; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment