added recompilation of templates to NodeBB Reloading - #2010

v1.18.x
Julian Lam 11 years ago
parent eef200be10
commit fcbdc5e271

@ -2,13 +2,12 @@
var ajaxify = ajaxify || {}; var ajaxify = ajaxify || {};
(function () { $(document).ready(function() {
require(['templates'], function (templatesModule) {
/*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/ /*global app, templates, utils, socket, translator, config, RELATIVE_PATH*/
var location = document.location || window.location, var location = document.location || window.location,
rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''), rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''),
templatesConfig = null,
availableTemplates = null,
apiXHR = null, apiXHR = null,
PRELOADER_RATE_LIMIT = 10000; PRELOADER_RATE_LIMIT = 10000;
@ -69,7 +68,7 @@ var ajaxify = ajaxify || {};
hash = window.location.hash ? window.location.hash : ''; hash = window.location.hash ? window.location.hash : '';
} }
if (ajaxify.isTemplateAvailable(tpl_url) && !!!templatesConfig.force_refresh[tpl_url]) { if (ajaxify.isTemplateAvailable(tpl_url) && !!!templatesModule.config.force_refresh[tpl_url]) {
ajaxify.currentPage = url; ajaxify.currentPage = url;
if (window.history && window.history.pushState) { if (window.history && window.history.pushState) {
@ -153,7 +152,7 @@ var ajaxify = ajaxify || {};
}; };
ajaxify.isTemplateAvailable = function(tpl) { ajaxify.isTemplateAvailable = function(tpl) {
return $.inArray(tpl + '.tpl', availableTemplates) !== -1; return $.inArray(tpl + '.tpl', templatesModule.available) !== -1;
}; };
ajaxify.getTemplateMapping = function(url) { ajaxify.getTemplateMapping = function(url) {
@ -181,10 +180,10 @@ var ajaxify = ajaxify || {};
}; };
ajaxify.getCustomTemplateMapping = function(tpl) { ajaxify.getCustomTemplateMapping = function(tpl) {
if (templatesConfig && templatesConfig.custom_mapping && tpl !== undefined) { if (templatesModule.config && templatesModule.config.custom_mapping && tpl !== undefined) {
for (var pattern in templatesConfig.custom_mapping) { for (var pattern in templatesModule.config.custom_mapping) {
if (tpl.match(pattern)) { if (tpl.match(pattern)) {
return (templatesConfig.custom_mapping[pattern]); return (templatesModule.config.custom_mapping[pattern]);
} }
} }
} }
@ -329,12 +328,14 @@ var ajaxify = ajaxify || {};
templates.registerLoader(ajaxify.loadTemplate); templates.registerLoader(ajaxify.loadTemplate);
$.getJSON(RELATIVE_PATH + '/api/get_templates_listing', function (data) { templatesModule.refresh(app.load);
templatesConfig = data.templatesConfig; // $.getJSON(RELATIVE_PATH + '/api/get_templates_listing', function (data) {
availableTemplates = data.availableTemplates; // templatesModule.config = data.templatesConfig;
// availableTemplates = data.availableTemplates;
app.load(); // app.load();
}); // });
}); });
}()); });
});

@ -0,0 +1,15 @@
define('templates', function() {
var Templates = {};
Templates.refresh = function(callback) {
$.getJSON(RELATIVE_PATH + '/api/get_templates_listing', function (data) {
Templates.config = data.templatesConfig;
Templates.available = data.availableTemplates;
if (callback) callback();
// app.load();
});
};
return Templates;
});

@ -17,7 +17,7 @@ var async = require('async'),
require('./meta/css')(Meta); require('./meta/css')(Meta);
require('./meta/sounds')(Meta); require('./meta/sounds')(Meta);
require('./meta/settings')(Meta); require('./meta/settings')(Meta);
Meta.templates = require('./meta/templates');
/* Assorted */ /* Assorted */
Meta.userOrGroupExists = function(slug, callback) { Meta.userOrGroupExists = function(slug, callback) {
@ -33,10 +33,16 @@ var async = require('async'),
plugins.reload(function() { plugins.reload(function() {
async.parallel([ async.parallel([
async.apply(Meta.js.minify, false), async.apply(Meta.js.minify, false),
async.apply(Meta.css.minify) async.apply(Meta.css.minify),
], function() { async.apply(Meta.templates.compile)
], function(err) {
if (!err) {
emitter.emit('nodebb:ready'); emitter.emit('nodebb:ready');
callback.apply(null, arguments); callback.apply(null, arguments);
} else {
console.log('failed!');
emitter.emit('nodebb:reload.failed');
}
}); });
}); });
}; };

@ -0,0 +1,89 @@
var mkdirp = require('mkdirp'),
rimraf = require('rimraf'),
winston = require('winston'),
async = require('async'),
path = require('path'),
fs = require('fs'),
nconf = require('nconf'),
emitter = require('../emitter'),
plugins = require('../plugins'),
utils = require('../../public/src/utils'),
Templates = {};
Templates.compile = function(callback) {
var baseTemplatesPath = nconf.get('base_templates_path'),
viewsPath = nconf.get('views_dir'),
themeTemplatesPath = nconf.get('theme_templates_path');
plugins.getTemplates(function(err, pluginTemplates) {
winston.info('[meta/templates] Compiling templates');
rimraf.sync(viewsPath);
mkdirp.sync(viewsPath);
async.parallel({
baseTpls: function(next) {
utils.walk(baseTemplatesPath, next);
},
themeTpls: function(next) {
utils.walk(themeTemplatesPath, next);
}
}, function(err, data) {
var baseTpls = data.baseTpls,
themeTpls = data.themeTpls,
paths = {};
if (!baseTpls || !themeTpls) {
winston.warn('[meta/templates] Could not find base template files at: ' + baseTemplatesPath);
}
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
baseTpls.forEach(function(el, i) {
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
});
themeTpls.forEach(function(el, i) {
paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]);
});
for (var tpl in pluginTemplates) {
if (pluginTemplates.hasOwnProperty(tpl)) {
paths[tpl] = pluginTemplates[tpl];
}
}
async.each(Object.keys(paths), function(relativePath, next) {
var file = fs.readFileSync(paths[relativePath]).toString(),
matches = null,
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
while(matches = file.match(regex)) {
var partial = "/" + matches[1];
if (paths[partial] && relativePath !== partial) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
} else {
winston.warn('[themes] Partial not loaded: ' + matches[1]);
file = file.replace(regex, "");
}
}
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/')));
fs.writeFile(path.join(viewsPath, relativePath), file, next);
}, function(err) {
if (err) {
winston.error(err);
} else {
winston.info('[themes] Successfully compiled templates.');
emitter.emit('templates:compiled');
if (callback) callback();
}
});
});
});
};
module.exports = Templates;

@ -24,10 +24,7 @@ var utils = require('./../../public/src/utils'),
session = require('express-session'), session = require('express-session'),
relativePath, relativePath,
viewsPath, themesPath;
themesPath,
baseTemplatesPath,
themeTemplatesPath;
var middleware = {}; var middleware = {};
@ -68,91 +65,17 @@ function routeCurrentTheme(app, themeId, themesData) {
// Theme's templates path // Theme's templates path
nconf.set('theme_templates_path', themeObj.templates ? path.join(themesPath, themeObj.id, themeObj.templates) : nconf.get('base_templates_path')); nconf.set('theme_templates_path', themeObj.templates ? path.join(themesPath, themeObj.id, themeObj.templates) : nconf.get('base_templates_path'));
themeTemplatesPath = nconf.get('theme_templates_path');
}
function compileTemplates(pluginTemplates) {
var mkdirp = require('mkdirp'),
rimraf = require('rimraf');
winston.info('[themes] Compiling templates');
rimraf.sync(viewsPath);
mkdirp.sync(viewsPath);
async.parallel({
baseTpls: function(next) {
utils.walk(baseTemplatesPath, next);
},
themeTpls: function(next) {
utils.walk(themeTemplatesPath, next);
}
}, function(err, data) {
var baseTpls = data.baseTpls,
themeTpls = data.themeTpls,
paths = {};
if (!baseTpls || !themeTpls) {
winston.warn('[themes] Could not find base template files at: ' + baseTemplatesPath);
}
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
baseTpls.forEach(function(el, i) {
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
});
themeTpls.forEach(function(el, i) {
paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]);
});
for (var tpl in pluginTemplates) {
if (pluginTemplates.hasOwnProperty(tpl)) {
paths[tpl] = pluginTemplates[tpl];
}
}
async.each(Object.keys(paths), function(relativePath, next) {
var file = fs.readFileSync(paths[relativePath]).toString(),
matches = null,
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
while(matches = file.match(regex)) {
var partial = "/" + matches[1];
if (paths[partial] && relativePath !== partial) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
} else {
winston.warn('[themes] Partial not loaded: ' + matches[1]);
file = file.replace(regex, "");
}
}
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/')));
fs.writeFile(path.join(viewsPath, relativePath), file, next);
}, function(err) {
if (err) {
winston.error(err);
} else {
winston.info('[themes] Successfully compiled templates.');
emitter.emit('templates:compiled');
}
});
});
} }
module.exports = function(app, data) { module.exports = function(app, data) {
middleware = require('./middleware')(app); middleware = require('./middleware')(app);
relativePath = nconf.get('relative_path'); relativePath = nconf.get('relative_path');
viewsPath = nconf.get('views_dir');
themesPath = nconf.get('themes_path'); themesPath = nconf.get('themes_path');
baseTemplatesPath = nconf.get('base_templates_path');
app.engine('tpl', templates.__express); app.engine('tpl', templates.__express);
app.set('view engine', 'tpl'); app.set('view engine', 'tpl');
app.set('views', viewsPath); app.set('views', nconf.get('views_dir'));
app.set('json spaces', process.env.NODE_ENV === 'development' ? 4 : 0); app.set('json spaces', process.env.NODE_ENV === 'development' ? 4 : 0);
app.use(flash()); app.use(flash());
@ -205,10 +128,7 @@ module.exports = function(app, data) {
routeCurrentTheme(app, data.currentThemeId, data.themesData); routeCurrentTheme(app, data.currentThemeId, data.themesData);
routeThemeScreenshots(app, data.themesData); routeThemeScreenshots(app, data.themesData);
plugins.getTemplates(function(err, pluginTemplates) { meta.templates.compile();
compileTemplates(pluginTemplates);
});
return middleware; return middleware;
}; };

Loading…
Cancel
Save