Skip to content

Commit 2e65824

Browse files
burt202evilebottnawi
authored andcommitted
feat: make webpack compilation hash value available to templates (#13)
* Make webpack compilation hash value available to templates * feat: drop node 4 (#14) * Drop node 4 support in travis ci * Add node 10 and 11 in travis build matrix * Update engines prop in package.json * Use LTS version of node v6 as a minimum
1 parent 932d2da commit 2e65824

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff 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.

src/NunjucksWebpackPlugin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/__tests__/NunjucksWebpackPlugin-test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
});

src/__tests__/fixtures/test3.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="{{__webpack__.hash}}.js"></script>

0 commit comments

Comments
 (0)