File tree Expand file tree Collapse file tree 4 files changed +49
-2
lines changed
Expand file tree Collapse file tree 4 files changed +49
-2
lines changed Original file line number Diff line number Diff line change @@ -67,7 +67,10 @@ export default {
6767
6868 * ` context ` - (optional) instead global ` context ` (see above), see
6969 [ render] ( https://mozilla.github.io/nunjucks/api.html#render ) second
70- argument.
70+ argument. The following webpack compilation variables are also sent
71+ through to the template under the ` __webpack__ ` object:
72+
73+ * hash
7174
7275 * ` callback ` - (optional) instead global ` callback ` (see above), see
7376 [ render] ( https://mozilla.github.io/nunjucks/api.html#render ) third argument.
Original file line number Diff line number Diff line change @@ -52,6 +52,12 @@ class NunjucksWebpackPlugin {
5252
5353 const promises = [ ] ;
5454
55+ const baseContext = {
56+ __webpack__ : {
57+ hash : compilation . hash
58+ }
59+ } ;
60+
5561 this . options . templates . forEach ( template => {
5662 if ( ! template . from ) {
5763 throw new Error ( "Each template should have `from` option" ) ;
@@ -67,7 +73,7 @@ class NunjucksWebpackPlugin {
6773
6874 const res = configure . render (
6975 template . from ,
70- template . context ? template . context : null ,
76+ Object . assign ( baseContext , template . context ) ,
7177 template . callback ? template . callback : null
7278 ) ;
7379
Original file line number Diff line number Diff line change @@ -144,3 +144,40 @@ test("should execute successfully when option `templates` is passed and `templat
144144 } ) ;
145145 } ) ;
146146} ) ;
147+
148+ test ( "should execute successfully when using props from '__webpack__' base context object" , t => {
149+ const tmpDirectory = tempy . directory ( ) ;
150+ const templateName = "test3.njk" ;
151+ const webpackConfig = Object . assign ( { } , webpackConfigBase , {
152+ output : {
153+ filename : "bundle.js" ,
154+ path : tmpDirectory
155+ } ,
156+ plugins : [
157+ new NunjucksWebpackPlugin ( {
158+ templates : [
159+ {
160+ from : path . join ( fixturesDir , templateName ) ,
161+ to : path . join ( tmpDirectory , path . basename ( templateName , ".njk" ) )
162+ }
163+ ]
164+ } )
165+ ]
166+ } ) ;
167+
168+ return pify ( webpack ) ( webpackConfig ) . then ( stats => {
169+ t . true ( stats . compilation . errors . length === 0 , "no compilation error" ) ;
170+
171+ return pify ( fs . readFile ) (
172+ path . join ( tmpDirectory , path . basename ( templateName , ".njk" ) )
173+ ) . then ( data => {
174+ const contents = data . toString ( ) ;
175+
176+ t . true (
177+ contents . trim ( ) === '<script src="029f359d6e9653eafd07.js"></script>'
178+ ) ;
179+
180+ return true ;
181+ } ) ;
182+ } ) ;
183+ } ) ;
Original file line number Diff line number Diff line change 1+ <script src =" {{__webpack__.hash}}.js" ></script >
You can’t perform that action at this time.
0 commit comments