Skip to content

Commit 3e19de5

Browse files
committed
Remove Code Splitting section from README, do example instead
1 parent d435423 commit 3e19de5

File tree

19 files changed

+6194
-101
lines changed

19 files changed

+6194
-101
lines changed

README.md

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![pyversions](https://img.shields.io/pypi/pyversions/django-webpack-loader)
66
![djversions](https://img.shields.io/pypi/djversions/django-webpack-loader)
77

8-
Integrate Webpack bundles in Django templates by using a simple templatetag:
8+
Integrate Webpack bundles in Django templates by using a simple template tag:
99

1010
```HTML+django
1111
{% load render_bundle from webpack_loader %}
@@ -17,14 +17,14 @@ Integrate Webpack bundles in Django templates by using a simple templatetag:
1717
</html>
1818
```
1919

20-
Behind the scenes, Django Webpack Loader consumes the **stats file** output generated by [webpack-bundle-tracker](https://github.com/django-webpack/webpack-bundle-tracker) and lets you use the generated bundles in Django.
20+
Behind the scenes, Django Webpack Loader consumes a **stats file** generated by [webpack-bundle-tracker](https://github.com/django-webpack/webpack-bundle-tracker) and lets you use the generated bundles in Django.
2121

2222
A [changelog](CHANGELOG.md) is available.
2323

2424
## Compatibility
2525

26-
Generally, Python and Django LTS releases will be supported until EOL. Check `tests/tox.ini` for details.
27-
Versions not listed in `tests/tox.ini` may still work, but maintainers will not test them, nor support them.
26+
Generally, Python, Django, and Node LTS releases will be supported until EOL. Check `tests/tox.ini` for details.
27+
Versions not listed in `tests/tox.ini` may still work, but maintainers will not test them, nor solve issues with them.
2828
Examples below are in Webpack 5.
2929

3030
## Install
@@ -52,7 +52,7 @@ module.exports = {
5252
context: __dirname,
5353
entry: "./assets/js/index",
5454
output: {
55-
path: path.resolve("./assets/webpack_bundles/"),
55+
path: path.resolve(__dirname, "assets/bundles/"),
5656
filename: "[name]-[contenthash].js",
5757
},
5858
plugins: [
@@ -115,7 +115,7 @@ In development, we can simply do:
115115

116116
```bash
117117
# in one shell
118-
npx webpack --config webpack.config.js --watch
118+
npx webpack --mode=development --watch
119119

120120
# in another shell
121121
python manage.py runserver
@@ -125,7 +125,7 @@ Check [the full example for development here](https://github.com/django-webpack/
125125

126126
Aditionally, hot reload is available through a specific config. Check [this section](#hot-reload).
127127

128-
> ⚠️ For compiling the frontend assets in production, check [this section](#using-in-production).
128+
> ⚠️ For compiling and serving the frontend assets in production, check [this section](#using-in-production).
129129
130130
## Rendering
131131

@@ -186,7 +186,11 @@ However, production usage for this package is **fairly flexible**, as the entire
186186

187187
### Hot reload
188188

189-
In case you wish to enable hot reload for your project, please check out [this example](https://github.com/django-webpack/django-webpack-loader/tree/master/examples/hot-reload), in particular how [webpack.config.js](https://github.com/django-webpack/django-webpack-loader/blob/master/examples/hot-reload/webpack.config.js) is configured.
189+
[Hot reload (Hot Module Replacement)](https://webpack.js.org/guides/hot-module-replacement/) is critical for a improving the development workflow. In case you wish to enable for your project, please check out [this example](https://github.com/django-webpack/django-webpack-loader/tree/master/examples/hot-reload), in particular how [webpack.config.js](https://github.com/django-webpack/django-webpack-loader/blob/master/examples/hot-reload/webpack.config.js) is configured. The key is to set the `devServer`.
190+
191+
### Dynamic Imports
192+
193+
In case you wish to use [Dynamic Imports](https://webpack.js.org/guides/code-splitting/#dynamic-imports), please check out [this example](https://github.com/django-webpack/django-webpack-loader/tree/master/examples/dynamic-imports), in particular how [webpack.config.js](https://github.com/django-webpack/django-webpack-loader/blob/master/examples/dynamic-imports/webpack.config.js) is configured. The key is to set the `publicPath`.
190194

191195
### Extra `WEBPACK_LOADER` settings in Django
192196

@@ -372,68 +376,6 @@ CKEDITOR.config.contentsCss = '{{ editor_css_files.0.url }}';
372376
</ul>
373377
```
374378

375-
### Code splitting
376-
377-
In case you wish to use [code-splitting](https://webpack.js.org/guides/code-splitting/), follow the recipe below on the Javascript side.
378-
379-
Create your entrypoint file and add elements to the DOM, while leveraging the lazy imports.
380-
381-
```js
382-
// src/principal.js
383-
function getComponent() {
384-
return import("lodash")
385-
.then(({ default: _ }) => {
386-
const element = document.createElement("div");
387-
388-
element.innerHTML = _.join(["Hello", "webpack"], " ");
389-
390-
return element;
391-
})
392-
.catch((error) => "An error occurred while loading the component");
393-
}
394-
395-
getComponent().then((component) => {
396-
document.body.appendChild(component);
397-
});
398-
```
399-
400-
On your configuration file, do the following:
401-
402-
```js
403-
// webpack.config.js
404-
module.exports = {
405-
context: __dirname,
406-
entry: {
407-
principal: "./src/principal",
408-
},
409-
output: {
410-
path: path.resolve("./dist/"),
411-
// publicPath should match your STATIC_URL config.
412-
// This is required otherwise Webpack will try to fetch
413-
// our chunk generated by the dynamic import from "/" instead of "/dist/".
414-
publicPath: "/dist/",
415-
chunkFilename: "[name].bundle.js",
416-
filename: "[name]-[contenthash].js",
417-
},
418-
plugins: [
419-
new BundleTracker({ path: __dirname, filename: "webpack-stats.json" }),
420-
],
421-
};
422-
```
423-
424-
On your template, render the bundle as usual:
425-
426-
```HTML+Django
427-
<!-- index.html -->
428-
{% load render_bundle from webpack_loader %}
429-
430-
<html>
431-
<body>
432-
{% render_bundle 'principal' 'js' %}
433-
</body>
434-
</html>
435-
```
436-
437379
### Jinja2 Configuration
438380

439381
If you need to output your assets in a jinja template, we provide a Jinja2 extension that's compatible with [django-jinja](https://github.com/niwinz/django-jinja).

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ Run django server
2929
Run webpack compiler
3030

3131
```bash
32-
./node_modules/.bin/webpack --config webpack.config.js --watch
32+
npx webpack --mode=development --watch
3333
```
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
var path = require("path");
2-
var BundleTracker = require('webpack-bundle-tracker');
3-
2+
var BundleTracker = require("webpack-bundle-tracker");
43

54
module.exports = {
65
context: __dirname,
76
entry: {
8-
'main': './assets/js/index',
9-
'other': './assets/js/other',
7+
main: "./assets/js/index",
8+
other: "./assets/js/other",
109
},
1110
output: {
12-
path: path.resolve(__dirname, 'assets/bundles/'),
11+
path: path.resolve(__dirname, "assets/bundles/"),
1312
filename: "[name]-[contenthash].js",
14-
chunkFilename: "[name]-[contenthash].js"
1513
},
1614

1715
plugins: [
18-
new BundleTracker({ path: __dirname, filename: 'webpack-stats.json' }),
16+
new BundleTracker({ path: __dirname, filename: "webpack-stats.json" }),
1917
],
2018

2119
module: {
@@ -25,13 +23,13 @@ module.exports = {
2523
test: /\.jsx?$/,
2624
exclude: /node_modules/,
2725
use: {
28-
loader: 'babel-loader',
26+
loader: "babel-loader",
2927
},
3028
},
3129
],
3230
},
3331

3432
resolve: {
35-
extensions: ['.js', '.jsx']
33+
extensions: [".js", ".jsx"],
3634
},
37-
}
35+
};

examples/dynamic-imports/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../README.md

examples/dynamic-imports/app/__init__.py

Whitespace-only changes.

examples/dynamic-imports/app/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for app project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
15+
16+
application = get_asgi_application()
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""
2+
Django settings for app project.
3+
4+
Generated by 'django-admin startproject' using Django 3.2.2.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/3.2/ref/settings/
11+
"""
12+
13+
from pathlib import Path
14+
import os
15+
16+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
17+
BASE_DIR = Path(__file__).resolve().parent.parent
18+
19+
20+
# Quick-start development settings - unsuitable for production
21+
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
22+
23+
# SECURITY WARNING: keep the secret key used in production secret!
24+
SECRET_KEY = 'django-insecure-&24ubb46zaej*fd9jz1^mw1t0)-@zd9g74f!hcs1a48-7loo0r'
25+
26+
# SECURITY WARNING: don't run with debug turned on in production!
27+
DEBUG = True
28+
29+
ALLOWED_HOSTS = []
30+
31+
32+
# Application definition
33+
34+
INSTALLED_APPS = [
35+
'django.contrib.admin',
36+
'django.contrib.auth',
37+
'django.contrib.contenttypes',
38+
'django.contrib.sessions',
39+
'django.contrib.messages',
40+
'django.contrib.staticfiles',
41+
'webpack_loader',
42+
]
43+
44+
MIDDLEWARE = [
45+
'django.middleware.security.SecurityMiddleware',
46+
'django.contrib.sessions.middleware.SessionMiddleware',
47+
'django.middleware.common.CommonMiddleware',
48+
'django.middleware.csrf.CsrfViewMiddleware',
49+
'django.contrib.auth.middleware.AuthenticationMiddleware',
50+
'django.contrib.messages.middleware.MessageMiddleware',
51+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
52+
]
53+
54+
ROOT_URLCONF = 'app.urls'
55+
56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': [os.path.join(BASE_DIR, "templates")],
60+
'APP_DIRS': True,
61+
'OPTIONS': {
62+
'context_processors': [
63+
'django.template.context_processors.debug',
64+
'django.template.context_processors.request',
65+
'django.contrib.auth.context_processors.auth',
66+
'django.contrib.messages.context_processors.messages',
67+
],
68+
},
69+
},
70+
]
71+
72+
WSGI_APPLICATION = 'app.wsgi.application'
73+
74+
75+
# Database
76+
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
77+
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.sqlite3',
81+
'NAME': BASE_DIR / 'db.sqlite3',
82+
}
83+
}
84+
85+
86+
# Internationalization
87+
# https://docs.djangoproject.com/en/3.2/topics/i18n/
88+
89+
LANGUAGE_CODE = 'en-us'
90+
91+
TIME_ZONE = 'UTC'
92+
93+
USE_I18N = True
94+
95+
USE_L10N = True
96+
97+
USE_TZ = True
98+
99+
100+
# Static files (CSS, JavaScript, Images)
101+
# https://docs.djangoproject.com/en/3.2/howto/static-files/
102+
103+
STATIC_URL = '/static/'
104+
105+
STATICFILES_DIRS = (
106+
os.path.join(BASE_DIR, 'assets'),
107+
)
108+
109+
WEBPACK_LOADER = {
110+
'DEFAULT': {
111+
'BUNDLE_DIR_NAME': 'bundles/',
112+
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json')
113+
}
114+
}
115+
116+
# Default primary key field type
117+
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
118+
119+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

examples/dynamic-imports/app/urls.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""app URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/3.2/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path
18+
from django.views.generic import TemplateView
19+
20+
urlpatterns = [
21+
path('admin/', admin.site.urls),
22+
path('', TemplateView.as_view(template_name='home.html'), name='home'),
23+
]

examples/dynamic-imports/app/wsgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for app project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
15+
16+
application = get_wsgi_application()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function getComponent() {
2+
return import("lodash")
3+
.then(({ default: _ }) => {
4+
const element = document.createElement("div");
5+
6+
element.innerHTML = _.join(["Hello", "webpack"], " ");
7+
8+
return element;
9+
})
10+
.catch((error) => "An error occurred while loading the component");
11+
}
12+
13+
getComponent().then((component) => {
14+
document.body.appendChild(component);
15+
});

0 commit comments

Comments
 (0)