Skip to content

Commit 926c176

Browse files
Dan Whitebmuenzenmeyer
authored andcommitted
Remove lodash and add ability to have user customizations
1 parent 98db8c0 commit 926c176

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

packages/patternengine-node-nunjucks/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,33 @@
55
To install the Nunjucks engine in your edition, `npm install patternengine-node-nunjucks` should do the trick.
66

77
Level of Support is more or less full. Partial calls and lineage hunting are supported. Nunjucks does not support the mustache-specific syntax extensions, style modifiers and pattern parameters, because their use cases are addressed by the core Nunjucks feature set.
8+
9+
## Extending the Nunjucks
10+
11+
To add custom filters or make customizations the nunjucks instance, create a file named `patternlab-nunjucks-config.js` in the root fo your Pattern Lab project. It should export a function that takes two paramerters. The first parameter being the nunjucks instance and the second the nunjucks instance environment.
12+
13+
```
14+
module.exports = function (nunjucks, env) {
15+
[YOUR CUSTOM CODE HERE]
16+
};
17+
```
18+
19+
EXAMPLE: patternlab-nunjucks-config.js file that uses lodash and adds three custom filters.
20+
```
21+
var _shuffle = require('lodash/shuffle'),
22+
_take = require('lodash/take');
23+
24+
exports = module.exports = function (nunjucks, env) {
25+
env.addFilter('shorten', function (str, count) {
26+
return str.slice(0, count || 5);
27+
});
28+
29+
env.addFilter('shuffle', (arr) => {
30+
return _shuffle(arr);
31+
});
32+
33+
env.addFilter('take', (arr, number) => {
34+
return _take(arr, number);
35+
});
36+
};
37+
```

packages/patternengine-node-nunjucks/lib/engine_nunjucks.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
- Look into if we need to handle partials in the render method
55
- Add try catch blocks to prevent Pl from crashing
66
- Test methods of including files
7-
- Add a way to extend the Nunjucks instance from the PL instance
8-
- This could be used to add our lodash filters. That way we're seperating concerns properly and making the engine more flexible.
97
- Compare features and syntax with the mustache version so we can document
108
- Document and submit to PatternLab
119
*/
@@ -15,21 +13,23 @@
1513
var path = require('path'),
1614
plPath = process.cwd(),
1715
plConfig = require(path.join(plPath, 'patternlab-config.json')),
18-
_shuffle = require('lodash/shuffle'),
19-
_take = require('lodash/take'),
2016
nunjucks = require('nunjucks'),
2117
env = nunjucks.configure(plConfig.paths.source.patterns),
2218
partialRegistry = [];
2319

2420

2521
////////////////////////////
26-
// NUNJUCKS CUSTOMIZATIONS
22+
// LOAD ANY USER NUNJUCKS CONFIGURATIONS
2723
////////////////////////////
28-
env.addFilter('shorten', function (str, count) {
29-
return str.slice(0, count || 5);
30-
});
31-
env.addFilter('shuffle', (arr) => _shuffle(arr));
32-
env.addFilter('take', (arr, number) => _take(arr, number));
24+
try {
25+
var nunjucksConfig = require(path.join(plPath, 'patternlab-nunjucks-config.js'));
26+
if (typeof nunjucksConfig == 'function') {
27+
nunjucksConfig(nunjucks, env);
28+
}
29+
}
30+
catch (err) {
31+
console.log('Cannot find module \'patternlab-nunjucks-config.js\'');
32+
}
3333

3434

3535
////////////////////////////

packages/patternengine-node-nunjucks/package-lock.json

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/patternengine-node-nunjucks/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"deprecated": false,
2727
"description": "The nunjucks engine for Pattern Lab / Node",
2828
"dependencies": {
29-
"lodash": "^4.17.4",
3029
"nunjucks": "^3.0.1"
3130
},
3231
"devDependencies": {},

0 commit comments

Comments
 (0)