Skip to content

Commit 1609967

Browse files
committed
Readme revamp
1 parent 747b23d commit 1609967

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

README.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,15 @@ WEBPACK_LOADER = {
8383
}
8484
```
8585

86-
For that setup, we're using the `DEBUG` variable provided by Django when bootstraping a new project. Since in a production environment (`DEBUG = False`) the files won't constantly change, we can safely cache the results and optimize our flow by only fetching the files once.
86+
For that setup, we're using the `DEBUG` variable provided by Django when bootstraping a new project. Since in a production environment (`DEBUG = False`) the assets files won't constantly change, we can safely cache the results (`CACHE=True`) and optimize our flow, as `django-webpack-loader` will read the stats file only once and store the assets files paths in memory. From that point pnwards, it will use these stored paths as the source of truth. If `CACHE=False`, we'll always read the stats file to get the assets paths.
87+
> If `CACHE=True`, any changes made in the assets files will only be read when the web workers are restarted.
8788
88-
During development, when files change a lot, we want to always poll for the updated files (in our case, we'll fetch the files every 0.1s, as defined on `POLL_INTERVAL`).
89+
During development, when the stats file changes a lot, we want to always poll for its updated version (in our case, we'll fetch it every 0.1s, as defined on `POLL_INTERVAL`).
90+
> In production (`DEBUG=False`), we'll only fetch the stats file once, so `POLL_INTERVAL` is ignored.
8991
90-
`django-webpack-loader` will look for the output file produced by `webpack-bundle-tracker`. Since on Webpack we've named it `webpack-stats.json` and stored it on the project root, we must replicate that setting on the back-end side.
92+
While `CACHE` isn't directly related to `POLL_INTERVAL`, it's interesting to keep `CACHE` binded to the `DEBUG` logic value (in this case, the negation of the logic value) in order to only cache the assets in production, as we'd not continuously poll the stats file in that environment.
93+
94+
The `STATS_FILE` parameter represents the output file produced by `webpack-bundle-tracker`. Since in the Webpack configuration file we've named it `webpack-stats.json` and stored it on the project root, we must replicate that setting on the back-end side.
9195

9296
`IGNORE` is a list of regular expressions. If a file generated by Webpack matches one of the expressions, the file will not be included in the template.
9397

@@ -229,8 +233,65 @@ qwe
229233
</html>
230234
```
231235

236+
### Multiple Webpack projects
237+
Version 1.0 and up of `django-webpack-loader` also supports multiple Webpack configurations. The following configuration defines 2 Webpack stats files in settings and uses the `config` argument in the template tags to influence which stats file to load the bundles from.
238+
239+
```python
240+
WEBPACK_LOADER = {
241+
'DEFAULT': {
242+
'BUNDLE_DIR_NAME': 'bundles/',
243+
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
244+
},
245+
'DASHBOARD': {
246+
'BUNDLE_DIR_NAME': 'dashboard_bundles/',
247+
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-dashboard.json'),
248+
}
249+
}
250+
```
251+
252+
```HTML+Django
253+
{% load render_bundle from webpack_loader %}
254+
255+
<html>
256+
<body>
257+
....
258+
{% render_bundle 'main' 'js' 'DEFAULT' %}
259+
{% render_bundle 'main' 'js' 'DASHBOARD' %}
260+
261+
<!-- or render all files from a bundle -->
262+
{% render_bundle 'main' config='DASHBOARD' %}
263+
264+
<!-- the following tags do the same thing -->
265+
{% render_bundle 'main' 'css' 'DASHBOARD' %}
266+
{% render_bundle 'main' extension='css' config='DASHBOARD' %}
267+
{% render_bundle 'main' config='DASHBOARD' extension='css' %}
268+
269+
<!-- add some extra attributes to the tag -->
270+
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async charset="UTF-8"'%}
271+
</body>
272+
</head>
273+
```
274+
275+
### File URLs instead of html tags
276+
277+
If you need the URL to an asset without the HTML tags, the `get_files` template tag can be used. A common use case is specifying the URL to a custom css file for a Javascript plugin.
278+
279+
`get_files` works exactly like `render_bundle` except it returns a list of matching files and lets you assign the list to a custom template variable. For example:
280+
281+
```HTML+Django
282+
{% get_files 'editor' 'css' as editor_css_files %}
283+
CKEDITOR.config.contentsCss = '{{ editor_css_files.0.publicPath }}';
284+
285+
<!-- or list down name, path and download url for every file -->
286+
<ul>
287+
{% for css_file in editor_css_files %}
288+
<li>{{ css_file.name }} : {{ css_file.path }} : {{ css_file.publicPath }}</li>
289+
{% endfor %}
290+
</ul>
291+
```
292+
232293
### Code splitting
233-
In case you wish to use code-splitting, follow the recipe below on the Javascript side.
294+
In case you wish to use [code-splitting](https://webpack.js.org/guides/code-splitting/), follow the recipe below on the Javascript side.
234295

235296
Create your entrypoint file and add elements to the DOM, while leveraging the lazy imports.
236297
```js

0 commit comments

Comments
 (0)