|
22 | 22 |
|
23 | 23 | const fs = require('fs-extra');
|
24 | 24 | const path = require('path');
|
25 |
| -const plPath = process.cwd(); |
26 |
| -const plConfig = require(path.join(plPath, 'patternlab-config.json')); |
27 | 25 | const nunjucks = require('nunjucks');
|
28 | 26 | const partialRegistry = [];
|
29 | 27 |
|
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; |
58 | 29 |
|
59 | 30 | // Nunjucks Engine
|
60 | 31 | const engine_nunjucks = {
|
@@ -153,6 +124,72 @@ const engine_nunjucks = {
|
153 | 124 | this.spawnFile(config, '_00-head.njk');
|
154 | 125 | this.spawnFile(config, '_01-foot.njk');
|
155 | 126 | },
|
| 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 | + }, |
156 | 193 | };
|
157 | 194 |
|
158 | 195 | module.exports = engine_nunjucks;
|
0 commit comments