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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

[![Build Status](https://circleci.com/gh/django-webpack/django-webpack-loader/tree/master.svg?style=svg)](https://circleci.com/gh/django-webpack/django-webpack-loader/tree/master)
[![Coverage Status](https://coveralls.io/repos/github/django-webpack/django-webpack-loader/badge.svg?branch=master)](https://coveralls.io/github/django-webpack/django-webpack-loader?branch=master)
Expand Down Expand Up @@ -297,6 +299,7 @@ WEBPACK_LOADER = {

<!-- add some extra attributes to the tag -->
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async charset="UTF-8"'%}
{% render_bundle 'main' 'js' 'DEFAULT' attrs='async charset="UTF-8"' suffix=".gz" %}
</body>
</head>
```
Expand Down
13 changes: 13 additions & 0 deletions tests/app/templates/append_extensions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% load render_bundle from webpack_loader %}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example</title>
{% render_bundle 'main' 'css' %}
</head>

<body>
{% render_bundle 'main' 'js' suffix='.gz' %}
</body>
</html>
8 changes: 8 additions & 0 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ def test_preload(self):
self.assertIn('<link href="/static/django_webpack_loader_bundles/main.css" rel="stylesheet" />', result.rendered_content)
self.assertIn('<script src="/static/django_webpack_loader_bundles/main.js" ></script>', result.rendered_content)

def test_append_extensions(self):
self.compile_bundles('webpack.config.gzipTest.js')
view = TemplateView.as_view(template_name='append_extensions.html')
request = self.factory.get('/')
result = view(request)

self.assertIn('<script src="/static/django_webpack_loader_bundles/main.js.gz" ></script>', result.rendered_content)

def test_jinja2(self):
self.compile_bundles('webpack.config.simple.js')
self.compile_bundles('webpack.config.app2.js')
Expand Down
3 changes: 2 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"mini-css-extract-plugin": "^0.9.0",
"react": "^16.0.0",
"webpack": "^4.0.0",
"webpack-bundle-tracker": "1.1.0",
"compression-webpack-plugin": "^6.1.1",
"webpack-bundle-tracker": "1.2.0",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.0.0"
}
Expand Down
4 changes: 3 additions & 1 deletion tests/webpack.config.gzipTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ var path = require("path");
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
var CompressionPlugin = require('compression-webpack-plugin');


module.exports = {
context: __dirname,
entry: './assets/js/index',
output: {
path: path.resolve('./assets/django_webpack_loader_bundles/'),
filename: "[name].js.gz"
filename: "[name].js"
},

plugins: [
new MiniCssExtractPlugin(),
new CompressionPlugin(),
new BundleTracker({path: __dirname, filename: './webpack-stats.json'}),
],

Expand Down
7 changes: 5 additions & 2 deletions webpack_loader/templatetags/webpack_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@


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


Expand Down
8 changes: 4 additions & 4 deletions webpack_loader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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='', is_preload=False):
def get_as_tags(bundle_name, extension=None, config='DEFAULT', suffix='', attrs='', is_preload=False):
'''
Get a list of formatted <script> & <link> tags for the assets in the
named bundle.
Expand All @@ -65,15 +65,15 @@ def get_as_tags(bundle_name, extension=None, config='DEFAULT', attrs='', is_prel
if is_preload:
tags.append((
'<link rel="preload" as="script" href="{0}" {1}/>'
).format(chunk['url'], attrs))
).format(''.join([chunk['url'], suffix]), attrs))
else:
tags.append((
'<script src="{0}" {1}></script>'
).format(chunk['url'], attrs))
).format(''.join([chunk['url'], suffix]), attrs))
elif chunk['name'].endswith(('.css', '.css.gz')):
tags.append((
'<link href="{0}" rel={2} {1}/>'
).format(chunk['url'], attrs, '"stylesheet"' if not is_preload else '"preload" as="style"'))
).format(''.join([chunk['url'], suffix]), attrs, '"stylesheet"' if not is_preload else '"preload" as="style"'))
return tags


Expand Down