diff options
-rw-r--r-- | node/server.js | 17 | ||||
-rw-r--r-- | node/utils/Minify.js | 74 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | static/js/ace.js | 2 | ||||
-rw-r--r-- | static/pad.html | 2 | ||||
-rw-r--r-- | static/timeslider.html | 2 |
6 files changed, 61 insertions, 37 deletions
diff --git a/node/server.js b/node/server.js index 0e61916c..e6cf9938 100644 --- a/node/server.js +++ b/node/server.js @@ -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) diff --git a/node/utils/Minify.js b/node/utils/Minify.js index f54dc4d3..20c7ac6b 100644 --- a/node/utils/Minify.js +++ b/node/utils/Minify.js @@ -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; diff --git a/package.json b/package.json index 27b2125a..e92905b7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/static/js/ace.js b/static/js/ace.js index 04930910..bdb2f4f3 100644 --- a/static/js/ace.js +++ b/static/js/ace.js @@ -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>'); diff --git a/static/pad.html b/static/pad.html index 0345d65b..3f7fe8f2 100644 --- a/static/pad.html +++ b/static/pad.html @@ -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 = {}; diff --git a/static/timeslider.html b/static/timeslider.html index 579e26ef..c629857a 100644 --- a/static/timeslider.html +++ b/static/timeslider.html @@ -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 = {}; |