mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	Precompile all templates
- Benchpress compilation is 33x faster now - Native module with JS fallback and pre-built binaries - Dev template build is <1sec now - Minified template build is ~5sec (uglify accounts for almost all)
This commit is contained in:
		| @@ -2,13 +2,11 @@ | ||||
|  | ||||
| var async = require('async'); | ||||
| var path = require('path'); | ||||
| var fs = require('fs'); | ||||
| var csrf = require('csurf'); | ||||
| var validator = require('validator'); | ||||
| var nconf = require('nconf'); | ||||
| var ensureLoggedIn = require('connect-ensure-login'); | ||||
| var toobusy = require('toobusy-js'); | ||||
| var Benchpress = require('benchpressjs'); | ||||
| var LRU = require('lru-cache'); | ||||
|  | ||||
| var plugins = require('../plugins'); | ||||
| @@ -207,58 +205,3 @@ middleware.delayLoading = function (req, res, next) { | ||||
|  | ||||
| 	setTimeout(next, 1000); | ||||
| }; | ||||
|  | ||||
| var viewsDir = nconf.get('views_dir'); | ||||
| var workingCache = {}; | ||||
|  | ||||
| middleware.templatesOnDemand = function (req, res, next) { | ||||
| 	var filePath = req.filePath || path.join(viewsDir, req.path); | ||||
| 	if (!filePath.endsWith('.js')) { | ||||
| 		return next(); | ||||
| 	} | ||||
| 	var tplPath = filePath.replace(/\.js$/, '.tpl'); | ||||
| 	if (workingCache[filePath]) { | ||||
| 		workingCache[filePath].push(next); | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	async.waterfall([ | ||||
| 		function (cb) { | ||||
| 			file.exists(filePath, cb); | ||||
| 		}, | ||||
| 		function (exists, cb) { | ||||
| 			if (exists) { | ||||
| 				return next(); | ||||
| 			} | ||||
|  | ||||
| 			// need to check here again | ||||
| 			// because compilation could have started since last check | ||||
| 			if (workingCache[filePath]) { | ||||
| 				workingCache[filePath].push(next); | ||||
| 				return; | ||||
| 			} | ||||
|  | ||||
| 			workingCache[filePath] = [next]; | ||||
| 			fs.readFile(tplPath, 'utf8', cb); | ||||
| 		}, | ||||
| 		function (source, cb) { | ||||
| 			Benchpress.precompile({ | ||||
| 				source: source, | ||||
| 				minify: global.env !== 'development', | ||||
| 			}, cb); | ||||
| 		}, | ||||
| 		function (compiled, cb) { | ||||
| 			if (!compiled) { | ||||
| 				return cb(new Error('[[error:templatesOnDemand.compiled-template-empty, ' + tplPath + ']]')); | ||||
| 			} | ||||
| 			fs.writeFile(filePath, compiled, cb); | ||||
| 		}, | ||||
| 	], function (err) { | ||||
| 		var arr = workingCache[filePath]; | ||||
| 		workingCache[filePath] = null; | ||||
|  | ||||
| 		arr.forEach(function (callback) { | ||||
| 			callback(err); | ||||
| 		}); | ||||
| 	}); | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user