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
?
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.
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.
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.
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.
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
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