Skip to content

Commit 0a65129

Browse files
committed
Removed unneeded mark_safe tag from render_bundle;
Modified jinja2ext to handle context; Added tests for takes_context and skip_common_chunks
1 parent a18948e commit 0a65129

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed

tests/app/tests/test_webpack.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.conf import settings
1010
from django.test import RequestFactory, TestCase
1111
from django.views.generic.base import TemplateView
12+
from django.template import Context, Template
1213
from django_jinja.builtins import DEFAULT_EXTENSIONS
1314
from unittest2 import skipIf
1415
from webpack_loader.exceptions import (
@@ -180,7 +181,6 @@ def test_jinja2(self):
180181
"extensions": DEFAULT_EXTENSIONS + [
181182
"webpack_loader.contrib.jinja2ext.WebpackExtension",
182183
]
183-
184184
}
185185
},
186186
]
@@ -283,3 +283,46 @@ def test_request_blocking(self):
283283
result.rendered_content
284284
elapsed = time.time() - then
285285
self.assertTrue(elapsed < wait_for)
286+
287+
def test_takes_context(self):
288+
self.compile_bundles('webpack.config.simple.js')
289+
self.compile_bundles('webpack.config.app2.js')
290+
template_to_test = Template(
291+
"{% load render_bundle from webpack_loader %}"
292+
"{% render_bundle 'main' 'css' %}"
293+
"{% render_bundle 'main' 'js' %}"
294+
)
295+
context = Context({})
296+
rendered_template = template_to_test.render(context)
297+
self.assertIn("webpack_loader_used_tags", context)
298+
used_tags = context["webpack_loader_used_tags"]
299+
self.assertIn('<link href="/static/django_webpack_loader_bundles/main.css" rel="stylesheet" />', used_tags)
300+
self.assertIn('<script src="/static/django_webpack_loader_bundles/main.js" ></script>', used_tags)
301+
302+
303+
304+
def test_skip_common_chunks(self):
305+
self.compile_bundles('webpack.config.skipCommon.js')
306+
# Test default case where duplicates will be generated
307+
template_to_test_duplicates = Template(
308+
"{% load render_bundle from webpack_loader %}"
309+
"{% render_bundle 'app1' %}"
310+
"{% render_bundle 'app2' %}"
311+
)
312+
context = Context({})
313+
rendered_template = template_to_test_duplicates.render(context)
314+
self.assertIn("webpack_loader_used_tags", context)
315+
used_tags = context["webpack_loader_used_tags"]
316+
self.assertEqual(rendered_template.count('<script src="/static/django_webpack_loader_bundles/vendors.js" ></script>'), 2)
317+
318+
template_to_test_skipped_chunks = Template(
319+
"{% load render_bundle from webpack_loader %}"
320+
"{% render_bundle 'app1' %}"
321+
"{% render_bundle 'app2' skip_common_chunks=True %}"
322+
)
323+
context = Context({})
324+
rendered_template = template_to_test_skipped_chunks.render(context)
325+
self.assertEqual(rendered_template.count('<script src="/static/django_webpack_loader_bundles/vendors.js" ></script>'), 1)
326+
327+
328+

tests/webpack.config.skipCommon.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var path = require("path");
2+
var webpack = require('webpack');
3+
var BundleTracker = require('webpack-bundle-tracker');
4+
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
5+
6+
7+
module.exports = {
8+
context: __dirname,
9+
entry: {
10+
app1: './assets/js/index',
11+
app2: './assets/js/index'
12+
},
13+
output: {
14+
path: path.resolve('./assets/django_webpack_loader_bundles/'),
15+
filename: "[name].js",
16+
chunkFilename: "[name].js"
17+
},
18+
19+
plugins: [
20+
new MiniCssExtractPlugin(),
21+
new BundleTracker({path: __dirname, filename: './webpack-stats.json'}),
22+
],
23+
24+
module: {
25+
rules: [
26+
// we pass the output from babel loader to react-hot loader
27+
{
28+
test: /\.jsx?$/,
29+
exclude: /node_modules/,
30+
use: {
31+
loader: 'babel-loader',
32+
options: {
33+
presets: ['@babel/preset-react']
34+
}
35+
}
36+
},
37+
{ test: /\.css$/, use: [MiniCssExtractPlugin.loader, 'css-loader'] }
38+
],
39+
},
40+
41+
resolve: {
42+
modules: ['node_modules'],
43+
extensions: ['.js', '.jsx']
44+
},
45+
46+
optimization: {
47+
splitChunks: {
48+
cacheGroups: {
49+
commons: {
50+
test: /[\\/]node_modules[\\/]/,
51+
name: 'vendors',
52+
chunks: 'all',
53+
enforce: true
54+
}
55+
}
56+
}
57+
}
58+
}

webpack_loader/contrib/jinja2ext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
class WebpackExtension(jinja2.ext.Extension):
77
def __init__(self, environment):
88
super(WebpackExtension, self).__init__(environment)
9-
environment.globals["render_bundle"] = lambda *a, **k: jinja2.Markup(render_bundle(*a, **k))
9+
environment.globals["context"] = dict()
10+
environment.globals["render_bundle"] = lambda *a, **k: jinja2.Markup(render_bundle(environment.globals["context"], *a, **k))

webpack_loader/templatetags/webpack_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def render_bundle(context, bundle_name, extension=None, config='DEFAULT', suffix
1717
context["webpack_loader_used_tags"] = set()
1818
used_tags = context["webpack_loader_used_tags"]
1919
if skip_common_chunks:
20-
tags = [mark_safe(tag) for tag in tags if tag not in used_tags]
20+
tags = [tag for tag in tags if tag not in used_tags]
2121
context["webpack_loader_used_tags"].update(tags)
2222

2323
return mark_safe('\n'.join(tags))

0 commit comments

Comments
 (0)