You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Integrate Webpack bundles in Django templates by using a simple templatetag:
8
+
Integrate Webpack bundles in Django templates by using a simple template tag:
9
9
10
10
```HTML+django
11
11
{% load render_bundle from webpack_loader %}
@@ -17,14 +17,14 @@ Integrate Webpack bundles in Django templates by using a simple templatetag:
17
17
</html>
18
18
```
19
19
20
-
Behind the scenes, Django Webpack Loader consumes the**stats file** output generated by [webpack-bundle-tracker](https://github.com/django-webpack/webpack-bundle-tracker) and lets you use the generated bundles in Django.
20
+
Behind the scenes, Django Webpack Loader consumes a**stats file** generated by [webpack-bundle-tracker](https://github.com/django-webpack/webpack-bundle-tracker) and lets you use the generated bundles in Django.
21
21
22
22
A [changelog](CHANGELOG.md) is available.
23
23
24
24
## Compatibility
25
25
26
-
Generally, Pythonand Django LTS releases will be supported until EOL. Check `tests/tox.ini` for details.
27
-
Versions not listed in `tests/tox.ini` may still work, but maintainers will not test them, nor support them.
26
+
Generally, Python, Django, and Node LTS releases will be supported until EOL. Check `tests/tox.ini` for details.
27
+
Versions not listed in `tests/tox.ini` may still work, but maintainers will not test them, nor solve issues with them.
28
28
Examples below are in Webpack 5.
29
29
30
30
## Install
@@ -52,7 +52,7 @@ module.exports = {
52
52
context:__dirname,
53
53
entry:"./assets/js/index",
54
54
output: {
55
-
path:path.resolve("./assets/webpack_bundles/"),
55
+
path:path.resolve(__dirname, "assets/bundles/"),
56
56
filename:"[name]-[contenthash].js",
57
57
},
58
58
plugins: [
@@ -115,7 +115,7 @@ In development, we can simply do:
115
115
116
116
```bash
117
117
# in one shell
118
-
npx webpack --config webpack.config.js --watch
118
+
npx webpack --mode=development --watch
119
119
120
120
# in another shell
121
121
python manage.py runserver
@@ -125,7 +125,7 @@ Check [the full example for development here](https://github.com/django-webpack/
125
125
126
126
Aditionally, hot reload is available through a specific config. Check [this section](#hot-reload).
127
127
128
-
> ⚠️ For compiling the frontend assets in production, check [this section](#using-in-production).
128
+
> ⚠️ For compiling and serving the frontend assets in production, check [this section](#using-in-production).
129
129
130
130
## Rendering
131
131
@@ -186,7 +186,11 @@ However, production usage for this package is **fairly flexible**, as the entire
186
186
187
187
### Hot reload
188
188
189
-
In case you wish to enable hot reload for your project, please check out [this example](https://github.com/django-webpack/django-webpack-loader/tree/master/examples/hot-reload), in particular how [webpack.config.js](https://github.com/django-webpack/django-webpack-loader/blob/master/examples/hot-reload/webpack.config.js) is configured.
189
+
[Hot reload (Hot Module Replacement)](https://webpack.js.org/guides/hot-module-replacement/) is critical for a improving the development workflow. In case you wish to enable for your project, please check out [this example](https://github.com/django-webpack/django-webpack-loader/tree/master/examples/hot-reload), in particular how [webpack.config.js](https://github.com/django-webpack/django-webpack-loader/blob/master/examples/hot-reload/webpack.config.js) is configured. The key is to set the `devServer`.
190
+
191
+
### Dynamic Imports
192
+
193
+
In case you wish to use [Dynamic Imports](https://webpack.js.org/guides/code-splitting/#dynamic-imports), please check out [this example](https://github.com/django-webpack/django-webpack-loader/tree/master/examples/dynamic-imports), in particular how [webpack.config.js](https://github.com/django-webpack/django-webpack-loader/blob/master/examples/dynamic-imports/webpack.config.js) is configured. The key is to set the `publicPath`.
.catch((error) =>"An error occurred while loading the component");
393
-
}
394
-
395
-
getComponent().then((component) => {
396
-
document.body.appendChild(component);
397
-
});
398
-
```
399
-
400
-
On your configuration file, do the following:
401
-
402
-
```js
403
-
// webpack.config.js
404
-
module.exports = {
405
-
context: __dirname,
406
-
entry: {
407
-
principal: "./src/principal",
408
-
},
409
-
output: {
410
-
path: path.resolve("./dist/"),
411
-
// publicPath should match your STATIC_URL config.
412
-
// This is required otherwise Webpack will try to fetch
413
-
// our chunk generated by the dynamic importfrom"/" instead of "/dist/".
414
-
publicPath: "/dist/",
415
-
chunkFilename: "[name].bundle.js",
416
-
filename: "[name]-[contenthash].js",
417
-
},
418
-
plugins: [
419
-
new BundleTracker({ path: __dirname, filename: "webpack-stats.json" }),
420
-
],
421
-
};
422
-
```
423
-
424
-
On your template, render the bundle as usual:
425
-
426
-
```HTML+Django
427
-
<!-- index.html -->
428
-
{% load render_bundle from webpack_loader %}
429
-
430
-
<html>
431
-
<body>
432
-
{% render_bundle 'principal''js'%}
433
-
</body>
434
-
</html>
435
-
```
436
-
437
379
### Jinja2 Configuration
438
380
439
381
If you need to output your assets in a jinja template, we provide a Jinja2 extension that's compatible with [django-jinja](https://github.com/niwinz/django-jinja).
0 commit comments