Fix #5549, improve tpl compilation (#5551)

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

@ -109,31 +109,58 @@ 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 (.+?) -->/;
preparePaths(baseTemplatesPaths, function (err, paths) { 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) { if (err) {
return callback(err); return callback(err);
} }
async.each(Object.keys(paths), function (relativePath, next) { var partialSource = file.toString();
var file = fs.readFileSync(paths[relativePath]).toString(); source = source.replace(regex, partialSource);
var regex = /[ \t]*<!-- IMPORT ([\s\S]*?)? -->[ \t]*/;
var matches = file.match(regex);
while (matches !== null) { processImports(paths, relativePath, source, callback);
var partial = '/' + matches[1]; });
if (paths[partial] && relativePath !== partial) {
file = file.replace(regex, fs.readFileSync(paths[partial]).toString());
} else { } else {
winston.warn('[meta/templates] Partial not loaded: ' + matches[1]); winston.warn('[meta/templates] Partial not loaded: ' + matches[1]);
file = file.replace(regex, ''); source = source.replace(regex, '');
processImports(paths, relativePath, source, callback);
} }
matches = file.match(regex);
} }
mkdirp.sync(path.join(viewsPath, relativePath.split('/').slice(0, -1).join('/'))); preparePaths(baseTemplatesPaths, function (err, paths) {
fs.writeFile(path.join(viewsPath, relativePath), file, next); if (err) {
return callback(err);
}
async.each(Object.keys(paths), function (relativePath, next) {
async.waterfall([
function (next) {
fs.readFile(paths[relativePath], next);
},
function (file, next) {
var source = file.toString();
processImports(paths, relativePath, source, next);
},
function (compiled, next) {
mkdirp(path.join(viewsPath, path.dirname(relativePath)), function (err) {
next(err, compiled);
});
},
function (compiled, next) {
fs.writeFile(path.join(viewsPath, relativePath), compiled, next);
},
], next);
}, function (err) { }, function (err) {
if (err) { if (err) {
winston.error('[meta/templates] ' + err.stack); winston.error('[meta/templates] ' + err.stack);

Loading…
Cancel
Save