Skip to content

Commit 889c5b4

Browse files
Handle transformed assets URL
Adding the following template tag ``` {% webpack_asset 'path/to/original/file' %} ``` Fixes #343 Depends on django-webpack/webpack-bundle-tracker#125
1 parent eb1ee36 commit 889c5b4

17 files changed

+1645
-0
lines changed

Diff for: Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ test:
3636
@$(activate_venv_if_not_active)
3737
@echo "Running tests..."
3838
@cd tests; coverage run --source=webpack_loader manage.py test
39+
@cd tests_webpack5; coverage run --source=webpack_loader manage.py test
3940

4041
publish: build
4142
@echo "Publishing to $(REPOSITORY)..."

Diff for: tests_webpack5/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
django_webpack_loader_bundles/
3+
webpack-stats.json

Diff for: tests_webpack5/app/__init__.py

Whitespace-only changes.

Diff for: tests_webpack5/app/settings.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""
2+
Django settings for app project.
3+
4+
Generated by 'django-admin startproject' using Django 1.8.2.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.8/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.8/ref/settings/
11+
"""
12+
13+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
14+
import os
15+
16+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
17+
18+
19+
# Quick-start development settings - unsuitable for production
20+
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
21+
22+
# SECURITY WARNING: keep the secret key used in production secret!
23+
SECRET_KEY = '+g25&y9i+-6_z$$z!ov$l2s%b#0kcmnx)n7y*2_ehy-w011p#k'
24+
25+
# SECURITY WARNING: don't run with debug turned on in production!
26+
DEBUG = True
27+
28+
ALLOWED_HOSTS = []
29+
30+
31+
# Application definition
32+
33+
INSTALLED_APPS = (
34+
'django.contrib.admin',
35+
'django.contrib.auth',
36+
'django.contrib.contenttypes',
37+
'django.contrib.sessions',
38+
'django.contrib.messages',
39+
'django.contrib.staticfiles',
40+
'app',
41+
'webpack_loader',
42+
'django_jinja',
43+
)
44+
45+
MIDDLEWARE = [
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.auth.middleware.SessionAuthenticationMiddleware',
51+
'django.contrib.messages.middleware.MessageMiddleware',
52+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
53+
'django.middleware.security.SecurityMiddleware',
54+
]
55+
56+
TEMPLATES = [
57+
{
58+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
59+
'DIRS': [],
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/1.8/ref/settings/#databases
77+
78+
DATABASES = {
79+
'default': {
80+
'ENGINE': 'django.db.backends.sqlite3',
81+
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
82+
}
83+
}
84+
85+
86+
# Internationalization
87+
# https://docs.djangoproject.com/en/1.8/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/1.8/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+
'CACHE': False,
112+
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
113+
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
114+
},
115+
}
116+

Diff for: tests_webpack5/app/templates/home.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% load webpack_asset from webpack_loader %}
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Example</title>
7+
</head>
8+
9+
<body>
10+
<a href="{% webpack_asset 'assets/js/resource.txt' %}">Download resource</a>
11+
</body>
12+
</html>

Diff for: tests_webpack5/app/tests/__init__.py

Whitespace-only changes.

Diff for: tests_webpack5/app/tests/test_webpack.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
from shutil import rmtree
3+
from subprocess import call
4+
5+
from django.test.client import RequestFactory
6+
from django.test.testcases import TestCase
7+
from django.views.generic.base import TemplateView
8+
9+
10+
class LoaderTestCase(TestCase):
11+
def setUp(self):
12+
self.factory = RequestFactory()
13+
self.cleanup_bundles_folder()
14+
15+
def cleanup_bundles_folder(self):
16+
rmtree('./assets/django_webpack_loader_bundles', ignore_errors=True)
17+
18+
def compile_bundles(self, config, wait=None):
19+
if wait:
20+
time.sleep(wait)
21+
call(['./node_modules/.bin/webpack', '--config', config])
22+
23+
def test_templatetags(self):
24+
self.compile_bundles('webpack.config.js')
25+
view = TemplateView.as_view(template_name='home.html')
26+
request = self.factory.get('/')
27+
result = view(request)
28+
self.assertIn(
29+
'<a href="/static/assets/resource-3c9e4020d3e3c7a09c68.txt">Download resource</a>', result.rendered_content)

Diff for: tests_webpack5/assets/js/resource.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world!

Diff for: tests_webpack5/assets/js/resources.jsx

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("./resource.txt")

Diff for: tests_webpack5/manage.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
7+
8+
from django.core.management import execute_from_command_line
9+
10+
execute_from_command_line(sys.argv)

0 commit comments

Comments
 (0)