Skip to content

Commit

Permalink
added exclude_runtime and base_entrypoint configs
Browse files Browse the repository at this point in the history
  • Loading branch information
alihazemfarouk committed Jan 9, 2019
1 parent 47c577d commit 88e49a6
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ WEBPACK_LOADER = {
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
'POLL_INTERVAL': 0.1,
'TIMEOUT': None,
'IGNORE': ['.+\.hot-update.js', '.+\.map']
'IGNORE': [r'.+\.hot-update.js', r'.+\.map'],
'EXCLUDE_RUNTIME': False,
'BASE_ENTRYPOINT': ''
}
}
```
Expand Down Expand Up @@ -166,6 +168,19 @@ and your webpack config is located at `/home/src/webpack.config.js`, then the va
<br>
#### EXCLUDE_RUNTIME
`EXCLUDE_RUNTIME` is meant to be used with `render_entrypoint`. When creating multi-page applications, it's common to want to
split a single runtime file that is used by all chunks. You can do that by using `{% render_bundle 'runtime' %}` in your base HTML file and setting `EXCLUDE_RUNTIME` to `True` in order to not include it again when using `{% render_entrypoint 'example_entry_point' %}`
<br>
#### BASE_ENTRYPOINT
`BASE_ENTRYPOINT` is meant to be used with `render_entrypoint`. When creating multi-page applications, it's common to want to
include common js in the base HTML. If the main entrypoint's name is `main`, you can do that by including `{% render_entrypoint 'main' %}` in your base HTML file. Now in another entrypoints (that extend the base HTML file), there might be some chunks that were already included in `main`, that means they would be included twice in the final rendered HTML, to avoid that, set `BASE_ENTRYPOINT` to `'main'`, then any duplicate chunks between an entrypoint and the main entrypoint would be included only once.
<br>
## Usage
<br>
Expand Down
6 changes: 6 additions & 0 deletions tests/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-app2.json'),
},
'EXCLUDE_RUNTIME': {
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
'EXCLUDE_RUNTIME': True
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/app/templates/main_template_exclude_runtime.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{% load render_entrypoint from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
{% render_entrypoint 'main' 'css' 'EXCLUDE_RUNTIME' %}
</head>

<body>
<div id="react-app"></div>
{% render_entrypoint 'main' 'js' 'EXCLUDE_RUNTIME' %}
</body>
</html>
9 changes: 9 additions & 0 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ def test_templatetags(self):
self.assertIn('<script type="text/javascript" src="/static/bundles/another_entrypoint.js" ></script>', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/runtime.js" ></script>', result.rendered_content)

def test_exclude_runtime(self):
self.compile_bundles('webpack.config.multipleEntrypoints.js')
view = TemplateView.as_view(template_name='main_template_exclude_runtime.html')
request = self.factory.get('/')
result = view(request)
self.assertIn('<link type="text/css" href="/static/bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script type="text/javascript" src="/static/bundles/main.js" ></script>', result.rendered_content)
self.assertNotIn('<script type="text/javascript" src="/static/bundles/runtime.js" ></script>', result.rendered_content)

def test_jinja2(self):
self.compile_bundles('webpack.config.simple.js')
self.compile_bundles('webpack.config.app2.js')
Expand Down
4 changes: 3 additions & 1 deletion webpack_loader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
# FIXME: Explore usage of fsnotify
'POLL_INTERVAL': 0.1,
'TIMEOUT': None,
'IGNORE': [r'.+\.hot-update.js', r'.+\.map']
'IGNORE': [r'.+\.hot-update.js', r'.+\.map'],
'EXCLUDE_RUNTIME': False,
'BASE_ENTRYPOINT': ''
}
}

Expand Down
10 changes: 10 additions & 0 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def get_entry(self, entry_name):
entry_files = assets['entryPoints'].get(entry_name, None)
if entry_files:
entry_files_flat = [entry_point for sublist in entry_files for entry_point in sublist]
if 'runtime' in entry_files_flat[0]['name'] and self.config['EXCLUDE_RUNTIME']:
entry_files_flat.pop(0)
if self.config['BASE_ENTRYPOINT'] and entry_name != self.config['BASE_ENTRYPOINT']:
base_entrypoint = self.config['BASE_ENTRYPOINT']
base_entry_files = assets['entryPoints'].get(base_entrypoint, None)
if base_entry_files:
base_entry_files_flat_names = [entry_point['name'] for sublist in base_entry_files for entry_point in sublist]
for i, entry in enumerate(entry_files_flat):
if entry['name'] in base_entry_files_flat_names:
entry_files_flat.pop(i)
else:
raise WebpackBundleLookupError('Cannot resolve entry {0}.'.format(entry_name))
else:
Expand Down

0 comments on commit 88e49a6

Please sign in to comment.