Minify serves all static assets.

pull/468/head
Chad Weider 2012-02-14 13:23:53 -08:00
parent df9e29087f
commit e0930005c3
2 changed files with 47 additions and 30 deletions

View File

@ -144,13 +144,7 @@ async.waterfall([
//serve static files //serve static files
var assetCache = new CachingMiddleware; var assetCache = new CachingMiddleware;
app.all('/static/js/:filename', assetCache.handle, minify.minifyJS); app.all('/static/:filename(*)', assetCache.handle, minify.minify);
app.get('/static/*', function(req, res)
{
var filePath = path.normalize(__dirname + "/.." +
req.url.replace(/\.\./g, '').split("?")[0]);
res.sendfile(filePath, { maxAge: exports.maxAge });
});
//serve minified files //serve minified files
var jsServer = new (Yajsml.Server)({ var jsServer = new (Yajsml.Server)({

View File

@ -30,9 +30,7 @@ var path = require('path');
var RequireKernel = require('require-kernel'); var RequireKernel = require('require-kernel');
var server = require('../server'); var server = require('../server');
var ROOT_DIR = path.normalize(__dirname + "/../" ); var ROOT_DIR = path.normalize(__dirname + "/../../static/");
var JS_DIR = path.normalize(ROOT_DIR + '../static/js/');
var CSS_DIR = ROOT_DIR + '../static/css/';
var TAR_PATH = path.join(__dirname, 'tar.json'); var TAR_PATH = path.join(__dirname, 'tar.json');
var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8')); var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8'));
@ -49,21 +47,40 @@ for (var key in tar) {
* @param req the Express request * @param req the Express request
* @param res the Express response * @param res the Express response
*/ */
exports.minifyJS = function(req, res, next) exports.minify = function(req, res, next)
{ {
var filename = req.params['filename']; var filename = req.params['filename'];
// No relative paths, especially if they may go up the file hierarchy. // No relative paths, especially if they may go up the file hierarchy.
filename = path.normalize(path.join(JS_DIR, filename)); filename = path.normalize(path.join(ROOT_DIR, filename));
if (filename.indexOf(JS_DIR) == 0) { if (filename.indexOf(ROOT_DIR) == 0) {
filename = filename.slice(JS_DIR.length); filename = filename.slice(ROOT_DIR.length);
} else { } else {
res.writeHead(404, {}); res.writeHead(404, {});
res.end(); res.end();
return; return;
} }
res.header("Content-Type","text/javascript"); // What content type should this be?
// TODO: This should use a MIME module.
var contentType;
if (filename.match(/\.js$/)) {
contentType = "text/javascript";
} else if (filename.match(/\.css$/)) {
contentType = "text/css";
} else if (filename.match(/\.html$/)) {
contentType = "text/html";
} else if (filename.match(/\.txt$/)) {
contentType = "text/plain";
} else if (filename.match(/\.png$/)) {
contentType = "image/png";
} else if (filename.match(/\.gif$/)) {
contentType = "image/gif";
} else if (filename.match(/\.ico$/)) {
contentType = "image/x-icon";
} else {
contentType = "application/octet-stream";
}
statFile(filename, function (error, date, exists) { statFile(filename, function (error, date, exists) {
if (date) { if (date) {
@ -88,11 +105,13 @@ exports.minifyJS = function(req, res, next)
res.end(); res.end();
} else { } else {
if (req.method == 'HEAD') { if (req.method == 'HEAD') {
res.header("Content-Type", contentType);
res.writeHead(200, {}); res.writeHead(200, {});
res.end(); res.end();
} else if (req.method == 'GET') { } else if (req.method == 'GET') {
getFileCompressed(filename, function (error, content) { getFileCompressed(filename, contentType, function (error, content) {
if(ERR(error)) return; if(ERR(error)) return;
res.header("Content-Type", contentType);
res.writeHead(200, {}); res.writeHead(200, {});
res.write(content); res.write(content);
res.end(); res.end();
@ -107,7 +126,7 @@ exports.minifyJS = function(req, res, next)
// find all includes in ace.js and embed them. // find all includes in ace.js and embed them.
function getAceFile(callback) { function getAceFile(callback) {
fs.readFile(JS_DIR + 'ace.js', "utf8", function(err, data) { fs.readFile(ROOT_DIR + 'js/ace.js', "utf8", function(err, data) {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
// Find all includes in ace.js and embed them // Find all includes in ace.js and embed them
@ -173,18 +192,18 @@ function getAceFile(callback) {
} }
function statFile(filename, callback) { function statFile(filename, callback) {
if (filename == 'ace.js') { if (filename == 'js/ace.js') {
lastModifiedDateOfEverything(function (error, date) { lastModifiedDateOfEverything(function (error, date) {
callback(error, date, !error); callback(error, date, !error);
}); });
} else { } else {
fs.stat(JS_DIR + filename, function (error, stats) { fs.stat(ROOT_DIR + filename, function (error, stats) {
if (error) { if (error) {
if (error.code == "ENOENT") { // Stat the directory instead. if (error.code == "ENOENT") { // Stat the directory instead.
fs.stat(JS_DIR, function (error, stats) { fs.stat(path.dirname(ROOT_DIR + filename), function (error, stats) {
if (error) { if (error) {
callback(error); callback(error);
} else if (filename == 'require-kernel.js') { } else if (filename == 'js/require-kernel.js') {
callback(null, stats.mtime.getTime(), true); callback(null, stats.mtime.getTime(), true);
} else { } else {
callback(null, stats.mtime.getTime(), false); callback(null, stats.mtime.getTime(), false);
@ -200,7 +219,7 @@ function statFile(filename, callback) {
} }
} }
function lastModifiedDateOfEverything(callback) { function lastModifiedDateOfEverything(callback) {
var folders2check = [CSS_DIR, JS_DIR]; var folders2check = [ROOT_DIR + 'js/', ROOT_DIR + 'css/'];
var latestModification = 0; var latestModification = 0;
//go trough this two folders //go trough this two folders
async.forEach(folders2check, function(path, callback) async.forEach(folders2check, function(path, callback)
@ -243,17 +262,21 @@ function requireDefinition() {
return 'var require = ' + RequireKernel.kernelSource + ';\n'; return 'var require = ' + RequireKernel.kernelSource + ';\n';
} }
function getFileCompressed(filename, callback) { function getFileCompressed(filename, contentType, callback) {
getFile(filename, function (error, content) { getFile(filename, function (error, content) {
if (error || !content) { if (error || !content) {
callback(error, content); callback(error, content);
} else { } else {
if (settings.minify) { if (settings.minify) {
if (contentType == 'text/javascript') {
try { try {
content = compressJS([content]) content = compressJS([content]);
} catch (error) { } catch (error) {
// silence // silence
} }
} else if (contentType == 'text/css') {
content = compressCSS([content]);
}
} }
callback(null, content); callback(null, content);
} }
@ -261,12 +284,12 @@ function getFileCompressed(filename, callback) {
} }
function getFile(filename, callback) { function getFile(filename, callback) {
if (filename == 'ace.js') { if (filename == 'js/ace.js') {
getAceFile(callback); getAceFile(callback);
} else if (filename == 'require-kernel.js') { } else if (filename == 'js/require-kernel.js') {
callback(undefined, requireDefinition()); callback(undefined, requireDefinition());
} else { } else {
fs.readFile(JS_DIR + filename, "utf8", callback); fs.readFile(ROOT_DIR + filename, callback);
} }
} }