Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Appending extensions in the url files #135

Merged
merged 10 commits into from
Aug 23, 2021
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# django-webpack-loader
# django-webpack-loader for gzip

Different from the original repository, it appends an extension in the creation of the tags (as .gzip). The append is also dynamic, and can be done for css or js

[![Join the chat at https://gitter.im/owais/django-webpack-loader](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/owais/django-webpack-loader?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/owais/django-webpack-loader.svg?branch=master)](https://travis-ci.org/owais/django-webpack-loader)
Expand Down Expand Up @@ -259,6 +261,7 @@ WEBPACK_LOADER = {

<!-- add some extra attributes to the tag -->
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async chatset="UTF-8"'%}
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async chatset="UTF-8"' suffix=".gz" %}
</body>
</head>
```
Expand Down
4 changes: 2 additions & 2 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def get_chunk_url(self, chunk):
public_path = chunk.get('publicPath')
if public_path:
return public_path

relpath = '{0}{1}'.format(
self.config['BUNDLE_DIR_NAME'], chunk['name']
)
Expand Down Expand Up @@ -80,7 +79,8 @@ 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))
raise WebpackBundleLookupError(
'Cannot resolve bundle {0}.'.format(bundle_name))
return self.filter_chunks(chunks)

elif assets.get('status') == 'error':
Expand Down
8 changes: 6 additions & 2 deletions webpack_loader/templatetags/webpack_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@


@register.simple_tag
def render_bundle(bundle_name, extension=None, config='DEFAULT', attrs=''):
tags = utils.get_as_tags(bundle_name, extension=extension, config=config, attrs=attrs)
def render_bundle(
bundle_name, extension=None, config='DEFAULT', suffix='', attrs=''):
tags = utils.get_as_tags(
bundle_name, extension=extension, config=config,
suffix=suffix, attrs=attrs
)
return mark_safe('\n'.join(tags))


Expand Down
7 changes: 4 additions & 3 deletions webpack_loader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def get_files(bundle_name, extension=None, config='DEFAULT'):
return list(_get_bundle(bundle_name, extension, config))


def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs=''):
def get_as_tags(
bundle_name, extension=None, config='DEFAULT', suffix='', attrs=''):
'''
Get a list of formatted <script> & <link> tags for the assets in the
named bundle.
Expand All @@ -48,11 +49,11 @@ def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs=''):
if chunk['name'].endswith(('.js', '.js.gz')):
tags.append((
'<script type="text/javascript" src="{0}" {1}></script>'
).format(chunk['url'], attrs))
).format(''.join([chunk['url'], suffix]), attrs))
elif chunk['name'].endswith(('.css', '.css.gz')):
tags.append((
'<link type="text/css" href="{0}" rel="stylesheet" {1}/>'
).format(chunk['url'], attrs))
).format(''.join([chunk['url'], suffix]), attrs))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not href="{0}{1}" {2}".format(url, suffix, attrs)?

return tags


Expand Down