Skip to content

Commit

Permalink
use precompiled templates
Browse files Browse the repository at this point in the history
  • Loading branch information
selaux committed Mar 9, 2014
1 parent 6bdcedf commit 9b91c5f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.idea/*
build/*
miner-webinterface.iml
node_modules/*
config/config.js
Expand Down
19 changes: 19 additions & 0 deletions Gruntfile.js
Expand Up @@ -7,6 +7,7 @@ module.exports = function (grunt) {
files: {
src: [
'**/*.js',
'!build/**/*.js',
'!node_modules/**/*.js',
'!frontend/public/**/*.js'
]
Expand Down Expand Up @@ -39,12 +40,30 @@ module.exports = function (grunt) {
'test/specs/**/*.js'
]
}
},

handlebars: {
compile: {
options: {
commonjs: true,
namespace: false,
processName: function (path) {
var filename = path.split('/')[1];
return filename.substr(0, filename.length-4);
}
},
files: {
'build/compiledTemplates.js': 'templates/*.hbs'
}
}
}

});

grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-handlebars');
grunt.loadNpmTasks('grunt-mocha-test');

grunt.registerTask('compile', ['handlebars']);
grunt.registerTask('test', ['jshint', 'mochaTest']);
};
7 changes: 2 additions & 5 deletions lib/View.js
@@ -1,9 +1,6 @@
'use strict';

var fs = require('fs'),
path = require('path'),

_ = require('lodash'),
var _ = require('lodash'),
extendMixin = require('./extend'),

Handlebars = require('./handlebars/handlebars'),
Expand All @@ -19,7 +16,7 @@ View = function (module) {
extendMixin(View);

View.prototype.getCompiledTemplate = function (templateName) {
return Handlebars.compile(fs.readFileSync(path.join(__dirname, '/../templates/' + templateName + '.hbs')).toString());
return require('../build/compiledTemplates')(Handlebars)[templateName];
};

View.prototype.render = function () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"socket.io": "0.9.16",
"grunt": "0.4.1",
"grunt-cli": "0.1.9",
"grunt-contrib-handlebars": "0.7.0",
"handlebars": "1.0.12",
"nodemailer": "0.5.3",
"numeral": "1.5.2",
Expand Down
12 changes: 3 additions & 9 deletions test/specs/lib/View.js
Expand Up @@ -88,13 +88,9 @@ describe('View', function () {

describe('getCompiledTemplate', function () {
var compiledTemplate = function () {},
compiledTemplatesStub = sinon.stub().returns({ templateName: compiledTemplate }),
requires = {
fs: {
readFileSync: sinon.stub().returns('template string')
},
'./handlebars/handlebars': {
compile: sinon.stub().returns(compiledTemplate)
}
'../build/compiledTemplates': compiledTemplatesStub
},
View = SandboxedModule.require('../../../lib/View', {
requires: requires
Expand All @@ -103,9 +99,7 @@ describe('View', function () {
it('should compile the template residing on the disk', function () {
var result = View.prototype.getCompiledTemplate('templateName');

expect(requires.fs.readFileSync).to.have.been.calledOnce;
expect(requires['./handlebars/handlebars'].compile).to.have.been.calledOnce;
expect(requires['./handlebars/handlebars'].compile).to.have.been.calledWith('template string');
expect(compiledTemplatesStub).to.have.been.calledOnce;
expect(result).to.equal(compiledTemplate);
});
});
Expand Down

0 comments on commit 9b91c5f

Please sign in to comment.