20
20
21
21
'use strict' ;
22
22
23
- let fs = require ( 'fs-extra' ) ,
24
- path = require ( 'path' ) ,
25
- plPath = process . cwd ( ) ,
26
- plConfig = require ( path . join ( plPath , 'patternlab-config.json' ) ) ,
27
- nunjucks = require ( 'nunjucks' ) ,
28
- env = nunjucks . configure ( plConfig . paths . source . patterns ) ,
29
- partialRegistry = [ ] ;
30
-
31
- ////////////////////////////
32
- // LOAD ANY USER NUNJUCKS CONFIGURATIONS
33
- ////////////////////////////
23
+ const fs = require ( 'fs-extra' ) ;
24
+ const path = require ( 'path' ) ;
25
+ const plPath = process . cwd ( ) ;
26
+ const plConfig = require ( path . join ( plPath , 'patternlab-config.json' ) ) ;
27
+ const nunjucks = require ( 'nunjucks' ) ;
28
+ const partialRegistry = [ ] ;
29
+
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
+ } ;
43
+ } ;
44
+
45
+ const env = new nunjucks . Environment ( new PatternLoader ( ) ) ;
46
+
47
+ // Load any user Defined configurations
34
48
try {
35
- let nunjucksConfig = require ( path . join (
49
+ const nunjucksConfig = require ( path . join (
36
50
plPath ,
37
51
'patternlab-nunjucks-config.js'
38
52
) ) ;
41
55
}
42
56
} catch ( err ) { }
43
57
44
- ////////////////////////////
45
- // HELPER FUNCTIONS
46
- // https://stackoverflow.com/questions/2116558/fastest-method-to-replace-all-instances-of-a-character-in-a-string
47
- // Might do some research on the solution.
48
- ////////////////////////////
49
- if ( ! String . prototype . replaceAll ) {
50
- String . prototype . replaceAll = function ( str1 , str2 , ignore ) {
51
- return this . replace (
52
- new RegExp (
53
- str1 . replace ( / ( [ \/ \, \! \\ \^ \$ \{ \} \[ \] \( \) \. \* \+ \? \| \< \> \- \& ] ) / g, '\\$&' ) ,
54
- ignore ? 'gi' : 'g'
55
- ) ,
56
- typeof str2 === 'string' ? str2 . replace ( / \$ / g, '$$$$' ) : str2
57
- ) ;
58
- } ;
59
- }
60
-
61
- ////////////////////////////
62
- // NUNJUCKS ENGINE
63
- ////////////////////////////
64
- let engine_nunjucks = {
58
+ // Nunjucks Engine
59
+ const engine_nunjucks = {
65
60
engine : nunjucks ,
66
61
engineName : 'nunjucks' ,
67
62
engineFileExtension : '.njk' ,
@@ -77,9 +72,7 @@ let engine_nunjucks = {
77
72
// render it
78
73
renderPattern : function renderPattern ( pattern , data ) {
79
74
try {
80
- // replace pattern names with their full path so Nunjucks can find them.
81
- pattern . extendedTemplate = this . replacePartials ( pattern ) ;
82
- let result = nunjucks . renderString ( pattern . extendedTemplate , data ) ;
75
+ const result = env . renderString ( pattern . extendedTemplate , data ) ;
83
76
return Promise . resolve ( result ) ;
84
77
} catch ( err ) {
85
78
console . error ( 'Failed to render pattern: ' + pattern . name ) ;
@@ -88,7 +81,7 @@ let engine_nunjucks = {
88
81
89
82
// find and return any Nunjucks style includes/imports/extends within pattern
90
83
findPartials : function findPartials ( pattern ) {
91
- let matches = pattern . template . match ( this . findPartialsRE ) ;
84
+ const matches = pattern . template . match ( this . findPartialsRE ) ;
92
85
return matches ;
93
86
} ,
94
87
@@ -116,38 +109,9 @@ let engine_nunjucks = {
116
109
}
117
110
} ,
118
111
119
- replacePartials : function ( pattern ) {
120
- try {
121
- let partials = this . findPartials ( pattern ) ;
122
- if ( partials !== null ) {
123
- for ( let i = 0 ; i < partials . length ; i ++ ) {
124
- // e.g. {% include "atoms-parent" %}
125
- let partialName = this . findPartial ( partials [ i ] ) ; // e.g. atoms-parent
126
- let partialFullPath = partialRegistry [ partialName ] ; // e.g. 00-atoms/01-parent.njk
127
- let newPartial = partials [ i ] . replaceAll (
128
- partialName ,
129
- partialFullPath ,
130
- true
131
- ) ; // e.g. {% include "00-atoms/01-parent.njk" %}
132
- pattern . extendedTemplate = pattern . extendedTemplate . replaceAll (
133
- partials [ i ] ,
134
- newPartial ,
135
- true
136
- ) ;
137
- }
138
- }
139
- return pattern . extendedTemplate ;
140
- } catch ( err ) {
141
- console . error (
142
- 'Error occurred in replacing partial names with paths for patern: ' +
143
- pattern . name
144
- ) ;
145
- }
146
- } ,
147
-
148
112
// still requires the mustache syntax because of the way PL handles lists
149
113
findListItems : function ( pattern ) {
150
- let matches = pattern . template . match ( this . findListItemsRE ) ;
114
+ const matches = pattern . template . match ( this . findListItemsRE ) ;
151
115
return matches ;
152
116
} ,
153
117
@@ -168,7 +132,6 @@ let engine_nunjucks = {
168
132
fs . statSync ( metaFilePath ) ;
169
133
} catch ( err ) {
170
134
//not a file, so spawn it from the included file
171
- const localMetaFilePath = path . resolve ( __dirname , '_meta/' , fileName ) ;
172
135
const metaFileContent = fs . readFileSync (
173
136
path . resolve ( __dirname , '..' , '_meta/' , fileName ) ,
174
137
'utf8'
0 commit comments