Fix #5549, improve tpl compilation (#5551)

v1.18.x
Peter Jaszkowiak 8 years ago committed by psychobunny
parent c1c96668af
commit ff09d6e0dd

@ -109,6 +109,34 @@ function compile(callback) {
var baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')]; var baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')];
var viewsPath = nconf.get('views_dir'); var viewsPath = nconf.get('views_dir');
function processImports(paths, relativePath, source, callback) {
var regex = /<!-- IMPORT (.+?) -->/;
var matches = source.match(regex);
if (!matches) {
return callback(null, source);
}
var partial = '/' + matches[1];
if (paths[partial] && relativePath !== partial) {
fs.readFile(paths[partial], function (err, file) {
if (err) {
return callback(err);
}
var partialSource = file.toString();
source = source.replace(regex, partialSource);
processImports(paths, relativePath, source, callback);
});
} else {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
source = source.replace(regex, '');
processImports(paths, relativePath, source, callback);
}
}
preparePaths(baseTemplatesPaths, function (err, paths) { preparePaths(baseTemplatesPaths, function (err, paths) {
if (err) { if (err) {
@ -116,24 +144,23 @@ function compile(callback) {
} }
async.each(Object.keys(paths), function (relativePath, next) { async.each(Object.keys(paths), function (relativePath, next) {
var file = fs.readFileSync(paths[relativePath]).toString(); async.waterfall([
var regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/; function (next) {
var matches = file.match(regex); fs.readFile(paths[relativePath], next);
},
while (matches !== null) { function (file, next) {
var partial = '/' + matches[1]; var source = file.toString();
processImports(paths, relativePath, source, next);
if (paths[partial] && relativePath !== partial) { },
file = file.replace(regex, fs.readFileSync(paths[partial]).toString()); function (compiled, next) {
} else { mkdirp(path.join(viewsPath, path.dirname(relativePath)), function (err) {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]); next(err, compiled);
file = file.replace(regex, ''); });
} },
matches = file.match(regex); function (compiled, next) {
} fs.writeFile(path.join(viewsPath, relativePath), compiled, next);
},
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/'))); ], next);
fs.writeFile(path.join(viewsPath, relativePath), file, next);
}, function (err) { }, function (err) {
if (err) { if (err) {
winston.error('[meta/templates] ' + err.stack); winston.error('[meta/templates] ' + err.stack);

Loading…
Cancel
Save