Skip to content

Commit

Permalink
Handle transformed assets URL
Browse files Browse the repository at this point in the history
Adding the following template tag

```
{% webpack_asset 'path/to/original/file' %}
```

Fixes django-webpack#343

Depends on django-webpack/webpack-bundle-tracker#125
  • Loading branch information
batistadasilva committed Apr 2, 2024
1 parent eb1ee36 commit 65dddf2
Show file tree
Hide file tree
Showing 17 changed files with 1,645 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test:
@$(activate_venv_if_not_active)
@echo "Running tests..."
@cd tests; coverage run --source=webpack_loader manage.py test
@cd tests_webpack5; coverage run --source=webpack_loader manage.py test

publish: build
@echo "Publishing to $(REPOSITORY)..."
Expand Down
3 changes: 3 additions & 0 deletions tests_webpack5/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/
django_webpack_loader_bundles/
webpack-stats.json
Empty file added tests_webpack5/app/__init__.py
Empty file.
116 changes: 116 additions & 0 deletions tests_webpack5/app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
"""
Django settings for app project.
Generated by 'django-admin startproject' using Django 1.8.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+g25&y9i+-6_z$$z!ov$l2s%b#0kcmnx)n7y*2_ehy-w011p#k'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'webpack_loader',
'django_jinja',
)

MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
]

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'assets'),
)

WEBPACK_LOADER = {
'DEFAULT': {
'CACHE': False,
'BUNDLE_DIR_NAME': 'django_webpack_loader_bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
},
}

12 changes: 12 additions & 0 deletions tests_webpack5/app/templates/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% load webpack_asset from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
</head>

<body>
<a href="{% webpack_asset 'assets/js/resource.txt' %}">Download resource</a>
</body>
</html>
Empty file.
29 changes: 29 additions & 0 deletions tests_webpack5/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import time
from shutil import rmtree
from subprocess import call

from django.test.client import RequestFactory
from django.test.testcases import TestCase
from django.views.generic.base import TemplateView


class LoaderTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.cleanup_bundles_folder()

def cleanup_bundles_folder(self):
rmtree('./assets/django_webpack_loader_bundles', ignore_errors=True)

def compile_bundles(self, config, wait=None):
if wait:
time.sleep(wait)
call(['./node_modules/.bin/webpack', '--config', config])

def test_templatetags(self):
self.compile_bundles('webpack.config.js')
view = TemplateView.as_view(template_name='home.html')
request = self.factory.get('/')
result = view(request)
self.assertIn(
'<a href="/static/assets/resource-3c9e4020d3e3c7a09c68.txt">Download resource</a>', result.rendered_content)
1 change: 1 addition & 0 deletions tests_webpack5/assets/js/resource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world!
1 change: 1 addition & 0 deletions tests_webpack5/assets/js/resources.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require("./resource.txt")
10 changes: 10 additions & 0 deletions tests_webpack5/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")

from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)
Loading

0 comments on commit 65dddf2

Please sign in to comment.