Skip to content

Commit e54e3b3

Browse files
committed
feat(engine-nunjucks): Configurable extension locations; Use usePatternlabConfig()
1 parent a01e040 commit e54e3b3

File tree

2 files changed

+87
-32
lines changed

2 files changed

+87
-32
lines changed

packages/engine-nunjucks/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,34 @@ Level of Support is more or less full. Partial calls and lineage hunting are sup
1717

1818
## Extending the Nunjucks instance
1919

20-
To add custom filters or make customizations to the nunjucks instance, create a file named `patternlab-nunjucks-config.js` in the root of your Pattern Lab project. `patternlab-nunjucks-config.js` should export a function with the Nunjucks environment as parameter.
20+
To add custom filters or make customizations to the nunjucks instance, add the following to `patternlab-config.json`:
2121

22+
```json
23+
{
24+
...
25+
"engines": {
26+
"nunjucks": {
27+
"extensions": [
28+
"nunjucks-extensions/*.js"
29+
]
30+
}
31+
}
32+
}
2233
```
34+
35+
...or use the default file name: `patternlab-nunjucks-config.js` (in the root of your Pattern Lab project).
36+
37+
Each file providing extensions should export a function with the Nunjucks environment as parameter.
38+
39+
```js
2340
module.exports = function (env) {
2441
[YOUR CUSTOM CODE HERE]
2542
};
2643
```
2744

2845
Example: `patternlab-nunjucks-config.js` file that uses lodash and adds three custom filters.
29-
```
46+
47+
```js
3048
var _shuffle = require('lodash/shuffle'),
3149
_take = require('lodash/take');
3250

packages/engine-nunjucks/lib/engine_nunjucks.js

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,10 @@
2222

2323
const fs = require('fs-extra');
2424
const path = require('path');
25-
const plPath = process.cwd();
26-
const plConfig = require(path.join(plPath, 'patternlab-config.json'));
2725
const nunjucks = require('nunjucks');
2826
const partialRegistry = [];
2927

30-
// Create Pattern Loader
31-
// Since Pattern Lab includes are not path based we need a custom loader for Nunjucks.
32-
function PatternLoader() {}
33-
34-
PatternLoader.prototype.getSource = function(name) {
35-
const fullPath = path.resolve(
36-
plConfig.paths.source.patterns,
37-
partialRegistry[name]
38-
);
39-
return {
40-
src: fs.readFileSync(fullPath, 'utf-8'),
41-
path: fullPath,
42-
noCache: true
43-
};
44-
};
45-
46-
const env = new nunjucks.Environment(new PatternLoader());
47-
48-
// Load any user Defined configurations
49-
try {
50-
const nunjucksConfig = require(path.join(
51-
plPath,
52-
'patternlab-nunjucks-config.js'
53-
));
54-
if (typeof nunjucksConfig === 'function') {
55-
nunjucksConfig(env);
56-
}
57-
} catch (err) {}
28+
let env;
5829

5930
// Nunjucks Engine
6031
const engine_nunjucks = {
@@ -153,6 +124,72 @@ const engine_nunjucks = {
153124
this.spawnFile(config, '_00-head.njk');
154125
this.spawnFile(config, '_01-foot.njk');
155126
},
127+
128+
/**
129+
* Accept a Pattern Lab config object from the core and use the settings to
130+
* load helpers.
131+
*
132+
* @param {object} config - the global config object from core
133+
*/
134+
usePatternLabConfig: function(config) {
135+
// Create Pattern Loader
136+
// Since Pattern Lab includes are not path based we need a custom loader for Nunjucks.
137+
function PatternLoader() {}
138+
139+
PatternLoader.prototype.getSource = function(name) {
140+
const fullPath = path.resolve(
141+
config.paths.source.patterns,
142+
partialRegistry[name]
143+
);
144+
return {
145+
src: fs.readFileSync(fullPath, 'utf-8'),
146+
path: fullPath,
147+
noCache: true,
148+
};
149+
};
150+
151+
env = new nunjucks.Environment(new PatternLoader());
152+
153+
let extensions;
154+
155+
try {
156+
// Look for helpers in the config
157+
extensions = config.engines.nunjucks.helpers;
158+
159+
if (typeof extensions === 'string') {
160+
extensions = [extensions];
161+
}
162+
} catch (error) {
163+
// No defined path(s) found, look in default location
164+
165+
const configPath = 'patternlab-nunjucks-config.js';
166+
if (fs.existsSync(path.join(process.cwd(), configPath))) {
167+
extensions = [configPath];
168+
}
169+
}
170+
171+
if (extensions) {
172+
extensions.forEach(extensionPath => {
173+
// Load any user Defined configurations
174+
const nunjucksConfigPath = path.join(process.cwd(), extensionPath);
175+
176+
try {
177+
const nunjucksConfig = require(nunjucksConfigPath);
178+
if (typeof nunjucksConfig === 'function') {
179+
nunjucksConfig(env);
180+
} else {
181+
console.error(
182+
`Failed to load Nunjucks extension: Expected ${extensionPath} to export a function.`
183+
);
184+
}
185+
} catch (err) {
186+
console.error(
187+
`Failed to load Nunjucks extension ${nunjucksConfigPath}.`
188+
);
189+
}
190+
});
191+
}
192+
},
156193
};
157194

158195
module.exports = engine_nunjucks;

0 commit comments

Comments
 (0)