Django Webpack Loader
Django Webpack Loader transparently integrates Webpack-generated frontend assets (JavaScript, CSS, images, etc.) into Django templates. It achieves this by consuming a `webpack-stats.json` file, which is typically generated by the `webpack-bundle-tracker` plugin. The library is actively maintained, currently at version 3.2.3, and generally aligns its compatibility with Django and Node.js LTS releases.
Warnings
- breaking Versions >= 1.0.0 of `django-webpack-loader` require `webpack-bundle-tracker@1.0.0` or higher due to a change in the `webpack-stats.json` file format. It is recommended to maintain at least minor version parity between both packages for full compatibility.
- breaking Version 3.0.0 introduced improved support for `publicPath: 'auto'` in Webpack configurations. If your existing setup relied on specific workarounds for `publicPath` behavior with earlier versions, you might need to review and adjust your Webpack and `WEBPACK_LOADER` configurations.
- gotcha The default `TIMEOUT` for `webpack_loader` in development is `0` (never timeout). This can cause your Django development server to hang silently if the Webpack development server is not running, is slow to compile, or gets stuck in a 'compile' state, blocking all requests without explicit error messages. Version 3.2.3 added a warning log for this scenario.
- gotcha Incorrectly configured `STATS_FILE` or `BUNDLE_DIR_NAME` in `WEBPACK_LOADER` settings relative to your Webpack output and Django's `STATICFILES_DIRS` is a very common setup error. These paths must precisely match how Webpack is configured to output `webpack-stats.json` and its bundles.
- gotcha The `skip_common_chunks=True` option in `render_bundle` requires the Django `request` object to be present in the template context (e.g., via `django.template.context_processors.request` middleware) to function correctly and deduplicate chunks. Without it, common chunks might be duplicated in the output, and you might receive console warnings.
Install
-
pip install django-webpack-loader -
npm install --save-dev webpack-bundle-tracker
Imports
- webpack_loader
INSTALLED_APPS = ['webpack_loader', ...]
- render_bundle
{% load render_bundle from webpack_loader %} - webpack_static
{% load webpack_static from webpack_loader %}
Quickstart
import os
# settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
# ... other Django apps
'webpack_loader',
]
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'), # Where your frontend source and webpack bundles might live
)
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'webpack_bundles/', # Webpack output directory within STATICFILES_DIRS
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'), # Path to webpack-stats.json
'POLL_INTERVAL': 0.1, # Optional: Poll interval for stats file changes in dev mode (default 0.1s)
'TIMEOUT': 10, # Optional: Timeout for webpack compilation in dev mode (default 0, never timeouts)
'CACHE': not DEBUG, # Cache results in production
'PUBLIC_PATH': '/static/webpack_bundles/', # Typically matches output.publicPath in webpack config
}
}
# myapp/templates/myapp/index.html
# {% load render_bundle from webpack_loader %}
# <!DOCTYPE html>
# <html>
# <head>
# <title>My Django App</title>
# {% render_bundle 'main' 'css' %}
# </head>
# <body>
# <div id="app"></div>
# {% render_bundle 'main' 'js' %}
# </body>
# </html>
# Corresponding minimal webpack.config.js snippet (requires webpack-bundle-tracker):
# const path = require('path');
# const BundleTracker = require('webpack-bundle-tracker');
#
# module.exports = {
# context: __dirname,
# entry: './assets/js/index.js',
# output: {
# path: path.resolve('./assets/webpack_bundles/'),
# filename: '[name]-[contenthash].js',
# publicPath: '/static/webpack_bundles/',
# },
# plugins: [
# new BundleTracker({ filename: './webpack-stats.json' }),
# ],
# };