mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 17:16:14 +01:00
optimizing template compilation
This commit is contained in:
@@ -71,57 +71,64 @@ function compileTemplates(pluginTemplates) {
|
||||
rimraf.sync(viewsPath);
|
||||
mkdirp.sync(viewsPath);
|
||||
|
||||
utils.walk(baseTemplatesPath, function(err, baseTpls) {
|
||||
utils.walk(themeTemplatesPath, function (err, themeTpls) {
|
||||
var paths = {};
|
||||
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);
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
|
||||
themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
|
||||
async.each(Object.keys(paths), function(relativePath, next) {
|
||||
var file = fs.readFileSync(paths[relativePath]).toString(),
|
||||
matches = null,
|
||||
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
|
||||
|
||||
baseTpls.forEach(function(el, i) {
|
||||
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
|
||||
});
|
||||
while(matches = file.match(regex)) {
|
||||
var partial = "/" + matches[1];
|
||||
|
||||
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);
|
||||
if (paths[partial] && relativePath !== partial) {
|
||||
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
|
||||
} else {
|
||||
winston.info('[themes] Successfully compiled templates.');
|
||||
emitter.emit('templates:compiled');
|
||||
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');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user