Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: django-webpack/django-webpack-loader
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: agrimgt/django-webpack-loader
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 4 commits
  • 4 files changed
  • 1 contributor

Commits on Sep 9, 2023

  1. Copy the full SHA
    48c4325 View commit details
  2. Copy the full SHA
    f81d9b9 View commit details
  3. Copy the full SHA
    f2764ff View commit details
  4. render_bundle will spit out a paragraph rather than erroring if it ca…

    …n't load webpack stats.
    wizpig64 committed Sep 9, 2023
    Copy the full SHA
    f03b8bc View commit details
Showing with 22 additions and 8 deletions.
  1. +1 −0 webpack_loader/config.py
  2. +7 −3 webpack_loader/loader.py
  3. +11 −3 webpack_loader/templatetags/webpack_loader.py
  4. +3 −2 webpack_loader/utils.py
1 change: 1 addition & 0 deletions webpack_loader/config.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
# FIXME: Explore usage of fsnotify
'POLL_INTERVAL': 0.1,
'TIMEOUT': None,
'PUBLIC_PATH_PREFIX': '',
'IGNORE': [r'.+\.hot-update.js', r'.+\.map'],
'LOADER_CLASS': 'webpack_loader.loader.WebpackLoader',
'INTEGRITY': False,
10 changes: 7 additions & 3 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
@@ -76,16 +76,20 @@ def map_chunk_files_to_url(self, chunks):
else:
yield {'name': chunk, 'url': url}

def prefix_url(self, url):
prefix = self.config.get('PUBLIC_PATH_PREFIX', '')
return prefix + url

def get_chunk_url(self, chunk_file):
public_path = chunk_file.get('publicPath')
if public_path:
return public_path
return self.prefix_url(public_path)

# Use os.path.normpath for Windows paths
relpath = os.path.normpath(
os.path.join(self.config['BUNDLE_DIR_NAME'], chunk_file['name'])
)
return staticfiles_storage.url(relpath)
return self.prefix_url(staticfiles_storage.url(relpath))

def get_bundle(self, bundle_name):
assets = self.get_assets()
@@ -111,7 +115,7 @@ def get_bundle(self, bundle_name):
if assets.get('status') == 'done':
chunks = assets['chunks'].get(bundle_name, None)
if chunks is None:
raise WebpackBundleLookupError('Cannot resolve bundle {0}.'.format(bundle_name))
return []

filtered_chunks = self.filter_chunks(chunks)

14 changes: 11 additions & 3 deletions webpack_loader/templatetags/webpack_loader.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
from django.template import Library
from django.utils.safestring import mark_safe

from webpack_loader.exceptions import WebpackLoaderBadStatsError
from .. import utils

register = Library()
@@ -19,9 +20,16 @@ def render_bundle(
attrs='', is_preload=False, skip_common_chunks=None):
if skip_common_chunks is None:
skip_common_chunks = utils.get_skip_common_chunks(config)
tags = utils.get_as_tags(
bundle_name, extension=extension, config=config, suffix=suffix,
attrs=attrs, is_preload=is_preload)
try:
tags = utils.get_as_tags(
bundle_name, extension=extension, config=config, suffix=suffix,
attrs=attrs, is_preload=is_preload)
except WebpackLoaderBadStatsError:
tags = [
'<p class="text-danger">'
f'Could not load webpack stats for bundle "{bundle_name}" :('
'</p>'
]
request = context.get('request')
if request is None:
if skip_common_chunks:
5 changes: 3 additions & 2 deletions webpack_loader/utils.py
Original file line number Diff line number Diff line change
@@ -72,7 +72,8 @@ def get_as_tags(bundle_name, extension=None, config='DEFAULT', suffix='', attrs=
tags = []

for chunk in bundle:
if chunk['name'].endswith(('.js', '.js.gz')):
chunk_name = chunk['name'].split('?')[0]
if chunk_name.endswith(('.js', '.js.gz')):
if is_preload:
tags.append((
'<link rel="preload" as="script" href="{0}" {1}/>'
@@ -85,7 +86,7 @@ def get_as_tags(bundle_name, extension=None, config='DEFAULT', suffix='', attrs=
attrs,
loader.get_integrity_attr(chunk),
))
elif chunk['name'].endswith(('.css', '.css.gz')):
elif chunk_name.endswith(('.css', '.css.gz')):
tags.append((
'<link href="{0}" rel={2}{3}{1}/>'
).format(