Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ coverage/
run/
.DS_Store
*.swp

package-lock.json
1 change: 1 addition & 0 deletions .nyc_output/6132da34-f6a6-454d-9681-83c7886dfaec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"/Users/daifee/coding/egg-view-handlebars/config/config.default.js":{"path":"/Users/daifee/coding/egg-view-handlebars/config/config.default.js","statementMap":{"0":{"start":{"line":3,"column":13},"end":{"line":3,"column":28}},"1":{"start":{"line":5,"column":0},"end":{"line":31,"column":3}},"2":{"start":{"line":5,"column":29},"end":{"line":31,"column":1}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":5,"column":17},"end":{"line":5,"column":18}},"loc":{"start":{"line":5,"column":29},"end":{"line":31,"column":1}},"line":5}},"branchMap":{},"s":{"0":1,"1":1,"2":4},"f":{"0":4},"b":{},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"b31e9eeacb3f92f584a91defd204946625b414ed","contentHash":"55579a9bdfda8553a4ee7f7a32922b5c"},"/Users/daifee/coding/egg-view-handlebars/app.js":{"path":"/Users/daifee/coding/egg-view-handlebars/app.js","statementMap":{"0":{"start":{"line":3,"column":13},"end":{"line":3,"column":28}},"1":{"start":{"line":4,"column":11},"end":{"line":4,"column":27}},"2":{"start":{"line":5,"column":19},"end":{"line":5,"column":40}},"3":{"start":{"line":7,"column":16},"end":{"line":7,"column":33}},"4":{"start":{"line":8,"column":19},"end":{"line":8,"column":28}},"5":{"start":{"line":11,"column":0},"end":{"line":51,"column":2}},"6":{"start":{"line":12,"column":2},"end":{"line":12,"column":23}},"7":{"start":{"line":16,"column":6},"end":{"line":16,"column":25}},"8":{"start":{"line":20,"column":21},"end":{"line":20,"column":36}},"9":{"start":{"line":21,"column":20},"end":{"line":21,"column":64}},"10":{"start":{"line":22,"column":21},"end":{"line":22,"column":41}},"11":{"start":{"line":25,"column":6},"end":{"line":27,"column":7}},"12":{"start":{"line":26,"column":8},"end":{"line":26,"column":34}},"13":{"start":{"line":29,"column":6},"end":{"line":36,"column":7}},"14":{"start":{"line":30,"column":24},"end":{"line":30,"column":55}},"15":{"start":{"line":31,"column":8},"end":{"line":31,"column":51}},"16":{"start":{"line":33,"column":8},"end":{"line":35,"column":9}},"17":{"start":{"line":34,"column":10},"end":{"line":34,"column":41}},"18":{"start":{"line":37,"column":6},"end":{"line":37,"column":31}},"19":{"start":{"line":41,"column":23},"end":{"line":41,"column":54}},"20":{"start":{"line":42,"column":6},"end":{"line":42,"column":31}},"21":{"start":{"line":46,"column":6},"end":{"line":46,"column":71}},"22":{"start":{"line":47,"column":6},"end":{"line":47,"column":50}},"23":{"start":{"line":50,"column":2},"end":{"line":50,"column":45}},"24":{"start":{"line":54,"column":19},"end":{"line":54,"column":35}},"25":{"start":{"line":55,"column":2},"end":{"line":59,"column":3}},"26":{"start":{"line":56,"column":4},"end":{"line":58,"column":5}},"27":{"start":{"line":57,"column":6},"end":{"line":57,"column":53}},"28":{"start":{"line":64,"column":23},"end":{"line":64,"column":57}},"29":{"start":{"line":68,"column":19},"end":{"line":68,"column":21}},"30":{"start":{"line":69,"column":16},"end":{"line":69,"column":44}},"31":{"start":{"line":70,"column":2},"end":{"line":79,"column":3}},"32":{"start":{"line":71,"column":17},"end":{"line":71,"column":46}},"33":{"start":{"line":72,"column":17},"end":{"line":72,"column":34}},"34":{"start":{"line":73,"column":4},"end":{"line":73,"column":33}},"35":{"start":{"line":73,"column":24},"end":{"line":73,"column":33}},"36":{"start":{"line":75,"column":4},"end":{"line":77,"column":65}},"37":{"start":{"line":77,"column":35},"end":{"line":77,"column":63}},"38":{"start":{"line":78,"column":4},"end":{"line":78,"column":54}},"39":{"start":{"line":80,"column":2},"end":{"line":80,"column":18}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":11,"column":17},"end":{"line":11,"column":18}},"loc":{"start":{"line":11,"column":24},"end":{"line":51,"column":1}},"line":11},"1":{"name":"(anonymous_1)","decl":{"start":{"line":15,"column":4},"end":{"line":15,"column":5}},"loc":{"start":{"line":15,"column":21},"end":{"line":17,"column":5}},"line":15},"2":{"name":"(anonymous_2)","decl":{"start":{"line":19,"column":4},"end":{"line":19,"column":5}},"loc":{"start":{"line":19,"column":41},"end":{"line":38,"column":5}},"line":19},"3":{"name":"(anonymous_3)","decl":{"start":{"line":40,"column":4},"end":{"line":40,"column":5}},"loc":{"start":{"line":40,"column":50},"end":{"line":43,"column":5}},"line":40},"4":{"name":"(anonymous_4)","decl":{"start":{"line":45,"column":4},"end":{"line":45,"column":5}},"loc":{"start":{"line":45,"column":32},"end":{"line":48,"column":5}},"line":45},"5":{"name":"registerPartial","decl":{"start":{"line":53,"column":9},"end":{"line":53,"column":24}},"loc":{"start":{"line":53,"column":30},"end":{"line":60,"column":1}},"line":53},"6":{"name":"loadPartial","decl":{"start":{"line":63,"column":9},"end":{"line":63,"column":20}},"loc":{"start":{"line":63,"column":26},"end":{"line":81,"column":1}},"line":63},"7":{"name":"(anonymous_7)","decl":{"start":{"line":77,"column":30},"end":{"line":77,"column":31}},"loc":{"start":{"line":77,"column":35},"end":{"line":77,"column":63}},"line":77}},"branchMap":{"0":{"loc":{"start":{"line":21,"column":20},"end":{"line":21,"column":64}},"type":"binary-expr","locations":[{"start":{"line":21,"column":20},"end":{"line":21,"column":37}},{"start":{"line":21,"column":41},"end":{"line":21,"column":64}}],"line":21},"1":{"loc":{"start":{"line":25,"column":6},"end":{"line":27,"column":7}},"type":"if","locations":[{"start":{"line":25,"column":6},"end":{"line":27,"column":7}},{"start":{"line":25,"column":6},"end":{"line":27,"column":7}}],"line":25},"2":{"loc":{"start":{"line":29,"column":6},"end":{"line":36,"column":7}},"type":"if","locations":[{"start":{"line":29,"column":6},"end":{"line":36,"column":7}},{"start":{"line":29,"column":6},"end":{"line":36,"column":7}}],"line":29},"3":{"loc":{"start":{"line":33,"column":8},"end":{"line":35,"column":9}},"type":"if","locations":[{"start":{"line":33,"column":8},"end":{"line":35,"column":9}},{"start":{"line":33,"column":8},"end":{"line":35,"column":9}}],"line":33},"4":{"loc":{"start":{"line":55,"column":2},"end":{"line":59,"column":3}},"type":"if","locations":[{"start":{"line":55,"column":2},"end":{"line":59,"column":3}},{"start":{"line":55,"column":2},"end":{"line":59,"column":3}}],"line":55},"5":{"loc":{"start":{"line":73,"column":4},"end":{"line":73,"column":33}},"type":"if","locations":[{"start":{"line":73,"column":4},"end":{"line":73,"column":33}},{"start":{"line":73,"column":4},"end":{"line":73,"column":33}}],"line":73}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":2,"7":9,"8":7,"9":7,"10":7,"11":7,"12":5,"13":7,"14":6,"15":6,"16":6,"17":1,"18":7,"19":2,"20":2,"21":8,"22":8,"23":2,"24":7,"25":7,"26":6,"27":12,"28":7,"29":6,"30":6,"31":6,"32":18,"33":18,"34":18,"35":6,"36":12,"37":12,"38":12,"39":6},"f":{"0":2,"1":9,"2":7,"3":2,"4":8,"5":7,"6":7,"7":12},"b":{"0":[7,7],"1":[5,2],"2":[6,1],"3":[1,5],"4":[6,1],"5":[6,12]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"4e191ee4ef643d66f4de19d6711a6ae58aec23f0","contentHash":"53255dbcf595fd964c777e6c43175b10"}}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ $ npm i egg-view-handlebars --save
exports.handlebars = {
enable: true,
package: 'egg-view-handlebars',
// support cache.
// attention: the `options` param also will be cached
cache: true,
};
```

Expand Down
50 changes: 38 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,61 @@ const fs = require('mz/fs');
const handlebars = require('handlebars');

const COMPILE = Symbol('compile');
const cacheStore = new Map();


module.exports = app => {
const partials = loadPartial(app);
if (partials) {
for (const key of Object.keys(partials)) {
handlebars.registerPartial(key, partials[key]);
}
}
registerPartial(app);

class HandlebarsView {
constructor(ctx) {
this.app = ctx.app;
}

async render(name, context, options) {
const content = await fs.readFile(name, 'utf8');
return this[COMPILE](content, context, options);
const config = this.app.config;
const cache = config.handlebars && config.handlebars.cache;
let compiled = cacheStore.get(name);

// recompile partials
if (!cache) {
registerPartial(this.app);
}

if (!compiled) {
const content = await fs.readFile(name, 'utf8');
compiled = this[COMPILE](content, options);

if (cache) {
cacheStore.set(name, compiled);
}
}
return compiled(context);
}

async renderString(tpl, context, options) {
return this[COMPILE](tpl, context, options);
async renderString(content, context, options) {
const compiled = this[COMPILE](content, options);
return compiled(context);
}

[COMPILE](tpl, context, options) {
return handlebars.compile(tpl, Object.assign({}, this.app.config.handlebars, options))(context);
[COMPILE](content, options) {
options = Object.assign({}, this.app.config.handlebars, options);
return handlebars.compile(content, options);
}
}
app.view.use('handlebars', HandlebarsView);
};

function registerPartial(app) {
const partials = loadPartial(app);
if (partials) {
for (const key of Object.keys(partials)) {
handlebars.registerPartial(key, partials[key]);
}
}
}


function loadPartial(app) {
const partialsPath = app.config.handlebars.partialsPath;
// istanbul ignore next
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/apps/handlebars-cache/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = app => {
app.get('/cache', function* () {
yield this.render('cache');
});

};
1 change: 1 addition & 0 deletions test/fixtures/apps/handlebars-cache/app/view/cache.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version-1
29 changes: 29 additions & 0 deletions test/fixtures/apps/handlebars-cache/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
/**
* app keys
* @type {string}
*/
exports.keys = 'e6099001cb0a0ec3eb8b0e1a54b27060';
/**
* view config
* @member
* @property defaultViewEngine: string setup default view engine
* @property defaultExtension: string template file extension
* @property mapping: Object {string: string}
*/
exports.view = {
defaultViewEngine: 'handlebars',
defaultExtension: '.hbs',
mapping: {
'.hbs': 'handlebars',
},
};

/**
* handlebars config
* @member
* @property {boolean} cache cache template by filename
*/
exports.handlebars = {
cache: true,
};
4 changes: 4 additions & 0 deletions test/fixtures/apps/handlebars-cache/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "handlebars-cache-test",
"version": "0.0.1"
}
4 changes: 4 additions & 0 deletions test/fixtures/apps/handlebars-test/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ module.exports = app => {
});
});

app.get('/fresh', function* () {
yield this.render('fresh');
});

};
2 changes: 2 additions & 0 deletions test/fixtures/apps/handlebars-test/app/view/fresh.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{> freshHead}}
body
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version-1
46 changes: 46 additions & 0 deletions test/handlebars-cache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const request = require('supertest');
const mm = require('egg-mock');
const fs = require('fs');
const path = require('path');

describe('test/handlebars-cache.test.js', () => {
let app;

function writeHbsFile(app, content) {
const tplPath = path.resolve(app.config.view.root[0], './cache.hbs');
fs.writeFileSync(tplPath, content, 'utf-8');
}

before(() => {
app = mm.app({
baseDir: 'apps/handlebars-cache',
});
return app.ready().then(() => {
writeHbsFile(app, 'version-1');
});
});

after(() => {
writeHbsFile(app, 'version-1');
app.close();
});
afterEach(mm.restore);

it('cache compiled', () => {
return request(app.callback())
.get('/cache')
.expect(/version-1/)
.expect(200)
.then(() => {
// 修改模板
writeHbsFile(app, 'version-2');

return request(app.callback())
.get('/cache')
.expect(/version-1/)
.expect(200);
});
});
});
38 changes: 38 additions & 0 deletions test/handlebars.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

const request = require('supertest');
const mm = require('egg-mock');
const path = require('path');
const fs = require('fs');


describe('test/handlebars.test.js', () => {
let app;
Expand Down Expand Up @@ -56,4 +59,39 @@ describe('test/handlebars.test.js', () => {
.expect(/<h2>By Yehuda Katz<\/h2>\n<div class="body">/)
.expect(200);
});


describe('优化本地开发,不缓存partials', () => {
function writeHbsFile(app, content) {
const tplPath = path.resolve(app.config.view.root[0], './partials/fresh_head.hbs');
fs.writeFileSync(tplPath, content, 'utf-8');
}

before(() => {
return app.ready(() => {
writeHbsFile(app, 'version-1');
});
});

after(() => {
writeHbsFile(app, 'version-1');
});

it('should GET /fresh', () => {
return request(app.callback())
.get('/fresh')
.expect(/version-1\n+body/)
.expect(200)
.then(() => {
// 编辑文件
writeHbsFile(app, 'version-2');

return request(app.callback())
.get('/fresh')
.expect(/version-2\n+body/)
.expect(200);
});
});
});

});