Baris Soner Usakli 11 years ago
commit 9683dc27db

@ -223,6 +223,7 @@ var fs = require('fs'),
};
Meta.js = {
cache: undefined,
scripts: [
'vendor/jquery/js/jquery.js',
'vendor/jquery/js/jquery-ui-1.10.4.custom.js',
@ -240,7 +241,7 @@ var fs = require('fs'),
'src/translator.js',
'src/utils.js'
],
minFile: path.join(__dirname, '..', 'public/src/nodebb.min.js'),
minFile: nconf.get('relative_path') + 'nodebb.min.js',
get: function (callback) {
plugins.fireHook('filter:scripts.get', this.scripts, function(err, scripts) {
var ctime,
@ -265,46 +266,9 @@ var fs = require('fs'),
Meta.js.scripts = jsPaths.filter(function(path) { return path !== null });
if (process.env.NODE_ENV !== 'development') {
async.parallel({
ctime: function (next) {
async.map(jsPaths, fs.stat, function (err, stats) {
async.reduce(stats, 0, function (memo, item, next) {
if(item) {
ctime = +new Date(item.ctime);
next(null, ctime > memo ? ctime : memo);
} else {
next(null, memo);
}
}, next);
});
},
minFile: function (next) {
if (!fs.existsSync(Meta.js.minFile)) {
winston.info('No minified client-side library found');
return next(null, 0);
}
fs.stat(Meta.js.minFile, function (err, stat) {
next(err, +new Date(stat.ctime));
});
}
}, function (err, results) {
if (results.minFile > results.ctime) {
winston.info('No changes to client-side libraries -- skipping minification');
callback(null, [path.relative(path.join(__dirname, '../public'), Meta.js.minFile)]);
} else {
winston.info('Minifying client-side libraries -- please wait');
Meta.js.minify(function () {
callback(null, [
path.relative(path.join(__dirname, '../public'), Meta.js.minFile)
]);
});
}
});
} else {
callback(null, scripts);
}
callback(null, [
Meta.js.minFile
]);
});
},
minify: function (callback) {
@ -317,17 +281,8 @@ var fs = require('fs'),
}
minified = uglifyjs.minify(jsPaths);
fs.writeFile(Meta.js.minFile, minified.code, function (err) {
if (!err) {
if (process.env.NODE_ENV === 'development') {
winston.info('Minified client-side libraries');
}
callback();
} else {
winston.error('Problem minifying client-side libraries, exiting.');
process.exit();
}
});
this.cache = minified.code;
callback();
}
};

@ -48,5 +48,15 @@ var path = require('path'),
}
});
});
app.get('/nodebb.min.js', function(req, res) {
if (meta.js.cache) {
res.type('text/javascript').send(meta.js.cache);
} else {
meta.js.minify(function() {
res.type('text/javascript').send(meta.js.cache);
});
}
});
};
})(exports);

@ -33,7 +33,7 @@ var path = require('path'),
userRoute = require('./routes/user'),
apiRoute = require('./routes/api'),
feedsRoute = require('./routes/feeds'),
themeRoute = require('./routes/theme');
metaRoute = require('./routes/meta');
if(nconf.get('ssl')) {
server = require('https').createServer({
@ -483,7 +483,7 @@ process.on('uncaughtException', function(err) {
app.namespace(nconf.get('relative_path'), function () {
auth.registerApp(app);
themeRoute.createRoutes(app);
metaRoute.createRoutes(app);
admin.createRoutes(app);
userRoute.createRoutes(app);
apiRoute.createRoutes(app);

Loading…
Cancel
Save