Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should we use django-webpack-loader?

I use both webpack and django. Now I move bundled assets to /static/ directory of django each time, so I'd like to make more effective process.
I read some articles and many people recommend to use django-webpack-loader, but I don't fully understand what it does.

I've already read the official documents below.
https://owais.lone.pw/blog/webpack-plus-reactjs-and-django/
https://406.ch/writing/our-approach-to-configuring-django-webpack-and-manifeststaticfilesstorage/

I think it's for collecting bundled assets located outside of django projects, but it seems almost same as creating a symbolic link from django project to dist/ directory in webpack.
Is there any other useful feature in django-webpack-loader?

like image 509
lipsum Avatar asked Jun 19 '18 08:06

lipsum


People also ask

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.

Do I need webpack for django?

Making Django and Webpack play nice together turns out to be pretty straightforward, without needing any Webpack or Django plugins. Django has built-in support for handling static assets. Django can serve static assets in development and compress files and hash filenames during production deployment.

What is webpack used for?

This is why webpack exists. It's a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.


2 Answers

I think you might be missing that webpack will append a random hash code (so new builds bust caches). Without some special logic, django won't know how to account for the hash.

In my opinion all the other stuff the other answerer mentioned are sort of extra bonuses to make your life easier.

like image 28
Maus Avatar answered Oct 22 '22 01:10

Maus


It's a handy little tool. This gist of webpack loader is create a mechanism to link up to your latest bundle in an automated fashion.

A "render_bundle" template tag is provided that outputs link to load in either your latest JS or CSS bundle.

The tag is based from a hash of the bundle code (so this will change if your bundle changes) therefore browsers will always load in the most up-to-date version of your static assets. This cache-busting technique is useful when testing on mobile devices or situations where performing a 'hard' refresh of the page is not straightforward.

I believe this is achieved by the template tag referring to the output of BundleTracker, which outputs metadata about the status of your webpack bundle in webpack-stats.json.

https://www.npmjs.com/package/webpack-bundle-tracker

like image 155
user368604 Avatar answered Oct 22 '22 01:10

user368604