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 minify = require('./utils/Minify');
var CachingMiddleware = require('./utils/caching_middleware');
var Yajsml = require('yajsml');
var formidable = require('formidable');
var apiHandler;
var exportHandler;
@ -147,6 +148,8 @@ async.waterfall([
res.write(minify.requireDefinition());
res.end();
});
var assetCache = new CachingMiddleware;
app.all('/static/js/:filename', assetCache.handle, minify.minifyJS);
app.get('/static/*', function(req, res)
{
var filePath = path.normalize(__dirname + "/.." +
@ -155,8 +158,18 @@ async.waterfall([
});
//serve minified files
var assetCache = new CachingMiddleware;
app.all('/minified/:filename', assetCache.handle, minify.minifyJS);
var jsServer = new (Yajsml.Server)({
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
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 = 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
* @param req the Express request
@ -43,21 +51,7 @@ var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8'));
*/
exports.minifyJS = function(req, res, next)
{
var jsFilename = 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) {
var filename = req.params['filename'];
res.header("Content-Type","text/javascript");
lastModifiedDate(function (date) {
@ -70,7 +64,7 @@ function _handle(req, res, jsFilename, jsFiles) {
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.code == "ENOENT") {
res.writeHead(404, {});
@ -90,17 +84,12 @@ function _handle(req, res, jsFilename, jsFiles) {
res.writeHead(200, {});
res.end();
} else if (req.method == 'GET') {
res.writeHead(200, {});
tarCode(
jsFiles
, function (content) {
res.write(content);
}
, function (err) {
if(ERR(err)) return;
res.end();
}
);
getFileCompressed(filename, function (error, content) {
if(ERR(error)) return;
res.writeHead(200, {});
res.write(content);
res.end();
});
} else {
res.writeHead(405, {'allow': 'HEAD, GET'});
res.end();
@ -222,15 +211,36 @@ function requireDefinition() {
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) {
write('require.define({');
var initialEntry = true;
async.forEach(jsFiles, function (filename, callback){
if (filename == 'ace.js') {
getAceFile(handleFile);
} else {
fs.readFile(JS_DIR + filename, "utf8", handleFile);
}
getFile(filename, handleFile)
function handleFile(err, data) {
if(ERR(err, callback)) return;

View File

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

View File

@ -238,7 +238,7 @@ function Ace2Editor()
} else {
file = ACE_SOURCE;
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('require("/ace2_inner");');
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="../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">
var clientVars = {};

View File

@ -199,7 +199,7 @@
<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="../../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" >
var clientVars = {};