Use Yajsml to combine files.

Minify only constructs individual files and optionally compresses them with UglifyJS.
This commit is contained in:
Chad Weider 2012-01-29 03:30:31 -08:00
parent b661ef5960
commit dcc0740621
6 changed files with 61 additions and 37 deletions

View File

@ -32,6 +32,7 @@ var express = require('express');
var path = require('path'); var path = require('path');
var minify = require('./utils/Minify'); var minify = require('./utils/Minify');
var CachingMiddleware = require('./utils/caching_middleware'); var CachingMiddleware = require('./utils/caching_middleware');
var Yajsml = require('yajsml');
var formidable = require('formidable'); var formidable = require('formidable');
var apiHandler; var apiHandler;
var exportHandler; var exportHandler;
@ -147,6 +148,8 @@ async.waterfall([
res.write(minify.requireDefinition()); res.write(minify.requireDefinition());
res.end(); res.end();
}); });
var assetCache = new CachingMiddleware;
app.all('/static/js/:filename', assetCache.handle, minify.minifyJS);
app.get('/static/*', function(req, res) app.get('/static/*', function(req, res)
{ {
var filePath = path.normalize(__dirname + "/.." + var filePath = path.normalize(__dirname + "/.." +
@ -155,8 +158,18 @@ async.waterfall([
}); });
//serve minified files //serve minified files
var assetCache = new CachingMiddleware; var jsServer = new (Yajsml.Server)({
app.all('/minified/:filename', assetCache.handle, minify.minifyJS); rootPath: 'minified/'
, rootURI: 'http://' + settings.ip + ":" + settings.port + '/static/js/'
});
var StaticAssociator = Yajsml.associators.StaticAssociator;
var associations =
Yajsml.associators.associationsForSimpleMapping(minify.tar);
var associator = new StaticAssociator(associations);
jsServer.setAssociator(associator);
var assetCache_other = new CachingMiddleware;
app.all('/minified/:filename', assetCache_other.handle);
app.use(jsServer);
//checks for padAccess //checks for padAccess
function hasPadAccess(req, res, callback) function hasPadAccess(req, res, callback)

View File

@ -36,6 +36,14 @@ 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'));
exports.tar = {};
for (var key in tar) {
exports.tar['/' + key] =
tar[key].map(function (p) {return '/' + p}).concat(
tar[key].map(function (p) {return '/' + p.replace(/\.js$/, '')})
);
}
/** /**
* creates the minifed javascript for the given minified name * creates the minifed javascript for the given minified name
* @param req the Express request * @param req the Express request
@ -43,21 +51,7 @@ var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8'));
*/ */
exports.minifyJS = function(req, res, next) exports.minifyJS = function(req, res, next)
{ {
var jsFilename = req.params['filename']; var filename = req.params['filename'];
//choose the js files we need
var jsFiles = undefined;
if (Object.prototype.hasOwnProperty.call(tar, jsFilename)) {
jsFiles = tar[jsFilename];
_handle(req, res, jsFilename, jsFiles)
} else {
// Not in tar list, but try anyways, if it fails, pass to `next`.
jsFiles = [jsFilename];
_handle(req, res, jsFilename, jsFiles);
}
}
function _handle(req, res, jsFilename, jsFiles) {
res.header("Content-Type","text/javascript"); res.header("Content-Type","text/javascript");
lastModifiedDate(function (date) { lastModifiedDate(function (date) {
@ -70,7 +64,7 @@ function _handle(req, res, jsFilename, jsFiles) {
res.setHeader('cache-control', 'max-age=' + server.maxAge); res.setHeader('cache-control', 'max-age=' + server.maxAge);
} }
fs.stat(JS_DIR + jsFiles[0], function (error, stats) { fs.stat(JS_DIR + filename, function (error, stats) {
if (error) { if (error) {
if (error.code == "ENOENT") { if (error.code == "ENOENT") {
res.writeHead(404, {}); res.writeHead(404, {});
@ -90,17 +84,12 @@ function _handle(req, res, jsFilename, jsFiles) {
res.writeHead(200, {}); res.writeHead(200, {});
res.end(); res.end();
} else if (req.method == 'GET') { } else if (req.method == 'GET') {
res.writeHead(200, {}); getFileCompressed(filename, function (error, content) {
tarCode( if(ERR(error)) return;
jsFiles res.writeHead(200, {});
, function (content) { res.write(content);
res.write(content); res.end();
} });
, function (err) {
if(ERR(err)) return;
res.end();
}
);
} else { } else {
res.writeHead(405, {'allow': 'HEAD, GET'}); res.writeHead(405, {'allow': 'HEAD, GET'});
res.end(); res.end();
@ -222,15 +211,36 @@ function requireDefinition() {
return 'var require = ' + RequireKernel.kernelSource + ';\n'; return 'var require = ' + RequireKernel.kernelSource + ';\n';
} }
function getFileCompressed(filename, callback) {
getFile(filename, function (error, content) {
if (error || !content) {
callback(error, content);
} else {
if (settings.minify) {
try {
content = compressJS([content])
} catch (error) {
// silence
}
}
callback(null, content);
}
});
}
function getFile(filename, callback) {
if (filename == 'ace.js') {
getAceFile(callback);
} else {
fs.readFile(JS_DIR + filename, "utf8", callback);
}
}
function tarCode(jsFiles, write, callback) { function tarCode(jsFiles, write, callback) {
write('require.define({'); write('require.define({');
var initialEntry = true; var initialEntry = true;
async.forEach(jsFiles, function (filename, callback){ async.forEach(jsFiles, function (filename, callback){
if (filename == 'ace.js') { getFile(filename, handleFile)
getAceFile(handleFile);
} else {
fs.readFile(JS_DIR + filename, "utf8", handleFile);
}
function handleFile(err, data) { function handleFile(err, data) {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;

View File

@ -10,6 +10,7 @@
"name": "Robin Buse" } "name": "Robin Buse" }
], ],
"dependencies" : { "dependencies" : {
"yajsml" : "1.1.1",
"require-kernel" : "1.0.3", "require-kernel" : "1.0.3",
"socket.io" : "0.8.7", "socket.io" : "0.8.7",
"ueberDB" : "0.1.7", "ueberDB" : "0.1.7",

View File

@ -238,7 +238,7 @@ function Ace2Editor()
} else { } else {
file = ACE_SOURCE; file = ACE_SOURCE;
file = file.replace(/^\.\.\/static\/js\//, '../minified/'); file = file.replace(/^\.\.\/static\/js\//, '../minified/');
buffer.push('<script type="application/javascript" src="' + file + '"><\/script>'); buffer.push('<script type="application/javascript" src="' + file + '?callback=require.define"><\/script>');
buffer.push('<script type="text/javascript">'); buffer.push('<script type="text/javascript">');
buffer.push('require("/ace2_inner");'); buffer.push('require("/ace2_inner");');
buffer.push('<\/script>'); buffer.push('<\/script>');

View File

@ -290,7 +290,7 @@
<script type="text/javascript" src="../static/js/require-kernel.js"></script> <script type="text/javascript" src="../static/js/require-kernel.js"></script>
<script type="text/javascript" src="../socket.io/socket.io.js"></script> <script type="text/javascript" src="../socket.io/socket.io.js"></script>
<script type="text/javascript" src="../minified/pad.js"></script> <script type="text/javascript" src="../minified/pad.js?callback=require.define"></script>
<script type="text/javascript" src="../static/custom/pad.js"></script> <script type="text/javascript" src="../static/custom/pad.js"></script>
<script type="text/javascript"> <script type="text/javascript">
var clientVars = {}; var clientVars = {};

View File

@ -199,7 +199,7 @@
<script type="text/javascript" src="../../static/js/require-kernel.js"></script> <script type="text/javascript" src="../../static/js/require-kernel.js"></script>
<script type="text/javascript" src="../../socket.io/socket.io.js"></script> <script type="text/javascript" src="../../socket.io/socket.io.js"></script>
<script type="text/javascript" src="../../minified/timeslider.js"></script> <script type="text/javascript" src="../../minified/timeslider.js?callback=require.define"></script>
<script type="text/javascript" src="../../static/custom/timeslider.js"></script> <script type="text/javascript" src="../../static/custom/timeslider.js"></script>
<script type="text/javascript" > <script type="text/javascript" >
var clientVars = {}; var clientVars = {};