var gulp = require('gulp');
var handlebars = require('gulp-handlebars');
var wrap = require('gulp-wrap');
var declare = require('gulp-declare');
var concat = require('gulp-concat');
var through = require('through');

var cache = '';

function TemplateCache() {}

TemplateCache.prototype.compile = function compile()
{
	if (cache)
		return;

	var sourcePaths = [
		__dirname+'/../views/**/*.handlebars',
		'!'+__dirname+'/../views/layouts/*.handlebars',
		'!'+__dirname+'/../views/*.handlebars',
	];

	gulp.src(sourcePaths)
	.pipe(handlebars())
	.pipe(wrap('Handlebars.template(<%= contents %>)'))
	.pipe(declare(
	{
		namespace: 'E2',
		noRedeclare: true, // Avoid duplicate declarations
		processName: declare.processNameByPath
	}))
	.pipe(concat('templates.js'))
	.on('data', function(data) {
		cache += data.contents;
	});

	return this;
};

TemplateCache.prototype.helper = function()
{
	return {
		templateCache: function()
		{
			return cache;
		}
	};
};

module.exports = TemplateCache;

@@ -516,16 +516,17 @@
 ePaths)%0A
+%09
 %09.pipe(h
@@ -538,16 +538,17 @@
 bars())%0A
+%09
 %09.pipe(w
@@ -592,16 +592,17 @@
  %25%3E)'))%0A
+%09
 %09.pipe(d
@@ -609,19 +609,21 @@
 eclare(%0A
+%09
 %09%7B%0A
+%09
 %09%09namesp
@@ -633,16 +633,17 @@
 : 'E2',%0A
+%09
 %09%09noRede
@@ -689,16 +689,17 @@
 tions%0A%09%09
+%09
 processN
@@ -733,13 +733,15 @@
 ath%0A
+%09
 %09%7D))%0A
+%09
 %09.pi
@@ -767,16 +767,17 @@
 s.js'))%0A
+%09
 %09.on('da
@@ -800,16 +800,17 @@
 ta) %7B%0A%09%09
+%09
 cache +=
@@ -825,16 +825,17 @@
 ntents;%0A
+%09
 %09%7D);%0A%0A%09r
@@ -896,16 +896,35 @@
 ion()%0A%7B%0A
+%09var that = this;%0A%0A
 %09return 
@@ -953,20 +953,113 @@
 ction()%0A
-
 %09%09%7B%0A
+%09%09%09if (process.env.NODE_ENV !== 'production')%0A%09%09%09%7B%0A%09%09%09%09cache = '';%0A%09%09%09%09that.compile();%0A%09%09%09%7D%0A%0A
 %09%09%09retur

recompile templates every time in dev