diff --git a/.gitignore b/.gitignore index 8f5c444..907d18c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea/* +build/* miner-webinterface.iml node_modules/* config/config.js diff --git a/Gruntfile.js b/Gruntfile.js index 70ca736..3bb753d 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -7,6 +7,7 @@ module.exports = function (grunt) { files: { src: [ '**/*.js', + '!build/**/*.js', '!node_modules/**/*.js', '!frontend/public/**/*.js' ] @@ -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']); }; \ No newline at end of file diff --git a/lib/View.js b/lib/View.js index 52b3ffe..2aa18cb 100644 --- a/lib/View.js +++ b/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'), @@ -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 () { diff --git a/package.json b/package.json index 5e1d258..0187203 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/specs/lib/View.js b/test/specs/lib/View.js index 013a712..0094f7f 100644 --- a/test/specs/lib/View.js +++ b/test/specs/lib/View.js @@ -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 @@ -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); }); });