2011-05-28 20:05:43 +00:00
|
|
|
|
/**
|
2011-05-30 14:53:11 +00:00
|
|
|
|
* This Module manages all /minified/* requests. It controls the
|
|
|
|
|
* minification && compression of Javascript and CSS.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
2011-08-11 14:26:41 +00:00
|
|
|
|
* 2011 Peter 'Pita' Martischka (Primary Technology Ltd)
|
2011-05-28 20:05:43 +00:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-12-04 15:50:02 +00:00
|
|
|
|
var ERR = require("async-stacktrace");
|
2011-07-27 17:52:23 +00:00
|
|
|
|
var settings = require('./Settings');
|
2011-05-28 17:09:17 +00:00
|
|
|
|
var async = require('async');
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
var cleanCSS = require('clean-css');
|
|
|
|
|
var jsp = require("uglify-js").parser;
|
|
|
|
|
var pro = require("uglify-js").uglify;
|
|
|
|
|
var path = require('path');
|
|
|
|
|
var Buffer = require('buffer').Buffer;
|
2012-01-25 21:00:08 +00:00
|
|
|
|
var zlib = require('zlib');
|
2012-01-16 06:07:45 +00:00
|
|
|
|
var RequireKernel = require('require-kernel');
|
2011-07-27 17:52:23 +00:00
|
|
|
|
var server = require('../server');
|
2011-08-04 12:42:02 +00:00
|
|
|
|
var os = require('os');
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
2012-01-17 10:16:25 +00:00
|
|
|
|
var ROOT_DIR = path.normalize(__dirname + "/../" );
|
|
|
|
|
var JS_DIR = ROOT_DIR + '../static/js/';
|
|
|
|
|
var CSS_DIR = ROOT_DIR + '../static/css/';
|
|
|
|
|
var CACHE_DIR = ROOT_DIR + '../var/';
|
2012-01-15 00:12:03 +00:00
|
|
|
|
var TAR_PATH = path.join(__dirname, 'tar.json');
|
|
|
|
|
var tar = JSON.parse(fs.readFileSync(TAR_PATH, 'utf8'));
|
2011-07-26 16:39:25 +00:00
|
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
|
/**
|
2011-07-26 16:39:25 +00:00
|
|
|
|
* creates the minifed javascript for the given minified name
|
2011-05-30 14:53:11 +00:00
|
|
|
|
* @param req the Express request
|
|
|
|
|
* @param res the Express response
|
2011-05-28 17:09:17 +00:00
|
|
|
|
*/
|
2012-01-17 08:57:59 +00:00
|
|
|
|
exports.minifyJS = function(req, res, next)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2012-01-17 08:57:59 +00:00
|
|
|
|
var jsFilename = req.params['filename'];
|
2011-07-26 16:39:25 +00:00
|
|
|
|
|
|
|
|
|
//choose the js files we need
|
2012-01-15 00:12:03 +00:00
|
|
|
|
var jsFiles = undefined;
|
|
|
|
|
if (Object.prototype.hasOwnProperty.call(tar, jsFilename)) {
|
|
|
|
|
jsFiles = tar[jsFilename];
|
2012-01-17 09:22:23 +00:00
|
|
|
|
_handle(req, res, jsFilename, jsFiles)
|
2012-01-15 00:12:03 +00:00
|
|
|
|
} else {
|
2012-01-17 09:22:23 +00:00
|
|
|
|
// Not in tar list, but try anyways, if it fails, pass to `next`.
|
|
|
|
|
jsFiles = [jsFilename];
|
|
|
|
|
fs.stat(JS_DIR + jsFilename, function (error, stats) {
|
|
|
|
|
if (error || !stats.isFile()) {
|
|
|
|
|
next();
|
|
|
|
|
} else {
|
|
|
|
|
_handle(req, res, jsFilename, jsFiles);
|
|
|
|
|
}
|
|
|
|
|
});
|
2011-07-26 16:39:25 +00:00
|
|
|
|
}
|
2012-01-17 09:22:23 +00:00
|
|
|
|
}
|
2012-01-05 10:31:39 +00:00
|
|
|
|
|
2012-01-17 09:22:23 +00:00
|
|
|
|
function _handle(req, res, jsFilename, jsFiles) {
|
2012-01-17 08:57:59 +00:00
|
|
|
|
res.header("Content-Type","text/javascript");
|
|
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
|
//minifying is enabled
|
|
|
|
|
if(settings.minify)
|
|
|
|
|
{
|
|
|
|
|
var fileValues = {};
|
|
|
|
|
var embeds = {};
|
|
|
|
|
var latestModification = 0;
|
|
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
//find out the highest modification date
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
2012-01-17 10:16:25 +00:00
|
|
|
|
var folders2check = [CSS_DIR, JS_DIR];
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
|
|
|
|
//go trough this two folders
|
|
|
|
|
async.forEach(folders2check, function(path, callback)
|
|
|
|
|
{
|
|
|
|
|
//read the files in the folder
|
|
|
|
|
fs.readdir(path, function(err, files)
|
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err, callback)) return;
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
|
|
|
|
//we wanna check the directory itself for changes too
|
|
|
|
|
files.push(".");
|
|
|
|
|
|
|
|
|
|
//go trough all files in this folder
|
|
|
|
|
async.forEach(files, function(filename, callback)
|
|
|
|
|
{
|
|
|
|
|
//get the stat data of this file
|
|
|
|
|
fs.stat(path + "/" + filename, function(err, stats)
|
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err, callback)) return;
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
|
|
|
|
//get the modification time
|
|
|
|
|
var modificationTime = stats.mtime.getTime();
|
|
|
|
|
|
|
|
|
|
//compare the modification time to the highest found
|
|
|
|
|
if(modificationTime > latestModification)
|
|
|
|
|
{
|
|
|
|
|
latestModification = modificationTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
}, callback);
|
|
|
|
|
});
|
|
|
|
|
}, callback);
|
|
|
|
|
},
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
|
|
|
|
//check the modification time of the minified js
|
2012-01-17 10:16:25 +00:00
|
|
|
|
fs.stat(CACHE_DIR + "/minified_" + jsFilename, function(err, stats)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(err && err.code != "ENOENT")
|
|
|
|
|
{
|
|
|
|
|
ERR(err, callback);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
|
|
|
|
//there is no minfied file or there new changes since this file was generated, so continue generating this file
|
|
|
|
|
if((err && err.code == "ENOENT") || stats.mtime.getTime() < latestModification)
|
|
|
|
|
{
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
//the minified file is still up to date, stop minifying
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
callback("stop");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
//load all js files
|
|
|
|
|
function (callback)
|
|
|
|
|
{
|
|
|
|
|
async.forEach(jsFiles, function (item, callback)
|
|
|
|
|
{
|
2012-01-17 10:16:25 +00:00
|
|
|
|
fs.readFile(JS_DIR + item, "utf-8", function(err, data)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err, callback)) return;
|
2011-05-28 17:09:17 +00:00
|
|
|
|
fileValues[item] = data;
|
2011-12-04 15:50:02 +00:00
|
|
|
|
callback();
|
2011-05-28 17:09:17 +00:00
|
|
|
|
});
|
|
|
|
|
}, callback);
|
|
|
|
|
},
|
|
|
|
|
//find all includes in ace.js and embed them
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
2011-07-26 16:39:25 +00:00
|
|
|
|
//if this is not the creation of pad.js, skip this part
|
|
|
|
|
if(jsFilename != "pad.js")
|
|
|
|
|
{
|
|
|
|
|
callback();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-16 06:53:44 +00:00
|
|
|
|
var founds = fileValues["ace.js"].match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"-]+\)/gi);
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
|
|
|
|
//go trough all includes
|
|
|
|
|
async.forEach(founds, function (item, callback)
|
|
|
|
|
{
|
|
|
|
|
var filename = item.match(/"[^"]*"/g)[0].substr(1);
|
|
|
|
|
filename = filename.substr(0,filename.length-1);
|
|
|
|
|
|
|
|
|
|
var type = item.match(/INCLUDE_[A-Z]+/g)[0].substr("INCLUDE_".length);
|
|
|
|
|
|
|
|
|
|
//read the included file
|
2012-01-16 06:07:45 +00:00
|
|
|
|
var shortFilename = filename.replace(/^..\/static\/js\//, '');
|
|
|
|
|
if (shortFilename == 'require-kernel.js') {
|
|
|
|
|
// the kernel isn’t actually on the file system.
|
|
|
|
|
handleEmbed(null, requireDefinition());
|
|
|
|
|
} else {
|
|
|
|
|
fs.readFile(ROOT_DIR + filename, "utf-8", handleEmbed);
|
|
|
|
|
}
|
|
|
|
|
function handleEmbed(err, data)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err, callback)) return;
|
2012-01-15 05:42:47 +00:00
|
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
|
if(type == "JS")
|
|
|
|
|
{
|
2012-01-16 06:07:45 +00:00
|
|
|
|
if (shortFilename == 'require-kernel.js') {
|
|
|
|
|
embeds[filename] = compressJS([data]);
|
|
|
|
|
} else {
|
|
|
|
|
embeds[filename] = compressJS([isolateJS(data, shortFilename)]);
|
|
|
|
|
}
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-01-15 05:42:47 +00:00
|
|
|
|
embeds[filename] = compressCSS([data]);
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
2011-12-04 15:50:02 +00:00
|
|
|
|
callback();
|
2012-01-16 06:07:45 +00:00
|
|
|
|
}
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}, function(err)
|
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err, callback)) return;
|
2012-01-15 05:42:47 +00:00
|
|
|
|
|
|
|
|
|
fileValues["ace.js"] += ';\n'
|
|
|
|
|
fileValues["ace.js"] +=
|
|
|
|
|
'Ace2Editor.EMBEDED = Ace2Editor.EMBED || {};\n'
|
|
|
|
|
for (var filename in embeds)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2012-01-15 05:42:47 +00:00
|
|
|
|
fileValues["ace.js"] +=
|
|
|
|
|
'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = '
|
|
|
|
|
+ JSON.stringify(embeds[filename]) + ';\n';
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
2012-01-15 05:42:47 +00:00
|
|
|
|
|
2011-12-04 15:50:02 +00:00
|
|
|
|
callback();
|
2011-05-28 17:09:17 +00:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
//put all together and write it into a file
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
|
|
|
|
//minify all javascript files to one
|
2012-01-17 07:26:30 +00:00
|
|
|
|
var values = [];
|
|
|
|
|
tarCode(jsFiles, fileValues, function (content) {values.push(content)});
|
2011-05-28 17:09:17 +00:00
|
|
|
|
var result = compressJS(values);
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
//write the results plain in a file
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
2012-01-17 10:16:25 +00:00
|
|
|
|
fs.writeFile(CACHE_DIR + "minified_" + jsFilename, result, "utf8", callback);
|
2011-05-28 17:09:17 +00:00
|
|
|
|
},
|
|
|
|
|
//write the results compressed in a file
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
2012-01-25 21:00:08 +00:00
|
|
|
|
zlib.gzip(result, function(err, compressedResult){
|
|
|
|
|
//weird gzip bug that returns 0 instead of null if everything is ok
|
|
|
|
|
err = err === 0 ? null : err;
|
|
|
|
|
|
|
|
|
|
if(ERR(err, callback)) return;
|
2011-12-04 15:50:02 +00:00
|
|
|
|
|
2012-01-25 21:00:08 +00:00
|
|
|
|
fs.writeFile(CACHE_DIR + "minified_" + jsFilename + ".gz", compressedResult, callback);
|
|
|
|
|
});
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
],callback);
|
|
|
|
|
}
|
|
|
|
|
], function(err)
|
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(err && err != "stop")
|
|
|
|
|
{
|
|
|
|
|
if(ERR(err)) return;
|
|
|
|
|
}
|
2011-05-28 17:09:17 +00:00
|
|
|
|
|
|
|
|
|
//check if gzip is supported by this browser
|
|
|
|
|
var gzipSupport = req.header('Accept-Encoding', '').indexOf('gzip') != -1;
|
|
|
|
|
|
|
|
|
|
var pathStr;
|
2011-08-04 12:42:02 +00:00
|
|
|
|
if(gzipSupport && os.type().indexOf("Windows") == -1)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2012-01-17 10:16:25 +00:00
|
|
|
|
pathStr = path.normalize(CACHE_DIR + "minified_" + jsFilename + ".gz");
|
2011-05-28 17:09:17 +00:00
|
|
|
|
res.header('Content-Encoding', 'gzip');
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-01-17 10:16:25 +00:00
|
|
|
|
pathStr = path.normalize(CACHE_DIR + "minified_" + jsFilename );
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-30 11:40:37 +00:00
|
|
|
|
res.sendfile(pathStr, { maxAge: server.maxAge });
|
2011-05-28 17:09:17 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2011-07-24 19:18:47 +00:00
|
|
|
|
//minifying is disabled, so put the files together in one file
|
2011-05-28 17:09:17 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2011-07-24 19:18:47 +00:00
|
|
|
|
var fileValues = {};
|
2011-07-14 19:11:03 +00:00
|
|
|
|
|
2011-07-24 19:18:47 +00:00
|
|
|
|
//read all js files
|
|
|
|
|
async.forEach(jsFiles, function (item, callback)
|
2011-05-28 17:09:17 +00:00
|
|
|
|
{
|
2012-01-17 10:16:25 +00:00
|
|
|
|
fs.readFile(JS_DIR + item, "utf-8", function(err, data)
|
2011-12-04 15:50:02 +00:00
|
|
|
|
{
|
|
|
|
|
if(ERR(err, callback)) return;
|
2011-07-24 19:18:47 +00:00
|
|
|
|
fileValues[item] = data;
|
2011-12-04 15:50:02 +00:00
|
|
|
|
callback();
|
2011-07-24 19:18:47 +00:00
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
//send all files together
|
|
|
|
|
function(err)
|
|
|
|
|
{
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err)) return;
|
2011-07-24 19:18:47 +00:00
|
|
|
|
|
2012-01-17 07:26:30 +00:00
|
|
|
|
tarCode(jsFiles, fileValues, function (content) {res.write(content)});
|
2011-07-24 19:18:47 +00:00
|
|
|
|
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-16 06:07:45 +00:00
|
|
|
|
exports.requireDefinition = requireDefinition;
|
|
|
|
|
function requireDefinition() {
|
|
|
|
|
return 'var require = ' + RequireKernel.kernelSource + ';\n';
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-17 07:26:30 +00:00
|
|
|
|
function tarCode(filesInOrder, files, write) {
|
|
|
|
|
for(var i = 0, ii = filesInOrder.length; i < filesInOrder.length; i++) {
|
|
|
|
|
var filename = filesInOrder[i];
|
|
|
|
|
write("\n\n\n/*** File: static/js/" + filename + " ***/\n\n\n");
|
2012-01-16 06:56:41 +00:00
|
|
|
|
write(isolateJS(files[filename], filename));
|
2012-01-17 07:26:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-16 01:23:48 +00:00
|
|
|
|
// Wrap the following code in a self executing function and assign exports to
|
|
|
|
|
// global. This is a first step towards removing symbols from the global scope.
|
2012-01-16 03:46:50 +00:00
|
|
|
|
// exports is global and require is a function that returns global.
|
2012-01-16 06:56:41 +00:00
|
|
|
|
function isolateJS(code, filename) {
|
2012-01-16 06:07:45 +00:00
|
|
|
|
var srcPath = JSON.stringify('/' + filename);
|
|
|
|
|
var srcPathAbbv = JSON.stringify('/' + filename.replace(/\.js$/, ''));
|
|
|
|
|
return 'require.define({'
|
|
|
|
|
+ srcPath + ': '
|
|
|
|
|
+ 'function (require, exports, module) {' + code + '}'
|
|
|
|
|
+ (srcPath != srcPathAbbv ? '\n,' + srcPathAbbv + ': null' : '')
|
|
|
|
|
+ '});\n';
|
2012-01-16 01:23:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-28 17:09:17 +00:00
|
|
|
|
function compressJS(values)
|
|
|
|
|
{
|
|
|
|
|
var complete = values.join("\n");
|
|
|
|
|
var ast = jsp.parse(complete); // parse code and get the initial AST
|
|
|
|
|
ast = pro.ast_mangle(ast); // get a new AST with mangled names
|
|
|
|
|
return pro.gen_code(ast); // compressed code here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function compressCSS(values)
|
|
|
|
|
{
|
|
|
|
|
var complete = values.join("\n");
|
|
|
|
|
return cleanCSS.process(complete);
|
|
|
|
|
}
|