optimizing template compilation

v1.18.x
psychobunny 11 years ago
parent c97d6635fc
commit 257491801d

@ -71,57 +71,64 @@ function compileTemplates(pluginTemplates) {
rimraf.sync(viewsPath); rimraf.sync(viewsPath);
mkdirp.sync(viewsPath); mkdirp.sync(viewsPath);
utils.walk(baseTemplatesPath, function(err, baseTpls) { async.parallel({
utils.walk(themeTemplatesPath, function (err, themeTpls) { baseTpls: function(next) {
var paths = {}; 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) { if (!baseTpls || !themeTpls) {
winston.warn('[themes] Could not find base template files at: ' + baseTemplatesPath); winston.warn('[themes] Could not find base template files at: ' + baseTemplatesPath);
} }
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); }); baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); }); themeTpls = !themeTpls ? [] : themeTpls.map(function(tpl) { return tpl.replace(themeTemplatesPath, ''); });
baseTpls.forEach(function(el, i) { baseTpls.forEach(function(el, i) {
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]); paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]);
}); });
themeTpls.forEach(function(el, i) { themeTpls.forEach(function(el, i) {
paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]); paths[themeTpls[i]] = path.join(themeTemplatesPath, themeTpls[i]);
}); });
for (var tpl in pluginTemplates) { for (var tpl in pluginTemplates) {
if (pluginTemplates.hasOwnProperty(tpl)) { if (pluginTemplates.hasOwnProperty(tpl)) {
paths[tpl] = pluginTemplates[tpl]; paths[tpl] = pluginTemplates[tpl];
}
} }
}
async.each(Object.keys(paths), function(relativePath, next) { async.each(Object.keys(paths), function(relativePath, next) {
var file = fs.readFileSync(paths[relativePath]).toString(), var file = fs.readFileSync(paths[relativePath]).toString(),
matches = null, matches = null,
regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/; regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
while(matches = file.match(regex)) {
var partial = "/" + matches[1];
if (paths[partial] && relativePath !== partial) { while(matches = file.match(regex)) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString()); var partial = "/" + matches[1];
} else {
winston.warn('[themes] Partial not loaded: ' + matches[1]);
file = file.replace(regex, "");
}
}
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/'))); if (paths[partial] && relativePath !== partial) {
fs.writeFile(path.join(viewsPath, relativePath), file, next); file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
}, function(err) {
if (err) {
winston.error(err);
} else { } else {
winston.info('[themes] Successfully compiled templates.'); winston.warn('[themes] Partial not loaded: ' + matches[1]);
emitter.emit('templates:compiled'); 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');
}
}); });
}); });
} }

Loading…
Cancel
Save