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)
|
|
|
|
|
{
|
2012-01-29 00:24:11 +00:00
|
|
|
|
var result = undefined;
|
2011-05-28 17:09:17 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2012-01-29 00:24:11 +00:00
|
|
|
|
var values = [];
|
|
|
|
|
tarCode(
|
|
|
|
|
jsFiles
|
|
|
|
|
, function (content) {values.push(content)}
|
|
|
|
|
, function (err) {
|
|
|
|
|
if(ERR(err)) return;
|
2012-01-15 05:42:47 +00:00
|
|
|
|
|
2012-01-29 00:24:11 +00:00
|
|
|
|
result = values.join('');
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2011-05-28 17:09:17 +00:00
|
|
|
|
},
|
|
|
|
|
//put all together and write it into a file
|
|
|
|
|
function(callback)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
2012-01-29 00:24:11 +00:00
|
|
|
|
tarCode(
|
|
|
|
|
jsFiles
|
|
|
|
|
, function (content) {res.write(content)}
|
|
|
|
|
, function (err) {
|
2011-12-04 15:50:02 +00:00
|
|
|
|
if(ERR(err)) return;
|
2011-07-24 19:18:47 +00:00
|
|
|
|
res.end();
|
|
|
|
|
});
|
2011-05-28 17:09:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 02:29:00 +00:00
|
|
|
|
// find all includes in ace.js and embed them.
|
|
|
|
|
function getAceFile(callback) {
|
|
|
|
|
fs.readFile(JS_DIR + 'ace.js', "utf8", function(err, data) {
|
|
|
|
|
if(ERR(err, callback)) return;
|
|
|
|
|
|
|
|
|
|
// Find all includes in ace.js and embed them
|
|
|
|
|
var founds = data.match(/\$\$INCLUDE_[a-zA-Z_]+\([a-zA-Z0-9.\/_"-]+\)/gi);
|
|
|
|
|
if (!settings.minify) {
|
|
|
|
|
founds = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data += ';\n';
|
|
|
|
|
data += 'Ace2Editor.EMBEDED = Ace2Editor.EMBEDED || {};\n';
|
|
|
|
|
|
|
|
|
|
//go trough all includes
|
|
|
|
|
async.forEach(founds, function (item, callback) {
|
|
|
|
|
var filename = item.match(/"([^"]*)"/)[1];
|
|
|
|
|
var type = item.match(/INCLUDE_([A-Z]+)/)[1];
|
|
|
|
|
var shortFilename = (filename.match(/^..\/static\/js\/(.*)$/, '')||[])[1];
|
|
|
|
|
|
|
|
|
|
//read the included files
|
|
|
|
|
if (shortFilename) {
|
|
|
|
|
if (shortFilename == 'require-kernel.js') {
|
|
|
|
|
// the kernel isn’t actually on the file system.
|
|
|
|
|
handleEmbed(null, requireDefinition());
|
|
|
|
|
} else {
|
|
|
|
|
fs.readFile(ROOT_DIR + filename, "utf8", function (error, data) {
|
|
|
|
|
handleEmbed(error, isolateJS(data, shortFilename));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fs.readFile(ROOT_DIR + filename, "utf8", handleEmbed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleEmbed(error, data_) {
|
|
|
|
|
if (error) {
|
|
|
|
|
return; // Don't bother to include it.
|
|
|
|
|
}
|
|
|
|
|
if (settings.minify) {
|
|
|
|
|
if (type == "JS") {
|
|
|
|
|
try {
|
|
|
|
|
data_ = compressJS([data_]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Ignore, include uncompresseed, which will break in browser.
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
data_ = compressCSS([data_]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
data += 'Ace2Editor.EMBEDED[' + JSON.stringify(filename) + '] = '
|
|
|
|
|
+ JSON.stringify(data_) + ';\n';
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}, function(error) {
|
|
|
|
|
callback(error, data);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-16 06:07:45 +00:00
|
|
|
|
exports.requireDefinition = requireDefinition;
|
|
|
|
|
function requireDefinition() {
|
|
|
|
|
return 'var require = ' + RequireKernel.kernelSource + ';\n';
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-29 00:24:11 +00:00
|
|
|
|
function tarCode(jsFiles, write, callback) {
|
2012-01-31 22:29:48 +00:00
|
|
|
|
write('require.define({');
|
|
|
|
|
var initialEntry = true;
|
|
|
|
|
async.forEach(jsFiles, function (filename, callback){
|
|
|
|
|
if (filename == 'ace.js') {
|
2012-01-29 00:24:11 +00:00
|
|
|
|
getAceFile(handleFile);
|
|
|
|
|
} else {
|
2012-01-31 22:29:48 +00:00
|
|
|
|
fs.readFile(JS_DIR + filename, "utf8", handleFile);
|
2012-01-29 00:24:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleFile(err, data) {
|
|
|
|
|
if(ERR(err, callback)) return;
|
2012-01-31 22:29:48 +00:00
|
|
|
|
var srcPath = JSON.stringify('/' + filename);
|
|
|
|
|
var srcPathAbbv = JSON.stringify('/' + filename.replace(/\.js$/, ''));
|
|
|
|
|
if (!initialEntry) {
|
|
|
|
|
write('\n,');
|
|
|
|
|
} else {
|
|
|
|
|
initialEntry = false;
|
|
|
|
|
}
|
|
|
|
|
write(srcPath + ': ')
|
|
|
|
|
data = '(function (require, exports, module) {' + data + '})';
|
2012-01-29 00:24:11 +00:00
|
|
|
|
if (settings.minify) {
|
2012-01-31 22:29:48 +00:00
|
|
|
|
write(compressJS([data]));
|
2012-01-29 00:24:11 +00:00
|
|
|
|
} else {
|
2012-01-31 22:29:48 +00:00
|
|
|
|
write(data);
|
2012-01-29 00:24:11 +00:00
|
|
|
|
}
|
2012-01-31 22:29:48 +00:00
|
|
|
|
if (srcPath != srcPathAbbv) {
|
|
|
|
|
write('\n,' + srcPathAbbv + ': null');
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-29 00:24:11 +00:00
|
|
|
|
callback();
|
|
|
|
|
}
|
2012-01-31 22:29:48 +00:00
|
|
|
|
}, function () {
|
|
|
|
|
write('});\n');
|
|
|
|
|
callback();
|
|
|
|
|
});
|
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
|
2012-02-02 10:26:36 +00:00
|
|
|
|
ast = pro.ast_squeeze(ast); // get an AST with compression optimizations
|
2011-05-28 17:09:17 +00:00
|
|
|
|
return pro.gen_code(ast); // compressed code here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function compressCSS(values)
|
|
|
|
|
{
|
|
|
|
|
var complete = values.join("\n");
|
|
|
|
|
return cleanCSS.process(complete);
|
|
|
|
|
}
|