Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack setup with Django

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.

like image 348
Toby Avatar asked Apr 05 '18 23:04

Toby


People also ask

How do I add a Webpack to Django?

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.

Do you need Webpack with Django?

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.

What is Django Webpack loader?

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.

Can I use JavaScript with Django?

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.


2 Answers

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' %}
like image 123
Toby Avatar answered Nov 14 '22 00:11

Toby


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'
    }), 
]
like image 34
Leon Mak Avatar answered Nov 13 '22 22:11

Leon Mak