Skip to content

Commit a12df36

Browse files
committed
feat(engine-handlebars): Load Handlebars helpers specified in the config
1 parent ad1e782 commit a12df36

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/engine-handlebars/lib/engine_handlebars.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
const fs = require('fs-extra');
2626
const path = require('path');
2727
const Handlebars = require('handlebars');
28+
const glob = require('glob');
2829

2930
// regexes, stored here so they're only compiled once
3031
const findPartialsRE = /{{#?>\s*([\w-\/.]+)(?:.|\s+)*?}}/g;
@@ -122,6 +123,30 @@ const engine_handlebars = {
122123
this.spawnFile(config, '_00-head.hbs');
123124
this.spawnFile(config, '_01-foot.hbs');
124125
},
126+
127+
/**
128+
* Accept a Pattern Lab config object from the core and put it in
129+
* this module's closure scope so we can configure engine behavior.
130+
*
131+
* @param {object} config - the global config object from core
132+
*/
133+
usePatternLabConfig: function(config) {
134+
try {
135+
let helpers = config.engines.handlebars.helpers;
136+
137+
if (typeof helpers === 'string') {
138+
helpers = [helpers];
139+
}
140+
141+
helpers.forEach(globPattern => {
142+
glob.sync(globPattern).forEach(filePath => {
143+
require(path.join(process.cwd(), filePath))(Handlebars);
144+
});
145+
});
146+
} catch (error) {
147+
// No helpers to load
148+
}
149+
},
125150
};
126151

127152
module.exports = engine_handlebars;

packages/engine-handlebars/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"main": "lib/engine_handlebars.js",
66
"dependencies": {
77
"fs-extra": "0.30.0",
8-
"handlebars": "4.0.5"
8+
"handlebars": "4.0.5",
9+
"glob": "7.0.0"
910
},
1011
"keywords": [
1112
"Pattern Lab",

0 commit comments

Comments
 (0)