serving unminified libraries in development mode, minified otherwise

v1.18.x
Julian Lam 12 years ago
parent 66cb1fb6ad
commit 14720057c2

@ -28,7 +28,6 @@
winston = require('winston'), winston = require('winston'),
pkg = require('./package.json'), pkg = require('./package.json'),
path = require('path'), path = require('path'),
uglifyjs = require('uglify-js'),
meta; meta;
// Runtime environment // Runtime environment
@ -72,61 +71,6 @@
winston.info('Base Configuration OK.'); winston.info('Base Configuration OK.');
} }
// Minify JS
var toMinify = [
'/vendor/jquery/js/jquery.js',
'/vendor/jquery/js/jquery-ui-1.10.3.custom.min.js',
'/vendor/jquery/js/jquery.timeago.js',
'/vendor/bootstrap/js/bootstrap.min.js',
'/src/app.js',
'/vendor/requirejs/require.js',
'/vendor/bootbox/bootbox.min.js',
'/src/templates.js',
'/src/ajaxify.js',
'/src/jquery.form.js',
'/src/utils.js'
],
minified, mtime;
toMinify = toMinify.map(function (jsPath) {
return path.join(__dirname + '/public', jsPath);
});
async.parallel({
mtime: function (next) {
async.map(toMinify, fs.stat, function (err, stats) {
async.reduce(stats, 0, function (memo, item, callback) {
mtime = +new Date(item.mtime);
callback(null, mtime > memo ? mtime : memo);
}, next);
});
},
minFile: function (next) {
var minFile = path.join(__dirname, 'public/src/nodebb.min.js');
if (!fs.existsSync(minFile)) {
winston.warn('No minified client-side library found');
return next(null, 0);
}
fs.stat(minFile, function (err, stat) {
next(err, +new Date(stat.mtime));
});
}
}, function (err, results) {
if (results.minFile > results.mtime) {
winston.info('No changes to client-side libraries -- skipping minification');
} else {
winston.info('Minifying client-side libraries');
minified = uglifyjs.minify(toMinify);
fs.writeFile(path.join(__dirname, '/public/src', 'nodebb.min.js'), minified.code, function (err) {
if (!err) {
winston.info('Minified client-side libraries');
} else {
winston.error('Problem minifying client-side libraries, exiting.');
process.exit();
}
});
}
});
meta.configs.init(function () { meta.configs.init(function () {
// Initial setup for Redis & Reds // Initial setup for Redis & Reds
var reds = require('reds'), var reds = require('reds'),

@ -9,7 +9,9 @@
var RELATIVE_PATH = "{relative_path}"; var RELATIVE_PATH = "{relative_path}";
</script> </script>
<script src="{relative_path}/socket.io/socket.io.js"></script> <script src="{relative_path}/socket.io/socket.io.js"></script>
<script src="{relative_path}/src/nodebb.min.js"></script> <!-- BEGIN clientScripts -->
<script src="{relative_path}{clientScripts.script}"></script>
<!-- END clientScripts -->
<script> <script>
require.config({ require.config({
baseUrl: "{relative_path}/src/modules", baseUrl: "{relative_path}/src/modules",

@ -6,19 +6,21 @@ var utils = require('./../public/src/utils.js'),
winston = require('winston'), winston = require('winston'),
nconf = require('nconf'); nconf = require('nconf');
(function(Meta) { (function (Meta) {
Meta.configs = { Meta.configs = {
init: function(callback) { init: function (callback) {
Meta.configs.list(function(err, config) { Meta.configs.list(function (err, config) {
if (!err) { if (!err) {
Meta.config = config; Meta.config = config;
callback(); callback();
} else winston.error(err); } else {
winston.error(err);
}
}); });
}, },
list: function(callback) { list: function (callback) {
RDB.hgetall('config', function(err, config) { RDB.hgetall('config', function (err, config) {
if (!err) { if (!err) {
config = config || {}; config = config || {};
config.status = 'ok'; config.status = 'ok';
@ -28,52 +30,63 @@ var utils = require('./../public/src/utils.js'),
} }
}); });
}, },
get: function(field, callback) { get: function (field, callback) {
RDB.hget('config', field, callback); RDB.hget('config', field, callback);
}, },
getFields: function(fields, callback) { getFields: function (fields, callback) {
RDB.hmgetObject('config', fields, callback); RDB.hmgetObject('config', fields, callback);
}, },
set: function(field, value, callback) { set: function (field, value, callback) {
RDB.hset('config', field, value, function(err, res) { RDB.hset('config', field, value, function (err, res) {
if (callback) if (callback) {
callback(err, res); callback(err, res);
}
}); });
}, },
setOnEmpty: function(field, value, callback) { setOnEmpty: function (field, value, callback) {
this.get(field, function(err, curValue) { this.get(field, function (err, curValue) {
if (!curValue) Meta.configs.set(field, value, callback); if (!curValue) {
else callback(); Meta.configs.set(field, value, callback);
} else {
callback();
}
}); });
}, },
remove: function(field) { remove: function (field) {
RDB.hdel('config', field); RDB.hdel('config', field);
} }
} };
Meta.themes = { Meta.themes = {
get: function(callback) { get: function (callback) {
var themePath = path.join(__dirname, '../node_modules'); var themePath = path.join(__dirname, '../node_modules');
fs.readdir(themePath, function(err, files) { fs.readdir(themePath, function (err, files) {
async.filter(files, function(file, next) { async.filter(files, function (file, next) {
fs.stat(path.join(themePath, file), function(err, fileStat) { fs.stat(path.join(themePath, file), function (err, fileStat) {
if (err) next(false); if (err) {
next(false);
}
next((fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-')); next((fileStat.isDirectory() && file.slice(0, 13) === 'nodebb-theme-'));
}); });
}, function(themes) { }, function (themes) {
async.map(themes, function(theme, next) { async.map(themes, function (theme, next) {
var config = path.join(themePath, theme, 'theme.json'); var config = path.join(themePath, theme, 'theme.json');
if (fs.existsSync(config)) { if (fs.existsSync(config)) {
fs.readFile(config, function(err, file) { fs.readFile(config, function (err, file) {
var configObj = JSON.parse(file.toString()); var configObj = JSON.parse(file.toString());
if (!configObj.screenshot) configObj.screenshot = nconf.get('relative_path') + '/images/themes/default.png'; if (!configObj.screenshot) {
configObj.screenshot = nconf.get('relative_path') + '/images/themes/default.png';
}
next(err, configObj); next(err, configObj);
}); });
} else next(); } else {
}, function(err, themes) { next();
themes = themes.filter(function(theme) { }
}, function (err, themes) {
themes = themes.filter(function (theme) {
return (theme !== undefined); return (theme !== undefined);
}); });
callback(null, themes); callback(null, themes);
@ -81,30 +94,33 @@ var utils = require('./../public/src/utils.js'),
}); });
}); });
} }
} };
Meta.title = { Meta.title = {
build: function(urlFragment, current_user, callback) { build: function (urlFragment, current_user, callback) {
var self = this, var self = this,
user = require('./user'); user = require('./user');
async.parallel({ async.parallel({
title: function(next) { title: function (next) {
self.parseFragment(urlFragment, next); self.parseFragment(urlFragment, next);
}, },
notifCount: function(next) { notifCount: function (next) {
user.notifications.getUnreadCount(current_user, next); user.notifications.getUnreadCount(current_user, next);
} }
}, function(err, values) { }, function (err, values) {
var title; var title;
if (err) title = Meta.config.title || 'NodeBB'; if (err) {
else title = (values.title ? values.title + ' | ' : '') + (Meta.config.title || 'NodeBB'); title = Meta.config.title || 'NodeBB';
} else {
title = (values.title ? values.title + ' | ' : '') + (Meta.config.title || 'NodeBB');
}
callback(null, title, values.notifCount); callback(null, title, values.notifCount);
}); });
}, },
parseFragment: function(urlFragment, callback) { parseFragment: function (urlFragment, callback) {
if (urlFragment === '') { if (urlFragment === '') {
callback(null, 'Index'); callback(null, 'Index');
} else if (urlFragment === 'recent') { } else if (urlFragment === 'recent') {
@ -116,18 +132,94 @@ var utils = require('./../public/src/utils.js'),
} else if (/^category\/\d+\/?/.test(urlFragment)) { } else if (/^category\/\d+\/?/.test(urlFragment)) {
var cid = urlFragment.match(/category\/(\d+)/)[1]; var cid = urlFragment.match(/category\/(\d+)/)[1];
require('./categories').getCategoryField(cid, 'name', function(err, name) { require('./categories').getCategoryField(cid, 'name', function (err, name) {
callback(null, name); callback(null, name);
}); });
} else if (/^topic\/\d+\/?/.test(urlFragment)) { } else if (/^topic\/\d+\/?/.test(urlFragment)) {
var tid = urlFragment.match(/topic\/(\d+)/)[1]; var tid = urlFragment.match(/topic\/(\d+)/)[1];
require('./topics').getTopicField(tid, 'title', function(err, title) { require('./topics').getTopicField(tid, 'title', function (err, title) {
callback(null, title); callback(null, title);
}); });
} else callback(null); } else {
callback(null);
}
} }
} };
Meta.js = {
scripts: [
'/vendor/jquery/js/jquery.js',
'/vendor/jquery/js/jquery-ui-1.10.3.custom.min.js',
'/vendor/jquery/js/jquery.timeago.js',
'/vendor/bootstrap/js/bootstrap.min.js',
'/src/app.js',
'/vendor/requirejs/require.js',
'/vendor/bootbox/bootbox.min.js',
'/src/templates.js',
'/src/ajaxify.js',
'/src/jquery.form.js',
'/src/utils.js'
],
minFile: path.join(__dirname, '..', 'public/src/nodebb.min.js'),
get: function (callback) {
var mtime,
jsPaths = this.scripts.map(function (jsPath) {
return path.join(__dirname, '..', '/public', jsPath);
});
if (process.env.NODE_ENV !== 'development') {
async.parallel({
mtime: function (next) {
async.map(jsPaths, fs.stat, function (err, stats) {
async.reduce(stats, 0, function (memo, item, callback) {
mtime = +new Date(item.mtime);
callback(null, mtime > memo ? mtime : memo);
}, next);
});
},
minFile: function (next) {
if (!fs.existsSync(Meta.js.minFile)) {
winston.warn('No minified client-side library found');
return next(null, 0);
}
fs.stat(Meta.js.minFile, function (err, stat) {
next(err, +new Date(stat.mtime));
});
}
}, function (err, results) {
if (results.minFile > results.mtime) {
winston.info('No changes to client-side libraries -- skipping minification');
callback(null, [path.relative(path.join(__dirname, '../public'), Meta.js.minFile)]);
} else {
Meta.js.minify(function () {
callback(null, [path.relative(path.join(__dirname, '../public'), Meta.js.minFile)]);
});
}
});
} else {
callback(null, this.scripts);
}
},
minify: function (callback) {
var uglifyjs = require('uglify-js'),
jsPaths = this.scripts.map(function (jsPath) {
return path.join(__dirname, '..', '/public', jsPath);
}),
minified;
winston.info('Minifying client-side libraries');
minified = uglifyjs.minify(jsPaths);
fs.writeFile(Meta.js.minFile, minified.code, function (err) {
if (!err) {
winston.info('Minified client-side libraries');
callback();
} else {
winston.error('Problem minifying client-side libraries, exiting.');
process.exit();
}
});
}
};
}(exports)); }(exports));

@ -25,7 +25,17 @@ var express = require('express'),
nconf = require('nconf'); nconf = require('nconf');
(function (app) { (function (app) {
var templates = null; var templates = null,
clientScripts;
// Minify client-side libraries
meta.js.get(function (err, scripts) {
clientScripts = scripts.map(function (script) {
return script = {
script: script
}
});
});
/** /**
* `options` object requires: req, res * `options` object requires: req, res
@ -44,7 +54,7 @@ var express = require('express'),
}, { }, {
property: 'og:site_name', property: 'og:site_name',
content: meta.config.title || 'NodeBB' content: meta.config.title || 'NodeBB'
}, ], }],
metaString = utils.buildMetaTags(defaultMetaTags.concat(options.metaTags || [])), metaString = utils.buildMetaTags(defaultMetaTags.concat(options.metaTags || [])),
templateValues = { templateValues = {
cssSrc: meta.config['theme:src'] || nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css', cssSrc: meta.config['theme:src'] || nconf.get('relative_path') + '/vendor/bootstrap/css/bootstrap.min.css',
@ -52,9 +62,11 @@ var express = require('express'),
browserTitle: meta.config.title || 'NodeBB', browserTitle: meta.config.title || 'NodeBB',
csrf: options.res.locals.csrf_token, csrf: options.res.locals.csrf_token,
relative_path: nconf.get('relative_path'), relative_path: nconf.get('relative_path'),
meta_tags: metaString meta_tags: metaString,
clientScripts: clientScripts
}; };
console.log(templateValues);
callback(null, templates.header.parse(templateValues)); callback(null, templates.header.parse(templateValues));
}; };

Loading…
Cancel
Save