I working on a Vue app in Django via django-webpack-loader, running locally I'm able to get it to work by using the following in my base.html
file:
{% load render_bundle from webpack_loader %}
...
...
{% render_bundle 'app' %}
However, in production this doesn't work - I believe because the webpack production config uses the CommonChunksPlugin to split the bundles into app
, manifest
and vendor
.
There isn't much documentation online for merging Webpack with Django - I'm wondering if there is a way to include all chunks in the Django template.
After setting up the frontend part, we must now configure Django to see the compiled assets. You can then install Django Webpack Loader by running pip install django-webpack-loader in your environment. To persist the installed packages so they can be used in other environments, you can run pip freeze > requirements.
Django-webpack-loader is there to make it easy to link to bundles (with their changing names) from inside of your Django templates. If you're not sure why there's a plugin for this, you might be missing out on an important thing Webpack can provide you - unique hashes in filenames. Read on to find out more.
Use webpack to generate your static bundles without django's staticfiles or opaque wrappers. Django webpack loader consumes the output generated by webpack-bundle-tracker and lets you use the generated bundles in django. A changelog is also available.
While most of Django core is Python, the admin and gis contrib apps contain JavaScript code. Please follow these coding standards when writing JavaScript code for inclusion in Django.
The problem in the end was due to code splitting. In dev, a single JS file is created, but in the production config the Webpack CommonChunksPlugin
was configured to split the app into 3 files (manifest, vendor and app).
This solution isn't ideal and might not scale well, but by placing a conditional tag in the Django template I was able to correctly reference the necessary files.
{% if STAGE or PRODUCTION %}
{% render_bundle 'vendor' 'js' %}
{% render_bundle 'manifest' 'js' %}
{% endif %}
{% render_bundle 'app' 'js' %}
Did you edit settings.py to point to the bundle directory?
APP_DIR = os.path.join(BASE_DIR, 'app')
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'dist/'
}
}
STATICFILES_DIRS = (
os.path.join(APP_DIR, 'assets'),
)
Then use HtmlWebpackPlugin to point to chunks? https://github.com/jantimon/html-webpack-plugin/blob/master/README.md#writing-your-own-templates
plugins: [
new HtmlWebpackPlugin({
template: 'static/app/index.html'
}),
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With