- Introduction
- Setup Webpack Project with Django
- Load Webpack bundles in Django
- Linting in Webpack
- Load Webpack hash bundle in Django
- Code splitting with Webpack
- How to config HMR with Webpack and Django
- How to use HtmlWebpackPlugin to load Webpack bundle in Django
If you want a quick start with Webpack and Django, please check python-webpack-boilerplate
Background
For many people, it is not easy to make Django work with modern frontend tech, such as ES6
, SCSS
.
If we check online, many blog posts and tutorials are from view of frontend (such as building Single-page application), which might not work very well if we want to put JS, CSS files in Django template.
What is more, popular Django boilerplate projects on Github (such as cookiecutter-django) are still using a little outdated frontend tech (gulp
workflow).
This is a PAIN, so I decide to help people solve it!
Why Webpack
Webpack
is the most popular bundle solution in the frontend community today, it has received 50k stars on Github.- It has a great ecosystem, many plugins, loaders. If we search
webpack
on npmjs.com, we can get 20k resulst. - If we do not need
React
,Vue
, we can still use Webpack to help us compileES6
,SCSS
and do many other things (Many people do not know that!) - With a proper config, Webpack can save time and let us build modern web application in quick way.
Objectives
This tutorial series will teach you how to config Webpack to work with Django.
We will not build SPA (separate frontend from backend), but use Webpack to do bundle work, and then load the bundle file in Django template.
By the end of this course, you will be able to:
- Create a
Webpack
project and make it run inwatch
mode. - Understand Webpack loader and Webpack plugin workflow.
- Learn the difference between
development
mode andproduction
mode in Webpack. - Use
Eslint
andWebpack
to check code style of JS. - Use
Stylelint
andWebpack
to check code style of SCSS, CSS. - Understand
hash
,chunkhash
, andcontenthash
in Webpack - Learn what is Webpack
PublicPath
, and config it to work with Django. - Use
django-webpack-loader
to load hash bundle file in Django. - Understand the code splitting workflow in Webpack.
- Learn how to config splitChunks to control the splitting behavior.
- Understand what is Webpack
HMR
and the workflow - Learn to make webpack-dev-server work with Django
- Learn what is
HtmlWebpackPlugin
and how it works. - Use
HtmlWebpackPlugin
and Django Template inheritance to load Webpack bundle
Benefits
- We can still use Django template syntax and HTML as we are familiar with
- We do not need DRY (Django REST framework) and similar frameworks to expose data through API.
- We do not need to implement JWT, but using Django
standard auth
. - We can write JS and SCSS in modern syntax, the code is more readable and easy to maintain.
- Code linter can help check JS and CSS code style.
- We can develop in flexible way, for example, we can use
React
orVue.js
to build some components in the web pages, and write other parts in Django template.
Tech
- Django 3.1
- Webpack 5
- jQuery 3.5.1
- Bootstrap 4.5
Table of content
- Introduction
- Setup Webpack Project with Django
- Load Webpack bundles in Django
- Linting in Webpack
- Load Webpack hash bundle in Django
- Code splitting with Webpack
- How to config HMR with Webpack and Django
- How to use HtmlWebpackPlugin to load Webpack bundle in Django
If you want a quick start with Webpack and Django, please check python-webpack-boilerplate